#include <LiquidCrystal_I2C.h>
#include <SoftwareSerial.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
SoftwareSerial sim900(7, 8); // RX, TX for GSM
volatile unsigned long pulseCount = 0;
float kWh = 0;
float price = 0;
unsigned long lastMillis = 0;
const int impPerKwh = 1600; // Meter spec
const float ratePerKwh = 12.0; // Local rate
// === Interrupt Service Routine ===
void pulseISR() {
pulseCount++;
Serial.println("[INFO] imp Detected");
}
void setup() {
lcd.init();
lcd.backlight();
pinMode(2, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(2), pulseISR, FALLING);
sim900.begin(9600);
Serial.begin(9600);
// Set SMS text mode
sim900.println("AT+CMGF=1");
delay(500);
// Configure SIM900 to forward received SMS to serial
sim900.println("AT+CNMI=1,2,0,0,0");
delay(500);
lcd.setCursor(0, 0);
lcd.print("Energy Monitor");
delay(1500);
lcd.clear();
}
void loop() {
if (millis() - lastMillis >= 1000) { // Update every 1s
lastMillis = millis();
// Compute kWh and price
kWh = pulseCount / (float)impPerKwh;
price = kWh * ratePerKwh;
// === LCD Display (no flicker) ===
lcd.setCursor(0, 0);
lcd.print("kWh: ");
lcd.print(kWh, 3);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print("Php: ");
lcd.print(price, 2);
lcd.print(" ");
// === Serial Monitor ===
Serial.print("kWh = ");
Serial.print(kWh, 3);
Serial.print(" | Php = ");
Serial.println(price, 2);
}
// === Check for incoming SMS ===
if (sim900.available()) {
String sms = sim900.readString();
sms.toLowerCase(); // make lowercase for comparison
Serial.println("SMS Received: " + sms);
if (sms.indexOf("bill") >= 0) {
sendBill();
}
}
}
// === Function to send bill ===
void sendBill() {
sim900.println("AT+CMGF=1");
delay(500);
sim900.println("AT+CMGS=\"+63XXXXXXXXXX\""); // Replace with your number
delay(500);
sim900.print("Total = ");
sim900.print(kWh, 3);
sim900.print(" kWh, Price = ");
sim900.print(price, 2);
sim900.println(" Php");
sim900.write(26); // Ctrl+Z to send SMS
Serial.println("Bill Sent!");
}
16*2 i2c LCD
SY-168 Led Pin
SIM GSM900 D7 RX , D8 TX