#include <LiquidCrystal_I2C.h>
int h1 = 1;
int h2 = 1;
int h3 = 1;
int h4 = 1;
int h5 = 1;
int slot = 1;
int theft = 0;
String d1;
String d2;
String d3;
String d4;
String d5;
String Date;
String Time;
int Amount;
int buzz = 2;
int relay = 4;
int rled = 5;
int gled = 18;
int Voltage = 220;
float Current = 0.00;
float Power = 0.00;
float Energy = 0.00;
float Prev_Balance = 5000;
float Cost = 0.00;
float Balance = 5000;
unsigned long RuntimeS = 0;
unsigned long Runtime = 0;
unsigned long Ptime = 0;
unsigned long Ctime = 0;
//int current_value = 0;
int lcdColumns = 20;
int lcdRows = 4;
int disp = 0;
int rechargevalue = 0;
int newrecharge = 0;
int Sec = 0;
float Min = 0;
float Hr = 0;
int count = 0;
String Meterstatus;
LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows);
int randoms;
int progress = 3;
/* Custom characters */
byte Bell[8] = {
0b00100,
0b01110,
0b01110,
0b01110,
0b11111,
0b00000,
0b00100,
0b00000
};
byte KWH[8] = {
B01001,
B01010,
B01100,
B01010,
B01001,
B10001,
B10101,
B11011
};
byte START_DIV_0_OF_1[] = {
B01111,
B11000,
B10000,
B10000,
B10000,
B10000,
B11000,
B01111
}; // Char beginning 0 / 1
byte START_DIV_1_OF_1[] = {
B01111,
B11000,
B10011,
B10111,
B10111,
B10011,
B11000,
B01111
}; // Char beginning 1 / 1
byte DIV_0_OF_2[] = {
B11111,
B00000,
B00000,
B00000,
B00000,
B00000,
B00000,
B11111
}; // Char Middle 0 / 2
byte DIV_1_OF_2[] = {
B11111,
B00000,
B11000,
B11000,
B11000,
B11000,
B00000,
B11111
}; // Char Middle 1 / 2
byte DIV_2_OF_2[] = {
B11111,
B00000,
B11011,
B11011,
B11011,
B11011,
B00000,
B11111
}; // Char Middle 2 / 2
byte END_DIV_0_OF_1[] = {
B11110,
B00011,
B00001,
B00001,
B00001,
B00001,
B00011,
B11110
}; // Char End 0 / 1
byte END_DIV_1_OF_1[] = {
B11110,
B00011,
B11001,
B11101,
B11101,
B11001,
B00011,
B11110
}; // Char End 1 / 1
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
// put your setup code here, to run once:
Serial.begin(115200);
//EEPROM.begin(4000);
pinMode(buzz, OUTPUT);
pinMode(relay, OUTPUT);
pinMode(rled, OUTPUT);
pinMode(gled, OUTPUT);
digitalWrite(relay, HIGH);
digitalWrite(buzz, HIGH);
delay(1000);
digitalWrite(relay, LOW);
digitalWrite(buzz, LOW);
// pinMode(current_value, INPUT);
lcd.init();
lcd.backlight();
lcd.createChar(0, Bell);
lcd.createChar(1, START_DIV_1_OF_1);//{|
lcd.createChar(2, DIV_0_OF_2);//=
lcd.createChar(3, DIV_1_OF_2);// |
lcd.createChar(4, DIV_2_OF_2);// |
lcd.createChar(5, END_DIV_0_OF_1);//}
lcd.createChar(6, END_DIV_1_OF_1);// |}
lcd.createChar(7, KWH);// |}
Serial.println(d1);
Serial.println(d2);
Serial.println(d3);
Serial.println(d4);
Serial.println(d5);
Serial.println(slot);
Serial.println(h1);
Serial.println(h2);
Serial.println(h3);
Serial.println(h4);
Serial.println(h5);
Serial.println(slot);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" SMART PREPAID ");
lcd.setCursor(0, 1);
lcd.print(" METER ");
delay(1000);
lcd.clear();
loading();
}
void loading() {
lcd.setCursor(0, 0);
lcd.print (" --RavaBot-- ");
lcd.setCursor(0, 1);
lcd.print ("SMART PREPAID METER ");
lcd.setCursor(0, 2);
lcd.print ("SYSTEM INITIALIZING ");
randoms = random(100, 400);
lcd.setCursor(0, 3);
lcd.write(1);
for (int i = 1; i < 14; i++) {
int load = 0 + i;
lcd.setCursor(load, 3);
lcd.write(2);
}
lcd.write(5);
delay(randoms);
Serial.println("LOADING");
for (int i = 1; i < 15; i++) {
int cursor = i;
// Serial.println(i);
// progress++;
randoms = random(300, 500);
Serial.print("#");
delay(randoms);
lcd.setCursor(cursor, 3);
lcd.write(progress);
delay(randoms);
// Serial.print(randoms);
if (progress == 3) {
progress = 4;
}
else {
progress = 3;
}
lcd.setCursor(cursor, 3);
lcd.write(progress);
progress = 3;
lcd.setCursor(16, 3);
int status = map(i, 0, 14, 0, 100);
lcd.print(status);
lcd.print("%");
if (i == 14) {
lcd.setCursor(cursor, 3);
lcd.write(6);
lcd.setCursor(18, 1);
lcd.write(0);
delay(2000);
}
}
lcd.clear();
}
void loop() {
Ctime = millis();
Serial.println(Ctime);
// delay(1000);
// digitalWrite(buzz, HIGH);
// delay(1000);
// digitalWrite(buzz, LOW);
// return;
//start
if (Prev_Balance > 0.00) {
Meterstatus = "ON";
digitalWrite(relay, HIGH);
if ((Ctime - Ptime) >= 1000) {
Sec++;
Ptime = Ctime;
}
if (Sec == 60) {
Min++;
count = 1;
Sec = 0;
}
if (Min == 60) {
Hr++;
Min = 0;
}
if (RuntimeS > 10) {
RuntimeS = 0;
}
Runtime = (Hr * 3600) + (Min * 60) + Sec; // all in sec
//Serial.println(Runtime);
//Serial.println(count);
Runtime = Runtime / 60; // Min
//Runtime = 1; // all in sec
Voltage = random(215, 230);
// Current = random(1, 5);
Current = random(5, 3);
Serial.println(Current);
if (Current <= 0.20) {
Current = 0.00;
}
Power = Voltage * Current;
Energy = Power * Runtime; // 200
if (count == 1) { // 1min
Cost = Cost + Power;
Balance = Prev_Balance - (Power * 1); // 1000-200
Prev_Balance = Balance;
count = 0;
Serial.println(Energy);
Serial.println(Prev_Balance);
}
if (disp < 30)
{
lcd.setCursor(0, 0);
lcd.print("VOLTAGE: ");
lcd.print(Voltage);
lcd.print("V ");
lcd.setCursor(0, 1);
lcd.print("CURRENT: ");
lcd.print(Current);
lcd.print("A ");
lcd.setCursor(0, 2);
lcd.print("POWER : ");
lcd.print(Power);
lcd.print("kW ");
lcd.setCursor(0, 3);
lcd.print("ENERGY : ");
lcd.print(Energy);
lcd.print("kW/m ");
}
else if (disp > 30 && disp < 60) {
lcd.setCursor(0, 0);
lcd.print("BALANCE: ");
lcd.print(Prev_Balance);
lcd.print("kWm ");
lcd.setCursor(0, 1);
lcd.print("RUNTIME: ");
lcd.print(Runtime);
lcd.print("min ");
lcd.setCursor(0, 2);
lcd.print("STATUS : ON (OK)");
lcd.print(" ");
lcd.setCursor(0, 3);
lcd.print("--------------------");
}
if (disp >= 60) {
disp = 0;
}
}
else {
// read current sensor
digitalWrite(relay, LOW);
// Current = currentsensor.getCurrentAC();
Current = 0.00;
if (Current >= 0.20) {
Theft();
}
else {
digitalWrite(buzz, LOW);
delay(200);
theft = 0;
}
Voltage = 0;
Meterstatus = "OFF";
Recharge();
}
//end
delay(100);
disp++;
}
void Recharge() {
Serial.println("RECHARGE MODE");
for (int i = 0; i <= 2; i++) {
lcd.setCursor(0, 0);
lcd.print(" BALANCE LOW!!! ");
lcd.setCursor(0, 1);
lcd.print(" RECHARGE ");
lcd.setCursor(0, 2);
lcd.print(" METER ");
lcd.setCursor(0, 2);
lcd.print("--------------------");
digitalWrite(buzz, HIGH);
delay(200);
digitalWrite(buzz, LOW);
delay(200);
}
lcd.setCursor(0, 0);
lcd.print("--------------------");
lcd.setCursor(0, 1);
lcd.print("TOTAL UNIT(S): ");
lcd.print(Cost);
lcd.print(" ");
lcd.setCursor(0, 2);
lcd.print("AVAILABLE BAL: ");
lcd.print("0.00 ");
lcd.setCursor(0, 3);
lcd.print("--------------------");
delay(1200);
}
void Theft() {
for (int i = 0; i <= 2; i++) {
lcd.setCursor(0, 0);
lcd.print(" BYPASS DETECTED!!! ");
lcd.setCursor(0, 1);
lcd.print("_-_-_-_-_-_-_-_-_-_-_-");
digitalWrite(buzz, HIGH);
delay(2000);
theft = 1;
}
}
void Confirm(int Rvalue) {
newrecharge = 1;
rechargevalue = Rvalue;
}