#include <WiFi.h>
#include <ThingSpeak.h>
#include <WiFiClient.h>
#include <HTTPClient.h>
const char *ssid = "Wokwi-GUEST";
const char *password = "";
const String readApiUrl = "https://api.thingspeak.com/channels/2805605/fields/1.json?api_key=W2N7HIKM621EXJO7&results=2";
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi.");
}
void loop() {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin(readApiUrl);
int httpCode = http.GET();
if (httpCode > 0) {
String payload = http.getString();
Serial.println("Data Retrieved: ");
Serial.println(payload);
} else {
Serial.println("Error in HTTP request");
}
http.end();
}
delay(10000); // Fetch data every 10 seconds
}