MPPT_Code_ESP8266.ino 29 KB

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