#include <WiFi.h>
#include <HTTPClient.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// WiFi credentials
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// ThingSpeak API
const char* server = "http://api.thingspeak.com/update";
const char* apiKey = "J3LP8C8RJ4BV1G2O";
// I2C LCD setup: Address 0x27, 20 columns, 4 rows
LiquidCrystal_I2C lcd(0x27, 20, 4);
// Sensor input pins (simulated)
#define VOLTAGE_PIN 34
#define CURRENT_PIN 35
#define TEMP_PIN 32
// Output pins
#define GREEN_LED 4
#define YELLOW_LED 5
#define RED_LED 18
#define BUZZER 19
// Thresholds
const float VOLTAGE_WARNING = 230.0;
const float VOLTAGE_FAULT = 240.0;
const float CURRENT_WARNING = 10.0;
const float CURRENT_FAULT = 15.0;
const float TEMP_WARNING = 50.0;
const float TEMP_FAULT = 60.0;
unsigned long lastDataSentTime = 0;
bool displayDataSent = false;
void setup() {
// Initialize serial monitor
Serial.begin(115200);
// Initialize LCD
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Initializing.......");
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Smart Grid System");
delay(2000);
lcd.clear();
// Initialize LEDs and buzzer
pinMode(GREEN_LED, OUTPUT);
pinMode(YELLOW_LED, OUTPUT);
pinMode(RED_LED, OUTPUT);
pinMode(BUZZER, OUTPUT);
// Connect to WiFi
lcd.setCursor(0, 0);
lcd.print("Connecting to WiFi");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
lcd.setCursor(0, 1);
lcd.print(".");
}
Serial.println("\nWiFi connected!");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("WiFi Connected!");
delay(2000);
}
void loop() {
// Simulate sensor readings (replace with real values if available)
float voltage = analogRead(VOLTAGE_PIN) * (250.0 / 4095.0);
float current = analogRead(CURRENT_PIN) * (20.0 / 4095.0);
float temperature = analogRead(TEMP_PIN) * (100.0 / 4095.0);
// Debug: Print sensor values
Serial.printf("Voltage: %.1f V, Current: %.1f A, Temperature: %.1f C\n", voltage, current, temperature);
// Update LCD rows
lcd.setCursor(0, 0);
lcd.printf("V: %.1fV C: %.1fA", voltage, current);
lcd.setCursor(0, 1);
lcd.printf("T: %.1fC ", temperature);
// Fault detection and LED/buzzer control
bool isBuzzerOn = false;
String alertMessage = "System Normal"; // Default message
if (voltage > VOLTAGE_FAULT || current > CURRENT_FAULT || temperature > TEMP_FAULT) {
digitalWrite(RED_LED, HIGH);
digitalWrite(YELLOW_LED, LOW);
digitalWrite(GREEN_LED, LOW);
digitalWrite(BUZZER, HIGH);
isBuzzerOn = true;
alertMessage = "CRITICAL ALERT!"; // Critical alert
} else if (voltage > VOLTAGE_WARNING || current > CURRENT_WARNING || temperature > TEMP_WARNING) {
digitalWrite(YELLOW_LED, HIGH);
digitalWrite(GREEN_LED, LOW);
digitalWrite(RED_LED, LOW);
digitalWrite(BUZZER, LOW);
isBuzzerOn = false;
alertMessage = "Warning Detected!"; // Warning alert
} else {
digitalWrite(GREEN_LED, HIGH);
digitalWrite(YELLOW_LED, LOW);
digitalWrite(RED_LED, LOW);
digitalWrite(BUZZER, LOW);
isBuzzerOn = false;
}
// Display buzzer status and alert message
lcd.setCursor(9, 1);
lcd.print(isBuzzerOn ? "Buzzer:ON " : "Buzzer:OFF");
lcd.setCursor(0, 2); // Fourth row
lcd.print(alertMessage);
// Send data to ThingSpeak
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
String url = String(server) + "?api_key=" + apiKey +
"&field1=" + String(voltage) +
"&field2=" + String(current) +
"&field3=" + String(temperature);
http.begin(url.c_str());
int httpCode = http.GET();
if (httpCode > 0) {
Serial.println("Data sent to ThingSpeak!");
lcd.setCursor(0, 3);
lcd.print("Data Sent to TS!");
displayDataSent = true;
lastDataSentTime = millis();
} else {
Serial.println("Error sending data to ThingSpeak!");
}
http.end();
} else {
Serial.println("WiFi disconnected!");
}
// Clear "Data Sent" message after 2 seconds
if (displayDataSent && (millis() - lastDataSentTime > 2000)) {
lcd.setCursor(0, 2);
lcd.print(" "); // Clear line
displayDataSent = false;
}
delay(20000); // Limit updates to every 20 seconds
}