I am using ESP8266 and a thermistor sensor. Can anyone help why this code is not sending data to thingspeak, even though it runs successively.
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266HTTPClient.h>
const char* ssid = "BabakerUH4th_5G@unifi";
const char* password = "BabakerSt2021";
const char* thingspeakAPIKey = "CU1DL1Y5ZJF59YIO";
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
}
void loop() {
// Read the temperature sensor value
int temperatureReading = analogRead(A0);
Serial.print("Temperature Reading: ");
Serial.println(temperatureReading);
// Convert the reading to a temperature value (in degrees Celsius)
float temperature = temperatureReading / 1023.0 * 3.3 / 0.01;
Serial.print("Temperature: ");
Serial.println(temperature);
// Create a WiFiClient object
WiFiClient client;
// Build the HTTP POST request
String postData = "field1=" + String(temperature);
HTTPClient http;
http.begin(client, "http://api.thingspeak.com/update?api_key=" + String(thingspeakAPIKey));
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
int httpResponseCode = http.POST(postData);
// Check for error
if (httpResponseCode > 0) {
Serial.print("HTTP Response Code: ");
Serial.println(httpResponseCode);
} else {
Serial.print("Error: ");
Serial.println(http.errorToString(httpResponseCode).c_str());
}
// Wait a minute before sending the next update
delay(60000);
}