Why thingspeak cant receive data from sensor ?

8 ビュー (過去 30 日間)
Cristin Manora
Cristin Manora 2021 年 8 月 19 日
編集済み: Vinod 2021 年 8 月 20 日
I made a project with a soil moisture sensor but thingspeak can't receive the measurement data. i am using esp8266 nodemcu. I've also tried via https://api.thingspeak.com/update?api_key=xxxxxxxxxxxxxxxx&field1=0 manually..the result is incoming data.
But not logging in by using esp866.
this is my code :
th#include <ESP8266WiFi.h>
char ssid [] = "Manorangi";
char pw [] = "carpediem";
String apiKey = "xxxxxxxxxxxxxxxx";
char server [] = "api.thingspeak.com";
int soil_sensor = A0;
WiFiClient client;
void setup() {
Serial.begin (115200);
delay (10);
WiFi.begin (ssid, pw);
while (WiFi.status () != WL_CONNECTED)
{
delay (100);
Serial.print ("...");
}
Serial.print ("\n Connect to... ");
Serial.println (ssid);
Serial.print (" IP Address : ");
Serial.print (WiFi.localIP ());
Serial.print ("\n");
}
void loop ()
{
int persenhumid = map (analogRead (soil_sensor),0,1023,100,0);
if (client.connect (server,80))
{
String postStr = apiKey+"&field1="+String(persenhumid)+"\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.print ("Kelembaban: ");
Serial.print (persenhumid);
Serial.println ("%");
Serial.print ("Mengirim data ke Thingpeak");
Serial.print ("\n");
}
client.stop ();
Serial.println ("Waiting...");
for (unsigned int i = 0; i< 20 ; i++)
{
delay (1000);
}
}

回答 (1 件)

Vinod
Vinod 2021 年 8 月 19 日
I'd recommend taking a look at the examples in the ThingSpeak library as a starting point for your project.
  1 件のコメント
Christopher Stapels
Christopher Stapels 2021 年 8 月 19 日
Vinod is right. The library helps you deal with conection issues. In your code, you are also disconnecting too fast.

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

コミュニティ

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

カテゴリ

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

製品

Community Treasure Hunt

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

Start Hunting!

Translated by