-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolarBoxMonitor.ino
617 lines (532 loc) · 13.5 KB
/
SolarBoxMonitor.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
/*************************************************************
Download latest Blynk library here:
https://github.com/blynkkk/blynk-library/releases/latest
Blynk is a platform with iOS and Android apps to control
Arduino, Raspberry Pi and the likes over the Internet.
You can easily build graphic interfaces for all your
projects by simply dragging and dropping widgets.
Downloads, docs, tutorials: http://www.blynk.cc
Sketch generator: http://examples.blynk.cc
Blynk community: http://community.blynk.cc
Social networks: http://www.fb.com/blynkapp
http://twitter.com/blynk_app
Blynk library is licensed under MIT license
This example code is in public domain.
*************************************************************
This example runs directly on ESP8266 chip.
Note: This requires ESP8266 support package:
https://github.com/esp8266/Arduino
Please be sure to select the right ESP8266 module
in the Tools -> Board menu!
Change WiFi ssid, pass, and Blynk auth token to run :)
*************************************************************/
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include "SparkFun_Si7021_Breakout_Library.h"
#include <Wire.h>
#include <EEPROM.h>
#include "Config.h"
// --------------------------------------------------------
float humidity;
float temp;
float voltage = 0;
byte chargeLevel = 0;
bool isSoilDry = true;
bool isDayTime;
enum statuses
{
STATUS_OK,
STATUS_NOTICE,
STATUS_WARNING,
STATUS_DANGER
} envStatus;
enum batteryStatuses
{
STATUS_DISCONNECTED,
STATUS_DISCHARGED,
STATUS_CHARGED,
STATUS_FLOAT,
STATUS_BULK,
STATUS_BOOST,
STATUS_EQUALIZE,
STATUS_OVERVOLTAGE
} batteryStatus;
int prevEnvStatus, prevBatteryStatus;
char statusText[100], batteryStatusText[100];
WidgetLED ledWeather(V2);
WidgetLED ledBattery(V5);
WidgetLED ledSoil(V8);
//Create Instance of SI7021 temp and humidity sensor
Weather sensor;
//--------------------------------------------------------------
#define BUILTIN_LED 5
void setup()
{
// Debug console
Serial.begin(9600);
// disable WiFi to prevent interferences when reading sensors
WiFi.mode( WIFI_OFF );
WiFi.forceSleepBegin();
// read sensors
readVoltage();
sensor.begin();
readWeather();
// connect to wifi
networkSetup();
// read soil sensor
pinMode(SOIL_SENSOR_INPUT_PIN, INPUT);
pinMode(SOIL_SENSOR_POWER, OUTPUT);
// prepare data
readPreviousParameters();
calculateParams();
// soil moisture
if (isDayTime)
{
// soil
readSoilMoisture();
if (isSoilDry && envStatus < STATUS_DANGER)
{
envStatus = STATUS_WARNING;
strcat(statusText, " Sol uscat."); // dry soil
}
}
// wait until wifi connects or timeout
waitForWifiConnection();
if (WiFi.status() == WL_CONNECTED)
{
logToBlynk();
sendAlerts();
}
goToSleep();
}
//--------------------------------------------------------------
void loop()
{
// no need to run Blynk because we only run the setup function
//Blynk.run();
}
/**
* Read parameters saved in EEPROM
*/
void readPreviousParameters()
{
unsigned short addr = 0;
EEPROM.begin(2);
// read saved status
prevEnvStatus = EEPROM.read(addr);
prevBatteryStatus = EEPROM.read(++addr);
}
/**
* Connect to wifi and blynk using a fixed ip and specific channel/bssid
* Will force wake up from sleep
*
* @return bool success
*/
bool networkSetup()
{
// Disable the WiFi persistence. The ESP8266 will not load and save WiFi settings in the flash memory.
WiFi.persistent(false);
WiFi.forceSleepWake();
WiFi.mode(WIFI_STA);
// Bring up the WiFi connection
WiFi.config(ip, gateway, subnet, dns);
//WiFi.begin(ssid, pass, channel, bssid);
WiFi.begin(ssid, pass);
}
bool waitForWifiConnection()
{
int startMillis = millis();
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(WIFI_WAIT_DELAY);
if ((millis() - startMillis) > WIFI_CONNECTION_TIMEOUT)
{
Serial.println("wifiSetup(): Wifi connection timeout.");
break;
}
}
Serial.println("");
if (WiFi.status() != WL_CONNECTED)
{
Serial.printf("Could not connect to %s\n", ssid);
return false;
}
//Serial.printf("Connected to %s in %d ms\n", ssid, wifiTime);
Blynk.config(BLYNK_TOKEN); // in place of Blynk.begin(auth, ssid, pass);
Blynk.connect(BLYNK_SERVER_TIMEOUT);
//Serial.printf("Connected to Blynk in %d ms\n", blynkTime);
return true;
}
//--------------------------------------------------------------
/**
* Calculates the parameters after reading the sensors
*
* - envStatus
* - statusText
*/
void calculateParams()
{
strcpy(statusText, " ");
envStatus = STATUS_OK;
if (humidity > 98)
{ // solar controller operates at maximum 96% humidity
strcpy(statusText, "EXCES UMIDITATE."); // excessive humidity
envStatus = STATUS_DANGER;
}
else if (humidity > 94)
{
envStatus = STATUS_WARNING;
strcpy(statusText, "Avert. Umiditate."); // humidity warning
}
else if (humidity > 85)
{
envStatus = STATUS_NOTICE;
strcpy(statusText, "Umiditate crescuta."); // humidity warning
}
// battery charge 0 - 40
// Discharge: -15 ~ 50°C
// 40°C 103%, 25°C 100%, 0°C 86%
if (temp < -25)
{
envStatus = STATUS_DANGER;
strcat(statusText, " INGHET.");
}
else if (temp < 0)
{
if (envStatus < STATUS_DANGER)
{
envStatus = STATUS_WARNING;
}
strcat(statusText, " Risc inghet."); // low temp, freezing
}
else if (temp < 10)
{
if (envStatus < STATUS_WARNING)
{
envStatus = STATUS_NOTICE;
}
strcat(statusText, " Temp. scazuta."); // low temp notice
}
else if (temp > 60)
{
envStatus = STATUS_DANGER;
strcat(statusText, " INCENDIU."); // possible fire
}
else if (temp > 50)
{
if (envStatus < STATUS_DANGER)
{
envStatus = STATUS_WARNING;
}
strcat(statusText, " Temp. f. ridicata."); // very high temp
}
else if (temp > 37)
{
if (envStatus < STATUS_WARNING)
{
envStatus = STATUS_NOTICE;
}
strcat(statusText, " Temp. ridicata."); // high temp
}
float compensatedVoltage = (temp - 25) * 0.03;
// battery voltage status
strcpy(batteryStatusText, "");
if (voltage > 15.55)
{
batteryStatus = STATUS_OVERVOLTAGE;
strcpy(batteryStatusText, "Supra-tensiune");
strcat(statusText, " SUPRA-TENSIUNE BATERIE."); // over-voltage
}
else if (voltage > 14.52 - compensatedVoltage)
{
batteryStatus = STATUS_EQUALIZE;
strcpy(batteryStatusText, "Incarcare Egalizare");
}
else if (voltage > 14.3 - compensatedVoltage)
{
batteryStatus = STATUS_BOOST;
strcpy(batteryStatusText, "Incarcare Boost");
}
else if (voltage > 13.7 - compensatedVoltage)
{
batteryStatus = STATUS_FLOAT;
strcpy(batteryStatusText, "Incarcare Float");
}
else if (voltage > 13.2 - compensatedVoltage)
{
batteryStatus = STATUS_BULK;
strcpy(batteryStatusText, "Incarcare Bulk");
}
else if (voltage > 11.9)
{
byte capacity = 100;
if (temp > 25)
{
capacity += (temp - 25) * 0.2;
}
else
{
capacity -= (25 - temp) * 0.55;
}
sprintf(batteryStatusText, "Se descarca. %d %", round(capacity));
batteryStatus = STATUS_CHARGED;
}
else if (voltage > 3)
{
batteryStatus = STATUS_DISCHARGED;
strcat(statusText, " BATERIE DESCARCATA."); // disconnected
strcpy(batteryStatusText, "Sub-tensiune");
}
else
{
voltage = 0;
batteryStatus = STATUS_DISCONNECTED;
strcat(statusText, " BATERIE DECONECTATA."); // discharged
strcpy(batteryStatusText, "Deconectat");
}
chargeLevel = getSoc(voltage, temp);
isDayTime = voltage == 0 || voltage > 13;
}
/**
* Temperature corrected State Of Charge
*/
byte getSoc(float voltage, float temp)
{
float voltageMap[] = {11.5, 11.80, 11.9, 12.1, 12.20, 12.4, 12.47, 12.57, 12.67, 12.75, 12.85};
float diff;
int i;
if (temp > 27)
{
voltage += diff / 7.0 * 0.005;
}
else
{
diff = 27 - temp;
voltage -= (diff / 7.0 * 0.005) + (diff * 0.001);
}
for (i = 0; i <= 10; i++)
{
if (voltage <= voltageMap[i])
{
return i * 10;
}
}
return 100;
}
/**
* Measure Relative Humidity from the Si7021 sensor
*/
void readWeather()
{
humidity = sensor.getRH();
// unconnected sensor give -6
if (humidity < 0)
{
humidity = NULL;
}
// Temperature is measured every time RH is requested.
// It is faster, therefore, to read it from previous RH
// measurement with getTemp() instead with readTemp()
temp = sensor.getTemp();
if (temp < -45)
{
temp = NULL;
}
}
void readSoilMoisture()
{
isSoilDry = (digitalRead(SOIL_SENSOR_INPUT_PIN) == HIGH);
// disable sensor
digitalWrite(SOIL_SENSOR_POWER, LOW);
}
/**
* Read voltage using ADC pin on Sparkfun 8266 Thing
* Uses an average over 10 samples
*
*/
void readVoltage()
{
// read the input on analog pin 0:
int adcValue = 0;
const uint8_t numberOfSamples = 10;
// discard first reading
analogRead(A0);
// Get sensor samples with delay and calculate the sum.
for (int i = 0; i < numberOfSamples; i++)
{
// Add sample to the sum counter.
// Analog reading goes from 0 - 1023. ADC voltage range is 0 - 1V
adcValue += analogRead(A0);
// Delay for 5ms. entire operation takes 50ms this way
delay(5);
}
// Get the average sensor value (ignore the fraction).
adcValue = round(adcValue / numberOfSamples);
// determine de actual voltage by multiplying the raw value to a known calibration factor
voltage = adcValue * VOLTAGE_CALIBRATION_FACTOR;
Serial.println(voltage);
}
/**
* Send alerts using Blynk
*/
void sendAlerts()
{
unsigned short addr = 0;
bool hasAlert = false;
// no notification if the state is not changed
if (prevEnvStatus == envStatus && batteryStatus == prevBatteryStatus)
{
return;
}
// todo: send this alert only once if conditions go away but not more than 10 times a day
if (prevEnvStatus != envStatus && envStatus == STATUS_DANGER)
{
hasAlert = true;
}
if (batteryStatus != prevBatteryStatus && (batteryStatus == STATUS_DISCHARGED || batteryStatus == STATUS_OVERVOLTAGE || batteryStatus == STATUS_DISCONNECTED))
{
hasAlert = true;
}
if (hasAlert)
{
Blynk.email("SolarBox Alert", statusText);
char buff[50];
sprintf(buff, "SolarBox Alert: %s", statusText);
Blynk.notify(buff);
}
// write the changed values to the appropriate address of the EEPROM.
// these values will remain there when the board is turned off.
addr = 0;
if (prevEnvStatus != envStatus)
{
EEPROM.write(addr, envStatus);
}
if (batteryStatus != prevBatteryStatus)
{
EEPROM.write(++addr, batteryStatus);
}
EEPROM.commit();
yield();
}
/**
* Publish measurements to the Blynk service
*
* Virtual Pins:
*
* - V0 - `float` temperature rounded to one decimal in range -40-125 C
* - V1 - `int` humidity in range 0-100
* - V2 - LED widget for environment status
* - V3 - `char` weather status text
* - V4 - `float` SLA battery voltage in range 0-16V
* - V5 - LED widget for battery status
* - V6 - `char` battery status text
* - V7 - `byte` battery charge level
* - V8 - `bool` soil is dry
*/
void logToBlynk()
{
if (!Blynk.connected())
{
return;
}
Blynk.virtualWrite(0, round(temp * 10) / 10.0); // round to one decimal
Blynk.virtualWrite(1, round(humidity * 10) / 10.0);
switch (envStatus)
{
case STATUS_NOTICE:
ledWeather.setColor(COLOR_YELLOW);
break;
case STATUS_WARNING:
ledWeather.setColor(COLOR_ORANGE);
break;
case STATUS_DANGER:
ledWeather.setColor(COLOR_RED);
break;
default:
ledWeather.setColor(COLOR_GREEN);
}
ledWeather.on();
switch (batteryStatus)
{
case STATUS_DISCONNECTED:
ledBattery.setColor(COLOR_GREY);
break;
case STATUS_OVERVOLTAGE:
case STATUS_DISCHARGED:
ledBattery.setColor(COLOR_RED);
break;
case STATUS_EQUALIZE:
ledBattery.setColor(COLOR_ORANGE);
break;
case STATUS_BOOST:
ledBattery.setColor(COLOR_YELLOW);
break;
case STATUS_FLOAT:
ledBattery.setColor(COLOR_BLUE);
break;
default:
ledBattery.setColor(COLOR_GREEN);
}
if (isDayTime)
{
if (isSoilDry)
{
ledSoil.setColor(COLOR_ORANGE);
}
else
{
ledSoil.setColor(COLOR_BLUE);
}
}
else
{
ledSoil.setColor(COLOR_GREY);
}
ledSoil.on();
ledBattery.on();
Blynk.virtualWrite(3, statusText);
Blynk.virtualWrite(4, voltage);
Blynk.virtualWrite(6, batteryStatusText);
Blynk.virtualWrite(7, chargeLevel);
Blynk.run();
}
//---------------------------------------------------------------
void goToSleep()
{
unsigned short seconds;
if (envStatus == STATUS_DANGER || batteryStatus == STATUS_OVERVOLTAGE)
{
// 30 sec sleep if danger
seconds = 30;
}
else if (envStatus != STATUS_DANGER && (voltage > 12.3 && voltage < 13))
{
// 3 mins sleep at night if nothing happens
seconds = 180;
}
else
{
// normal 1 min sleep
seconds = 60;
}
ESP.deepSleep(seconds * 1000000);
}
//---------------------------------------------------------------
void debugInfo()
{
//This function prints the weather data out to the default Serial Port
Serial.print("Temp:");
Serial.print(temp);
Serial.print("C, ");
Serial.print("Humidity:");
Serial.print(humidity);
Serial.println("%");
Serial.print("Status");
Serial.println(statusText);
Serial.print("Voltage:");
Serial.println(voltage);
Serial.printf("SoC: %d\n", chargeLevel);
}