-
Notifications
You must be signed in to change notification settings - Fork 20
/
MiniWX_v1.0.ino
2235 lines (1950 loc) · 74.5 KB
/
MiniWX_v1.0.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
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************
Thanks to F4GOH Antoine (f4goh@orange.fr) for his first release
compiler options:
ArduinoIDE 1.8.6
SDK:2.2.1(cfd48f3)/Core:2.4.2/lwIP:2.0.3(STABLE-2_0_3_RELEASE/glue:arduino-2.4.1-13-g163bb82)/BearSSL:6d1cefc
- upload to weather underground
- added aprs telemetry packets
- using Ticker() for counting transmit delay
- blinking leds for infos
- ESP.getVcc() in telemetry
- more robust NTP sync
- Implemented 'SoftClock' and less aggressive ntpsync (user defined delay)
- Extended comment in APRS packet
- realtime APRS packets instead of 'timestamp-ed' ones
- Dynamics webpages for infos and config parameters
- Added OTA firmware flashing
- Added more verbose WiFi Scan routine
- Added webconfig page
- Works with BMP280 too (only pressure and temperature obviously)
- Localization (Spanish by EA1CDV - Italian by IZ5MMH)
- Modifieds functions (faster) for read/write files in SPIFFS
- In "Config wifi access point menu" added new entry for reset DNS over StaticIP
- More verbosity at first time use
- Checks for browser type and serve better formatted pages in mobile devices
- Fixed "exit" button issues for non-firefox users
- New css "MiniWX" style for buttons
- Setting for Serial Console baudrate speed
- TIME ZONE setting
- Fixed uptime refresh in main page
***************************************************************
Marco Campinoti - IU5HKU (mrcodemail@gmail.com)
***************************************************************/
#include <Wire.h>
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ArduinoOTA.h>
#include <Ticker.h>
#include <NTPtimeESP.h>
#include "FS.h"
#include "EN_Locale.h"
#include "ES_Locale.h"
#include "IT_Locale.h"
#include "SystemWebpages.h"
#include <SparkFunBME280.h>
// Global sensor object
BME280 mySensor;
// webserver for parameters & main pages
ESP8266WebServer server(80);
//**** INTERNAL USE & DEBUG ****
const char SOFT_VER[] = "v1.0";
//#define DEBUG_READSETTINGSFILE
//#define DEBUG_FORM_REPLIES
#define DISPLAY_RW_OUTPUT
//**************************************
//* ADDED FEATURES - SWITCHES *
//**************************************
//**** CHOOSE WEBPAGES LANGUAGE
#define LANG_ENGLISH
//#define LANG_SPANISH
//#define LANG_ITALIAN
//**** CHOOSE SERIAL MONITOR BAUD RATE
//#define SER_MON_BAUDRATE 115200
#define SER_MON_BAUDRATE 74880
//**** How the station is named in your NET
const char* WiFi_hostname = "MiniWX";
//**** APRS PASSWORD (use -1 if you are using a CWOP callsign)
const char* AprsPassw = "YourAprsNumericalPASS";
//**** APRS COMMENT, you can set this string as you want (max 43 chars)
const char* APRS_CMNT = "MiniWX Station .:.YourHomeTown.:.";
//**** APRS_PRJ, Telemetry Project Title (max 23 chars)
const char* APRS_PRJ = "MiniWX Project";
//**** Comment this for ESP.getVcc() value in telemetry
//**** getVcc function (system_get_vdd33) is only available when A0 pin17 is suspended (floating),
//**** this function measure the power voltage of VDD3P3 pin 3 and 4 (in the ESP8266 chip)
//#define HAVE_BATTERY
//**** uncomment this for weatherunderground upload,remember to set ID and PASSWORD of your account
//#define USE_WUNDER
//* change ID and PASSWORD with yours
const char ID [] = "YourWunderID";
const char PASSWORD [] = "YourWunderPASSW";
//**** show BME280 registers in Serial Output;
//#define DISPLAY_BME_REGS
//**** show BME280 values in Serial Output;
#define DISPLAY_BME_VAL
//**** blinking led to show that into the 10 minutes the system is still alive WILL BE ELIMINATED IN BATTERY POWERED VERSION (1" blink)
//**** NOTE: WEMOS D1 Mini doens't have this led, NodeMCU V0.9 & V1.0 have it.
//#define BLINK_RED_LED
//**** blinking led to show that ESP8266 is transmitting WILL BE ELIMINATED IN BATTERY POWERED VERSION (0.5" blink)
#define BLINK_BLUE_LED
//**** show (annoying) animated clock in the serial output
//#define SHOW_TICKS
//**** Sync the soft clock every 12 hours
#define NTPSYNC_DELAY 12
//**** NTP Server to use
const char* NTP_Server = "ntp1.inrim.it"; //italian national institute for measures
//**** Your time zone UTC related (floating point number)
#define TIME_ZONE 1.0f
//**** Set credential for OTA firmware upgrade <<--->>
//*uncomment the #define if you wanna use this handy feature
#define USE_OTA_UPGRADE
#ifdef USE_OTA_UPGRADE
const char* OTA_hostname = "MINIWX";
//PLEASE CHANGE THIS PASSWORD WITH YOUR OWN FOR SECURITY REASON
const char* OTA_passw = "esp8266";
#endif
//**** use static ip instead of dns one
//#define USE_STATIC_IP
//* change to reflect your net configuration
String static_ip = "192.168.0.200"; // STATIC IP
String static_gateway = "192.168.0.1"; // GATEWAY
String static_mask = "255.255.225.0"; // SUBNET MASK
String static_dns1 = "8.8.8.8"; // DNS1
String static_dns2 = "4.4.2.2"; // DNS2
IPAddress ip,gateway,mask,dns1,dns2;
//**************************************************************************************************
//* time related structures and vars
//* time server for ntp function
NTPtime NTPch(NTP_Server);
strDateTime dateTime;
byte nextMinTx;
byte nextHour;
byte nextSecTx;
// flags & Time counters
Ticker TkAlarm;
bool bSendFlag;
Ticker TkSeconds;
bool bSecsFlag;
Ticker TkBlueLed;
bool bNtpSyncFlag;
Ticker TkNtpSync;
//for the CheckSensor()
#define MOD_BMP280 0x58
#define MOD_BME280 0x60
typedef struct {
//Sensor_type (BME280 or BMP280 autodetected)
uint8_t ChipModel;
//*APRS
char AprsPassw[6];
char APRS_CMNT[44];
char APRS_PRJ[24];
//*WUNDER
bool usewunder;
char wunderid[20];
char wunderpassw[20];
//*STATIC IP
bool usestaticip;
char static_ip[16];
char static_gateway[16];
char static_mask[16];
char static_dns1[16];
char static_dns2[16];
//*NTP
char NTP_Server[20];
unsigned char NTP_SYNC_DELAY;
} Settings, *SettingsPtr;
Settings sets;
SettingsPtr sets_ptr = &sets;
typedef struct {
long rssi;
unsigned int vbat;
} Telemetry, *TelemetryPtr;
Telemetry tl; // declare telemetry structure
TelemetryPtr tl_ptr = &tl;
typedef struct {
float temperatureC;
float temperatureF;
float pressure ;
float humidity;
float heatindex;
double fdewptf;
} WeatherStruct, *WeatherStructPtr;
WeatherStruct wx; //declare la structure
WeatherStructPtr wx_ptr = &wx;
// HEAT INDEX ROUTINE - (C) 2016 Tyler Glenn
// Thanks, the whole library is a great work indeed,
// maybe i'll use it in a next release.
/*
Copyright (C) 2016 Tyler Glenn
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Written: Dec 30 2015.
Last Updated: Dec 23 2017.
This header must be included in any derived code or copies of the code.
*/
//*** Heat Index coefficients from the above library
#define hi_coeff1 -42.379
#define hi_coeff2 2.04901523
#define hi_coeff3 10.14333127
#define hi_coeff4 -0.22475541
#define hi_coeff5 -0.00683783
#define hi_coeff6 -0.05481717
#define hi_coeff7 0.00122874
#define hi_coeff8 0.00085282
#define hi_coeff9 -0.00000199
typedef struct {
char ssid[50];
char password[50];
} configStruct, *configStructPtr;
configStruct internet; //declare la structure
configStructPtr internet_ptr = &internet;
//for connections to clients (APRS servers and WUNDER server)
//better keep this declaration global (as seen in forums)
WiFiClient client;
typedef struct {
char callsign[10];
char tlm_callsign[10];
char longitude[10];
char latitude[10];
char clientAddress[20];
int clientPort;
long transmitDelay;
byte logger;
int altitude;
} positionStruct, *positionStructPtr;
positionStruct station; //declare la structure
positionStructPtr station_ptr = &station;
char car;
//**************************************
//* WUNDERGROUND
//**************************************
const char WGserver[] PROGMEM = "weatherstation.wunderground.com";
const char WEBPAGE[] PROGMEM = "GET /weatherstation/updateweatherstation.php?";
void calcDewPoint(){
// Calculate dew Point
double A0= 373.15/(273.15 + wx.temperatureF);
double SUM = -7.90298 * (A0-1);
SUM += 5.02808 * log10(A0);
SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ;
SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ;
SUM += log10(1013.246);
double VP = pow(10, SUM-3) * wx.humidity;
double T = log(VP/0.61078);
wx.fdewptf = (241.88 * T) / (17.558-T);
}
void Send2Wunder(){
// Calculate dew Point
calcDewPoint();
// connect to wunderground
if (!client.connect(FPSTR(WGserver), 80)) {
Serial.println(F("Send2Wunder Fail"));
return;
}
Serial.print(F("WeatherUnderground page updating...."));
client.print(FPSTR(WEBPAGE));
client.print(F("ID="));
client.print(sets.wunderid);
client.print(F("&PASSWORD="));
client.print(sets.wunderpassw);
client.print(F("&dateutc="));
client.print("now");
client.print(F("&tempf="));
client.print(wx.temperatureF);
client.print(F("&dewptf="));
client.print(wx.fdewptf);
client.print(F("&humidity="));
client.print(wx.humidity);
client.print(F("&baromin="));
client.print((wx.pressure/100)*0.02953f); // 1 mbar = 0.02953 inHg
//more compliant WU output by EA1CDV Antonio
client.print(F("&softwaretype=MiniWX%20Station%20"));
client.print(SOFT_VER);
client.print(F("&action=updateraw"));
client.println();
delay(2500);
//Serial.println("done!");
//print server reply
Serial.print(F("server reply:"));
while (client.available()) {
String line = client.readStringUntil('\r');
Serial.print(line);
}
client.stop();
}
//******************************************
//* ARDUINO OTA SET HANDLERS
//* for flashing the firmware through TCP/IP
//******************************************
#ifdef USE_OTA_UPGRADE
void SetOTA(){
ArduinoOTA.setHostname(OTA_hostname);
ArduinoOTA.setPassword(OTA_passw);
ArduinoOTA.onStart([]() {
Serial.println("Start");
});
ArduinoOTA.onEnd([]() {
Serial.println("\nEnd");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
});
ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println(F("Auth Failed"));
else if (error == OTA_BEGIN_ERROR) Serial.println(F("Begin Failed"));
else if (error == OTA_CONNECT_ERROR) Serial.println(F("Connect Failed"));
else if (error == OTA_RECEIVE_ERROR) Serial.println(F("Receive Failed"));
else if (error == OTA_END_ERROR) Serial.println(F("End Failed"));
});
ArduinoOTA.begin();
Serial.println(F("OTA ready"));
Serial.println();
}
#endif
//************************************************************************
//* TICKERS INTERRUPT ROUTINES
//* keep as short as posssible!
//* WARNING: the ESP can only use 32 bits to measure time, as times are calculated in uS this gives a total of 71 mins.
//* 32 bits = 4,294,967,295 / 1,000,000 = 4,294 Seconds / 60 = 71 Minutes.
//************************************************************************
void SetSendFlag(void){
bSendFlag=true;
}
//Soft Clock, quite precise indeed...
void SetSecsFlag(void){
bSecsFlag=true;
dateTime.second = (dateTime.second + 1) % 60;
if (dateTime.second == 0) {
dateTime.minute = (dateTime.minute + 1) % 60;
if (dateTime.minute == 0) {
dateTime.hour = (dateTime.hour + 1) % 24;
}
}
}
//NTP Sync every NTPSYNC_DELAY hours
char ntpdelaycnt=0x00;
void SetNtpSyncFlag(void){
if(ntpdelaycnt++ == NTPSYNC_DELAY){
bNtpSyncFlag=true;
ntpdelaycnt = 0x00;
}
}
#ifdef BLINK_BLUE_LED
void BlinkBlueLed(){
digitalWrite(D4, digitalRead(D4)^1); // turn the ESP-12 LED off and on (HIGH is the voltage level and meaning OFF)
}
#endif
//**************************************
//* MAIN SETUP ROUTINE
//**************************************
void setup(void)
{
strcpy(station.clientAddress, "cwop.aprs.net"); // default, reccomended over rotate.aprs.net
station.clientPort = 14580;
station.transmitDelay = 10; // expressed in minutes
station.logger = 0;
FSInfo fs_info;
Serial.begin(SER_MON_BAUDRATE); // to read the esp8266 core and startup messages
Serial.println();
delay(10);
SPIFFS.begin(); // mount SPIFFS
if(SPIFFS.info(fs_info)){
Serial.println("SPIFFS successfully mounted");
Serial.print("Total avail : ");Serial.print(fs_info.totalBytes);Serial.println(" bytes");
Serial.print("Used : ");Serial.print(fs_info.usedBytes);Serial.println(" bytes");
}
if (SPIFFS.exists("/ssid.txt") == 0) {
Serial.println("^:WARNING:^ no ssid config found.");
Serial.println(":if this is the absolute first time use:");
Serial.println(": remember to format SPIFFS !! :");
configMenu();
}
else{
readSsidFile();
}
if (SPIFFS.exists("/station.txt") == 0) {
Serial.println("^:WARNING:^ no station config found.");
configMenu();
}
else{
readStationFile();
}
//*DEFAULT VALUES for the SETTINGS STRUCTURE
// APRS
strcpy(sets.AprsPassw, AprsPassw);
strcpy(sets.APRS_CMNT, APRS_CMNT);
strcpy(sets.APRS_PRJ, APRS_PRJ);
// WUNDER
#ifdef USE_WUNDER
sets.usewunder = true;
#else
sets.usewunder = false;
#endif
strcpy(sets.wunderid, ID);
strcpy(sets.wunderpassw, PASSWORD);
// STATIC IP
#ifdef USE_STATIC_IP
sets.usestaticip = true;
#else
sets.usestaticip = false;
#endif
static_ip.toCharArray(sets.static_ip, 16);
static_gateway.toCharArray(sets.static_gateway, 16);
static_mask.toCharArray(sets.static_mask, 16);
static_dns1.toCharArray(sets.static_dns1, 16);
static_dns2.toCharArray(sets.static_dns2, 16);
// NTP
strcpy(sets.NTP_Server, NTP_Server);
sets.NTP_SYNC_DELAY = NTPSYNC_DELAY;
if (SPIFFS.exists("/settings.txt") == 0) {
// absolutely 'first time use', write the defaults values
Serial.println("^:WARNING:^ no settings config found, write defaults.");
writeSettingsFile();
}
else{
//read the saved settings
readSettingsFile();
}
if (detectMenu() == 1) configMenu();
ssidConnect();
initBme();
// this for save ChipID in defaults values, just in case you change from BMP to BME
writeSettingsFile();
printBme();
#ifdef USE_OTA_UPGRADE
//** Init the required handlers for OTA firmware flashing
SetOTA();
#endif
server.on("/", handleRoot);
server.on("/submit", handleSubmit);
server.on("/settings", handleSettings);
server.on("/jquery", handleJQuery);
server.on("/ssedata", handleSSE);
server.onNotFound(handleNotFound);
//here the list of headers to be recorded
const char * headerkeys[] = {"User-Agent","X-Forwarded-For"};
size_t headerkeyssize = sizeof(headerkeys) / sizeof(char*);
//ask server to track these headers
server.collectHeaders(headerkeys, headerkeyssize);
server.begin();
Serial.println("Server started.");
// First and most important sync with NTP Server, next sync will be done after 'xx' hours,
// default 12 hours, but you can change it in 'settings' webpage.
ntp();
//set bSecsFlag for tickin'the clock
bSecsFlag=false;
TkSeconds.attach( 1, SetSecsFlag);
//initialize random seed
randomSeed(analogRead(A0));
//NodeMCU V0.9 pinout for onboard LED
#ifdef BLINK_BLUE_LED
pinMode(D4, OUTPUT); // Initialize GPIO2 pin as an output
digitalWrite(D4, HIGH); // Led OFF
#endif
#ifdef BLINK_RED_LED
pinMode(D0, OUTPUT); // Initialize GPIO16 pin as an output WARNING: DO NOT USE in deepsleep() mode!!!
digitalWrite(D0, HIGH); // Led OFF
#endif
#ifdef HAVE_BATTERY
pinMode(A0, INPUT); // read from A0
#endif
//set bSendFlag to true every transmitDelay seconds
bSendFlag=false;
TkAlarm.attach( station.transmitDelay*60, SetSendFlag);
//set bNtpSyncFlag for NTPsync() every 3600 seconds
bNtpSyncFlag=false;
TkNtpSync.attach( 3600, SetNtpSyncFlag);
}
//* tracking System Uptime
int sysUpTimeSec;
int sysUpTimeMn;
int sysUpTimeHr;
int sysUpTimeDy;
void SystemUpTime() {
long millisecs = millis(); //milliseconds since system poweron
sysUpTimeSec = int((millisecs / (1000)) % 60);
sysUpTimeMn = int((millisecs / (1000 * 60)) % 60);
sysUpTimeHr = int((millisecs / (1000 * 60 * 60)) % 24);
sysUpTimeDy = int((millisecs / (1000 * 60 * 60 * 24)) % 365);
}
/****************************************************************/
//FYI: https://ehp.niehs.nih.gov/1206273/ in detail this flow graph: https://ehp.niehs.nih.gov/wp-content/uploads/2013/10/ehp.1206273.g003.png
float CalcHeatIndex( float temperature, float humidity)
{
float heatIndex(NAN);
if ( isnan(temperature) || isnan(humidity) )
{
return heatIndex;
}
temperature = (temperature * (9.0 / 5.0) + 32.0); /*conversion to [°F]*/
// Using both Rothfusz and Steadman's equations
// http://www.wpc.ncep.noaa.gov/html/heatindex_equation.shtml
if (temperature <= 40)
{
heatIndex = temperature; //first red block
}
else
{
heatIndex = 0.5 * (temperature + 61.0 + ((temperature - 68.0) * 1.2) + (humidity * 0.094)); //calculate A -- from the official site, not the flow graph
if (heatIndex >= 79)
{
/*
* calculate B
* the following calculation is optimized. Simply spoken, reduzed cpu-operations to minimize used ram and runtime.
* Check the correctness with the following link:
* http://www.wolframalpha.com/input/?source=nav&i=b%3D+x1+%2B+x2*T+%2B+x3*H+%2B+x4*T*H+%2B+x5*T*T+%2B+x6*H*H+%2B+x7*T*T*H+%2B+x8*T*H*H+%2B+x9*T*T*H*H
*/
heatIndex = hi_coeff1
+ (hi_coeff2 + hi_coeff4 * humidity + temperature * (hi_coeff5 + hi_coeff7 * humidity)) * temperature
+ (hi_coeff3 + humidity * (hi_coeff6 + temperature * (hi_coeff8 + hi_coeff9 * temperature))) * humidity;
//third red block
if ((humidity < 13) && (temperature >= 80.0) && (temperature <= 112.0))
{
heatIndex -= ((13.0 - humidity) * 0.25) * sqrt((17.0 - abs(temperature - 95.0)) * 0.05882);
} //fourth red block
else if ((humidity > 85.0) && (temperature >= 80.0) && (temperature <= 87.0))
{
heatIndex += (0.02 * (humidity - 85.0) * (87.0 - temperature));
}
}
}
return (heatIndex - 32.0) * (5.0 / 9.0); /*conversion back to [°C]*/
}
//***********************************************************
//* CHECK BROWSER TYPE & ADJUST FIELDSETS SIZE
//***********************************************************
void AdjustFieldsets( String* page){
// Checking if the client is mobile or not, quite naive, but functional
if (server.hasHeader("User-Agent")){
//here you can adjust the <fieldset style='width: '> to match your needs
const char* mobile_fieldsizes[] = {"97%","44%","32%","18%","98%","18%","78%","98%","*"};
const char* pc_fieldsizes[] = {"49%","50%","22%","18%","50%","28%","68%","50%","*"};
String repl("{{fieldsize");
int i=0;
if (server.header("User-Agent").indexOf("Android") > 0){
//ANDROID
while (mobile_fieldsizes[i] != "*"){
repl += String(i) + "}}";
page->replace(repl, mobile_fieldsizes[i++]);
repl = "{{fieldsize";
}
}
else{
//ALL THE OTHERS (PC for the great majority)
while (mobile_fieldsizes[i] != "*"){
repl += String(i) + "}}";
page->replace(repl, pc_fieldsizes[i++]);
repl = "{{fieldsize";
}
}
}
}
//***********************************************************
//* MINIWX STATION - handle root page request
//***********************************************************
void handleRoot() {
#ifdef LANG_ENGLISH
String page = FPSTR(PAGE_Main_EN);
#endif
#ifdef LANG_SPANISH
String page = FPSTR(PAGE_Main_ES);
#endif
#ifdef LANG_ITALIAN
String page = FPSTR(PAGE_Main_IT);
#endif
char buffer[20];
float dpdegc;
readSettingsFile();
AdjustFieldsets(&page);
page.replace(F("{{callsign}}"), station.callsign);
page.replace(F("{{lat}}"), station.latitude);
page.replace(F("{{long}}"), station.longitude);
page.replace(F("{{alt}}"), String(station.altitude));
SystemUpTime();
String sysUpTime("Days " + String(sysUpTimeDy) + ": Hrs " + String(sysUpTimeHr) + ": Min" + String(sysUpTimeMn) + ": Sec" + String(sysUpTimeSec));
page.replace(F("{{uptime}}"), sysUpTime);
getBmeValues();
// Zero-ing values that can't be display cause lack of rH% in bmp280
switch(sets.ChipModel){
case MOD_BMP280: //temp,press
page.replace(F("{{ChipModel}}"), String("BMP280"));
wx.humidity = 0.0f;
wx.heatindex = 0.0f;
dpdegc = 0.0f;
break;
case MOD_BME280: //temp,press,rhum
page.replace(F("{{ChipModel}}"), String("BME280"));
calcDewPoint();
dpdegc = (wx.fdewptf-32.0f)*0.55f;// Fahrenheit to Celsius
break;
case 0x00: // NO SENSOR AT ALL
page.replace(F("{{ChipModel}}"), String("NO SENSOR"));
wx.temperatureC = 0.0f;
wx.temperatureF = 0.0f;
wx.pressure = 0.0f;
wx.humidity = 0.0f;
wx.heatindex = 0.0f;
dpdegc = 0.0f;
break;
}
page.replace(F("{{degC}}"), String((wx.temperatureC),2));
page.replace(F("{{mbar}}"), String((wx.pressure/100),2));
page.replace(F("{{rHum}}"), String(wx.humidity,2));
page.replace(F("{{DPdegC}}"), String(dpdegc,2));
page.replace(F("{{HIdegC}}"), String(wx.heatindex,2));
sprintf(buffer, "%02d:%02d:%02d", nextHour, nextMinTx, nextSecTx);
page.replace(F("{{nexttx}}"), buffer);
sprintf(buffer, "%02d:%02d:%02d", dateTime.hour, dateTime.minute, dateTime.second);
page.replace(F("{{time}}"), buffer);
page.replace(F("{{SSID}}"), internet.ssid);
page.replace(F("{{RSSI}}"), String(WiFi.RSSI()));
page.replace(F("{{BSSID}}"), String(WiFi.BSSIDstr()));
page.replace(F("{{myip}}"), WiFi.localIP().toString());
if(sets.usewunder)
page.replace(F("{{wunderstate}}"), F(" "));
else
page.replace(F("{{wunderstate}}"), F("disabled"));
page.replace(F("{{SOFT_VER}}"), SOFT_VER);
// Serve page actually
server.sendHeader(F("Content-Length"), String(page.length()));
server.send ( 200, F("text/html"), page );
}
//***********************************************************
//* MINIWX STATION - handle the main page menu buttons
//***********************************************************
void handleSubmit(){
String message;
if (server.args() > 0 ) {
//** common elements of the various pages
message += FPSTR(HTTP_HEAD);
message += FPSTR(HTTP_STYLE);
message.replace(F("{{callsign}}"), station.callsign);
//*Reboot button *********************************************
if (server.argName(0) == "Reboot" && server.arg(0) == "true"){
message += FPSTR(HTTP_REBOOT_SCRIPT);
message += FPSTR(HTTP_BODY);
// Serve page actually
message += F("<fieldset style='width:49%'><legend style='text-shadow: 2px 1px grey; font-size: 18px;'>MiniWX™ system message </legend>");
message += F("<form><div class='divTable'><div class='divRow'><div class='divColumn' style='width:98%'>");
message += F("<div class='notabheader'>MiniWX™ is rebooting, please wait 20 secs...</div>");
message += FPSTR(HTTP_FOOT);
server.sendHeader(F("Content-Length"), String(message.length()));
server.send ( 200, "text/html", message );
delay(1000);
ESP.restart();
wdt_reset();
}
//*SendAPRS button *********************************************
if (server.argName(0) == "SendAPRS" && server.arg(0) == "true"){
message += FPSTR(HTTP_SCRIPT);
message += FPSTR(HTTP_BODY);
#ifdef LANG_ENGLISH
message.replace(F("{{language}}"), "en");
#endif
#ifdef LANG_SPANISH
message.replace(F("{{language}}"), "es");
#endif
#ifdef LANG_ITALIAN
message.replace(F("{{language}}"), "it");
#endif
//Display sysmsg in a new page and come back
message += F("<fieldset style='width:49%'><legend style='text-shadow: 2px 1px grey; font-size: 18px;'>MiniWX™ system message </legend>");
#ifdef LANG_ENGLISH
message += F("<form><div class='divTable'><div class='divRow'><div class='divColumn' style='width:98%'><div class='notabheader'>Sending packets to APRS server...</div>");
#endif
#ifdef LANG_SPANISH
message += F("<form><div class='divTable'><div class='divRow'><div class='divColumn' style='width:98%'><div class='notabheader'>Enviando trama al servidor APRS...</div>");
#endif
#ifdef LANG_ITALIAN
message += F("<form><div class='divTable'><div class='divRow'><div class='divColumn' style='width:98%'><div class='notabheader'>Invio dati al server APRS...</div>");
#endif
message += F("</div></div></div></form></fieldset>");
message += FPSTR(HTTP_FOOT);
server.sendHeader(F("Content-Length"), String(message.length()));
server.send ( 200, "text/html", message );
getBmeValues();
Send2APRS();
}
//*SendWUNDER button *********************************************
if (server.argName(0) == "SendWUNDER" && server.arg(0) == "true"){
message += FPSTR(HTTP_SCRIPT);
message += FPSTR(HTTP_BODY);
//Display sysmsg in a new page and come backe
message += F("<fieldset style='width:49%'><legend style='text-shadow: 2px 1px grey; font-size: 18px;'>MiniWX™ system message </legend>");
#ifdef LANG_ENGLISH
message += F("<form><div class='divTable'><div class='divRow'><div class='divColumn' style='width:98%'><div class='notabheader'>Sending packets to WUNDER server...</div>");
#endif
#ifdef LANG_SPANISH
message += F("<form><div class='divTable'><div class='divRow'><div class='divColumn' style='width:98%'><div class='notabheader'>Enviando trama al servidor WUNDER...</div>");
#endif
#ifdef LANG_ITALIAN
message += F("<form><div class='divTable'><div class='divRow'><div class='divColumn' style='width:98%'><div class='notabheader'>Invio dati al server WUNDER...</div>");
#endif
message += F("</div></div></div></form></fieldset>");
message += FPSTR(HTTP_FOOT);
server.sendHeader(F("Content-Length"), String(message.length()));
server.send ( 200, "text/html", message );
getBmeValues();
if(sets.usewunder) Send2Wunder();
}
//*NTPSYNC button *********************************************
if (server.argName(0) == "NTPSync" && server.arg(0) == "true"){
message += FPSTR(HTTP_SSE_SCRIPT);
message += FPSTR(HTTP_BODY);
//Display sysmsg in a new page and come backe
message += F("<fieldset style='width:49%'><legend style='text-shadow: 2px 1px grey; font-size: 18px;'>MiniWX™ system message </legend>");
#ifdef LANG_ENGLISH
message += F("<form><div class='divTable'><div class='divRow'><div class='divColumn' style='width:98%'><div class='notabheader'>Sending NTP SYNC request to server...</div>");
#endif
#ifdef LANG_SPANISH
message += F("<form><div class='divTable'><div class='divRow'><div class='divColumn' style='width:98%'><div class='notabheader'>Enviando sync solicitud al servidor NTP...</div>");
#endif
#ifdef LANG_ITALIAN
message += F("<form><div class='divTable'><div class='divRow'><div class='divColumn' style='width:98%'><div class='notabheader'>Invio richiesta sync al server NTP...</div>");
#endif
//SSE CONTENT
message += "<div id='result'></div>";
message += F("</div></div></div></form></fieldset>");
server.sendHeader(F("Content-Length"), String(message.length()));
server.send ( 200, "text/html", message );
ntp();
}
// WiFi Scan and put results in a table with CSS Style
if (server.argName(0) == "WiFiScan" && server.arg(0) == "true"){
message += FPSTR(HTTP_NO_SCRIPT);
message += FPSTR(HTTP_BUTN_STYLE);
message += FPSTR(HTTP_BODY);
int i=0;
int AvailNets = WiFi.scanNetworks();//Scan for total networks available
// and prepare the table to show in webpage
String stations(F("<div><table style='font-size: 20px'><thead><tr><th>SSID</th><th>RSSI (dbm)</th><th>CHANNEL</th><th>BSSID</th><th>ENCRYPTION TYPE</th></tr></thead><tbody>"));
for ( i; i < AvailNets; ++i)
{
// Print SSID and RSSI for each network found
stations += "<tr>";
stations += "<td <td style='text-align:left'>";
stations += WiFi.SSID(i);
stations += "</td><td>";
stations += WiFi.RSSI(i);
stations += "</td><td>";
stations += WiFi.channel(i);
stations += "</td><td>";
stations += WiFi.BSSIDstr(i);
stations += "</td><td style='text-align:left'>";
switch(WiFi.encryptionType(i))
{
case ENC_TYPE_WEP:
stations += String(lock) + F(" WEP ");
break;
case ENC_TYPE_TKIP:
stations += String(lock) + F(" WPA/PSK ");
break;
case ENC_TYPE_CCMP:
stations += String(lock) + F(" WPA2/PSK ");
break;
case ENC_TYPE_NONE:
stations += String(unlock) + F(" OPEN ");
break;
case ENC_TYPE_AUTO:
stations += String(lock) + F(" WPA/WPA2/PSK ");
break;
}
stations += "</td>";
}
stations += "</tr>";
message += F("<fieldset style='width:52%'><legend style='text-shadow: 2px 1px grey; font-size: 18px;'>MiniWX™ scanning found ");
message += String(i);
message += F(" networks </legend>");
message += stations;
message += F("</tbody></table></div></fieldset>");
message += F("<form>");
message += FPSTR(HTTP_EXIT_BUTN);
message += F("</form>");
message += FPSTR(HTTP_FOOT);
server.sendHeader(F("Content-Length"), String(message.length()));
server.send ( 200, "text/html", message );
}
//***************************
// HANDLE SETTINGS PAGE FORM
//***************************
if(server.args() > 2){
for(uint8_t i=0; i < server.args(); i++)
{
//station
if (server.argName(i) == F("callsign")) server.arg(i).toCharArray(station.callsign, 10);
if (server.argName(i) == F("latitude")) server.arg(i).toCharArray(station.latitude, 10);
if (server.argName(i) == F("longitude")) server.arg(i).toCharArray(station.longitude, 10);
if (server.argName(i) == F("altitude")) station.altitude = server.arg(i).toInt();
//aprs
if (server.argName(i) == F("aprsserveraddr")) server.arg(i).toCharArray(station.clientAddress, 20);
if (server.argName(i) == F("aprspassw")) server.arg(i).toCharArray(sets.AprsPassw, 6);
if (server.argName(i) == F("aprsserverport")) station.clientPort = server.arg(i).toInt();
if (server.argName(i) == F("txdelay")) station.transmitDelay = server.arg(i).toInt();
if (server.argName(i) == F("aprscmnt1")) server.arg(i).toCharArray(sets.APRS_CMNT, 44);
if (server.argName(i) == F("aprscmnt2")) server.arg(i).toCharArray(sets.APRS_PRJ, 24);
//wunder
if (server.argName(i) == F("usewunder") && server.arg(i) == "true") sets.usewunder = true;
if (server.argName(i) == F("usewunder") && server.arg(i) == "false") sets.usewunder = false;
if (server.argName(i) == F("wunderid")) server.arg(i).toCharArray(sets.wunderid, 20);
if (server.argName(i) == F("wunderpassw")) server.arg(i).toCharArray(sets.wunderpassw, 20);
//ntp
if (server.argName(i) == F("ntpserver")) server.arg(i).toCharArray(sets.NTP_Server, 20);
if (server.argName(i) == F("ntpsyncdelay")) sets.NTP_SYNC_DELAY = server.arg(i).toInt();
//static ip
if (server.argName(i) == F("usestatic") && server.arg(i) == "true") sets.usestaticip = true;
if (server.argName(i) == F("usestatic") && server.arg(i) == "false") sets.usestaticip = false;
if (server.argName(i) == F("staticip")) server.arg(i).toCharArray(sets.static_ip, 16);
if (server.argName(i) == F("staticgateway")) server.arg(i).toCharArray(sets.static_gateway, 16);
if (server.argName(i) == F("staticmask")) server.arg(i).toCharArray(sets.static_mask, 16);
if (server.argName(i) == F("staticdns1")) server.arg(i).toCharArray(sets.static_dns1, 16);
if (server.argName(i) == F("staticdns2")) server.arg(i).toCharArray(sets.static_dns2, 16);
#ifdef DEBUG_FORM_REPLIES
Serial.print(i);
Serial.print(" ");
Serial.print("argName: ");Serial.print(server.argName(i));
Serial.print(" arg: ");Serial.println(server.arg(i));
#endif
}
//save new values in flash
writeStationFile();
writeSettingsFile();
message += FPSTR(HTTP_SETS_SCRIPT);
message += FPSTR(HTTP_BODY);
message += F("<fieldset style='width:49%'><legend style='text-shadow: 2px 1px grey; font-size: 18px;'>MiniWX™ system message </legend>");
message += F("<form><div class='divTable'><div class='divRow'><div class='divColumn' style='width:98%'><div class='notabheader'>Saving new settings in Flash memory...</div>");
message += F("</div></div></div></form></fieldset>");
message += FPSTR(HTTP_FOOT);
server.sendHeader(F("Content-Length"), String(message.length()));
server.send ( 200, "text/html", message );
}
}
}
//***********************************************************
//* MINIWX STATION - JQUERY REPLIES
//***********************************************************
void handleJQuery() {
char espclock[20];
char nexttx[20];
char uptime[40];
float dpdegc;
getBmeValues();
SystemUpTime();
// Zero-ing values that can't be display cause lack of rH% in bmp280
switch(sets.ChipModel){
case MOD_BMP280: //temp,press
wx.humidity = 0.0f;
wx.heatindex = 0.0f;
dpdegc = 0.0f;
break;
case MOD_BME280: //temp,press,rhum
calcDewPoint();
dpdegc = (wx.fdewptf-32.0f)*0.55f;// Fahrenheit to Celsius
break;
case 0x00: // NO SENSOR AT ALL