|
@@ -54,6 +54,9 @@
|
|
///////// Definitions /////////////////////////////////////////////////////////////////////////////////////////////////
|
|
///////// Definitions /////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
|
|
+#define ENABLE_DATALOGGER 0
|
|
|
|
+
|
|
|
|
+#define SOL_AMPS_CHAN 1 // Defining the adc channel to read solar amps
|
|
#define SOL_VOLTS_CHAN 0 // defining the adc channel to read solar volts
|
|
#define SOL_VOLTS_CHAN 0 // defining the adc channel to read solar volts
|
|
#define SOL_AMPS_CHAN 1 // Defining the adc channel to read solar amps
|
|
#define SOL_AMPS_CHAN 1 // Defining the adc channel to read solar amps
|
|
#define BAT_VOLTS_CHAN 2 // defining the adc channel to read battery volts
|
|
#define BAT_VOLTS_CHAN 2 // defining the adc channel to read battery volts
|
|
@@ -184,9 +187,14 @@ LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I
|
|
|
|
|
|
void setup() // run once, when the sketch starts
|
|
void setup() // run once, when the sketch starts
|
|
{
|
|
{
|
|
- pinMode(LED_RED, OUTPUT); // sets the digital pin as output
|
|
|
|
- pinMode(LED_GREEN, OUTPUT); // sets the digital pin as output
|
|
|
|
- pinMode(LED_YELLOW, OUTPUT); // sets the digital pin as output
|
|
|
|
|
|
+ lcd.begin(20,4); // initialize the lcd for 16 chars 2 lines, turn on backlight
|
|
|
|
+ lcd.backlight();
|
|
|
|
+ lcd.createChar(1,solar);
|
|
|
|
+ lcd.createChar(2,battery);
|
|
|
|
+ lcd.createChar(3,_PWM);
|
|
|
|
+ pinMode(LED_RED, OUTPUT);
|
|
|
|
+ pinMode(LED_GREEN, OUTPUT);
|
|
|
|
+ pinMode(LED_YELLOW, OUTPUT);
|
|
pinMode(PWM_ENABLE_PIN, OUTPUT); // sets the digital pin as output
|
|
pinMode(PWM_ENABLE_PIN, OUTPUT); // sets the digital pin as output
|
|
Timer1.initialize(20); // initialize timer1, and set a 20uS period
|
|
Timer1.initialize(20); // initialize timer1, and set a 20uS period
|
|
Timer1.pwm(PWM_PIN, 0); // setup pwm on pin 9, 0% duty cycle
|
|
Timer1.pwm(PWM_PIN, 0); // setup pwm on pin 9, 0% duty cycle
|
|
@@ -195,16 +203,22 @@ void setup() // run once, when the sketch starts
|
|
Serial.begin(9600); // open the serial port at 9600 bps:
|
|
Serial.begin(9600); // open the serial port at 9600 bps:
|
|
ser.begin(9600); // enable software serial
|
|
ser.begin(9600); // enable software serial
|
|
ser.println("AT+RST"); // reset ESP8266
|
|
ser.println("AT+RST"); // reset ESP8266
|
|
- pwm = PWM_START; // starting value for pwm
|
|
|
|
- charger_state = on; // start with charger state as off
|
|
|
|
- pinMode(BACK_LIGHT_PIN, INPUT); // backlight on button
|
|
|
|
- pinMode(LOAD_PIN,OUTPUT); // output for the LOAD MOSFET (LOW = on, HIGH = off)
|
|
|
|
- digitalWrite(LOAD_PIN,HIGH); // default load state is OFF
|
|
|
|
- lcd.begin(20,4); // initialize the lcd for 16 chars 2 lines, turn on backlight
|
|
|
|
- lcd.noBacklight(); // turn off the backlight
|
|
|
|
- lcd.createChar(1,solar); // turn the bitmap into a character
|
|
|
|
- lcd.createChar(2,battery); // turn the bitmap into a character
|
|
|
|
- lcd.createChar(3,_PWM); // turn the bitmap into a character
|
|
|
|
|
|
+ pwm = PWM_START; //starting value for pwm
|
|
|
|
+ charger_state = off; // start with charger state as off
|
|
|
|
+ pinMode(BACK_LIGHT_PIN, INPUT);
|
|
|
|
+ pinMode(LOAD_PIN,OUTPUT);
|
|
|
|
+ digitalWrite(LOAD_PIN,LOW); // default load state is OFF
|
|
|
|
+ digitalWrite(BACK_LIGHT_PIN,LOW); // default LCd back light is OFF
|
|
|
|
+
|
|
|
|
+ // display the constant stuff on the LCD
|
|
|
|
+ lcd.setCursor(0, 0);
|
|
|
|
+ lcd.print("SOL");
|
|
|
|
+ lcd.setCursor(4, 0);
|
|
|
|
+ lcd.write(1);
|
|
|
|
+ lcd.setCursor(8, 0);
|
|
|
|
+ lcd.print("BAT");
|
|
|
|
+ lcd.setCursor(12, 0);
|
|
|
|
+ lcd.write(2);
|
|
}
|
|
}
|
|
|
|
|
|
//------------------------------------------------------------------------------------------------------
|
|
//------------------------------------------------------------------------------------------------------
|
|
@@ -393,6 +407,21 @@ void run_charger(void) {
|
|
break;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+//------------------------------------------------------------------------------------------------------
|
|
|
|
+// Main loop.
|
|
|
|
+//
|
|
|
|
+//------------------------------------------------------------------------------------------------------
|
|
|
|
+void loop()
|
|
|
|
+{
|
|
|
|
+ read_data(); //read data from inputs
|
|
|
|
+ run_charger(); //run the charger state machine
|
|
|
|
+ //print_data(); //print data
|
|
|
|
+ load_control(); // control the connected load
|
|
|
|
+ led_output(); // led indication
|
|
|
|
+ lcd_display(); // lcd display
|
|
|
|
+ wifi_datalog();
|
|
|
|
+ delay(500);
|
|
|
|
+}
|
|
|
|
|
|
//----------------------------------------------------------------------------------------------------------------------
|
|
//----------------------------------------------------------------------------------------------------------------------
|
|
/////////////////////////////////////////////LOAD CONTROL/////////////////////////////////////////////////////
|
|
/////////////////////////////////////////////LOAD CONTROL/////////////////////////////////////////////////////
|
|
@@ -451,37 +480,31 @@ void print_data(void) {
|
|
//-------------------------------------------------------------------------------------------------
|
|
//-------------------------------------------------------------------------------------------------
|
|
//---------------------------------Led Indication--------------------------------------------------
|
|
//---------------------------------Led Indication--------------------------------------------------
|
|
//-------------------------------------------------------------------------------------------------
|
|
//-------------------------------------------------------------------------------------------------
|
|
-
|
|
|
|
-void led_output(void)
|
|
|
|
|
|
+// light an individual LED
|
|
|
|
+// we remember which one was on before in last_lit and turn it off if different
|
|
|
|
+void light_led(char pin)
|
|
{
|
|
{
|
|
- if(bat_volts > 14.1 )
|
|
|
|
- {
|
|
|
|
- leds_off_all();
|
|
|
|
- digitalWrite(LED_YELLOW, HIGH);
|
|
|
|
- }
|
|
|
|
- else if(bat_volts > 11.9 && bat_volts < 14.1)
|
|
|
|
- {
|
|
|
|
- leds_off_all();
|
|
|
|
- digitalWrite(LED_GREEN, HIGH);
|
|
|
|
- }
|
|
|
|
- else if(bat_volts < 11.8)
|
|
|
|
- {
|
|
|
|
- leds_off_all;
|
|
|
|
- digitalWrite(LED_RED, HIGH);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
|
|
+ static char last_lit;
|
|
|
|
+ if (last_lit == pin)
|
|
|
|
+ return;
|
|
|
|
+ if (last_lit != 0)
|
|
|
|
+ digitalWrite(last_lit, LOW);
|
|
|
|
+ digitalWrite(pin, HIGH);
|
|
|
|
+ last_lit = pin;
|
|
}
|
|
}
|
|
-
|
|
|
|
-//------------------------------------------------------------------------------------------------------
|
|
|
|
-//
|
|
|
|
-// This function is used to turn all the leds off
|
|
|
|
-//
|
|
|
|
-//------------------------------------------------------------------------------------------------------
|
|
|
|
-void leds_off_all(void)
|
|
|
|
|
|
+// display the current state via LED as follows:
|
|
|
|
+// YELLOW means overvoltage (over 14.1 volts)
|
|
|
|
+// RED means undervoltage (under 11.9 volts)
|
|
|
|
+// GREEN is between 11.9 and 14.1 volts
|
|
|
|
+void led_output(void)
|
|
{
|
|
{
|
|
- digitalWrite(LED_GREEN, LOW);
|
|
|
|
- digitalWrite(LED_RED, LOW);
|
|
|
|
- digitalWrite(LED_YELLOW, LOW);
|
|
|
|
|
|
+ static char last_lit;
|
|
|
|
+ if(bat_volts > 14.1 )
|
|
|
|
+ light_led(LED_YELLOW);
|
|
|
|
+ else if(bat_volts > 11.9)
|
|
|
|
+ light_led(LED_GREEN);
|
|
|
|
+ else
|
|
|
|
+ light_led(LED_RED);
|
|
}
|
|
}
|
|
|
|
|
|
//------------------------------------------------------------------------------------------------------
|
|
//------------------------------------------------------------------------------------------------------
|
|
@@ -489,51 +512,40 @@ void leds_off_all(void)
|
|
//-------------------------------------------------------------------------------------------------------
|
|
//-------------------------------------------------------------------------------------------------------
|
|
void lcd_display()
|
|
void lcd_display()
|
|
{
|
|
{
|
|
|
|
+ static bool current_backlight_state = -1;
|
|
back_light_pin_State = digitalRead(BACK_LIGHT_PIN);
|
|
back_light_pin_State = digitalRead(BACK_LIGHT_PIN);
|
|
|
|
+ if (current_backlight_state != back_light_pin_State) {
|
|
|
|
+ current_backlight_state = back_light_pin_State;
|
|
|
|
+ if (back_light_pin_State == HIGH)
|
|
|
|
+ lcd.backlight();// finish with backlight on
|
|
|
|
+ else
|
|
|
|
+ lcd.noBacklight();
|
|
|
|
+ }
|
|
|
|
+
|
|
if (back_light_pin_State == HIGH)
|
|
if (back_light_pin_State == HIGH)
|
|
{
|
|
{
|
|
time = millis(); // If any of the buttons are pressed, save the time in millis to "time"
|
|
time = millis(); // If any of the buttons are pressed, save the time in millis to "time"
|
|
}
|
|
}
|
|
|
|
|
|
- lcd.setCursor(0, 0);
|
|
|
|
- lcd.print("SOL");
|
|
|
|
- lcd.setCursor(4, 0);
|
|
|
|
- lcd.write(1);
|
|
|
|
lcd.setCursor(0, 1);
|
|
lcd.setCursor(0, 1);
|
|
lcd.print(sol_volts);
|
|
lcd.print(sol_volts);
|
|
- lcd.print("V");
|
|
|
|
|
|
+ lcd.print("V ");
|
|
lcd.setCursor(0, 2);
|
|
lcd.setCursor(0, 2);
|
|
lcd.print(sol_amps);
|
|
lcd.print(sol_amps);
|
|
lcd.print("A");
|
|
lcd.print("A");
|
|
lcd.setCursor(0, 3);
|
|
lcd.setCursor(0, 3);
|
|
lcd.print(sol_watts);
|
|
lcd.print(sol_watts);
|
|
lcd.print("W ");
|
|
lcd.print("W ");
|
|
- lcd.setCursor(8, 0);
|
|
|
|
- lcd.print("BAT");
|
|
|
|
- lcd.setCursor(12, 0);
|
|
|
|
- lcd.write(2);
|
|
|
|
lcd.setCursor(8, 1);
|
|
lcd.setCursor(8, 1);
|
|
lcd.print(bat_volts);
|
|
lcd.print(bat_volts);
|
|
lcd.setCursor(8,2);
|
|
lcd.setCursor(8,2);
|
|
-
|
|
|
|
|
|
+
|
|
if (charger_state == on)
|
|
if (charger_state == on)
|
|
- {
|
|
|
|
- lcd.print(" ");
|
|
|
|
- lcd.setCursor(8,2);
|
|
|
|
- lcd.print("on");
|
|
|
|
- }
|
|
|
|
|
|
+ lcd.print("on ");
|
|
else if (charger_state == off)
|
|
else if (charger_state == off)
|
|
- {
|
|
|
|
- lcd.print(" ");
|
|
|
|
- lcd.setCursor(8,2);
|
|
|
|
- lcd.print("off");
|
|
|
|
- }
|
|
|
|
|
|
+ lcd.print("off ");
|
|
else if (charger_state == bulk)
|
|
else if (charger_state == bulk)
|
|
- {
|
|
|
|
- lcd.print(" ");
|
|
|
|
- lcd.setCursor(8,2);
|
|
|
|
- lcd.print("bulk");
|
|
|
|
- }
|
|
|
|
|
|
+ lcd.print("bulk ");
|
|
else if (charger_state == bat_float)
|
|
else if (charger_state == bat_float)
|
|
{
|
|
{
|
|
lcd.print(" ");
|
|
lcd.print(" ");
|
|
@@ -545,28 +557,14 @@ void lcd_display()
|
|
//--------------------Battery State Of Charge ---------------
|
|
//--------------------Battery State Of Charge ---------------
|
|
//-----------------------------------------------------------
|
|
//-----------------------------------------------------------
|
|
lcd.setCursor(8,3);
|
|
lcd.setCursor(8,3);
|
|
- if ( bat_volts >= 12.7)
|
|
|
|
- lcd.print( "100%");
|
|
|
|
- else if (bat_volts >= 12.5 && bat_volts < 12.7)
|
|
|
|
- lcd.print( "90%");
|
|
|
|
- else if (bat_volts >= 12.42 && bat_volts < 12.5)
|
|
|
|
- lcd.print( "80%");
|
|
|
|
- else if (bat_volts >= 12.32 && bat_volts < 12.42)
|
|
|
|
- lcd.print( "70%");
|
|
|
|
- else if (bat_volts >= 12.2 && bat_volts < 12.32)
|
|
|
|
- lcd.print( "60%");
|
|
|
|
- else if (bat_volts >= 12.06 && bat_volts < 12.2)
|
|
|
|
- lcd.print( "50%");
|
|
|
|
- else if (bat_volts >= 11.90 && bat_volts < 12.06)
|
|
|
|
- lcd.print( "40%");
|
|
|
|
- else if (bat_volts >= 11.75 && bat_volts < 11.90)
|
|
|
|
- lcd.print( "30%");
|
|
|
|
- else if (bat_volts >= 11.58 && bat_volts < 11.75)
|
|
|
|
- lcd.print( "20%");
|
|
|
|
- else if (bat_volts >= 11.31 && bat_volts < 11.58)
|
|
|
|
- lcd.print( "10%");
|
|
|
|
- else if (bat_volts < 11.3)
|
|
|
|
- lcd.print( "0%");
|
|
|
|
|
|
+ int pct = 100.0*(bat_volts - 11.3)/(12.7 - 11.3);
|
|
|
|
+ if (pct < 0)
|
|
|
|
+ pct = 0;
|
|
|
|
+ else if (pct > 100)
|
|
|
|
+ pct = 100;
|
|
|
|
+ pct = pct - (pct%10);
|
|
|
|
+ lcd.print(pct);
|
|
|
|
+ lcd.print("% ");
|
|
|
|
|
|
//---------------------------------------------------------------------
|
|
//---------------------------------------------------------------------
|
|
//------------------Duty Cycle-----------------------------------------
|
|
//------------------Duty Cycle-----------------------------------------
|
|
@@ -587,17 +585,14 @@ void lcd_display()
|
|
lcd.print("Load");
|
|
lcd.print("Load");
|
|
lcd.setCursor(15,3);
|
|
lcd.setCursor(15,3);
|
|
if (load_status == 1)
|
|
if (load_status == 1)
|
|
- {
|
|
|
|
- lcd.print(" ");
|
|
|
|
- lcd.setCursor(15,3);
|
|
|
|
- lcd.print("On");
|
|
|
|
|
|
+ {
|
|
|
|
+ lcd.print("On ");
|
|
}
|
|
}
|
|
else
|
|
else
|
|
- {
|
|
|
|
- lcd.print(" ");
|
|
|
|
- lcd.setCursor(15,3);
|
|
|
|
- lcd.print("Off");
|
|
|
|
- }
|
|
|
|
|
|
+ {
|
|
|
|
+ lcd.print("Off ");
|
|
|
|
+ }
|
|
|
|
+ spinner();
|
|
backLight_timer(); // call the backlight timer function in every loop
|
|
backLight_timer(); // call the backlight timer function in every loop
|
|
}
|
|
}
|
|
|
|
|
|
@@ -606,13 +601,26 @@ void backLight_timer(){
|
|
lcd.backlight(); // finish with backlight on
|
|
lcd.backlight(); // finish with backlight on
|
|
else
|
|
else
|
|
lcd.noBacklight(); // if it's been more than 15 secs, turn the backlight off
|
|
lcd.noBacklight(); // if it's been more than 15 secs, turn the backlight off
|
|
|
|
+>>>>>>> bad0d746d2a86af933d66a9ca0fdc61c6db8619a
|
|
}
|
|
}
|
|
|
|
+void spinner(void) {
|
|
|
|
+ static int cspinner;
|
|
|
|
+ static char spinner_chars[] = { '/','-','\\','|' };
|
|
|
|
+ cspinner++;
|
|
|
|
+ lcd.print(spinner_chars[cspinner%4]);
|
|
|
|
+}
|
|
|
|
+
|
|
//-------------------------------------------------------------------------
|
|
//-------------------------------------------------------------------------
|
|
//----------------------------- ESP8266 WiFi ------------------------------
|
|
//----------------------------- ESP8266 WiFi ------------------------------
|
|
//--------------------------Plot System data on thingspeak.com-------------
|
|
//--------------------------Plot System data on thingspeak.com-------------
|
|
//-------------------------------------------------------------------------
|
|
//-------------------------------------------------------------------------
|
|
void wifi_datalog()
|
|
void wifi_datalog()
|
|
{
|
|
{
|
|
|
|
+ // thingspeak needs 15 sec delay between updates
|
|
|
|
+ static int lastlogged;
|
|
|
|
+ if ( seconds - lastlogged < 16 )
|
|
|
|
+ return;
|
|
|
|
+ lastlogged = seconds;
|
|
// convert to string
|
|
// convert to string
|
|
char buf[16];
|
|
char buf[16];
|
|
String strTemp = dtostrf( sol_volts, 4, 1, buf);
|
|
String strTemp = dtostrf( sol_volts, 4, 1, buf);
|
|
@@ -649,8 +657,5 @@ void wifi_datalog()
|
|
ser.println("AT+CIPCLOSE");
|
|
ser.println("AT+CIPCLOSE");
|
|
// alert user
|
|
// alert user
|
|
Serial.println("AT+CIPCLOSE");
|
|
Serial.println("AT+CIPCLOSE");
|
|
- }
|
|
|
|
-
|
|
|
|
- // thingspeak needs 15 sec delay between updates
|
|
|
|
- delay(16000);
|
|
|
|
|
|
+ }
|
|
}
|
|
}
|