ESP 8266 Wifi not collecting data from TS | ESP8266 Wi-Fi Module | Forum

Avatar

Please consider registering
Guest

sp_LogInOut Log In sp_Registration Register

Register | Lost password?
Advanced Search

— Forum Scope —






— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

sp_Feed sp_TopicIcon
ESP 8266 Wifi not collecting data from TS
No permission to create posts
December 29, 2017
12:10 am
Avatar
ashebray

Silver
Members
Forum Posts: 8
Member Since:
December 28, 2017
sp_UserOfflineSmall Offline

My Thing Speak is attached to my ESP 8266 but no data is being recorded. I did a test on it with my public channel and it worked. However it will not update data on my current private channel. Here is the code. Any help will be appreciated! 

 

 

#include <ESP8266WiFi.h>
#include "DHT.h"

 

/***************************
* Begin Settings
**************************/

 

const char* ssid = "WuTangLAN";
const char* password = "spranktank7664A";

const char* host = "api.thingspeak.com";

const char* THINGSPEAK_API_KEY = "9DAGDL2109WC8DIJ";

// DHT Settings
#define DHTPIN D6 // what digital pin we're connected to. If you are not using NodeMCU change D6 to real pin

 

// Uncomment whatever type you're using!
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)

const boolean IS_METRIC = true;

// Update every 420 seconds = 7 minutes. Min with Thingspeak is ~20 seconds
const int UPDATE_INTERVAL_SECONDS = 420;

/***************************
* End Settings
**************************/

// Initialize the temperature/ humidity sensor
DHT dht(DHTPIN, DHTTYPE);

void setup() {
Serial.begin(115200);
delay(10);

// We start by connecting to a WiFi network

Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}

Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}

void loop() {
Serial.print("connecting to ");
Serial.println(host);

// Use WiFiClient class to create TCP connections
WiFiClient client;
const int httpPort = 80;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}

// read values from the sensor
float humidity = dht.readHumidity();
float temperature = dht.readTemperature(!IS_METRIC);

// We now create a URI for the request
String url = "/update?api_key=";
url += THINGSPEAK_API_KEY;
url += "&field1=";
url += String(temperature);
url += "&field2=";
url += String(humidity);

Serial.print("Requesting URL: ");
Serial.println(url);

// This will send the request to the server
client.print(String("GET ") + url + " HTTP/1.1
" +
"Host: " + host + "
" +
"Connection: close

");
delay(10);
while(!client.available()){
delay(100);
Serial.print(".");
}
// Read all the lines of the reply from server and print them to Serial
while(client.available()){
String line = client.readStringUntil('
');
Serial.print(line);
}

Serial.println();
Serial.println("closing connection");

 

// Go back to sleep. If your sensor is battery powered you might
// want to use deep sleep here
delay(1000 * UPDATE_INTERVAL_SECONDS);
}

December 29, 2017
10:56 am
Avatar
cstapels
Moderator
Members


Moderators
Forum Posts: 332
Member Since:
March 7, 2017
sp_UserOfflineSmall Offline

I think the order of your commands is wrong.  You are printing the GET verb after you provide the address.  Have a look at the soil moisture example in the doc.  It uses POST instead of GET, but the process is similar.  Let us know if you get it going.

December 30, 2017
9:40 pm
Avatar
ashebray

Silver
Members
Forum Posts: 8
Member Since:
December 28, 2017
sp_UserOfflineSmall Offline

cstapels said

I think the order of your commands is wrong.  You are printing the GET verb after you provide the address.  Have a look at the soil moisture example in the doc.  It uses POST instead of GET, but the process is similar.  Let us know if you get it going.  

How do I change the order? Or change from GET to POST? Sorry for the questions, I'm just having a rough time with this!

Forum Timezone: America/New_York

Most Users Ever Online: 114

Currently Online:
29 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

rw950431: 261

Vinod: 196

piajola: 85

turgo: 70

vespapierre: 63

Adarsh_Murthy: 62

Member Stats:

Guest Posters: 1

Members: 5703

Moderators: 0

Admins: 2

Forum Stats:

Groups: 4

Forums: 17

Topics: 1313

Posts: 4565

Newest Members:

James181, kimberlypg2, JamesAdorb, waynesmith, paulettebw60, ashleytb4

Administrators: Hans: 387, lee: 457