MPPT_Code_ESP8266.ino 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. //------------------------------------------------------------------------------------------------------
  2. //
  3. // ARDUINO SOLAR CHARGE CONTROLLER (MPPT)
  4. //
  5. // This code is a modified version of sample code from http://www.timnolan.com/.
  6. // modified by deba168
  7. // dated 08/02/2015
  8. //Last updated on 26/03/2015
  9. //------------------------------------------------------------------------------------------------------
  10. #include "TimerOne.h" // using Timer1 library from http://www.arduino.cc/playground/Code/Timer1
  11. #include <LiquidCrystal_I2C.h> // using the LCD I2C Library from https://bitbucket.org/fmalpartida/new-liquidcrystal/downloads
  12. #include <Wire.h>
  13. #include <SoftwareSerial.h>
  14. // SDA....>A4
  15. // SCL....>A5
  16. //------------------------------------------------------------------------------------------------------
  17. // definitions
  18. #define SOL_AMPS_CHAN 1 // Defining the adc channel to read solar amps
  19. #define SOL_VOLTS_CHAN 0 // defining the adc channel to read solar volts
  20. #define BAT_VOLTS_CHAN 2 // defining the adc channel to read battery volts
  21. #define AVG_NUM 8 // number of iterations of the adc routine to average the adc readings
  22. #define SOL_AMPS_SCALE 0.02637 // the scaling value for raw adc reading to get solar amps
  23. #define SOL_VOLTS_SCALE 0.02928 // the scaling value for raw adc reading to get solar volts // (5/1024)*(R1+R2)/R2
  24. #define BAT_VOLTS_SCALE 0.02928 // the scaling value for raw adc reading to get battery volts
  25. #define PWM_PIN 9 // the output pin for the pwm (only pin 9 avaliable for timer 1 at 50kHz)
  26. #define PWM_ENABLE_PIN 8 // pin used to control shutoff function of the IR2104 MOSFET driver (hight the mosfet driver is on)
  27. #define PWM_FULL 1023 // the actual value used by the Timer1 routines for 100% pwm duty cycle
  28. #define PWM_MAX 100 // the value for pwm duty cyle 0-100%
  29. #define PWM_MIN 60 // the value for pwm duty cyle 0-100% (below this value the current running in the system is = 0)
  30. #define PWM_START 90 // the value for pwm duty cyle 0-100%
  31. #define PWM_INC 1 //the value the increment to the pwm value for the ppt algorithm
  32. #define TRUE 1
  33. #define FALSE 0
  34. #define ON TRUE
  35. #define OFF FALSE
  36. #define TURN_ON_MOSFETS digitalWrite(PWM_ENABLE_PIN, HIGH) // enable MOSFET driver
  37. #define TURN_OFF_MOSFETS digitalWrite(PWM_ENABLE_PIN, LOW) // disable MOSFET driver
  38. #define ONE_SECOND 50000 //count for number of interrupt in 1 second on interrupt period of 20us
  39. #define LOW_SOL_WATTS 5.00 //value of solar watts // this is 5.00 watts
  40. #define MIN_SOL_WATTS 1.00 //value of solar watts // this is 1.00 watts
  41. #define MIN_BAT_VOLTS 11.00 //value of battery voltage // this is 11.00 volts
  42. #define MAX_BAT_VOLTS 14.10 //value of battery voltage// this is 14.10 volts
  43. #define HIGH_BAT_VOLTS 13.00 //value of battery voltage // this is 13.00 volts
  44. #define LVD 11.5 //Low voltage disconnect setting for a 12V system
  45. #define OFF_NUM 9 // number of iterations of off charger state
  46. //------------------------------------------------------------------------------------------------------
  47. //Defining led pins for indication
  48. #define LED_RED 11
  49. #define LED_GREEN 12
  50. #define LED_YELLOW 13
  51. //-----------------------------------------------------------------------------------------------------
  52. // Defining load control pin
  53. #define LOAD_PIN 6 // pin-2 is used to control the load
  54. //-----------------------------------------------------------------------------------------------------
  55. // Defining lcd back light pin
  56. #define BACK_LIGHT_PIN 5 // pin-2 is used to control the load
  57. //------------------------------------------------------------------------------------------------------
  58. /////////////////////////////////////////BIT MAP ARRAY//////////////////////////////////////////////////
  59. //-------------------------------------------------------------------------------------------------------
  60. byte solar[8] = //icon for termometer
  61. {
  62. 0b11111,
  63. 0b10101,
  64. 0b11111,
  65. 0b10101,
  66. 0b11111,
  67. 0b10101,
  68. 0b11111,
  69. 0b00000
  70. };
  71. byte battery[8]=
  72. {
  73. 0b01110,
  74. 0b11011,
  75. 0b10001,
  76. 0b10001,
  77. 0b11111,
  78. 0b11111,
  79. 0b11111,
  80. 0b11111,
  81. };
  82. byte _PWM [8]=
  83. {
  84. 0b11101,
  85. 0b10101,
  86. 0b10101,
  87. 0b10101,
  88. 0b10101,
  89. 0b10101,
  90. 0b10101,
  91. 0b10111,
  92. };
  93. //-------------------------------------------------------------------------------------------------------
  94. // global variables
  95. int count = 0;
  96. int pwm = 0; //pwm duty cycle 0-100%
  97. float sol_amps; // solar amps
  98. float sol_volts; // solar volts
  99. float bat_volts; // battery volts
  100. float sol_watts; // solar watts
  101. float old_sol_watts = 0; // solar watts from previous time through ppt routine
  102. unsigned int seconds = 0; // seconds from timer routine
  103. unsigned int prev_seconds = 0; // seconds value from previous pass
  104. unsigned int interrupt_counter = 0; // counter for 20us interrrupt
  105. boolean led_on = TRUE;
  106. int led_counter = 0;
  107. int delta = PWM_INC; // variable used to modify pwm duty cycle for the ppt algorithm
  108. enum charger_mode {off, on, bulk, bat_float} charger_state; // enumerated variable that holds state for charger state machine
  109. // set the LCD address to 0x27 for a 20 chars 4 line display
  110. // Set the pins on the I2C chip used for LCD connections:
  111. // addr, en,rw,rs,d4,d5,d6,d7,bl,blpol
  112. LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address
  113. //int back_light_Pin = 5;
  114. //int load_pin =6;
  115. int back_light_pin_State = 0;
  116. int load_status=0;
  117. // ---------------------------For ESP8266--------------------------------------------------------------
  118. ///////////////////////////////////////////////////////////////////////////////////////////////////////
  119. // replace with your channel's thingspeak API key
  120. String apiKey = "DPK8RMTFY2B1XCAF";
  121. // connect 2 to TX of Serial USB
  122. // connect 3 to RX of serial USB
  123. SoftwareSerial ser(2,3); // RX, TX
  124. //------------------------------------------------------------------------------------------------------
  125. // This routine is automatically called at powerup/reset
  126. //------------------------------------------------------------------------------------------------------
  127. void setup() // run once, when the sketch starts
  128. {
  129. pinMode(LED_RED, OUTPUT);
  130. pinMode(LED_GREEN, OUTPUT);
  131. pinMode(LED_YELLOW, OUTPUT);
  132. pinMode(PWM_ENABLE_PIN, OUTPUT); // sets the digital pin as output
  133. Timer1.initialize(20); // initialize timer1, and set a 20uS period
  134. Timer1.pwm(PWM_PIN, 0); // setup pwm on pin 9, 0% duty cycle
  135. TURN_OFF_MOSFETS; //turn off MOSFET driver chip
  136. Timer1.attachInterrupt(callback); // attaches callback() as a timer overflow interrupt
  137. Serial.begin(9600); // open the serial port
  138. ser.begin(9600); // enable software serial
  139. ser.println("AT+RST"); // reset ESP8266
  140. pwm = PWM_START; //starting value for pwm
  141. charger_state = off; // start with charger state as off
  142. pinMode(BACK_LIGHT_PIN, INPUT);
  143. pinMode(LOAD_PIN,OUTPUT);
  144. digitalWrite(LOAD_PIN,LOW); // default load state is OFF
  145. digitalWrite(BACK_LIGHT_PIN,LOW); // default LCd back light is OFF
  146. lcd.begin(20,4); // initialize the lcd for 16 chars 2 lines, turn on backlight
  147. lcd.noBacklight();
  148. lcd.createChar(1,solar);
  149. lcd.createChar(2,battery);
  150. lcd.createChar(3,_PWM);
  151. }
  152. //------------------------------------------------------------------------------------------------------
  153. // This is interrupt service routine for Timer1 that occurs every 20uS.
  154. //
  155. //------------------------------------------------------------------------------------------------------
  156. void callback()
  157. {
  158. if (interrupt_counter++ > ONE_SECOND) { //increment interrupt_counter until one second has passed
  159. interrupt_counter = 0;
  160. seconds++; //then increment seconds counter
  161. }
  162. }
  163. //------------------------------------------------------------------------------------------------------
  164. // This routine reads and averages the analog inputs for this system, solar volts, solar amps and
  165. // battery volts.
  166. //------------------------------------------------------------------------------------------------------
  167. int read_adc(int channel){
  168. int sum = 0;
  169. int temp;
  170. int i;
  171. for (i=0; i<AVG_NUM; i++) { // loop through reading raw adc values AVG_NUM number of times
  172. temp = analogRead(channel); // read the input pin
  173. sum += temp; // store sum for averaging
  174. delayMicroseconds(50); // pauses for 50 microseconds
  175. }
  176. return(sum / AVG_NUM); // divide sum by AVG_NUM to get average and return it
  177. }
  178. //------------------------------------------------------------------------------------------------------
  179. // This routine uses the Timer1.pwm function to set the pwm duty cycle.
  180. //------------------------------------------------------------------------------------------------------
  181. void set_pwm_duty(void) {
  182. if (pwm > PWM_MAX) { // check limits of PWM duty cyle and set to PWM_MAX
  183. pwm = PWM_MAX;
  184. }
  185. else if (pwm < PWM_MIN) { // if pwm is less than PWM_MIN then set it to PWM_MIN
  186. pwm = PWM_MIN;
  187. }
  188. if (pwm < PWM_MAX) {
  189. Timer1.pwm(PWM_PIN,(PWM_FULL * (long)pwm / 100), 20); // use Timer1 routine to set pwm duty cycle at 20uS period
  190. //Timer1.pwm(PWM_PIN,(PWM_FULL * (long)pwm / 100));
  191. }
  192. else if (pwm == PWM_MAX) { // if pwm set to 100% it will be on full but we have
  193. Timer1.pwm(PWM_PIN,(PWM_FULL - 1), 1000); // keep switching so set duty cycle at 99.9% and slow down to 1000uS period
  194. //Timer1.pwm(PWM_PIN,(PWM_FULL - 1));
  195. }
  196. }
  197. //------------------------------------------------------------------------------------------------------
  198. // This routine prints all the data out to the serial port.
  199. //------------------------------------------------------------------------------------------------------
  200. void print_data(void) {
  201. Serial.print(seconds,DEC);
  202. Serial.print(" ");
  203. Serial.print("Charging = ");
  204. if (charger_state == on) Serial.print("on ");
  205. else if (charger_state == off) Serial.print("off ");
  206. else if (charger_state == bulk) Serial.print("bulk ");
  207. else if (charger_state == bat_float) Serial.print("float");
  208. Serial.print(" ");
  209. Serial.print("pwm = ");
  210. Serial.print(pwm,DEC);
  211. Serial.print(" ");
  212. Serial.print("Current (panel) = ");
  213. //print_int100_dec2(sol_amps);
  214. Serial.print(sol_amps);
  215. Serial.print(" ");
  216. Serial.print("Voltage (panel) = ");
  217. Serial.print(sol_volts);
  218. //print_int100_dec2(sol_volts);
  219. Serial.print(" ");
  220. Serial.print("Power (panel) = ");
  221. Serial.print(sol_volts);
  222. // print_int100_dec2(sol_watts);
  223. Serial.print(" ");
  224. Serial.print("Battery Voltage = ");
  225. Serial.print(bat_volts);
  226. //print_int100_dec2(bat_volts);
  227. Serial.print(" ");
  228. Serial.print("\n\r");
  229. delay(1000);
  230. }
  231. //------------------------------------------------------------------------------------------------------
  232. // This routine reads all the analog input values for the system. Then it multiplies them by the scale
  233. // factor to get actual value in volts or amps.
  234. //------------------------------------------------------------------------------------------------------
  235. void read_data(void) {
  236. sol_amps = (read_adc(SOL_AMPS_CHAN) * SOL_AMPS_SCALE -13.51); //input of solar amps
  237. sol_volts = read_adc(SOL_VOLTS_CHAN) * SOL_VOLTS_SCALE; //input of solar volts
  238. bat_volts = read_adc(BAT_VOLTS_CHAN) * BAT_VOLTS_SCALE; //input of battery volts
  239. sol_watts = sol_amps * sol_volts ; //calculations of solar watts
  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 > MAX_BAT_VOLTS) { //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 > MAX_BAT_VOLTS) { //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. set_pwm_duty();
  309. TURN_OFF_MOSFETS;
  310. }
  311. else if (bat_volts > MAX_BAT_VOLTS) { //since we're in the battery float state if the battery voltage
  312. pwm -= 1; //is above the float voltage back off the pwm to lower it
  313. set_pwm_duty();
  314. }
  315. else if (bat_volts < MAX_BAT_VOLTS) { //else if the battery voltage is less than the float voltage
  316. pwm += 1; //increment the pwm to get it back up to the float voltage
  317. set_pwm_duty();
  318. if (pwm >= 100) { //if pwm gets up to 100 it means we can't keep the battery at
  319. charger_state = bulk; //float voltage so jump to charger bulk state to charge the battery
  320. }
  321. }
  322. break;
  323. case off: //when we jump into the charger off state, off_count is set with OFF_NUM
  324. if (off_count > 0) { //this means that we run through the off state OFF_NUM of times with out doing
  325. off_count--; //anything, this is to allow the battery voltage to settle down to see if the
  326. } //battery has been disconnected
  327. else if ((bat_volts > HIGH_BAT_VOLTS) && (bat_volts < MAX_BAT_VOLTS) && (sol_volts > bat_volts)) {
  328. charger_state = bat_float; //if battery voltage is still high and solar volts are high
  329. set_pwm_duty(); //change charger state to battery float
  330. TURN_ON_MOSFETS;
  331. }
  332. else if ((bat_volts > MIN_BAT_VOLTS) && (bat_volts < MAX_BAT_VOLTS) && (sol_volts > bat_volts)) {
  333. pwm = PWM_START; //if battery volts aren't quite so high but we have solar volts
  334. set_pwm_duty(); //greater than battery volts showing it is day light then
  335. charger_state = on; //change charger state to on so we start charging
  336. TURN_ON_MOSFETS;
  337. } //else stay in the off state
  338. break;
  339. default:
  340. TURN_OFF_MOSFETS;
  341. break;
  342. }
  343. }
  344. //------------------------------------------------------------------------------------------------------
  345. // Main loop.
  346. //
  347. //------------------------------------------------------------------------------------------------------
  348. void loop()
  349. {
  350. read_data(); //read data from inputs
  351. run_charger(); //run the charger state machine
  352. //print_data(); //print data
  353. load_control(); // control the connected load
  354. led_output(); // led indication
  355. lcd_display(); // lcd display
  356. wifi_datalog();
  357. }
  358. //------------------------------------------------------------------------------------------------------
  359. //
  360. //This function displays the currnet state with the help ot the 3 LEDs
  361. //
  362. //------------------------------------------------------------------------------------------------------
  363. //----------------------------------------------------------------------------------------------------------------------
  364. /////////////////////////////////////////////LOAD CONTROL/////////////////////////////////////////////////////
  365. //----------------------------------------------------------------------------------------------------------------------
  366. void load_control()
  367. {
  368. if (sol_watts < MIN_SOL_WATTS) // load will on when night
  369. {
  370. if(bat_volts >LVD) // check if battery is healthy
  371. {
  372. load_status=1;
  373. digitalWrite(LOAD_PIN, LOW); // load is ON
  374. }
  375. else if(bat_volts < LVD)
  376. {
  377. load_status=0;
  378. digitalWrite(LOAD_PIN, HIGH); //load is OFF
  379. }
  380. }
  381. else // load will off during day
  382. {
  383. load_status=0;
  384. digitalWrite(LOAD_PIN, HIGH);
  385. }
  386. }
  387. //-------------------------------------------------------------------------------------------------
  388. //---------------------------------Led Indication--------------------------------------------------
  389. //-------------------------------------------------------------------------------------------------
  390. void led_output(void)
  391. {
  392. if(bat_volts > 14.1 )
  393. {
  394. leds_off_all();
  395. digitalWrite(LED_YELLOW, HIGH);
  396. }
  397. else if(bat_volts > 11.9 && bat_volts < 14.1)
  398. {
  399. leds_off_all();
  400. digitalWrite(LED_GREEN, HIGH);
  401. }
  402. else if(bat_volts < 11.8)
  403. {
  404. leds_off_all;
  405. digitalWrite(LED_RED, HIGH);
  406. }
  407. }
  408. //------------------------------------------------------------------------------------------------------
  409. //
  410. // This function is used to turn all the leds off
  411. //
  412. //------------------------------------------------------------------------------------------------------
  413. void leds_off_all(void)
  414. {
  415. digitalWrite(LED_GREEN, LOW);
  416. digitalWrite(LED_RED, LOW);
  417. digitalWrite(LED_YELLOW, LOW);
  418. }
  419. //------------------------------------------------------------------------------------------------------
  420. //-------------------------- LCD DISPLAY --------------------------------------------------------------
  421. //-------------------------------------------------------------------------------------------------------
  422. void lcd_display()
  423. {
  424. back_light_pin_State = digitalRead(BACK_LIGHT_PIN);
  425. if (back_light_pin_State == HIGH)
  426. {
  427. lcd.backlight();// finish with backlight on
  428. // Wait for 10 seconds and then turn off the display and backlight.
  429. delay(15000);
  430. lcd.noBacklight();
  431. }
  432. lcd.setCursor(0, 0);
  433. lcd.print("SOL");
  434. lcd.setCursor(4, 0);
  435. lcd.write(1);
  436. lcd.setCursor(0, 1);
  437. lcd.print(sol_volts);
  438. lcd.print("V");
  439. lcd.setCursor(0, 2);
  440. lcd.print(sol_amps);
  441. lcd.print("A");
  442. lcd.setCursor(0, 3);
  443. lcd.print(sol_watts);
  444. lcd.print("W ");
  445. lcd.setCursor(8, 0);
  446. lcd.print("BAT");
  447. lcd.setCursor(12, 0);
  448. lcd.write(2);
  449. lcd.setCursor(8, 1);
  450. lcd.print(bat_volts);
  451. lcd.setCursor(8,2);
  452. if (charger_state == on)
  453. lcd.print("on");
  454. else if (charger_state == off)
  455. lcd.print("off");
  456. else if (charger_state == bulk)
  457. lcd.print("bulk");
  458. else if (charger_state == bat_float)
  459. lcd.print("float");
  460. //-----------------------------------------------------------
  461. //--------------------Battery State Of Charge ---------------
  462. //-----------------------------------------------------------
  463. lcd.setCursor(8,3);
  464. if ( bat_volts >= 12.7)
  465. lcd.print( "100%");
  466. else if (bat_volts >= 12.5 && bat_volts < 12.7)
  467. lcd.print( "90%");
  468. else if (bat_volts >= 12.42 && bat_volts < 12.5)
  469. lcd.print( "80%");
  470. else if (bat_volts >= 12.32 && bat_volts < 12.42)
  471. lcd.print( "70%");
  472. else if (bat_volts >= 12.2 && bat_volts < 12.32)
  473. lcd.print( "60%");
  474. else if (bat_volts >= 12.06 && bat_volts < 12.2)
  475. lcd.print( "50%");
  476. else if (bat_volts >= 11.90 && bat_volts < 12.06)
  477. lcd.print( "40%");
  478. else if (bat_volts >= 11.75 && bat_volts < 11.90)
  479. lcd.print( "30%");
  480. else if (bat_volts >= 11.58 && bat_volts < 11.75)
  481. lcd.print( "20%");
  482. else if (bat_volts >= 11.31 && bat_volts < 11.58)
  483. lcd.print( "10%");
  484. else if (bat_volts < 11.3)
  485. lcd.print( "0%");
  486. //---------------------------------------------------------------------
  487. //------------------Duty Cycle-----------------------------------------
  488. //---------------------------------------------------------------------
  489. lcd.setCursor(15,0);
  490. lcd.print("PWM");
  491. lcd.setCursor(19,0);
  492. lcd.write(3);
  493. lcd.setCursor(15,1);
  494. lcd.print(pwm);
  495. lcd.print("%");
  496. //----------------------------------------------------------------------
  497. //------------------------Load Status-----------------------------------
  498. //----------------------------------------------------------------------
  499. lcd.setCursor(15,2);
  500. lcd.print("Load");
  501. lcd.setCursor(15,3);
  502. if (load_status == 1)
  503. {
  504. lcd.print("On");
  505. }
  506. else
  507. {
  508. lcd.print("Off");
  509. }
  510. }
  511. //-------------------------------------------------------------------------
  512. //----------------------------- ESP8266 WiFi ------------------------------
  513. //--------------------------Plot System data on thingspeak.com-------------
  514. //-------------------------------------------------------------------------
  515. void wifi_datalog()
  516. {
  517. // convert to string
  518. char buf[16];
  519. String strTemp = dtostrf( sol_volts, 4, 1, buf);
  520. Serial.println(strTemp);
  521. // TCP connection
  522. String cmd = "AT+CIPSTART=\"TCP\",\"";
  523. cmd += "184.106.153.149"; // api.thingspeak.com
  524. cmd += "\",80";
  525. ser.println(cmd);
  526. if(ser.find("Error")){
  527. Serial.println("AT+CIPSTART error");
  528. return;
  529. }
  530. // prepare GET string
  531. String getStr = "GET /update?api_key=";
  532. getStr += apiKey;
  533. getStr +="&field1=";
  534. getStr += String(strTemp);
  535. getStr += "\r\n\r\n";
  536. // send data length
  537. cmd = "AT+CIPSEND=";
  538. cmd += String(getStr.length());
  539. ser.println(cmd);
  540. if(ser.find(">")){
  541. ser.print(getStr);
  542. }
  543. else{
  544. ser.println("AT+CIPCLOSE");
  545. // alert user
  546. Serial.println("AT+CIPCLOSE");
  547. }
  548. // thingspeak needs 15 sec delay between updates
  549. delay(16000);
  550. }