#include <WiFi.h>
#include <HTTPClient.h>
#include <Arduino_JSON.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
String serverName = "https://api.thingspeak.com/channels/2574398/feeds/last.json?api_key=U3V4Y00AE8GNIFX9&status=true";
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.println("Connecting");
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println(WiFi.localIP());
pinMode(5, OUTPUT);
}
void loop() {
if(WiFi.status()== WL_CONNECTED)
{
HTTPClient http;
String serverPath = serverName;
http.begin(serverPath);
int httpResponseCode = http.GET();
if (httpResponseCode>0)
{
String payload = http.getString();
//Serial.println(payload);
JSONVar myObject = JSON.parse(payload);
JSONVar keys = myObject.keys();
JSONVar value = myObject[keys[2]];
byte databuzzer = atoi(value);
Serial.println(databuzzer);
if(databuzzer==200)
{
digitalWrite(5, HIGH);
}
else
{
digitalWrite(5, LOW);
}
}
else
{
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
http.end();
}
else
{
Serial.println("WiFi Disconnected");
}
delay(2000);
}