#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <ZMPT101B.h>
#include <SoftwareSerial.h>
ZMPT101B zmpt(A0);
LiquidCrystal_I2C lcd(0x27, 16, 2);
SoftwareSerial SIM800L(10, 11); // RX, TX for SIM800L
int relay = 8;
int yellow = 4;
int green = 5;
unsigned long printPeriod = 1000;
unsigned long previousMillis = 0;
bool isNormalVoltage = true;
void setup() {
Serial.begin(9600);
lcd.init();
lcd.init();
lcd.backlight();
lcd.begin(16, 2);
pinMode(relay, OUTPUT);
pinMode(yellow, OUTPUT);
pinMode(green, OUTPUT);
lcd.print("Voltage:");
delay(1000);
SIM800L.begin(9600);
}
void sendSMS(const char* message) {
SIM800L.println("AT+CMGF=1"); // Set the GSM Module in text mode
delay(1000);
SIM800L.println("AT+CMGS=\"+2349014028334\""); // Replace with your mobile number
delay(1000);
SIM800L.print(message);
delay(100);
SIM800L.println((char)26); // End the SMS by sending Ctrl+Z
delay(1000);
}
void loop() {
float current_Volts = zmpt.getRmsVoltage();
if ((unsigned long)(millis() - previousMillis) >= printPeriod) {
previousMillis = millis();
lcd.setCursor(9, 0);
lcd.print(current_Volts);
lcd.print("V");
}
if (current_Volts < 150 || current_Volts > 260) {
if (!isNormalVoltage) {
if (current_Volts < 150) {
lcd.setCursor(0, 1);
lcd.print("Under Voltage");
digitalWrite(relay, LOW);
digitalWrite(yellow, HIGH);
digitalWrite(green, LOW);
sendSMS("Under Voltage detected!");
delay(5000);
} else if (current_Volts > 260) {
lcd.setCursor(0, 1);
lcd.print("Over Voltage");
digitalWrite(relay, LOW);
digitalWrite(yellow, HIGH);
digitalWrite(green, LOW);
sendSMS("Over Voltage detected!");
delay(5000);
}
isNormalVoltage = true;
}
} else {
if (isNormalVoltage) {
lcd.setCursor(0, 1);
lcd.print("Normal Voltage");
digitalWrite(relay, HIGH);
digitalWrite(yellow, LOW);
digitalWrite(green, HIGH);
sendSMS("Normal Voltage!");
delay(5000);
isNormalVoltage = false;
}
}
}