ThingSpeak Does not logs data from sensor to server | ESP8266 Wi-Fi Module | Forum

Avatar

Please consider registering
Guest

Search

— Forum Scope —






— Match —





— Forum Options —





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

Register Lost password?
sp_Feed sp_TopicIcon
ThingSpeak Does not logs data from sensor to server
Avatar
steelbullets
New Member
Forum Posts: 3
sp_UserOfflineSmall Offline
1
April 18, 2017 - 5:07 am
sp_Permalink sp_Print

I am having this same problem using a DHT11 Sensor. No matter what I try, My thingspeak channel is not at all recording data sent to it. I am working on a IOT Project for my college. I have tried almost 15-19 tutorials on the internet. Using functions like ThingSpeak.writeField(channel, 1, t, apiKey), ThinkSpeak.setFields() to using POST methods from arduino etc. None Seems to work. I don't know where i am going wrong. However if I type the post method with the api key and field values in the address bar of the browser, the data gets logged into ThinkSpeak. 

Here is my Code: Parts of it, like the network ssid, passwords and API Keys are hidden. I have it in my sketch though.

#include
#include
#include
#include
#define dht_apin 4// Analog Pin sensor is connected to
#define IP "184.106.153.149"

DHT dht(dht_apin, DHT11,15);
const char* apiKey = ""; //Hidden due to privacy
String apiKey1 = ""; //Hidden due to privacy
unsigned long channel =; //Hidden due to privacy
const char* ssid = "//Hidden due to privacy";
const char* password = "//Hidden due to privacy";
const char* server = "api.thingspeak.com";
int h,t;

WiFiClient client;

void setup(){

Serial.begin(115200);
delay(500);//Delay to let system boot
dht.begin();
delay(1000);//Wait before accessing Sensor
WiFi.begin(ssid, password);
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");
ThingSpeak.begin(client);

}//end "setup()"

void loop(){
//Start of Program

// DHT.read11(dht_apin);

Serial.print("Current humidity = ");
Serial.print(dht.readHumidity());
h=dht.readHumidity();
Serial.print("% ");
Serial.print("temperature = ");
Serial.print(dht.readTemperature());
t=dht.readTemperature();
Serial.println("C ");
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}

This is creating the Headache i think. Please Look into this part of Code specifically. 

if (client.connect(server,80)) { // "184.106.153.149" or api.thingspeak.com
Serial.print("Temperature: ");
Serial.print(t);
ThingSpeak.setField(1,t);
Serial.print(" degrees Celcius Humidity: ");
Serial.print(h);
ThingSpeak.setField(2,h);
Serial.println("% send to Thingspeak");
String postStr = apiKey;
postStr +="&field1=";
postStr += String(t);
postStr +="&field2=";
postStr += String(h);
postStr += "

";

client.print("POST /update HTTP/1.1
");

client.print("Host: api.thingspeak.com
");

client.print("Connection: close
");

client.print("X-THINGSPEAKAPIKEY: "+apiKey1+"
");

client.print("Content-Type: application/x-www-form-urlencoded
");

client.print("Content-Length: ");
client.print(postStr.length());
client.print("
");

client.print(postStr);

// wait for response
/*while(!!!client.available()) {
delay(10);
}*/

// Read all the lines of the reply from server and print them to Serial
Serial.println("Response: ");
while(client.available()){
String line = client.readStringUntil('
');

Serial.print(line);
}

Serial.println();
Serial.println("closing connection");
//ThingSpeak.writeField(channel, 1, t, apiKey);
//ThingSpeak.writeField(channel, 2, h, apiKey);

// Write the fields that you've set all at once.
//ThingSpeak.writeFields(channel,apiKey);
}
else{
Serial.print("Cannot connect to the internet. Check Mobile Data");
Serial.print("
");
}
client.stop();

Serial.println("Waiting…");
// thingspeak needs minimum 15 sec delay between updates
delay(20000);

//Fastest should be once every two seconds.

}// end loop()

 

Thanking You

Regards

SteelBullets

Avatar
rw950431

Forum Posts: 224
sp_UserOfflineSmall Offline
2
April 18, 2017 - 9:40 pm
sp_Permalink sp_Print

have you tried the example from here? https://github.com/mathworks/thingspeak-arduino/tree/master/examples/WriteMultipleVoltages

At the moment your code is confused- for example you assemble a query string like you are going to do a GET but you end up POST-ing instead.  I always start with a working example then gradually modify it to work the way I want.

Avatar
steelbullets
New Member
Forum Posts: 3
sp_UserOfflineSmall Offline
3
April 19, 2017 - 2:58 am
sp_Permalink sp_Print

rw950431 said
have you tried the example from here? https://github.com/mathworks/thingspeak-arduino/tree/master/examples/WriteMultipleVoltages

At the moment your code is confused- for example you assemble a query string like you are going to do a GET but you end up POST-ing instead.  I always start with a working example then gradually modify it to work the way I want.  

I am so much thankful to your advice. I looked into my code and found that I made a mistake of using POST method instead of GET. Thank you so much.

Here is the final code i created now: Only the data sending part

if (client.connect(IP,80)) { // "184.106.153.149" or api.thingspeak.com
Serial.print("Temperature: ");
Serial.print(t);
ThingSpeak.setField(1,t);
Serial.print(" degrees Celcius Humidity: ");
Serial.print(h);
ThingSpeak.setField(2,h);
Serial.println("% send to Thingspeak");
String postStr = apiKey;
postStr +="&field1=";
postStr += String(t);
postStr +="&field2=";
postStr += String(h);
postStr += "

";

//This is the changed part

String cmd = "GET https://api.thingspeak.com/update?api_key=";
cmd += postStr;
client.print(cmd);

 

Thank you rw950431

Regards

SteelBullets

Avatar
cstapels
New Member
Forum Posts: 2
sp_UserOfflineSmall Offline
4
April 19, 2017 - 8:00 am
sp_Permalink sp_Print sp_EditHistory

It seems that you are combining thingspeak library commands with client write commands.  It is possible to write to ThingSpeak using the ThingSpeak library - these are the ThingSpeak.setField and ThingSpeak.writeFields etc. commands.  It is also possible to use your own client and build the HTTP POST call yourself, which is the client.print(cmd) method.  There are good reasons to use either, but I wouldn't suggest using both simultaneously.  

I would recommend using the library, but if you want to use the client version, I think you need a string that says "api_key=" before you append the actual apikey.  I haven't checked your code completely so you will want to follow the example closely. 

Forum Timezone: America/New_York

Most Users Ever Online: 114

Currently Online:
12 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

rw950431: 224

Vinod: 134

vespapierre: 63

piajola: 62

chrisjmears: 54

turgo: 54

Newest Members:

eli

sally

Metabolicz

RodrigoSos

tjheikki

manimb54

Forum Stats:

Groups: 4

Forums: 17

Topics: 1124

Posts: 3994

 

Member Stats:

Guest Posters: 1

Members: 5293

Moderators: 0

Admins: 2

Administrators: Hans, lee