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 CenterFile Exchange에서 ThingSpeak에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by