#include <WiFi.h>
#include <HTTPClient.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
/* ========== WIFI SETTINGS ========== */
const char* ssid = "Wokwi-GUEST";
const char* password = "";
/* ========== THINGSPEAK SETTINGS ========== */
String apiKey = "5HD7ORL7KQ025ASW";
const char* server = "http://api.thingspeak.com/update";
/* ========== SENSOR PINS ========== */
#define CURRENT_PIN 34 // ACS712
#define VOLTAGE_PIN 35 // ZMPT101B
/* ========== CALIBRATION VALUES ========== */
float currentSensitivity = 0.185; // ACS712 5A = 185mV/A
float voltageCalibration = 250.0; // Max 250V (simulation)
/* ========== THRESHOLD VALUES ========== */
float currentThresholdLow = 0.5; // Low current threshold (in Amps)
float currentThresholdHigh = 5.0; // High current threshold (in Amps)
float voltageThresholdLow = 100.0; // Low voltage threshold (in Volts)
float voltageThresholdHigh = 240.0; // High voltage threshold (in Volts)
/* ========== LCD SETTINGS ========== */
LiquidCrystal_I2C lcd(0x27, 16, 2); // Change the address and size if necessary
void setup() {
Serial.begin(115200);
// Initialize LCD
lcd.begin(16, 2);
lcd.print("Initializing...");
delay(2000);
// Connect to WiFi
WiFi.begin(ssid, password);
Serial.print("Connecting WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWiFi Connected");
// Display WiFi status on LCD
lcd.clear();
lcd.print("WiFi Connected");
delay(2000);
}
void loop() {
/* ========== CURRENT SENSOR ========== */
int rawCurrent = analogRead(CURRENT_PIN);
float voltageCurrent = (rawCurrent / 4095.0) * 3.3;
float current = (voltageCurrent - 1.65) / currentSensitivity;
if (current < 0) current = 0;
/* ========== VOLTAGE SENSOR ========== */
int rawVoltage = analogRead(VOLTAGE_PIN);
float voltage = (rawVoltage / 4095.0) * voltageCalibration;
// Threshold Check for Current
if (current < currentThresholdLow) {
Serial.println("Warning: Current below threshold!");
} else if (current > currentThresholdHigh) {
Serial.println("Warning: Current exceeds threshold!");
}
// Threshold Check for Voltage
if (voltage < voltageThresholdLow) {
Serial.println("Warning: Voltage below threshold!");
} else if (voltage > voltageThresholdHigh) {
Serial.println("Warning: Voltage exceeds threshold!");
}
// Display readings on Serial Monitor
Serial.print("Voltage: ");
Serial.print(voltage);
Serial.print(" V | Current: ");
Serial.print(current);
Serial.println(" A");
// Display readings on LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("V: " + String(voltage) + "V");
lcd.setCursor(0, 1);
lcd.print("I: " + String(current) + "A");
delay(2000); // Update every 2 seconds
/* ========== SEND TO THINGSPEAK ========== */
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
String url = server;
url += "?api_key=" + apiKey;
url += "&field1=" + String(voltage);
url += "&field2=" + String(current);
http.begin(url);
int httpCode = http.GET();
http.end();
Serial.print("ThingSpeak Response: ");
Serial.println(httpCode);
}
delay(15000); // ThingSpeak minimum 15 seconds
}