#include <WiFi.h>
#include <HTTPClient.h>
// ===== WiFi Simulation (Wokwi) =====
const char* ssid = "Wokwi-GUEST"; // Wokwi WiFi simulation
const char* password = ""; // empty password
// ===== ThingSpeak HTTP =====
const char* server = "http://api.thingspeak.com/update";
const char* writeAPIKey = "5HD7ORL7KQ025ASW"; // Replace with your ThingSpeak API key
// ===== Pins & sensor max values =====
#define CURRENT_PIN 34
#define VOLTAGE_PIN 35
float maxCurrent = 5.0; // 5A
float maxVoltage = 250.0; // 250V
// ===== Thresholds =====
float CURRENT_LIMIT = 3.0; // Ampere
float VOLTAGE_LOW = 200.0; // Volt
float VOLTAGE_HIGH = 240.0; // Volt
void setup() {
Serial.begin(115200);
Serial.println("===== Smart Grid Node Simulation =====");
// Connect WiFi (simulated in Wokwi)
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi...");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWiFi connected (simulated)");
}
void loop() {
// ===== Read simulated sensors =====
int rawCurrent = analogRead(CURRENT_PIN);
int rawVoltage = analogRead(VOLTAGE_PIN);
float current = (rawCurrent / 4095.0) * maxCurrent;
float voltage = (rawVoltage / 4095.0) * maxVoltage;
// ===== Check thresholds =====
String status = "NORMAL";
if(current > CURRENT_LIMIT){
status = "OVER CURRENT";
} else if(voltage < VOLTAGE_LOW){
status = "UNDER VOLTAGE";
} else if(voltage > VOLTAGE_HIGH){
status = "OVER VOLTAGE";
}
// ===== Print to Serial =====
Serial.print("Voltage: "); Serial.print(voltage,1); Serial.print(" V | ");
Serial.print("Current: "); Serial.print(current,2); Serial.print(" A | ");
Serial.print("Status: "); Serial.println(status);
// ===== Simulate HTTP request to ThingSpeak =====
if(WiFi.status() == WL_CONNECTED){
HTTPClient http;
String url = String(server) + "?api_key=" + writeAPIKey
+ "&field1=" + String(voltage,1)
+ "&field2=" + String(current,2);
http.begin(url);
int httpCode = http.GET(); // Simulated HTTP GET
Serial.print("HTTP Response code (simulated): "); Serial.println(httpCode);
http.end();
}
delay(15000); // ThingSpeak update limit = 15 seconds
}