#include <SoftwareSerial.h>
#include <EEPROM.h> //https://github.com/esp8266/Arduino/blob/master/libraries/EEPROM/EEPROM.h
SoftwareSerial NanoSerial(2, 3);//rx, tx
String str;
float CPKWH = 50.00; //Cost Per Kilowatt Hour example
float CPKWM = CPKWH / 60; // 0.83 CPKWH Cost Per Kilowatt Min example
float cost= 0.00;
float V = 0.00;
float I = 0.00;
int S1 = 0;
float M1 = 0.00;
float H1 = 0.00;
int S = 0;
float M = 0.00;
float H = 0.00;
float P = 0.00;
float KWH = 0.00;
float KWM = 0.00;
float costph = 0.00;
float costpm = 0.00;
//float initialbalance = 5.00;
//float balance = 5.00;
float initialbalance;
float balance;
int CurrentBal = 0; //eeprom address
int CurrentRec = 1; //eeprom address
int count=-1;
int action=1;
int volt = A0;
int current = A1;
int Relay = 5;
int Recharge = 6;
unsigned long currenttime = 0;
unsigned long runtime = 0;
unsigned long previoustime = 0;
int interval = 1000;
void setup()
{
//EEPROM.write(CurrentBal, 5);
//EEPROM.write(address, value);
//EEPROM.update(address, value);
//initialbalance = balance = EEPROM.read(CurrentBal);
initialbalance = balance = 10; //comment out later
Serial.begin(115200);
NanoSerial.begin(115200);
pinMode(volt, INPUT);
pinMode(current, INPUT);
pinMode(Recharge, INPUT);
pinMode(Relay, OUTPUT);
digitalWrite(Recharge, HIGH);
Serial.println("NANO READY");
Serial.print("Cost per Min:");
Serial.println(CPKWM);
//digitalWrite(Relay, HIGH);
if(balance == 0)
{
action=0;
lowbalance();
}
else
{
digitalWrite(Relay, HIGH);
}
}
void loop()
{
if(digitalRead(Recharge) == 0)
{
//balance = initialbalance = 5;
balance = initialbalance = balance + 5;
action=1;
digitalWrite(Relay, HIGH);
Serial.println("Recharged");
EEPROM.update(CurrentBal, balance);
}
if(action==1)
{
initiate();
}
else if(action==0)
{
lowbalance();
}
delay(1000);
}
void initiate()
{
V = analogRead(volt);
I = analogRead(current);
//float H = ( ( millis()/1000 ) / 60 ) / 60; //convert to hours
M = (runtime/60); // Min
H = M/60; // Hours
P = (I*V)/1000; // Kilowatt
KWH = P*H; //kwh energy
KWM = P*M; //kwm energy
costpm = KWM * CPKWM; // Total Cost Per Min
costph = KWH * CPKWH; // Total Cost Per Hour
//float COST = KWH * CPKWH; // total user cost
count++;
if(count==61)
{
count=1;
cost = costpm; // accumulated cost per min
balance = initialbalance - cost; // accumulated cost per min
EEPROM.update(CurrentBal, balance);
if (balance < 0)
{
balance = 0;
action=0;
}
}
currenttime = millis();
if ((currenttime - previoustime) >= interval)
{
S1++;
if(S1==60)
{
S1=0;
M1++;
}
if (M1==60)
{
M1=0;
H1++;
}
previoustime = currenttime;
// H =
runtime = S1 + (M1*60) + (H1*3600); // all in sec
//Serial.println(runtime);
}
//float E = analogRead(A0);
Serial.print("Volt: ");
Serial.print(V);
Serial.print("v ");
Serial.print(" Current: ");
Serial.print(I);
Serial.println("A");
Serial.print("millisec: ");
Serial.print(millis());
Serial.println("ms");
Serial.print("count: ");
Serial.print(count);
Serial.println("s");
Serial.print("Sec: ");
Serial.print(S1);
Serial.print(" || Min: ");
Serial.print(M);
Serial.print(" || Hours: ");
Serial.print(H);
Serial.print("h || Runtime(S): ");
Serial.println(runtime);
Serial.print("Power: ");
Serial.print(P);
Serial.println("Kw");
Serial.print("KWH: ");
Serial.print(KWH);
Serial.println("Kwh");
Serial.print("Cost Per H: ");
Serial.print("₦");
Serial.println(costph);
Serial.print("Cost Per M: ");
Serial.print("₦");
Serial.println(costpm);
Serial.print("Total Cost: ");
Serial.print("₦");
Serial.print(cost);
Serial.print(" Bal: ");
Serial.print("₦");
Serial.println(balance);
//Serial.print("Energy: ");
//Serial.print(E);
//Serial.println("J");
str =String("coming from arduino NaNo: ")+String("V= ")+String(V)+String("C= ")+String(I); // send this to NodeMcu
NanoSerial.println(str); //send to NodeMcu
}
void lowbalance()
{
cost = costpm; // accumulated cost per min
balance = 0.00; // accumulated cost per min
I = 0.00;
P = 0.00; // Kilowatt
KWH = 0.00; //kwh energy
KWM = 0.00; //kwm energy
costpm = 0.00; // Total Cost Per Min
costph = 0.00; // Total Cost Per Hour
H1 = M1 = S1 = 0; // reset timing
count=0; // reset count
digitalWrite(Relay, LOW);
Serial.println("PLEASE RECHARGE:");
}