ESP32 - Arduino - no data seen on Thingspeak

33 ビュー (過去 30 日間)
Deepak Tulsani
Deepak Tulsani 2020 年 5 月 1 日
コメント済み: Eric Durant 2021 年 1 月 13 日
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:
  1. ESP32 module in Arduino IDE v1.8.12
  2. On Thinkspeak, I have a channel by the name ESP32_Random where I've defined 3 fields.
  3. 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);
}

採用された回答

Deepak Tulsani
Deepak Tulsani 2020 年 5 月 4 日
I see that there's a dedicated Thingspeak library available. That might make things easier. I'll try it out and hope all works fine then.
Regards,
Deepak

その他の回答 (2 件)

Vinod
Vinod 2020 年 5 月 4 日
I'd recommend starting with the examples here. They will provide a good foundation as you develop your specific sketch.
  1 件のコメント
Eric Durant
Eric Durant 2021 年 1 月 13 日
Thank you

サインインしてコメントする。


Laura Gautschi
Laura Gautschi 2020 年 10 月 13 日
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;

コミュニティ

その他の回答  ThingSpeak コミュニティ

カテゴリ

Help Center および File ExchangeREST API についてさらに検索

製品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by