1:09 pm


Silver
February 7, 2019

My weather station on esp8266 saves data every 30 minutes to the Thingspeak server.
Today I noticed that the last data I have from 05-02-2019.
I did not change anything in the layout of the station or in the program.
Today I tried only with the new Write API Key but it did not do anything.
I do not know if this is a problem with Thingspeak server or my device, but as I wrote anything from my side I did not change.
You suggest where to look for a problem.
1:39 pm


MathWorks
May 1, 2016

We've seen several examples of code that did something like this:
void loop()
{
// read sensor value
if (client.connect(server,80))
{
String postStr = apiKey;
postStr +="&field1=";
postStr += String(h);
postStr += "&field2=";
postStr += String(t);
postStr += "";
client.print("POST /update HTTP/1.1
");
client.print("Host: api.thingspeak.com
");
client.print("Connection: close
");
client.print("X-THINGSPEAKAPIKEY: "+apiKey+"
");
client.print("Content-Type: application/x-www-form-urlencoded
");
client.print("Content-Length: ");
client.print(postStr.length());
client.print("
");
client.print(postStr);
}
client.stop();
delay(10000);
}
This code closes the TCPIP socket without waiting for a response from ThingSpeak or confirming that the data was indeed written. At the minimum, the code needs to be modified to something like this:
void loop()
if (client.connect(server,80))
{
String postStr = apiKey;
postStr +="&field1=";
postStr += String(h);
postStr += "&field2=";
postStr += String(t);
postStr += "";
client.print("POST /update HTTP/1.1
");
client.print("Host: api.thingspeak.com
");
client.print("Connection: close
");
client.print("X-THINGSPEAKAPIKEY: "+apiKey+"
");
client.print("Content-Type: application/x-www-form-urlencoded
");
client.print("Content-Length: ");
client.print(postStr.length());
client.print("
");
client.print(postStr);
}
delay(500); //<--- NOTICE NEW LINE OF CODE HERE !!!. YOU MAY NEED TO MODIFY THE DELAY BASED ON YOUR NETWORK CONNECTIVITY.
client.stop();
delay(10000);
}
Ideally, your code should be using the ThingSpeak library here: https://github.com/mathworks/thingspeak-arduino .
See installation instructions here: https://github.com/mathworks/thingspeak-arduino
Built in examples for ESP2866, NodeMCU, etc. here: https://github.com/mathworks/thingspeak-arduino/tree/master/examples/ESP32
1:00 pm


MathWorks
May 1, 2016

pier said
I wonder just why so many months worked properly and now it stopped suddenly?
You can see details in this post.
1:43 am
February 13, 2019

Hi Vinod, Still I am facing the error, Not updated to thinkspeak, even after changes made as per your suggestion.
One thing i didn't do is, i am not using "thinkspeak.h" library. instead i am using
what to change here.
here is my code
#include "DHTesp.h"
#include
#include
DHTesp dht;
String apiKey = "88888888888888";
const char* ssid = "******";
const char* pass = "************";
const char* server = "api.thingspeak.com";
float humidity = dht.getHumidity();
float temperature = dht.getTemperature();
unsigned long previousMillis = 0;
const long interval = 2000;
WiFiClient client;
void setup()
{
previousMillis = millis();
Serial.begin(9600);
Serial.println();
Serial.println("Status\tHumidity (%)\tTemperature (C)\t(F)");
dht.setup(5, DHTesp::DHT22);
Serial.print("Connecting to: ");
Serial.println(ssid);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED)
{
delay(100);
Serial.print("*");
}
Serial.println("");
Serial.println("***WiFi connected***");
}
void loop()
{
float humidity = dht.getHumidity();
float temperature = dht.getTemperature();
if ((millis () - previousMillis) >= interval) {
if(temperature >= 20)
{
Serial.println("10secs");
cloud();
previousMillis = millis();
}
}
}
void cloud()
{
float humidity = dht.getHumidity();
float temperature = dht.getTemperature();
if (client.connect(server,80)) // "184.106.153.149" or api.thingspeak.com
{
//String sendData = apiKey+"&field1="+String(temperature)+"
";
//String sendData = apiKey+"&field2="+String(humidity)+"
";
String sendData = apiKey;
sendData +="&field1=";
sendData += String(temperature);
sendData +="&field2=";
sendData += String(humidity);
sendData += "
";
//Serial.println(sendData);
client.print("POST /update HTTP/1.1
");
client.print("Host: api.thingspeak.com
");
client.print("Connection: close
");
client.print("X-THINGSPEAKAPIKEY: "+apiKey+"
");
client.print("Content-Type: application/x-www-form-urlencoded
");
client.print("Content-Length: ");
//client.print(sendData.length());
client.print(sendData.length());
client.print("
");
client.print(sendData);
Serial.print("temp: ");
Serial.print(temperature);
Serial.print("Humi: ");
Serial.print(humidity);
Serial.println("deg C. Connecting to Thingspeak..");
}
delay(500);
client.stop();
Serial.println("Sending....");
delay(10000);
}
3:01 pm


MathWorks
May 1, 2016

Sridhar,
Your code looks like one of the examples in the library:
https://github.com/mathworks/thingspeak-arduino/tree/master/examples/ESP8266/WriteMultipleFields
Why don't you try starting from there?
If you are unable to make that change, try increasing the delay(500) just before client.stop().
-Vinod
2:20 am
February 13, 2019

4:09 pm

April 5, 2020

10:29 am

April 23, 2020

Hi, i am having a similar problem and tried adding the delay to no avail...
my code was working perfectly fine 6 days ago and it stopped working all of a sudden. i didnt change the coding also...
Serial.println(F("Preparing Thingspeak readings"));
if (client.connect(serverThingspeak, 80))
{
String postStr = apiKeyThingspeak1;
postStr += "&field1=";
postStr += String(t);
postStr += "&field2=";
postStr += String(h);
postStr += "&field3=";
postStr += String(p);
postStr += "&field4=";
postStr += String(r);
postStr += "&field5=";
postStr += String(slp);
postStr += "&field6=";
postStr += String(ba);
postStr += "
";
client.print("POST /update HTTP/1.1
");
client.print("Host: api.thingspeak.com
");
client.print("Connection: close
");
client.print("X-THINGSPEAKAPIKEY: " + apiKeyThingspeak1 + "
");
client.print("Content-Type: application/x-www-form-urlencoded
");
client.print("Content-Length: ");
client.print(postStr.length());
client.print("
");
client.print(postStr);
Serial.println(F("Just uploaded to Thingspeak Channel ID: 1020683"));
}
4:49 pm


Moderators
March 7, 2017

As Vinod says above, we strongly recommend you use the library.
My guess is that your content length might be short by a character, try adding one or two in this line
client.print(postStr.length()+1);
You shouldn't need to provide the API key twice by the way. You have it in the body and in the header. Have a look at the documentation for some good examples if you cant use the library.
https://www.mathworks.com/help/thingspeak/writedata.html
FYI, MATLAB answers is the preferred location for ThingSpeak Community support.
Most Users Ever Online: 166
Currently Online:
35 Guest(s)
Currently Browsing this Page:
1 Guest(s)
Top Posters:
rw950431: 272
Vinod: 240
piajola: 95
turgo: 70
vespapierre: 63
Adarsh_Murthy: 62
Member Stats:
Guest Posters: 1
Members: 8665
Moderators: 1
Admins: 2
Forum Stats:
Groups: 3
Forums: 14
Topics: 1600
Posts: 5760
Newest Members:
huldacormier, bogart, estebanmateo93, mikewrite, onlinebusiness, winzy99Moderators: cstapels: 460
Administrators: Hans: 405, lee: 457