Why thingspeak cant receive data from sensor ?

8 vues (au cours des 30 derniers jours)
Cristin Manora
Cristin Manora le 19 Août 2021
Modifié(e) : Vinod le 20 Août 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);
}
}

Réponses (1)

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

Connectez-vous pour commenter.

Communautés

Plus de réponses dans  ThingSpeak Community

Catégories

En savoir plus sur ThingSpeak dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by