Next I'll include only the void setup:
void setup(){
Serial.begin(9600); // speed to talk to the computer
//myBatteryMonitor.begin(2400);
Serial1.begin(2400); // Digital 19 (Rx), Digital pin 18 (Tx) speed to read the Battery Monitor
//Section added below for Emon
//realpower, apparent power, Vrms, Irms, power factor
emon1.voltage(0, 140.00, 1.7); // Grid Voltage: input pin, calibration, phase_shift; calibLast=142.85,150.0,142.0,140.0,135.0,130.0,125.0,120.0,115.0
emon2.voltage(1, 137.00, 1.7); // Inverter Voltage: input pin, calibration, phase_shift;
emon1.current(2, 10); // Grid Current: input pin, calibration.
emon2.current(3, 10); // Inverter Current: input pin, calibration.
pinMode(loadTransfer, OUTPUT);//Pin 4, Output to switch main power relay
pinMode(invPower, OUTPUT); //Pin 5, Output to switch Inverter on
pinMode(invOnInd, INPUT); //Pin 6, Input verifying Inverter is on and ready
count = 0; //Count is initially set to 0. When % is found, it increments by 1.
countpctOn = 0; //Used to count successive battery % values to eliminate any one time glitches.
countpctOff = 0;
Serial.println("Battery %,Watts,Days Since Charged,Days Since Equalized (PowerUp),Battery Voltage,Filtered Battery Voltage,Amps,Filtered Amps,Amp-Hours,Inverter Status,Inverter On Indicator Status,Load Transfer Status,On Setpoint,Off Setpoint,Grid Real Power,Grid Apparent Power,Grid Vrms,Grid Irms,Grid Power Factor,Inverter Real Power,Inverter Apparent Power,Inverter Vrms,Inverter Irms,Inverter Power Factor");
}
void loop() {
//The coding below is for the Automatic Transfer Switch.
if(Serial1.available() > 0){ //If there is a signal coming in
char a = Serial1.read(); //place that character into character variable 'a'.
if(strBatteryLevel>0){ //'strBatteryLevel' is a character counter that only increments when a=%.
strBatteryLevel++; //The only way that 'strBatteryLevel' could be > 0 is if a=%.
if(a=='='){} //while still under the influence of 'a' being a %, looking at the next
else{batteryLevel+=a;} // otherwise, concatenate the Character value in 'a' to 'batteryLevel' String. Count the number of bytes after encountering a %.
}
if(strBatteryLevel==5){ //Checks to see if the number of characters in 'strBatteryLevel' are counted up to 5 characters. Check to see if the number of
//bytes is up to 5 after encountering a %=xxx is 5 characters long. Which would mean it is over 100.0 %.
strBatteryLevel=0; //Reset the count of characters stored in 'strBatteryLevel' to 0
batteryPerc = batteryLevel.toInt(); //batteryPerc becomes the Integer form of the String 'batteryLevel'.
batteryLevel=""; //The 'batteryLevel' string variable is set to be empty.
}
if(a =='%') { //If the character coming in is a '%' symbol
count++; //Increment the count integer. If 'a' is not %, skip this line.
strBatteryLevel++; //Increment the strBatteryLevel integer. If a is not %, skip this line.
}
if(count >=1){ //If the 'count' integer is 1 or more. If a was a %, then proceed.
count = 0; //Check to see if we have received a % symbol. Reset the 'count' integer to 0
if(ReturnValue("%",strData)>turnOffPoint) {
if (digitalRead(invPower)==HIGH && digitalRead(loadTransfer)==HIGH && digitalRead(invOnInd)==LOW) { //This would indicate an Inverter shut down scenario
if(countpctOff>=toggleDelay){
digitalWrite(loadTransfer,LOW);
loadTransferToggle = "LOW";
digitalWrite(invPower, LOW);
inverterToggle = "LOW";
}
countpctOff++;
countpctOn=0;
}
if(ReturnValue("%",strData)>=turnOnPoint){
if(countpctOn>=toggleDelay){
digitalWrite(invPower,HIGH);
inverterToggle = "HIGH";
}
if(digitalRead(invOnInd)==HIGH){
digitalWrite(loadTransfer,HIGH);
loadTransferToggle = "HIGH";
}
countpctOn++;
countpctOff=0;
}
}
if(ReturnValue("%",strData)<=turnOffPoint) {
if(countpctOff>=toggleDelay){
digitalWrite(loadTransfer,LOW);
loadTransferToggle = "LOW";
digitalWrite(invPower, LOW);
inverterToggle = "LOW";
}
countpctOff++;
countpctOn=0;
}
CheckDaysSinceCharge(ReturnValue("DSC",strData));
//("Battery %,Watts,Days Since Charged,Days Since Equalized (PowerUp),Battery Voltage,Filtered Battery Voltage,Amps,Filtered Amps,Amp-Hours,Inverter Status,Inverter On Indicator Status,Load Transfer Status,On Setpoint,Off Setpoint")
Serial.println("");
Serial.print(ReturnValue("%",strData)); //Print Battery %
Serial.print(",");
Serial.print(ReturnValue("W",strData)); //Print Watts
Serial.print(",");
Serial.print(ReturnValue("DSC",strData)); //Print DSC (Days Since Charged)
Serial.print(",");
Serial.print(ReturnValue("DSE",strData)); //Print DSE (Days Since Empty)Days Since Battery Disconnected
Serial.print(",");
Serial.print(ReturnValue("V",strData)); //Battery Voltage
Serial.print(",");
Serial.print(ReturnValue("FV",strData)); //Float Battery Voltage
Serial.print(",");
Serial.print(ReturnValue("A",strData)); //Amps, +Into or -Out of the Battery
Serial.print(",");
Serial.print(ReturnValue("FA",strData)); //Floating Amps, +Into or -Out of the Battery
Serial.print(",");
Serial.print(ReturnValue("AH",strData)); //-Amp Hours removed from a Full Charge at 0
Serial.print(",");
Serial.print(inverterToggle);
Serial.print(",");
Serial.print(digitalRead(invOnInd));
Serial.print(",");
Serial.print(loadTransferToggle);
Serial.print(",");
Serial.print(turnOnPoint);
Serial.print(",");
Serial.println(turnOffPoint);
Serial.println("");
delay(2000);
strData = "";
//This is the last section that will be added from the Emon Sketch
emon1.calcVI(20,2000); // Calculate all. No.of half wavelengths (crossings), time-out
emon2.calcVI(20,2000); // Calculate all. No.of half wavelengths (crossings), time-out
//Serial.print("Grid ");
emon1.serialprint(); // Print out all variables (realpower, apparent power, Vrms, Irms, power factor)
Serial.println("");
//Serial.print("Inverter ");
emon2.serialprint(); // Print out all variables (realpower, apparent power, Vrms, Irms, power factor)
Serial.println("");
delay(2000);
} //This is the closing brace for: if(count >=1)
strData += a; //Concatenate all data into 'strData'
} //This is the closing brace for the: if(Serial1.available() > 0)
} //This is the closing Brace for the void loop
//Search strData for searchStr and find accompaning value
float ReturnValue(String searchStr, String strData){
String strValue;
strValue = strData.substring(strData.indexOf(","+searchStr+"=")+searchStr.length()+2,strData.indexOf(",",strData.indexOf(searchStr)));
//Find value of following searchStr
return strValue.toFloat();
}
void CheckDaysSinceCharge(float days){
if(days>=14){
turnOnPoint=101;
}
if(days<=1){
turnOnPoint=94;
}
}