forked from 3KUdelta/Solar_WiFi_Weather_Station
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solar-WiFi-Weather-Station-V2.34.2-INFLUXDB.ino
748 lines (634 loc) · 29 KB
/
Solar-WiFi-Weather-Station-V2.34.2-INFLUXDB.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
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
/*----------------------------------------------------------------------------------------------------
Project Name : Solar Powered WiFi Weather Station V2.34.2
Features: temperature, dewpoint, dewpoint spread, heat index, humidity, absolute pressure, relative pressure, battery status and
the famous Zambretti Forecaster (multi lingual)
Authors: Keith Hungerford, Debasish Dutta and Marc Stähli
Website : www.opengreenenergy.com
Main microcontroller (ESP8266) and BME280 both sleep between measurements
BME280 is used in single shot mode ("forced mode")
CODE: https://github.com/3KUdelta/Solar_WiFi_Weather_Station
INSTRUCTIONS & HARDWARE: https://www.instructables.com/id/Solar-Powered-WiFi-Weather-Station-V20/
3D FILES: https://www.thingiverse.com/thing:3551386
CREDITS:
Inspiration and code fragments of Dewpoint and Heatindex calculations are taken from:
https://arduinotronics.blogspot.com/2013/12/temp-humidity-w-dew-point-calcualtions.html
For Zambretti Ideas:
http://drkfs.net/zambretti.htm or http://integritext.net/DrKFS/zambretti.htm
https://raspberrypiandstuff.wordpress.com
David Bird: https://github.com/G6EJD/ESP32_Weather_Forecaster_TN061
Needed libraries:
<Adafruit_Sensor.h> --> Adafruit unified sensor
<Adafruit_BME280.h> --> Adafrout BME280 sensor
<BlynkSimpleEsp8266.h> --> https://github.com/blynkkk/blynk-library
<ESP8266WiFi.h>
<WiFiUdp.h>
"FS.h"
<EasyNTPClient.h> --> https://github.com/aharshac/EasyNTPClient
<TimeLib.h> --> https://github.com/PaulStoffregen/Time.git
CREDITS for Adafruit libraries:
This is a library for the BME280 humidity, temperature & pressure sensor
Designed specifically to work with the Adafruit BME280 Breakout
----> http://www.adafruit.com/products/2650
These sensors use I2C or SPI to communicate, 2 or 4 pins are required
to interface. The device's I2C address is either 0x76 or 0x77.
Adafruit invests time and resources providing this open source code,
please support Adafruit andopen-source hardware by purchasing products
from Adafruit!
Written by Limor Fried & Kevin Townsend for Adafruit Industries.
BSD license, all text above must be included in any redistribution
Hardware Settings Mac:
LOLIN(WEMOS) D1 mini Pro, 80 MHz, Flash, 16M (14M SPIFFS), v2 Lower Memory, Disable, None, Only Sketch, 921600 on /dev/cu.SLAB_USBtoUART
major update on 15/05/2019
-added Zambretti Forecster
-added translation feature
-added English language
-added German language
updated on 03/06/2019
-added Dewpoint Spread
-minor code corrections
updated 28/06/19
-added Italian and Polish tranlation (Chak10) and (TomaszDom)
updated 27/11/19 to V2.32
-added battery protection at 3.3V, sending "batt empty" message and go to hybernate mode
updated 11/05/20 to v2.33
-corrected bug in adjustments for summer/winter
updated 27/05/20 to v2.34
- added August-Roche-Magnus approximation to automatically adjust humidity with temperature corrections
updated 18/10/20 to v2.34.2
- code cleanup. I dont need blynk, mqtt, thingspeak, ...
- implemented influxdb (thanks to FrankUlbrich for original code)
- fixed security issue with open unencrypted wifi ap which is created every start.
//// Features : //////////////////////////////////////////////////////////////////////////////////////////////////////
// 1. Connect to Wi-Fi, and upload the data to either Blynk App and/or Thingspeak
// 2. Monitoring Weather parameters like Temperature, Pressure abs, Pressure MSL and Humidity.
// 3. Extra Ports to add more Weather Sensors like UV Index, Light and Rain Guage etc.
// 4. Remote Battery Status Monitoring
// 5. Using Sleep mode to reduce the energy consumed
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/***************************************************
* VERY IMPORTANT: *
* *
* Enter your personal settings in Settings.h ! *
* *
**************************************************/
#include "Settings.h"
#include "Translation.h"
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include "FS.h"
#include <EasyNTPClient.h> //https://github.com/aharshac/EasyNTPClient
#include <TimeLib.h> //https://github.com/PaulStoffregen/Time.git
Adafruit_BME280 bme; // I2C
WiFiUDP udp;
EasyNTPClient ntpClient(udp, NTP_SERVER, TZ_SEC + DST_SEC);
// Added INFLUX FUlb
//#include <ESP8266WiFiMulti.h>
#include <InfluxDb.h>;
float measured_temp;
float measured_humi;
float measured_pres;
float adjusted_temp;
float adjusted_humi;
float SLpressure_hPa; // needed for rel pressure calculation
float HeatIndex; // Heat Index in °C
float volt;
int rel_pressure_rounded;
double DewpointTemperature;
float DewPointSpread; // Difference between actual temperature and dewpoint
// FORECAST CALCULATION
unsigned long current_timestamp; // Actual timestamp read from NTPtime_t now;
unsigned long saved_timestamp; // Timestamp stored in SPIFFS
float pressure_value[12]; // Array for the historical pressure values (6 hours, all 30 mins)
// where as pressure_value[0] is always the most recent value
float pressure_difference[12]; // Array to calculate trend with pressure differences
// FORECAST RESULT
int accuracy; // Counter, if enough values for accurate forecasting
String ZambrettisWords; // Final statement about weather forecast
String trend_in_words; // Trend in words
String forecast_in_words; // Weather forecast in words
String pressure_in_words; // Air pressure in words
String accuracy_in_words; // Zambretti's prediction accuracy in words
void(* resetFunc) (void) = 0; // declare reset function @ address 0
WiFiClient client;
void setup() {
Serial.begin(115200);
Serial.println();
Serial.println("Start of SolarWiFiWeatherStation V2.34.2");
//******Battery Voltage Monitoring*********************************************
// Voltage divider R1 = 220k+100k+220k =540k and R2=100k
float calib_factor = 5.28; // change this value to calibrate the battery voltage
unsigned long raw = analogRead(A0);
volt = raw * calib_factor/1024;
Serial.print( "Voltage = ");
Serial.print(volt, 2); // print with 2 decimal places
Serial.println (" V");
// **************Application going online**********************************
WiFi.mode(WIFI_STA);
WiFi.hostname(DEVICENAME); //This changes the hostname of the ESP8266 to display neatly on the network esp on router.
WiFi.begin(ssid, pass);
Serial.print("---> Connecting to WiFi ");
int i = 0;
while (WiFi.status() != WL_CONNECTED) {
delay(500);
i++;
if (i > 20) {
Serial.println("Could not connect to WiFi!");
Serial.println("Going to sleep for 10 minutes and try again.");
if (volt > 3.3){
goToSleep(10); // go to sleep and retry after 10 min
}
else{
goToSleep(0); // hybernate because batt empty
}
}
Serial.print(".");
}
Serial.println(" Wifi connected ok");
Serial.print("Your IP Address is: ");
Serial.println(WiFi.localIP().toString());
//*****************Checking if SPIFFS available********************************
Serial.println("SPIFFS Initialization: (First time run can last up to 30 sec - be patient)");
boolean mounted = SPIFFS.begin(); // load config if it exists. Otherwise use defaults.
if (!mounted) {
Serial.println("FS not formatted. Doing that now... (can last up to 30 sec).");
SPIFFS.format();
Serial.println("FS formatted...");
SPIFFS.begin();
}
//******** GETTING THE TIME FROM NTP SERVER ***********************************
Serial.println("---> Now reading time from NTP Server");
int ii = 0;
while(!ntpClient.getUnixTime()){
delay(100);
ii++;
if (ii > 20) {
Serial.println("Could not connect to NTP Server!");
Serial.println("Doing a reset now and retry a connection from scratch.");
resetFunc();
}
Serial.print(".");
}
current_timestamp = ntpClient.getUnixTime(); // get UNIX timestamp (seconds from 1.1.1970 on)
Serial.print("Current UNIX Timestamp: ");
Serial.println(current_timestamp);
Serial.print("Time & Date: ");
Serial.print(hour(current_timestamp));
Serial.print(":");
Serial.print(minute(current_timestamp));
Serial.print(":");
Serial.print(second(current_timestamp));
Serial.print("; ");
Serial.print(day(current_timestamp));
Serial.print(".");
Serial.print(month(current_timestamp)); // needed later: month as integer for Zambretti calcualtion
Serial.print(".");
Serial.println(year(current_timestamp));
//******** GETTING RELATIVE PRESSURE DATA FROM SENSOR (BME680) ********************
bool bme_status;
bme_status = bme.begin(0x76); //address either 0x76 or 0x77
if (!bme_status) {
Serial.println("Could not find a valid BME280 sensor, check wiring!");
}
Serial.println("forced mode, 1x temperature / 1x humidity / 1x pressure oversampling,");
Serial.println("filter off");
bme.setSampling(Adafruit_BME280::MODE_FORCED,
Adafruit_BME280::SAMPLING_X1, // temperature
Adafruit_BME280::SAMPLING_X1, // pressure
Adafruit_BME280::SAMPLING_X1, // humidity
Adafruit_BME280::FILTER_OFF );
measurementEvent(); //get all data from the different sensors
//*******************SPIFFS operations***************************************************************
ReadFromSPIFFS(); //read stored values and update data if more recent data is available
Serial.print("Timestamp difference: ");
Serial.println(current_timestamp - saved_timestamp);
if (current_timestamp - saved_timestamp > 21600){ // last save older than 6 hours -> re-initialize values
FirstTimeRun();
}
else if (current_timestamp - saved_timestamp > 1800){ // it is time for pressure update (1800 sec = 30 min)
for (int i = 11; i >= 1; i = i -1) {
pressure_value[i] = pressure_value[i-1]; // shifting values one to the right
}
pressure_value[0] = rel_pressure_rounded; // updating with acutal rel pressure (newest value)
if (accuracy < 12) {
accuracy = accuracy + 1; // one value more -> accuracy rises (up to 12 = 100%)
}
WriteToSPIFFS(current_timestamp); // update timestamp on storage
}
else {
WriteToSPIFFS(saved_timestamp); // do not update timestamp on storage
}
//**************************Calculate Zambretti Forecast*******************************************
int accuracy_in_percent = accuracy * 94 / 12; // 94% is the max predicion accuracy of Zambretti
if ( volt > 3.4 ) { // check if batt is still ok
ZambrettisWords = ZambrettiSays(char(ZambrettiLetter()));
forecast_in_words = TEXT_ZAMBRETTI_FORECAST;
pressure_in_words = TEXT_AIR_PRESSURE;
accuracy_in_words = TEXT_ZAMBRETTI_ACCURACY;
}
else {
ZambrettisWords = ZambrettiSays('0'); // send Message that battery is empty
}
Serial.println("********************************************************");
Serial.print(forecast_in_words);
Serial.print(": ");
Serial.println(ZambrettisWords);
Serial.print(pressure_in_words);
Serial.print(": ");
Serial.println(trend_in_words);
Serial.print(accuracy_in_words);
Serial.print(": ");
Serial.print(accuracy_in_percent);
Serial.println("%");
if (accuracy < 12){
Serial.println("Reason: Not enough weather data yet.");
Serial.print("We need ");
Serial.print((12 - accuracy) / 2);
Serial.println(" hours more to get sufficient data.");
}
Serial.println("********************************************************");
//**************************Sending Data to INFLUXDB*********************************
// code block for uploading data to INFLUXDB
if (App1 == "INFLUXDB") {
// Send data to local INFLUX DB
Serial.println("INFO: Open the INFLUX connection");
Influxdb influx(INFLUXDB_HOST, INFLUXDB_PORT);
influx.setDbAuth(INFLUXDB_DATABASE, INFLUXDB_USER, INFLUXDB_PASS);
InfluxData row1("weatherstation");
row1.addTag("node", DEVICENAME);
row1.addTag("sensor", "BME");
row1.addTag("mode", "single");
row1.addValue("BME_tempc", measured_temp);
row1.addValue("BME_tempc_adjusted", adjusted_temp);
row1.addValue("BME_humi", measured_humi);
row1.addValue("BME_humi_adjusted", adjusted_humi);
row1.addValue("BME_abshpa", measured_pres);
row1.addValue("BME_relhpa", rel_pressure_rounded);
row1.addValue("dewpointc", DewpointTemperature);
row1.addValue("spreadc", DewPointSpread);
row1.addValue("heatindexc", HeatIndex);
/* row1.addValue("zambrettiswords", ZambrettisWords);*/
row1.addValue("accuracy_in_percent", accuracy_in_percent);
/* row1.addValue("trend_in_words", trend_in_words);*/
influx.write(row1);
/* InfluxData row2("weatherstation");
row2.addTag("node", DEVICENAME);
row2.addTag("sensor", "DHT");
row2.addTag("mode", "single");
row2.addValue("DHT_humi", temperature_DHT);
row2.addValue("DHT_tempc", humidity_DHT);
influx.write(row2);*/
InfluxData row3("weatherstation");
row3.addTag("node", DEVICENAME);
row3.addTag("sensor", "systemboard");
row3.addTag("mode", "single");
row3.addValue("volt", volt);
influx.write(row3);
/* remove on weatherstation in garden
InfluxData row4("weatherstation");
row4.addTag("node", DEVICENAME);
row4.addTag("sensor", "CCS811");
row4.addTag("mode", "single");
row4.addValue("valueCO2", value_CO2);
row4.addValue("valueTVOC", value_TVOC);
influx.write(row4);
*/
/* InfluxData row5("weatherstation");
row5.addTag("node", DEVICENAME);
row5.addTag("sensor", "VEML6070");
row5.addTag("mode", "single");
row5.addValue("valueUVraw", value_UVindex);
row5.addValue("valueUVI", value_UVI);
influx.write(row5);*/
Serial.println("INFO: Closing the INFLUX connection");
}
if (volt > 3.3) { //check if batt still ok, if yes
goToSleep(sleepTimeMin); //go for a nap
}
else{ //if not,
goToSleep(0); //hybernate because batt is empty
}
} // end of void setup()
void loop() { //loop is not used
} // end of void loop()
void measurementEvent() {
//Measures absolute Pressure, Temperature, Humidity, Voltage, calculate relative pressure,
//Dewpoint, Dewpoint Spread, Heat Index
bme.takeForcedMeasurement();
// Get temperature
measured_temp = bme.readTemperature();
// print on serial monitor
Serial.print("Temp: ");
Serial.print(measured_temp);
Serial.print("°C; ");
// Get humidity
measured_humi = bme.readHumidity();
// print on serial monitor
Serial.print("Humidity: ");
Serial.print(measured_humi);
Serial.print("%; ");
// Get pressure
measured_pres = bme.readPressure() / 100.0F;
// print on serial monitor
Serial.print("Pressure: ");
Serial.print(measured_pres);
Serial.print("hPa; ");
// Calculate and print relative pressure
SLpressure_hPa = (((measured_pres * 100.0)/pow((1-((float)(ELEVATION))/44330), 5.255))/100.0);
rel_pressure_rounded=(int)(SLpressure_hPa+.5);
// print on serial monitor
Serial.print("Pressure rel: ");
Serial.print(rel_pressure_rounded);
Serial.print("hPa; ");
// Calculate dewpoint
double a = 17.271;
double b = 237.7;
double tempcalc = (a * measured_temp) / (b + measured_temp) + log(measured_humi*0.01);
DewpointTemperature = (b * tempcalc) / (a - tempcalc);
Serial.print("Dewpoint: ");
Serial.print(DewpointTemperature);
Serial.println("°C; ");
// With the dewpoint calculated we can correct temp and automatically calculate humidity
adjusted_temp = measured_temp + TEMP_CORR;
if (adjusted_temp < DewpointTemperature) adjusted_temp = DewpointTemperature; //compensation, if offset too high
//August-Roche-Magnus approximation (http://bmcnoldy.rsmas.miami.edu/Humidity.html)
adjusted_humi = 100 * (exp((a * DewpointTemperature) / (b + DewpointTemperature)) / exp((a * adjusted_temp) / (b + adjusted_temp)));
if (adjusted_humi > 100) adjusted_humi = 100; // just in case
// print on serial monitor
Serial.print("Temp adjusted: ");
Serial.print(adjusted_temp);
Serial.print("°C; ");
Serial.print("Humidity adjusted: ");
Serial.print(adjusted_humi);
Serial.print("%; ");
// Calculate dewpoint spread (difference between actual temp and dewpoint -> the smaller the number: rain or fog
DewPointSpread = measured_temp - DewpointTemperature;
Serial.print("Dewpoint Spread: ");
Serial.print(DewPointSpread);
Serial.println("°C; ");
// Calculate HI (heatindex in °C) --> HI starts working above 26,7 °C
if (measured_temp > 26.7) {
double c1 = -8.784, c2 = 1.611, c3 = 2.338, c4 = -0.146, c5= -1.230e-2, c6=-1.642e-2, c7=2.211e-3, c8=7.254e-4, c9=-2.582e-6 ;
double T = measured_temp;
double R = measured_humi;
double A = (( c5 * T) + c2) * T + c1;
double B = ((c7 * T) + c4) * T + c3;
double C = ((c9 * T) + c8) * T + c6;
HeatIndex = (C * R + B) * R + A;
}
else {
HeatIndex = measured_temp;
Serial.println("Not warm enough (less than 26.7 °C) for Heatindex");
}
Serial.print("HeatIndex: ");
Serial.print(HeatIndex);
Serial.print("°C; ");
} // end of void measurementEvent()
int CalculateTrend(){
int trend; // -1 falling; 0 steady; 1 raising
Serial.println("---> Calculating trend");
//--> giving the most recent pressure reads more weight
pressure_difference[0] = (pressure_value[0] - pressure_value[1]) * 1.5;
pressure_difference[1] = (pressure_value[0] - pressure_value[2]);
pressure_difference[2] = (pressure_value[0] - pressure_value[3]) / 1.5;
pressure_difference[3] = (pressure_value[0] - pressure_value[4]) / 2;
pressure_difference[4] = (pressure_value[0] - pressure_value[5]) / 2.5;
pressure_difference[5] = (pressure_value[0] - pressure_value[6]) / 3;
pressure_difference[6] = (pressure_value[0] - pressure_value[7]) / 3.5;
pressure_difference[7] = (pressure_value[0] - pressure_value[8]) / 4;
pressure_difference[8] = (pressure_value[0] - pressure_value[9]) / 4.5;
pressure_difference[9] = (pressure_value[0] - pressure_value[10]) / 5;
pressure_difference[10] = (pressure_value[0] - pressure_value[11]) / 5.5;
//--> calculating the average and storing it into [11]
pressure_difference[11] = ( pressure_difference[0]
+ pressure_difference[1]
+ pressure_difference[2]
+ pressure_difference[3]
+ pressure_difference[4]
+ pressure_difference[5]
+ pressure_difference[6]
+ pressure_difference[7]
+ pressure_difference[8]
+ pressure_difference[9]
+ pressure_difference[10]) / 11;
Serial.print("Current trend: ");
Serial.println(pressure_difference[11]);
if (pressure_difference[11] > 3.5) {
trend_in_words = TEXT_RISING_FAST;
trend = 1;}
else if (pressure_difference[11] > 1.5 && pressure_difference[11] <= 3.5) {
trend_in_words = TEXT_RISING;
trend = 1;
}
else if (pressure_difference[11] > 0.25 && pressure_difference[11] <= 1.5) {
trend_in_words = TEXT_RISING_SLOW;
trend = 1;
}
else if (pressure_difference[11] > -0.25 && pressure_difference[11] < 0.25) {
trend_in_words = TEXT_STEADY;
trend = 0;
}
else if (pressure_difference[11] >= -1.5 && pressure_difference[11] < -0.25) {
trend_in_words = TEXT_FALLING_SLOW;
trend = -1;
}
else if (pressure_difference[11] >= -3.5 && pressure_difference[11] < -1.5) {
trend_in_words = TEXT_FALLING;
trend = -1;
}
else if (pressure_difference[11] <= -3.5) {
trend_in_words = TEXT_FALLING_FAST;
trend = -1;
}
Serial.println(trend_in_words);
return trend;
}
char ZambrettiLetter() {
Serial.println("---> Calculating Zambretti letter");
char z_letter;
int(z_trend) = CalculateTrend();
// Case trend is falling
if (z_trend == -1) {
float zambretti = 0.0009746 * rel_pressure_rounded * rel_pressure_rounded - 2.1068 * rel_pressure_rounded + 1138.7019;
//A Winter falling generally results in a Z value lower by 1 unit
if (month(current_timestamp) < 4 || month(current_timestamp) > 9) zambretti = zambretti + 1;
if (zambretti > 9) zambretti = 9;
Serial.print("Calculated and rounded Zambretti in numbers: ");
Serial.println(round(zambretti));
switch (int(round(zambretti))) {
case 0: z_letter = 'A'; break; //Settled Fine
case 1: z_letter = 'A'; break; //Settled Fine
case 2: z_letter = 'B'; break; //Fine Weather
case 3: z_letter = 'D'; break; //Fine Becoming Less Settled
case 4: z_letter = 'H'; break; //Fairly Fine Showers Later
case 5: z_letter = 'O'; break; //Showery Becoming unsettled
case 6: z_letter = 'R'; break; //Unsettled, Rain later
case 7: z_letter = 'U'; break; //Rain at times, worse later
case 8: z_letter = 'V'; break; //Rain at times, becoming very unsettled
case 9: z_letter = 'X'; break; //Very Unsettled, Rain
}
}
// Case trend is steady
if (z_trend == 0) {
float zambretti = 138.24 - 0.133 * rel_pressure_rounded;
Serial.print("Calculated and rounded Zambretti in numbers: ");
Serial.println(round(zambretti));
switch (int(round(zambretti))) {
case 0: z_letter = 'A'; break; //Settled Fine
case 1: z_letter = 'A'; break; //Settled Fine
case 2: z_letter = 'B'; break; //Fine Weather
case 3: z_letter = 'E'; break; //Fine, Possibly showers
case 4: z_letter = 'K'; break; //Fairly Fine, Showers likely
case 5: z_letter = 'N'; break; //Showery Bright Intervals
case 6: z_letter = 'P'; break; //Changeable some rain
case 7: z_letter = 'S'; break; //Unsettled, rain at times
case 8: z_letter = 'W'; break; //Rain at Frequent Intervals
case 9: z_letter = 'X'; break; //Very Unsettled, Rain
case 10: z_letter = 'Z'; break; //Stormy, much rain
}
}
// Case trend is rising
if (z_trend == 1) {
float zambretti = 142.57 - 0.1376 * rel_pressure_rounded;
//A Summer rising, improves the prospects by 1 unit over a Winter rising
if (month(current_timestamp) >= 4 && month(current_timestamp) <= 9) zambretti = zambretti - 1;
if (zambretti < 0) zambretti = 0;
Serial.print("Calculated and rounded Zambretti in numbers: ");
Serial.println(round(zambretti));
switch (int(round(zambretti))) {
case 0: z_letter = 'A'; break; //Settled Fine
case 1: z_letter = 'A'; break; //Settled Fine
case 2: z_letter = 'B'; break; //Fine Weather
case 3: z_letter = 'C'; break; //Becoming Fine
case 4: z_letter = 'F'; break; //Fairly Fine, Improving
case 5: z_letter = 'G'; break; //Fairly Fine, Possibly showers, early
case 6: z_letter = 'I'; break; //Showery Early, Improving
case 7: z_letter = 'J'; break; //Changeable, Improving
case 8: z_letter = 'L'; break; //Rather Unsettled Clearing Later
case 9: z_letter = 'M'; break; //Unsettled, Probably Improving
case 10: z_letter = 'Q'; break; //Unsettled, short fine Intervals
case 11: z_letter = 'T'; break; //Very Unsettled, Finer at times
case 12: z_letter = 'Y'; break; //Stormy, possibly improving
case 13: z_letter = 'Z'; break;; //Stormy, much rain
}
}
Serial.print("This is Zambretti's famous letter: ");
Serial.println(z_letter);
return z_letter;
}
String ZambrettiSays(char code){
String zambrettis_words = "";
switch (code) {
case 'A': zambrettis_words = TEXT_ZAMBRETTI_A; break; //see Tranlation.h
case 'B': zambrettis_words = TEXT_ZAMBRETTI_B; break;
case 'C': zambrettis_words = TEXT_ZAMBRETTI_C; break;
case 'D': zambrettis_words = TEXT_ZAMBRETTI_D; break;
case 'E': zambrettis_words = TEXT_ZAMBRETTI_E; break;
case 'F': zambrettis_words = TEXT_ZAMBRETTI_F; break;
case 'G': zambrettis_words = TEXT_ZAMBRETTI_G; break;
case 'H': zambrettis_words = TEXT_ZAMBRETTI_H; break;
case 'I': zambrettis_words = TEXT_ZAMBRETTI_I; break;
case 'J': zambrettis_words = TEXT_ZAMBRETTI_J; break;
case 'K': zambrettis_words = TEXT_ZAMBRETTI_K; break;
case 'L': zambrettis_words = TEXT_ZAMBRETTI_L; break;
case 'M': zambrettis_words = TEXT_ZAMBRETTI_M; break;
case 'N': zambrettis_words = TEXT_ZAMBRETTI_N; break;
case 'O': zambrettis_words = TEXT_ZAMBRETTI_O; break;
case 'P': zambrettis_words = TEXT_ZAMBRETTI_P; break;
case 'Q': zambrettis_words = TEXT_ZAMBRETTI_Q; break;
case 'R': zambrettis_words = TEXT_ZAMBRETTI_R; break;
case 'S': zambrettis_words = TEXT_ZAMBRETTI_S; break;
case 'T': zambrettis_words = TEXT_ZAMBRETTI_T; break;
case 'U': zambrettis_words = TEXT_ZAMBRETTI_U; break;
case 'V': zambrettis_words = TEXT_ZAMBRETTI_V; break;
case 'W': zambrettis_words = TEXT_ZAMBRETTI_W; break;
case 'X': zambrettis_words = TEXT_ZAMBRETTI_X; break;
case 'Y': zambrettis_words = TEXT_ZAMBRETTI_Y; break;
case 'Z': zambrettis_words = TEXT_ZAMBRETTI_Z; break;
case '0': zambrettis_words = TEXT_ZAMBRETTI_0; break;
default: zambrettis_words = TEXT_ZAMBRETTI_DEFAULT; break;
}
return zambrettis_words;
}
void ReadFromSPIFFS() {
char filename [] = "/data.txt";
File myDataFile = SPIFFS.open(filename, "r"); // Open file for reading
if (!myDataFile) {
Serial.println("Failed to open file");
FirstTimeRun(); // no file there -> initializing
}
Serial.println("---> Now reading from SPIFFS");
String temp_data;
temp_data = myDataFile.readStringUntil('\n');
saved_timestamp = temp_data.toInt();
Serial.print("Timestamp from SPIFFS: "); Serial.println(saved_timestamp);
temp_data = myDataFile.readStringUntil('\n');
accuracy = temp_data.toInt();
Serial.print("Accuracy value read from SPIFFS: "); Serial.println(accuracy);
Serial.print("Last 12 saved pressure values: ");
for (int i = 0; i <= 11; i++) {
temp_data = myDataFile.readStringUntil('\n');
pressure_value[i] = temp_data.toInt();
Serial.print(pressure_value[i]);
Serial.print("; ");
}
myDataFile.close();
Serial.println();
}
void WriteToSPIFFS(int write_timestamp) {
char filename [] = "/data.txt";
File myDataFile = SPIFFS.open(filename, "w"); // Open file for writing (appending)
if (!myDataFile) {
Serial.println("Failed to open file");
}
Serial.println("---> Now writing to SPIFFS");
myDataFile.println(write_timestamp); // Saving timestamp to /data.txt
myDataFile.println(accuracy); // Saving accuracy value to /data.txt
for ( int i = 0; i <= 11; i++) {
myDataFile.println(pressure_value[i]); // Filling pressure array with updated values
}
myDataFile.close();
Serial.println("File written. Now reading file again.");
myDataFile = SPIFFS.open(filename, "r"); // Open file for reading
Serial.print("Found in /data.txt = ");
while (myDataFile.available()) {
Serial.print(myDataFile.readStringUntil('\n'));
Serial.print("; ");
}
Serial.println();
myDataFile.close();
}
void FirstTimeRun(){
Serial.println("---> Starting initializing process.");
accuracy = 1;
char filename [] = "/data.txt";
File myDataFile = SPIFFS.open(filename, "w"); // Open a file for writing
if (!myDataFile) {
Serial.println("Failed to open file");
Serial.println("Stopping process - maybe flash size not set (SPIFFS).");
exit(0);
}
myDataFile.println(current_timestamp); // Saving timestamp to /data.txt
myDataFile.println(accuracy); // Saving accuracy value to /data.txt
for ( int i = 0; i < 12; i++) {
myDataFile.println(rel_pressure_rounded); // Filling pressure array with current pressure
}
Serial.println("** Saved initial pressure data. **");
myDataFile.close();
Serial.println("---> Doing a reset now.");
resetFunc(); //call reset
}
void goToSleep(unsigned int sleepmin) {
Serial.println("INFO: Closing the Wifi connection");
WiFi.disconnect();
while (client.connected() || (WiFi.status() == WL_CONNECTED)) {
Serial.println("Waiting for shutdown before sleeping");
delay(10);
}
delay(50);
Serial.print ("Going to sleep now for ");
Serial.print (sleepmin);
Serial.print (" Minute(s).");
ESP.deepSleep(sleepmin * 60 * 1000000); // convert to microseconds
} // end of goToSleep()