#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
const char* ssid = "Wokwi-GUEST"; // Wokwi simulator default Wi-Fi
const char* password = ""; // No password for Wokwi-GUEST
const char* fetchUrl = "http://vedamation.mooo.com/aio.php"; // Update with your server URL
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
// Wait for connection
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.setTimeout(5000); // Set timeout to 5 seconds
http.begin(fetchUrl);
int httpCode = http.GET(); // Send the request
if (httpCode > 0) {
String payload = http.getString();
Serial.println("Payload received:");
Serial.println(payload);
parseSwitchData(payload); // Parse the JSON response
} else {
Serial.println("Error in HTTP request");
}
http.end(); // Close the connection
} else {
Serial.println("WiFi Disconnected");
}
delay(5000); // Wait before making another request
}
void parseSwitchData(String payload) {
StaticJsonDocument<512> doc;
DeserializationError error = deserializeJson(doc, payload);
if (error) {
Serial.print("Failed to parse JSON: ");
Serial.println(error.c_str());
return;
}
// Assuming the JSON response has keys like switch1, switch2, etc.
for (int i = 1; i <= 15; i++) {
String switchKey = "switch" + String(i);
if (doc.containsKey(switchKey)) {
int switchState = doc[switchKey];
Serial.print(switchKey);
Serial.print(": ");
Serial.println(switchState);
} else {
Serial.print(switchKey);
Serial.println(": Not found");
}
}
}