MPPT_Code_ESP8266.ino 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. //----------------------------------------------------------------------------------------------------
  2. // ARDUINO MPPT SOLAR CHARGE CONTROLLER (Version-3)
  3. // Author: Debasish Dutta/deba168
  4. // www.opengreenenergy.com
  5. //
  6. // This code is for an arduino Nano based Solar MPPT charge controller.
  7. // This code is a modified version of sample code from www.timnolan.com
  8. // updated on 16/06/2015
  9. //// Specifications : ////////////////////////////////////////////////////////////////////////////////////////////////////// //
  10. // 1.Solar panel power = 50W
  11. //
  12. // 2.Rated Battery Voltage= 12V ( lead acid type )
  13. // 3.Maximum Charging current = 5A //
  14. // 4.Maximum load current =10A //
  15. // 5. In put Voltage = Solar panel with Open circuit voltage from 17 to 25V //
  16. // 6. Datas dislay on 20x4 LCD //
  17. // 7. WiFi data logging throgh ESP8266 SOP //
  18. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  19. #include "TimerOne.h" // using Timer1 library from http://www.arduino.cc/playground/Code/Timer1
  20. #include <LiquidCrystal_I2C.h> // using the LCD I2C Library from https://bitbucket.org/fmalpartida/new-liquidcrystal/downloads
  21. #include <Wire.h>
  22. #include <SoftwareSerial.h> // using the Software Serial library Ref : http://www.arduino.cc/en/Reference/SoftwareSerialConstructor
  23. //----------------------------------------------------------------------------------------------------------
  24. //////// Arduino pins Connections//////////////////////////////////////////////////////////////////////////////////
  25. // A0 - Voltage divider (solar)
  26. // A1 - ACS 712 Out
  27. // A2 - Voltage divider (battery)
  28. // A4 - LCD SDA
  29. // A5 - LCD SCL
  30. // D2 - ESP8266 Tx
  31. // D3 - ESP8266 Rx through the voltage divider
  32. // D5 - LCD back control button
  33. // D6 - Load Control
  34. // D8 - 2104 MOSFET driver SD
  35. // D9 - 2104 MOSFET driver IN
  36. // D11- Green LED
  37. // D12- Yellow LED
  38. // D13- Red LED
  39. // Full scheatic is given at http://www.instructables.com/files/orig/F9A/LLR8/IAPASVA1/F9ALLR8IAPASVA1.pdf
  40. ///////// Definitions /////////////////////////////////////////////////////////////////////////////////////////////////
  41. #define SOL_VOLTS_CHAN 0 // defining the adc channel to read solar volts
  42. #define SOL_AMPS_CHAN 1 // Defining the adc channel to read solar amps
  43. #define BAT_VOLTS_CHAN 2 // defining the adc channel to read battery volts
  44. #define AVG_NUM 8 // number of iterations of the adc routine to average the adc readings
  45. // ACS 712 Current Sensor is used. Current Measured = (5/(1024 *0.185))*ADC - (2.5/0.185)
  46. #define SOL_AMPS_SCALE 0.026393581 // the scaling value for raw adc reading to get solar amps // 5/(1024*0.185)
  47. #define SOL_VOLTS_SCALE 0.029296875 // the scaling value for raw adc reading to get solar volts // (5/1024)*(R1+R2)/R2 // R1=100k and R2=20k
  48. #define BAT_VOLTS_SCALE 0.029442815 // the scaling value for raw adc reading to get battery volts
  49. #define PWM_PIN 9 // the output pin for the pwm (only pin 9 avaliable for timer 1 at 50kHz)
  50. #define PWM_ENABLE_PIN 8 // pin used to control shutoff function of the IR2104 MOSFET driver (hight the mosfet driver is on)
  51. #define PWM_FULL 1023 // the actual value used by the Timer1 routines for 100% pwm duty cycle
  52. #define PWM_MAX 100 // the value for pwm duty cyle 0-100%
  53. #define PWM_MIN 60 // the value for pwm duty cyle 0-100% (below this value the current running in the system is = 0)
  54. #define PWM_START 90 // the value for pwm duty cyle 0-100%
  55. #define PWM_INC 1 //the value the increment to the pwm value for the ppt algorithm
  56. #define TRUE 1
  57. #define FALSE 0
  58. #define ON TRUE
  59. #define OFF FALSE
  60. #define TURN_ON_MOSFETS digitalWrite(PWM_ENABLE_PIN, HIGH) // enable MOSFET driver
  61. #define TURN_OFF_MOSFETS digitalWrite(PWM_ENABLE_PIN, LOW) // disable MOSFET driver
  62. #define ONE_SECOND 50000 //count for number of interrupt in 1 second on interrupt period of 20us
  63. #define LOW_SOL_WATTS 5.00 //value of solar watts // this is 5.00 watts
  64. #define MIN_SOL_WATTS 1.00 //value of solar watts // this is 1.00 watts
  65. #define MIN_BAT_VOLTS 11.00 //value of battery voltage // this is 11.00 volts
  66. #define MAX_BAT_VOLTS 14.10 //value of battery voltage// this is 14.10 volts
  67. #define HIGH_BAT_VOLTS 13.00 //value of battery voltage // this is 13.00 volts
  68. #define LVD 11.5 //Low voltage disconnect setting for a 12V system
  69. #define OFF_NUM 9 // number of iterations of off charger state
  70. //------------------------------------------------------------------------------------------------------
  71. //Defining led pins for indication
  72. #define LED_RED 11
  73. #define LED_GREEN 12
  74. #define LED_YELLOW 13
  75. //-----------------------------------------------------------------------------------------------------
  76. // Defining load control pin
  77. #define LOAD_PIN 6 // pin-2 is used to control the load
  78. //-----------------------------------------------------------------------------------------------------
  79. // Defining lcd back light pin
  80. #define BACK_LIGHT_PIN 5 // pin-5 is used to control the lcd back light
  81. // ---------------------------For ESP8266--------------------------------------------------------------
  82. // replace with your channel's thingspeak API key
  83. String apiKey = "DPK8RMTFY2B1XCAF";
  84. // connect 2 to TX of Serial USB
  85. // connect 3 to RX of serial USB
  86. SoftwareSerial ser(2,3); // RX, TX
  87. //------------------------------------------------------------------------------------------------------
  88. /////////////////////////////////////////BIT MAP ARRAY//////////////////////////////////////////////////
  89. //-------------------------------------------------------------------------------------------------------
  90. byte solar[8] = //icon for termometer
  91. {
  92. 0b11111,
  93. 0b10101,
  94. 0b11111,
  95. 0b10101,
  96. 0b11111,
  97. 0b10101,
  98. 0b11111,
  99. 0b00000
  100. };
  101. byte battery[8]=
  102. {
  103. 0b01110,
  104. 0b11011,
  105. 0b10001,
  106. 0b10001,
  107. 0b11111,
  108. 0b11111,
  109. 0b11111,
  110. 0b11111,
  111. };
  112. byte _PWM [8]=
  113. {
  114. 0b11101,
  115. 0b10101,
  116. 0b10101,
  117. 0b10101,
  118. 0b10101,
  119. 0b10101,
  120. 0b10101,
  121. 0b10111,
  122. };
  123. //-------------------------------------------------------------------------------------------------------
  124. // global variables
  125. int count = 0;
  126. int pwm = 0; //pwm duty cycle 0-100%
  127. float sol_amps; // solar amps
  128. float sol_volts; // solar volts
  129. float bat_volts; // battery volts
  130. float sol_watts; // solar watts
  131. float old_sol_watts = 0; // solar watts from previous time through ppt routine
  132. unsigned int seconds = 0; // seconds from timer routine
  133. unsigned int prev_seconds = 0; // seconds value from previous pass
  134. unsigned int interrupt_counter = 0; // counter for 20us interrrupt
  135. boolean led_on = TRUE;
  136. int led_counter = 0;
  137. int delta = PWM_INC; // variable used to modify pwm duty cycle for the ppt algorithm
  138. enum charger_mode {off, on, bulk, bat_float} charger_state; // enumerated variable that holds state for charger state machine
  139. // set the LCD address to 0x27 for a 20 chars 4 line display
  140. // Set the pins on the I2C chip used for LCD connections:
  141. // addr, en,rw,rs,d4,d5,d6,d7,bl,blpol
  142. LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address
  143. long time = 0; // variable to store time the back light control button was pressed in millis
  144. int load_pin = 6;
  145. int back_light_pin_State = 0;
  146. int load_status=0;
  147. //------------------------------------------------------------------------------------------------------
  148. // This routine is automatically called at powerup/reset
  149. //------------------------------------------------------------------------------------------------------
  150. void setup() // run once, when the sketch starts
  151. {
  152. pinMode(LED_RED, OUTPUT);
  153. pinMode(LED_GREEN, OUTPUT);
  154. pinMode(LED_YELLOW, OUTPUT);
  155. pinMode(PWM_ENABLE_PIN, OUTPUT); // sets the digital pin as output
  156. Timer1.initialize(20); // initialize timer1, and set a 20uS period
  157. Timer1.pwm(PWM_PIN, 0); // setup pwm on pin 9, 0% duty cycle
  158. TURN_OFF_MOSFETS; //turn off MOSFET driver chip
  159. Timer1.attachInterrupt(callback); // attaches callback() as a timer overflow interrupt
  160. Serial.begin(9600); // open the serial port at 9600 bps:
  161. ser.begin(9600); // enable software serial
  162. ser.println("AT+RST"); // reset ESP8266
  163. pwm = PWM_START; //starting value for pwm
  164. charger_state = off; // start with charger state as off
  165. pinMode(BACK_LIGHT_PIN, INPUT);
  166. pinMode(LOAD_PIN,OUTPUT);
  167. digitalWrite(LOAD_PIN,LOW); // default load state is OFF
  168. lcd.begin(20,4); // initialize the lcd for 16 chars 2 lines, turn on backlight
  169. lcd.noBacklight();
  170. lcd.createChar(1,solar);
  171. lcd.createChar(2,battery);
  172. lcd.createChar(3,_PWM);
  173. }
  174. //------------------------------------------------------------------------------------------------------
  175. // Main loop
  176. //------------------------------------------------------------------------------------------------------
  177. void loop()
  178. {
  179. read_data(); //read data from inputs
  180. run_charger(); //run the charger state machine
  181. print_data(); //print data
  182. load_control(); // control the connected load
  183. led_output(); // led indication
  184. lcd_display(); // lcd display
  185. wifi_datalog(); // sends data to thingspeak
  186. }
  187. //------------------------------------------------------------------------------------------------------
  188. // This routine reads and averages the analog inputs for this system, solar volts, solar amps and
  189. // battery volts.
  190. //------------------------------------------------------------------------------------------------------
  191. int read_adc(int channel){
  192. int sum = 0;
  193. int temp;
  194. int i;
  195. for (i=0; i<AVG_NUM; i++) { // loop through reading raw adc values AVG_NUM number of times
  196. temp = analogRead(channel); // read the input pin
  197. sum += temp; // store sum for averaging
  198. delayMicroseconds(50); // pauses for 50 microseconds
  199. }
  200. return(sum / AVG_NUM); // divide sum by AVG_NUM to get average and return it
  201. }
  202. //------------------------------------------------------------------------------------------------------
  203. // This routine reads all the analog input values for the system. Then it multiplies them by the scale
  204. // factor to get actual value in volts or amps.
  205. //------------------------------------------------------------------------------------------------------
  206. void read_data(void) {
  207. sol_amps = (read_adc(SOL_AMPS_CHAN) * SOL_AMPS_SCALE -13.51); //input of solar amps
  208. sol_volts = read_adc(SOL_VOLTS_CHAN) * SOL_VOLTS_SCALE; //input of solar volts
  209. bat_volts = read_adc(BAT_VOLTS_CHAN) * BAT_VOLTS_SCALE; //input of battery volts
  210. sol_watts = sol_amps * sol_volts ; //calculations of solar watts
  211. }
  212. //------------------------------------------------------------------------------------------------------
  213. // This is interrupt service routine for Timer1 that occurs every 20uS.
  214. //
  215. //------------------------------------------------------------------------------------------------------
  216. void callback()
  217. {
  218. if (interrupt_counter++ > ONE_SECOND) { //increment interrupt_counter until one second has passed
  219. interrupt_counter = 0;
  220. seconds++; //then increment seconds counter
  221. }
  222. }
  223. //------------------------------------------------------------------------------------------------------
  224. // This routine uses the Timer1.pwm function to set the pwm duty cycle.
  225. //------------------------------------------------------------------------------------------------------
  226. void set_pwm_duty(void) {
  227. if (pwm > PWM_MAX) { // check limits of PWM duty cyle and set to PWM_MAX
  228. pwm = PWM_MAX;
  229. }
  230. else if (pwm < PWM_MIN) { // if pwm is less than PWM_MIN then set it to PWM_MIN
  231. pwm = PWM_MIN;
  232. }
  233. if (pwm < PWM_MAX) {
  234. Timer1.pwm(PWM_PIN,(PWM_FULL * (long)pwm / 100), 20); // use Timer1 routine to set pwm duty cycle at 20uS period
  235. //Timer1.pwm(PWM_PIN,(PWM_FULL * (long)pwm / 100));
  236. }
  237. else if (pwm == PWM_MAX) { // if pwm set to 100% it will be on full but we have
  238. Timer1.pwm(PWM_PIN,(PWM_FULL - 1), 1000); // keep switching so set duty cycle at 99.9% and slow down to 1000uS period
  239. //Timer1.pwm(PWM_PIN,(PWM_FULL - 1));
  240. }
  241. }
  242. //------------------------------------------------------------------------------------------------------
  243. // This routine is the charger state machine. It has four states on, off, bulk and float.
  244. // It's called once each time through the main loop to see what state the charger should be in.
  245. // The battery charger can be in one of the following four states:
  246. //
  247. // On State - this is charger state for MIN_SOL_WATTS < solar watts < LOW_SOL_WATTS. In this state isthe solar
  248. // watts input is too low for the bulk charging state but not low enough to go into the off state.
  249. // In this state we just set the pwm = 99.9% to get the most of low amount of power available.
  250. // Bulk State - this is charger state for solar watts > MIN_SOL_WATTS. This is where we do the bulk of the battery
  251. // charging and where we run the Peak Power Tracking alogorithm. In this state we try and run the maximum amount
  252. // of current that the solar panels are generating into the battery.
  253. // Float State - As the battery charges it's voltage rises. When it gets to the MAX_BAT_VOLTS we are done with the
  254. // bulk battery charging and enter the battery float state. In this state we try and keep the battery voltage
  255. // at MAX_BAT_VOLTS by adjusting the pwm value. If we get to pwm = 100% it means we can't keep the battery
  256. // voltage at MAX_BAT_VOLTS which probably means the battery is being drawn down by some load so we need to back
  257. // into the bulk charging mode.
  258. // Off State - This is state that the charger enters when solar watts < MIN_SOL_WATTS. The charger goes into this
  259. // state when there is no more power being generated by the solar panels. The MOSFETs are turned
  260. // off in this state so that power from the battery doesn't leak back into the solar panel.
  261. //------------------------------------------------------------------------------------------------------
  262. void run_charger(void) {
  263. static int off_count = OFF_NUM;
  264. switch (charger_state) {
  265. case on:
  266. if (sol_watts < MIN_SOL_WATTS) { //if watts input from the solar panel is less than
  267. charger_state = off; //the minimum solar watts then
  268. off_count = OFF_NUM; //go to the charger off state
  269. TURN_OFF_MOSFETS;
  270. }
  271. else if (bat_volts > MAX_BAT_VOLTS) { //else if the battery voltage has gotten above the float
  272. charger_state = bat_float; //battery float voltage go to the charger battery float state
  273. }
  274. else if (sol_watts < LOW_SOL_WATTS) { //else if the solar input watts is less than low solar watts
  275. pwm = PWM_MAX; //it means there is not much power being generated by the solar panel
  276. set_pwm_duty(); //so we just set the pwm = 100% so we can get as much of this power as possible
  277. } //and stay in the charger on state
  278. else {
  279. pwm = ((bat_volts * 10) / (sol_volts / 10)) + 5; //else if we are making more power than low solar watts figure out what the pwm
  280. charger_state = bulk; //value should be and change the charger to bulk state
  281. }
  282. break;
  283. case bulk:
  284. if (sol_watts < MIN_SOL_WATTS) { //if watts input from the solar panel is less than
  285. charger_state = off; //the minimum solar watts then it is getting dark so
  286. off_count = OFF_NUM; //go to the charger off state
  287. TURN_OFF_MOSFETS;
  288. }
  289. else if (bat_volts > MAX_BAT_VOLTS) { //else if the battery voltage has gotten above the float
  290. charger_state = bat_float; //battery float voltage go to the charger battery float state
  291. }
  292. else if (sol_watts < LOW_SOL_WATTS) { //else if the solar input watts is less than low solar watts
  293. charger_state = on; //it means there is not much power being generated by the solar panel
  294. TURN_ON_MOSFETS; //so go to charger on state
  295. }
  296. else { // this is where we do the Peak Power Tracking ro Maximum Power Point algorithm
  297. if (old_sol_watts >= sol_watts) { // if previous watts are greater change the value of
  298. delta = -delta; // delta to make pwm increase or decrease to maximize watts
  299. }
  300. pwm += delta; // add delta to change PWM duty cycle for PPT algorythm (compound addition)
  301. old_sol_watts = sol_watts; // load old_watts with current watts value for next time
  302. set_pwm_duty(); // set pwm duty cycle to pwm value
  303. }
  304. break;
  305. case bat_float:
  306. if (sol_watts < MIN_SOL_WATTS) { //if watts input from the solar panel is less than
  307. charger_state = off; //the minimum solar watts then it is getting dark so
  308. off_count = OFF_NUM; //go to the charger off state
  309. set_pwm_duty();
  310. TURN_OFF_MOSFETS;
  311. }
  312. else if (bat_volts > MAX_BAT_VOLTS) { //since we're in the battery float state if the battery voltage
  313. pwm -= 1; //is above the float voltage back off the pwm to lower it
  314. set_pwm_duty();
  315. }
  316. else if (bat_volts < MAX_BAT_VOLTS) { //else if the battery voltage is less than the float voltage
  317. pwm += 1; //increment the pwm to get it back up to the float voltage
  318. set_pwm_duty();
  319. if (pwm >= 100) { //if pwm gets up to 100 it means we can't keep the battery at
  320. charger_state = bulk; //float voltage so jump to charger bulk state to charge the battery
  321. }
  322. }
  323. break;
  324. case off: //when we jump into the charger off state, off_count is set with OFF_NUM
  325. if (off_count > 0) { //this means that we run through the off state OFF_NUM of times with out doing
  326. off_count--; //anything, this is to allow the battery voltage to settle down to see if the
  327. } //battery has been disconnected
  328. else if ((bat_volts > HIGH_BAT_VOLTS) && (bat_volts < MAX_BAT_VOLTS) && (sol_volts > bat_volts)) {
  329. charger_state = bat_float; //if battery voltage is still high and solar volts are high
  330. set_pwm_duty(); //change charger state to battery float
  331. TURN_ON_MOSFETS;
  332. }
  333. else if ((bat_volts > MIN_BAT_VOLTS) && (bat_volts < MAX_BAT_VOLTS) && (sol_volts > bat_volts)) {
  334. pwm = PWM_START; //if battery volts aren't quite so high but we have solar volts
  335. set_pwm_duty(); //greater than battery volts showing it is day light then
  336. charger_state = on; //change charger state to on so we start charging
  337. TURN_ON_MOSFETS;
  338. } //else stay in the off state
  339. break;
  340. default:
  341. TURN_OFF_MOSFETS;
  342. break;
  343. }
  344. }
  345. //----------------------------------------------------------------------------------------------------------------------
  346. /////////////////////////////////////////////LOAD CONTROL/////////////////////////////////////////////////////
  347. //----------------------------------------------------------------------------------------------------------------------
  348. void load_control()
  349. {
  350. if (sol_watts < MIN_SOL_WATTS) // load will on when night
  351. {
  352. if(bat_volts >LVD) // check if battery is healthy
  353. {
  354. load_status=1;
  355. digitalWrite(LOAD_PIN, LOW); // load is ON
  356. }
  357. else if(bat_volts < LVD)
  358. {
  359. load_status=0;
  360. digitalWrite(LOAD_PIN, HIGH); //load is OFF
  361. }
  362. }
  363. else // load will off during day
  364. {
  365. load_status=0;
  366. digitalWrite(LOAD_PIN, HIGH);
  367. }
  368. }
  369. //------------------------------------------------------------------------------------------------------
  370. // This routine prints all the data out to the serial port.
  371. //------------------------------------------------------------------------------------------------------
  372. void print_data(void) {
  373. Serial.print(seconds,DEC);
  374. Serial.print(" ");
  375. Serial.print("Charging = ");
  376. if (charger_state == on) Serial.print("on ");
  377. else if (charger_state == off) Serial.print("off ");
  378. else if (charger_state == bulk) Serial.print("bulk ");
  379. else if (charger_state == bat_float) Serial.print("float");
  380. Serial.print(" ");
  381. Serial.print("pwm = ");
  382. Serial.print(pwm,DEC);
  383. Serial.print(" ");
  384. Serial.print("Current (panel) = ");
  385. Serial.print(sol_amps);
  386. Serial.print(" ");
  387. Serial.print("Voltage (panel) = ");
  388. Serial.print(sol_volts);
  389. Serial.print(" ");
  390. Serial.print("Power (panel) = ");
  391. Serial.print(sol_watts);
  392. Serial.print(" ");
  393. Serial.print("Battery Voltage = ");
  394. Serial.print(bat_volts);
  395. Serial.print(" ");
  396. Serial.print("\n\r");
  397. delay(100);
  398. }
  399. //-------------------------------------------------------------------------------------------------
  400. //---------------------------------Led Indication--------------------------------------------------
  401. //-------------------------------------------------------------------------------------------------
  402. void led_output(void)
  403. {
  404. if(bat_volts > 14.1 )
  405. {
  406. leds_off_all();
  407. digitalWrite(LED_YELLOW, HIGH);
  408. }
  409. else if(bat_volts > 11.9 && bat_volts < 14.1)
  410. {
  411. leds_off_all();
  412. digitalWrite(LED_GREEN, HIGH);
  413. }
  414. else if(bat_volts < 11.8)
  415. {
  416. leds_off_all;
  417. digitalWrite(LED_RED, HIGH);
  418. }
  419. }
  420. //------------------------------------------------------------------------------------------------------
  421. //
  422. // This function is used to turn all the leds off
  423. //
  424. //------------------------------------------------------------------------------------------------------
  425. void leds_off_all(void)
  426. {
  427. digitalWrite(LED_GREEN, LOW);
  428. digitalWrite(LED_RED, LOW);
  429. digitalWrite(LED_YELLOW, LOW);
  430. }
  431. //------------------------------------------------------------------------------------------------------
  432. //-------------------------- LCD DISPLAY --------------------------------------------------------------
  433. //-------------------------------------------------------------------------------------------------------
  434. void lcd_display()
  435. {
  436. back_light_pin_State = digitalRead(BACK_LIGHT_PIN);
  437. if (back_light_pin_State == HIGH)
  438. {
  439. time = millis(); // If any of the buttons are pressed, save the time in millis to "time"
  440. }
  441. lcd.setCursor(0, 0);
  442. lcd.print("SOL");
  443. lcd.setCursor(4, 0);
  444. lcd.write(1);
  445. lcd.setCursor(0, 1);
  446. lcd.print(sol_volts);
  447. lcd.print("V");
  448. lcd.setCursor(0, 2);
  449. lcd.print(sol_amps);
  450. lcd.print("A");
  451. lcd.setCursor(0, 3);
  452. lcd.print(sol_watts);
  453. lcd.print("W ");
  454. lcd.setCursor(8, 0);
  455. lcd.print("BAT");
  456. lcd.setCursor(12, 0);
  457. lcd.write(2);
  458. lcd.setCursor(8, 1);
  459. lcd.print(bat_volts);
  460. lcd.setCursor(8,2);
  461. if (charger_state == on)
  462. lcd.print("on");
  463. else if (charger_state == off)
  464. lcd.print("off");
  465. else if (charger_state == bulk)
  466. lcd.print("bulk");
  467. else if (charger_state == bat_float)
  468. lcd.print("float");
  469. //-----------------------------------------------------------
  470. //--------------------Battery State Of Charge ---------------
  471. //-----------------------------------------------------------
  472. lcd.setCursor(8,3);
  473. if ( bat_volts >= 12.7)
  474. lcd.print( "100%");
  475. else if (bat_volts >= 12.5 && bat_volts < 12.7)
  476. lcd.print( "90%");
  477. else if (bat_volts >= 12.42 && bat_volts < 12.5)
  478. lcd.print( "80%");
  479. else if (bat_volts >= 12.32 && bat_volts < 12.42)
  480. lcd.print( "70%");
  481. else if (bat_volts >= 12.2 && bat_volts < 12.32)
  482. lcd.print( "60%");
  483. else if (bat_volts >= 12.06 && bat_volts < 12.2)
  484. lcd.print( "50%");
  485. else if (bat_volts >= 11.90 && bat_volts < 12.06)
  486. lcd.print( "40%");
  487. else if (bat_volts >= 11.75 && bat_volts < 11.90)
  488. lcd.print( "30%");
  489. else if (bat_volts >= 11.58 && bat_volts < 11.75)
  490. lcd.print( "20%");
  491. else if (bat_volts >= 11.31 && bat_volts < 11.58)
  492. lcd.print( "10%");
  493. else if (bat_volts < 11.3)
  494. lcd.print( "0%");
  495. //---------------------------------------------------------------------
  496. //------------------Duty Cycle-----------------------------------------
  497. //---------------------------------------------------------------------
  498. lcd.setCursor(15,0);
  499. lcd.print("PWM");
  500. lcd.setCursor(19,0);
  501. lcd.write(3);
  502. lcd.setCursor(15,1);
  503. lcd.print(pwm);
  504. lcd.print("%");
  505. //----------------------------------------------------------------------
  506. //------------------------Load Status-----------------------------------
  507. //----------------------------------------------------------------------
  508. lcd.setCursor(15,2);
  509. lcd.print("Load");
  510. lcd.setCursor(15,3);
  511. if (load_status == 1)
  512. {
  513. lcd.print("On");
  514. }
  515. else
  516. {
  517. lcd.print("Off");
  518. }
  519. backLight_timer(); // call the backlight timer function in every loop
  520. }
  521. void backLight_timer(){
  522. if((millis() - time) <= 15000) // if it's been less than the 15 secs, turn the backlight on
  523. lcd.backlight(); // finish with backlight on
  524. else
  525. lcd.noBacklight(); // if it's been more than 15 secs, turn the backlight off
  526. }
  527. //-------------------------------------------------------------------------
  528. //----------------------------- ESP8266 WiFi ------------------------------
  529. //--------------------------Plot System data on thingspeak.com-------------
  530. //-------------------------------------------------------------------------
  531. void wifi_datalog()
  532. {
  533. // convert to string
  534. char buf[16];
  535. String strTemp = dtostrf( sol_volts, 4, 1, buf);
  536. Serial.println(strTemp);
  537. // TCP connection
  538. String cmd = "AT+CIPSTART=\"TCP\",\"";
  539. cmd += "184.106.153.149"; // api.thingspeak.com
  540. cmd += "\",80";
  541. ser.println(cmd);
  542. if(ser.find("Error")){
  543. Serial.println("AT+CIPSTART error");
  544. return;
  545. }
  546. // prepare GET string
  547. String getStr = "GET /update?api_key=";
  548. getStr += apiKey;
  549. getStr +="&field1=";
  550. getStr += String(strTemp);
  551. getStr += "\r\n\r\n";
  552. // send data length
  553. cmd = "AT+CIPSEND=";
  554. cmd += String(getStr.length());
  555. ser.println(cmd);
  556. if(ser.find(">")){
  557. ser.print(getStr);
  558. }
  559. else{
  560. ser.println("AT+CIPCLOSE");
  561. // alert user
  562. Serial.println("AT+CIPCLOSE");
  563. }
  564. // thingspeak needs 15 sec delay between updates
  565. delay(16000);
  566. }