ESP32 - Arduino - no data seen on Thingspeak
Show older comments
Hello community,
My first stint at using Thingspeak to hookup some sensors as a hobbyist (I do programming but by no means an expert), and I hit a hurdle right away.
Here's what I'm doing:
- ESP32 module in Arduino IDE v1.8.12
- On Thinkspeak, I have a channel by the name ESP32_Random where I've defined 3 fields.
- Generating some random numbers and trying to upload them to Thingspeak. Once random numbers are seen on Thingspeak, I could add sensor data as well
So below is the source code. On the serial montor it appears that data is being uploaded to the server, but I don't see it on my Thingspeak channel visualisation. I used my mobile phone as a WiFi Hotspot, but without the mobile data connection, and I could see Connection Error on Serial Monitor, indicating that the HTTP request is being handled correctly in the code. But still no luck with seeing the data on my Thingspeak channel. Any idea what might be going wrong?
Also in the below code, is there a way to print the response code from the server as well? That would also help me confirm whether the data was sent out correctly or not.
Thanks so much for your help.
#include <WiFi.h>
String apiKey = "XXXXXXXXXXXXXXXXXX"; // Enter your Write API key from ThingSpeak
String answer = "0";
const char *ssid = "MyWiFi"; // replace with your wifi ssid and wpa2 key
const char *pass = "MyWiFi";
const char* server = "api.thingspeak.com";
WiFiClient client;
void setup()
{
Serial.begin(115200);
delay(10);
Serial.println("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
}
void loop()
{
int h = 0;
float t =0;
float p = 0;
h = random(100);
t = random(85);
p = random(2000); //changing temperature parameter to celsius
if (client.connect(server,80)) // "184.106.153.149" or api.thingspeak.com
{
String postStr = apiKey;
postStr +="&field1=";
postStr += String(t);
postStr += "&field2=";
postStr += String(p);
postStr += "&field3=";
postStr += String(h);
postStr += "\r\n\r\n";
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(postStr.length());
client.print("\n\n");
client.print(postStr);
Serial.println( postStr );
Serial.print("Pres: ");
Serial.println(p);
Serial.print("Temp:");
Serial.print(t);
Serial.println(" C");
Serial.print("Hum: ");
Serial.println(h);
Serial.println("%. Send to Thingspeak.");
}
else
{
Serial.println("Connection Failed");
}
client.stop();
Serial.println("Waiting...");
delay(20000);
}
Accepted Answer
More Answers (2)
Vinod
on 4 May 2020
0 votes
I'd recommend starting with the examples here. They will provide a good foundation as you develop your specific sketch.
1 Comment
Eric Durant
on 13 Jan 2021
Thank you
Laura Gautschi
on 13 Oct 2020
0 votes
Hi
I had the same problem.
First check again if you have took the WRITE api key not the read one.
Then check if your WIFI datas are correct and you should be connected to the same wifi with your computer as youve mentioned in your code.
maybe add this before your setup: unsigned long myChannelNumber = xxxxxx;
Communities
More Answers in the ThingSpeak Community
Categories
Find more on ThingSpeak in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!