#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* server = "api.thingspeak.com";
const char* apiKey = "O3NT564JEXPDWPW5";
const char* channelID = " 2393437";
const char* value1 ;
void setup() {
Serial.begin(115200);
pinMode(4, OUTPUT);
delay(100);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
// Make HTTP GET request to ThingSpeak
}
void loop() {
HTTPClient http;
String url = "http://" + String(server) + "/channels/" + String(channelID) + "/feeds/last.json?api_key=" + String(apiKey);
Serial.print("Connecting to ThingSpeak: ");
Serial.println(url);
http.begin(url);
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
String payload = http.getString();
Serial.println("Received data:");
Serial.println(payload);
const size_t capacity = JSON_OBJECT_SIZE(3) + 100; // Adjust capacity as needed
DynamicJsonDocument doc(capacity);
DeserializationError error = deserializeJson(doc, payload);
if (error) {
Serial.print("deserializeJson() failed: ");
Serial.println(error.c_str());
return;
}
int value1 = doc["field1"];
Serial.print("Value 1: ");
Serial.println(value1);
if(value1==1)
{
Serial.println("hihg");
digitalWrite(4,HIGH);
}
if(value1==0)
{
digitalWrite(4,LOW);
}
// Process the received data here
} else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
Serial.print("out");
Serial.println(value1);
http.end();
// Add any necessary continuous operations here
}