MPPT_Code_ESP8266.ino 30 KB

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