Why thingspeak cant receive data from sensor ?

7 visualizaciones (últimos 30 días)
Cristin Manora
Cristin Manora el 19 de Ag. de 2021
Editada: Vinod el 20 de Ag. de 2021
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);
}
}

Respuestas (1)

Vinod
Vinod el 19 de Ag. de 2021
I'd recommend taking a look at the examples in the ThingSpeak library as a starting point for your project.
  1 comentario
Christopher Stapels
Christopher Stapels el 19 de Ag. de 2021
Vinod is right. The library helps you deal with conection issues. In your code, you are also disconnecting too fast.

Iniciar sesión para comentar.

Comunidades de usuarios

Más respuestas en  ThingSpeak Community

Categorías

Más información sobre Read Data from Channel en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by