#include <WiFi.h>
#include <Wire.h> // Include the Wire library for I2C communication
#include <LiquidCrystal_I2C.h> // Include the I2C LCD library
#include <HTTPClient.h> // Include HTTPClient for ThingSpeak communication
// LCD Setup
LiquidCrystal_I2C lcd(0x27, 16, 2); // Set the LCD address and size
// Wi-Fi Credentials
const char* ssid = "Wokwi-GUEST"; // Replace with your Wi-Fi SSID
const char* password = ""; // Replace with your Wi-Fi password
// ThingSpeak API Key
String apiKey = "PTE1BI98R1ZM0IBK"; // Replace with your ThingSpeak Write API Key
// Sensor Pins
const int voltagePin = 36; // Analog input pin for voltage measurement (GPIO 36 / VP)
const int currentPin = 39; // Analog input pin for current measurement (GPIO 39 / VN)
// Load Control Pins
const int ledPin1 = 25; // GPIO 25 for LED 1 (light 1)
const int ledPin2 = 26; // GPIO 26 for LED 2 (light 2)
const int ledPin3 = 27; // GPIO 27 for LED 3 (light 3)
const int motorPin1 = 14; // GPIO 14 for DC motor 1 (fan 1)
const int motorPin2 = 12; // GPIO 12 for DC motor 2 (fan 2)
const int motorPin3 = 13; // GPIO 13 for DC motor 3 (fan 3)
// Switch Pins (DIP Switches)
// LED Switches (control LEDs and their resistors as a unit)
const int switchLed1 = 15; // GPIO 15 for switch controlling LED 1 (red)
const int switchLed2 = 2; // GPIO 2 for switch controlling LED 2 (yellow)
const int switchLed3 = 4; // GPIO 4 for switch controlling LED 3 (green)
// Motor Switches
const int switchMotor1 = 5; // GPIO 5 for switch controlling motor 1
const int switchMotor2 = 18; // GPIO 18 for switch controlling motor 2
const int switchMotor3 = 19; // GPIO 19 for switch controlling motor 3
// Buzzer Pin
const int buzzerPin = 23; // GPIO 23 for the buzzer
// Current sense resistor value (in ohms)
const float senseResistor = 1.0; // 1 ohm resistor for current sensing
// Power threshold (in watts)
const float powerThreshold = 0.6; // Threshold for over-power consumption
float voltage, current, power;
void setup() {
Serial.begin(115200);
lcd.init();
lcd.backlight();
// Set up pins for loads (outputs)
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT);
// ... existing setup code ...
// Explicitly turn off all outputs initially
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
// ... rest of setup ...
// Set up pins for switches (inputs)
pinMode(switchLed1, INPUT);
pinMode(switchLed2, INPUT);
pinMode(switchLed3, INPUT);
pinMode(switchMotor1, INPUT);
pinMode(switchMotor2, INPUT);
pinMode(switchMotor3, INPUT);
// Set up buzzer pin
pinMode(buzzerPin, OUTPUT);
digitalWrite(buzzerPin, LOW); // Ensure buzzer is off initially
// Set up analog pins for measurement
pinMode(voltagePin, INPUT);
pinMode(currentPin, INPUT);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
Serial.print("Connecting to Wi-Fi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nConnected to Wi-Fi!");
}
void loop() {
// Read switch states (HIGH when on, LOW when off)
bool led1State = digitalRead(switchLed1); // Red LED
bool led2State = digitalRead(switchLed2); // Yellow LED
bool led3State = digitalRead(switchLed3); // Green LED
bool motor1State = digitalRead(switchMotor1); // Motor 1
bool motor2State = digitalRead(switchMotor2); // Motor 2
bool motor3State = digitalRead(switchMotor3); // Motor 3
// Control LEDs and motors based on switch states
digitalWrite(ledPin1, led1State ? HIGH : LOW);
digitalWrite(ledPin2, led2State ? HIGH : LOW);
digitalWrite(ledPin3, led3State ? HIGH : LOW);
digitalWrite(motorPin1, motor1State ? HIGH : LOW);
digitalWrite(motorPin2, motor2State ? HIGH : LOW);
digitalWrite(motorPin3, motor3State ? HIGH : LOW);
// Read raw values from analog pins
int rawVoltage = analogRead(voltagePin);
int rawCurrent = analogRead(currentPin);
// Convert ADC values to voltage (ESP32 ADC is 12-bit, 0-4095, reference 3.3V)
float voltageDrop = (rawCurrent / 4095.0) * 3.3; // Voltage drop across the sense resistor
voltage = (rawVoltage / 4095.0) * 3.3; // Supply voltage (assumed 3.3V)
// Calculate current using Ohm's Law: I = V_drop / R_sense
current = voltageDrop / senseResistor;
// Calculate power using P = V * I
power = voltage * current;
// Print to Serial Monitor
Serial.print("Voltage: ");
Serial.print(voltage);
Serial.print(" V, Current: ");
Serial.print(current);
Serial.print(" A, Power: ");
Serial.print(power);
Serial.println(" W");
// Display values on LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("V:");
lcd.print(voltage, 2);
lcd.print("V I:");
lcd.print(current, 2);
lcd.print("A");
lcd.setCursor(0, 1);
if (power > powerThreshold) {
inAlertState = true;
lcd.print("High Power! ");
tone(buzzerPin, 1000); // 1000 Hz tone
} else {
if (inAlertState) {
// Only clear the alert state when power drops below threshold
inAlertState = false;
noTone(buzzerPin);
}
lcd.print("Power: ");
lcd.print(power, 2);
lcd.print(" W");
}
// Send Data to ThingSpeak
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
String url = "http://api.thingspeak.com/update?api_key=" + apiKey +
"&field1=" + String(voltage) +
"&field2=" + String(current) +
"&field3=" + String(power);
http.begin(url);
int httpResponseCode = http.GET();
http.end();
Serial.print("ThingSpeak Response: ");
Serial.println(httpResponseCode);
}
delay(5000); // Update every 5 seconds
}