123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661 |
-
-
- #include "TimerOne.h"
- #include <LiquidCrystal_I2C.h> // using the LCD I2C Library from https://bitbucket.org/fmalpartida/new-liquidcrystal/downloads
- #include <Wire.h>
- #include <SoftwareSerial.h> // using the Software Serial library Ref : http://www.arduino.cc/en/Reference/SoftwareSerialConstructor
-
- #define ENABLE_DATALOGGER 0
- #define SOL_AMPS_CHAN 1
- #define SOL_VOLTS_CHAN 0
- #define SOL_AMPS_CHAN 1
- #define BAT_VOLTS_CHAN 2
- #define AVG_NUM 8
- #define SOL_AMPS_SCALE 0.026393581
- #define SOL_VOLTS_SCALE 0.029296875
- #define BAT_VOLTS_SCALE 0.029296875
- #define PWM_PIN 9
- #define PWM_ENABLE_PIN 8
- #define PWM_FULL 1023
- #define PWM_MAX 100
- #define PWM_MIN 60
- #define PWM_START 90
- #define PWM_INC 1
- #define TRUE 1
- #define FALSE 0
- #define ON TRUE
- #define OFF FALSE
- #define TURN_ON_MOSFETS digitalWrite(PWM_ENABLE_PIN, HIGH)
- #define TURN_OFF_MOSFETS digitalWrite(PWM_ENABLE_PIN, LOW)
- #define ONE_SECOND 50000
- #define LOW_SOL_WATTS 5.00
- #define MIN_SOL_WATTS 1.00
- #define MIN_BAT_VOLTS 11.00
- #define MAX_BAT_VOLTS 14.10
- #define BATT_FLOAT 13.60
- #define HIGH_BAT_VOLTS 13.00
- #define LVD 11.5
- #define OFF_NUM 9
-
- #define LED_RED 11
- #define LED_GREEN 12
- #define LED_YELLOW 13
- #define LOAD_PIN 6
-
- #define BACK_LIGHT_PIN 5
- String apiKey = "DPK8RMTFY2B1XCAF";
- SoftwareSerial ser(2,3);
- byte solar[8] =
- {
- 0b11111,
- 0b10101,
- 0b11111,
- 0b10101,
- 0b11111,
- 0b10101,
- 0b11111,
- 0b00000
- };
- byte battery[8]=
- {
- 0b01110,
- 0b11011,
- 0b10001,
- 0b10001,
- 0b11111,
- 0b11111,
- 0b11111,
- 0b11111,
- };
- byte _PWM [8]=
- {
- 0b11101,
- 0b10101,
- 0b10101,
- 0b10101,
- 0b10101,
- 0b10101,
- 0b10101,
- 0b10111,
- };
- float sol_amps;
- float sol_volts;
- float bat_volts;
- float sol_watts;
- float old_sol_watts = 0;
- unsigned int seconds = 0;
- unsigned int prev_seconds = 0;
- unsigned int interrupt_counter = 0;
- unsigned long time = 0;
- int delta = PWM_INC;
- int pwm = 0;
- int back_light_pin_State = 0;
- int load_status = 0;
-
- enum charger_mode {off, on, bulk, bat_float} charger_state;
- LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
- void setup()
- {
- lcd.begin(20,4);
- 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);
- Timer1.initialize(20);
- Timer1.pwm(PWM_PIN, 0);
- TURN_ON_MOSFETS;
- Timer1.attachInterrupt(callback);
- Serial.begin(9600);
- ser.begin(9600);
- ser.println("AT+RST");
- pwm = PWM_START;
- charger_state = off;
- pinMode(BACK_LIGHT_PIN, INPUT);
- pinMode(LOAD_PIN,OUTPUT);
- digitalWrite(LOAD_PIN,LOW);
- digitalWrite(BACK_LIGHT_PIN,LOW);
-
- 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);
- }
- void loop()
- {
- read_data();
- run_charger();
- print_data();
- load_control();
- led_output();
- lcd_display();
- wifi_datalog();
-
- }
- int read_adc(int channel){
-
- int sum = 0;
- int temp;
- int i;
-
- for (i=0; i<AVG_NUM; i++) {
- temp = analogRead(channel);
- sum += temp;
- delayMicroseconds(50);
- }
- return(sum / AVG_NUM);
- }
- void read_data(void) {
-
- sol_amps = (read_adc(SOL_AMPS_CHAN) * SOL_AMPS_SCALE -12.01);
- sol_volts = read_adc(SOL_VOLTS_CHAN) * SOL_VOLTS_SCALE;
- bat_volts = read_adc(BAT_VOLTS_CHAN) * BAT_VOLTS_SCALE;
- sol_watts = sol_amps * sol_volts ;
- }
- void callback()
- {
- if (interrupt_counter++ > ONE_SECOND) {
- interrupt_counter = 0;
- seconds++;
- }
- }
- void set_pwm_duty(void) {
- if (pwm > PWM_MAX) {
- pwm = PWM_MAX;
- }
- else if (pwm < PWM_MIN) {
- pwm = PWM_MIN;
- }
- if (pwm < PWM_MAX) {
- Timer1.pwm(PWM_PIN,(PWM_FULL * (long)pwm / 100), 20);
-
- }
- else if (pwm == PWM_MAX) {
- Timer1.pwm(PWM_PIN,(PWM_FULL - 1), 20);
-
- }
- }
- void run_charger(void) {
-
- static int off_count = OFF_NUM;
- switch (charger_state) {
- case on:
- if (sol_watts < MIN_SOL_WATTS) {
- charger_state = off;
- off_count = OFF_NUM;
- TURN_OFF_MOSFETS;
- }
- else if (bat_volts > (BATT_FLOAT - 0.1)) {
- charger_state = bat_float;
- }
- else if (sol_watts < LOW_SOL_WATTS) {
- pwm = PWM_MAX;
- set_pwm_duty();
- }
- else {
- pwm = ((bat_volts * 10) / (sol_volts / 10)) + 5;
- charger_state = bulk;
- }
- break;
- case bulk:
- if (sol_watts < MIN_SOL_WATTS) {
- charger_state = off;
- off_count = OFF_NUM;
- TURN_OFF_MOSFETS;
- }
- else if (bat_volts > BATT_FLOAT) {
- charger_state = bat_float;
- }
- else if (sol_watts < LOW_SOL_WATTS) {
- charger_state = on;
- TURN_ON_MOSFETS;
- }
- else {
- if (old_sol_watts >= sol_watts) {
- delta = -delta;
- }
- pwm += delta;
- old_sol_watts = sol_watts;
- set_pwm_duty();
- }
- break;
- case bat_float:
- if (sol_watts < MIN_SOL_WATTS) {
- charger_state = off;
- off_count = OFF_NUM;
- TURN_OFF_MOSFETS;
- set_pwm_duty();
- }
- else if (bat_volts > BATT_FLOAT) {
- TURN_OFF_MOSFETS;
- pwm = PWM_MAX;
- set_pwm_duty();
- }
- else if (bat_volts < BATT_FLOAT) {
- pwm = PWM_MAX;
- set_pwm_duty();
- TURN_ON_MOSFETS;
- if (bat_volts < (BATT_FLOAT - 0.1)) {
- charger_state = bulk;
- }
- }
- break;
- case off:
- TURN_OFF_MOSFETS;
- if (off_count > 0) {
- off_count--;
- }
- else if ((bat_volts > BATT_FLOAT) && (sol_volts > bat_volts)) {
- charger_state = bat_float;
- }
- else if ((bat_volts > MIN_BAT_VOLTS) && (bat_volts < BATT_FLOAT) && (sol_volts > bat_volts)) {
- charger_state = bulk;
- }
- break;
- default:
- TURN_OFF_MOSFETS;
- break;
- }
- }
- void loop()
- {
- read_data();
- run_charger();
-
- load_control();
- led_output();
- lcd_display();
- wifi_datalog();
- delay(500);
- }
-
- void load_control(){
- if ((sol_watts < MIN_SOL_WATTS) && (bat_volts > LVD)){
- digitalWrite(LOAD_PIN, LOW);
- load_status = 1;
- }
- else{
- digitalWrite(LOAD_PIN, HIGH);
- load_status = 0;
- }
- }
- void print_data(void) {
-
- Serial.print(seconds,DEC);
- Serial.print(" ");
- Serial.print("Charging = ");
- if (charger_state == on) Serial.print("on ");
- else if (charger_state == off) Serial.print("off ");
- else if (charger_state == bulk) Serial.print("bulk ");
- else if (charger_state == bat_float) Serial.print("float");
- Serial.print(" ");
- Serial.print("pwm = ");
- Serial.print(pwm,DEC);
- Serial.print(" ");
- Serial.print("Current (panel) = ");
- Serial.print(sol_amps);
- Serial.print(" ");
- Serial.print("Voltage (panel) = ");
- Serial.print(sol_volts);
- Serial.print(" ");
- Serial.print("Power (panel) = ");
- Serial.print(sol_volts);
- Serial.print(" ");
- Serial.print("Battery Voltage = ");
- Serial.print(bat_volts);
- Serial.print(" ");
- Serial.print("\n\r");
-
- }
- void light_led(char pin)
- {
- static char last_lit;
- if (last_lit == pin)
- return;
- if (last_lit != 0)
- digitalWrite(last_lit, LOW);
- digitalWrite(pin, HIGH);
- last_lit = pin;
- }
- void led_output(void)
- {
- 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);
- }
- void lcd_display()
- {
- static bool current_backlight_state = -1;
- 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();
- else
- lcd.noBacklight();
- }
- if (back_light_pin_State == HIGH)
- {
- time = millis();
- }
-
- lcd.setCursor(0, 1);
- lcd.print(sol_volts);
- lcd.print("V ");
- lcd.setCursor(0, 2);
- lcd.print(sol_amps);
- lcd.print("A");
- lcd.setCursor(0, 3);
- lcd.print(sol_watts);
- lcd.print("W ");
- lcd.setCursor(8, 1);
- lcd.print(bat_volts);
- lcd.setCursor(8,2);
- if (charger_state == on)
- lcd.print("on ");
- else if (charger_state == off)
- lcd.print("off ");
- else if (charger_state == bulk)
- lcd.print("bulk ");
- else if (charger_state == bat_float)
- {
- lcd.print(" ");
- lcd.setCursor(8,2);
- lcd.print("float");
- }
-
-
-
-
- lcd.setCursor(8,3);
- 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("% ");
-
- lcd.setCursor(15,0);
- lcd.print("PWM");
- lcd.setCursor(19,0);
- lcd.write(3);
- lcd.setCursor(15,1);
- lcd.print(" ");
- lcd.setCursor(15,1);
- lcd.print(pwm);
- lcd.print("%");
-
-
-
- lcd.setCursor(15,2);
- lcd.print("Load");
- lcd.setCursor(15,3);
- if (load_status == 1)
- {
- lcd.print("On ");
- }
- else
- {
- lcd.print("Off ");
- }
- spinner();
- backLight_timer();
- }
- void backLight_timer(){
- if((millis() - time) <= 15000)
- lcd.backlight();
- else
- lcd.noBacklight();
- >>>>>>> bad0d746d2a86af933d66a9ca0fdc61c6db8619a
- }
- void spinner(void) {
- static int cspinner;
- static char spinner_chars[] = { '/','-','\\','|' };
- cspinner++;
- lcd.print(spinner_chars[cspinner%4]);
- }
- void wifi_datalog()
- {
-
- static int lastlogged;
- if ( seconds - lastlogged < 16 )
- return;
- lastlogged = seconds;
-
- char buf[16];
- String strTemp = dtostrf( sol_volts, 4, 1, buf);
-
- Serial.println(strTemp);
-
-
- String cmd = "AT+CIPSTART=\"TCP\",\"";
- cmd += "184.106.153.149";
- cmd += "\",80";
- ser.println(cmd);
-
- if(ser.find("Error")){
- Serial.println("AT+CIPSTART error");
- return;
- }
-
-
- String getStr = "GET /update?api_key=";
- getStr += apiKey;
- getStr +="&field1=";
- getStr += String(strTemp);
- getStr += "\r\n\r\n";
-
- cmd = "AT+CIPSEND=";
- cmd += String(getStr.length());
- ser.println(cmd);
- if(ser.find(">")){
- ser.print(getStr);
- }
- else{
- ser.println("AT+CIPCLOSE");
-
- Serial.println("AT+CIPCLOSE");
- }
- }
|