ThingSpeak Does not logs data from sensor to server | 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
ThingSpeak Does not logs data from sensor to server
No permission to create posts
April 18, 2017
5:07 am
Avatar
steelbullets
New Member
Members
Forum Posts: 3
Member Since:
April 18, 2017
sp_UserOfflineSmall Offline

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

April 18, 2017
9:40 pm
Avatar
rw950431

Top
Members
Forum Posts: 261
Member Since:
January 30, 2014
sp_UserOfflineSmall Offline

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.

April 19, 2017
2:58 am
Avatar
steelbullets
New Member
Members
Forum Posts: 3
Member Since:
April 18, 2017
sp_UserOfflineSmall Offline

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

April 19, 2017
8:00 am
Avatar
cstapels
Moderator
Members


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

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. 

June 23, 2017
3:50 am
Avatar
tomski
New Member
Members
Forum Posts: 1
Member Since:
June 23, 2017
sp_UserOfflineSmall Offline

Hi. I have similar problem. My data does not log to server, when I switch to different Wifi access point (of course SSID and PASSWORD are changed in the code before switching).

Here is my code:

#include
#include
#include
#include

#define ONE_WIRE_BUS 2

char ssid[] = "MY_SSID";
char pass[] = "MY_PASSWORD";

WiFiClient client;

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature DS18B20(&oneWire);

unsigned long myChannelNumber = MY_CHANNEL_NUMBER;
const char * myWriteAPIKey = "MY_API_KEY";

void setup() {
WiFi.begin(ssid, pass);
ThingSpeak.begin(client);
pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
digitalWrite(LED_BUILTIN, HIGH);
DS18B20.requestTemperatures();
float temp = DS18B20.getTempCByIndex(0);
ThingSpeak.writeField(myChannelNumber, 1, temp, myWriteAPIKey);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(19000);
}

June 26, 2017
6:16 pm
Avatar
cstapels
Moderator
Members


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

Some networks may need more time to connect, I would suggest adding some delay and a loop to trying connection repeatedly.  You could add it to your main loop in case the device looses connection after start up. The antenna on an ESP8266 is pretty small...

Have a look at the methods for connecting to wifi in here.

June 27, 2017
7:48 am
Avatar
jaysha4
New Member
Members
Forum Posts: 1
Member Since:
June 27, 2017
sp_UserOfflineSmall Offline

I also built my first Thingspeak project for monitoring temperature using DHT11, but I always send values through URL using POST function like below

sprintf(postUrl, "update?api_key=%s&field1=%s&field2=%s",api_key,humidStr,tempStr);
httpGet("api.thingspeak.com", postUrl, 80);

I followed this Arduino temperature monitoring article, and it worked for me as it is.
I will try to use ThingSpeak.writeField function next time, till then you can also try to send values to server using POST URL, read the referred article for detailed instruction.

Forum Timezone: America/New_York

Most Users Ever Online: 114

Currently Online:
18 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:

josephrj11, dennis55, jillhp4, tabathawg69, MarinaMa, Susandom

Administrators: Hans: 387, lee: 457