-
Notifications
You must be signed in to change notification settings - Fork 0
/
wx.ino
5774 lines (5347 loc) · 204 KB
/
wx.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
/*
python /home/dan/Arduino/hardware/espressif/esp32/tools/esptool/esptool.py --chip esp32 --port /dev/ttyUSB0 --baud 115200 --before default_reset --after hard_reset write_flash -z --flash_mode dio --flash_freq 80m --flash_size 4MB 0x10000 /home/dan/Arduino/hra/ok1hra/esp32/wx/20210407-wx.ino.esp32-poe.bin
3D printed WX station
----------------------
https://remoteqth.com/w/doku.php?id=3d_print_wx_station
TNX OK1IAK for code help
___ _ ___ _____ _ _
| _ \___ _ __ ___| |_ ___ / _ \_ _| || | __ ___ _ __
| / -_) ' \/ _ \ _/ -_) (_) || | | __ |_/ _/ _ \ ' \
|_|_\___|_|_|_\___/\__\___|\__\_\|_| |_||_(_)__\___/_|_|_|
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/>.
Send test packet
echo -n -e '\x00ms:bro;' | nc -u -w1 192.168.1.23 88 | hexdump -C
Remote USB access
screen /dev/ttyUSB0 115200
HARDWARE ESP32-POE
Changelog:
20231012 - mDNS support
20230917 - windy.com (not work, probably low memory)
20230916 - fix key login, http temperature
20230825 - add dew point, fix shift register, fix MQTT topic
20230815 - recalibrate rain
20221104 - calibrate rain
20220910 - HW rev5, DS18B20 autodetect,
20220429 - add html preview page on port 88
20220307 - add wind direction shift settings for non North orientation, update for new lib
20210513 - add enable support, RF module support by https://github.com/jarodan
20210407 - disable local CLI
20210322 - get actual data on request, via MQTT topic /get (any message)
20210321 - used inaccurate internal temperature sensor, if external DS18B20 disable
20210316 - web firmware upload
20210225 - eeprom bugfix
20210131 - add to menu erase windspeed max memory, fix max speed bug, disable internal temperature sensor
20210123 - calculate pressure with altitude TNX OK1IRG, add altitude settings in CLI
20210122 - bugfix DS18B20 eeprom set
20210116 - WDT bug fix, command listing after telnet login
20201211 - external sensor enable from CLI
20200815 - js url fix
20200814 - add WatchdogTimer for reset
20200620 - addd frenetic mode, calibrate rain sensor
20200509 - first function for calibrate wind sensor
20200424 - add support for external 1-wire temperature ensor (DS18B20)
ToDo
- web setup
- web selfcheck
- Windy
https://community.windy.com/topic/8168/report-your-weather-station-data-to-windy
https://github.com/zpukr/esp8266-WindStation/blob/master/esp8266-WindStation.ino
- Sunset https://github.com/buelowp/sunset/blob/master/examples/esp/example.ino
- telnet inactivity to close
- clear code
- https://github.com/Sensirion/arduino-sht
APRS-lora
https://how2electronics.com/esp32-lora-thingspeak-gateway-sensor-node/
https://learn.adafruit.com/adafruit-rfm69hcw-and-rfm96-rfm95-rfm98-lora-packet-padio-breakouts/rfm9x-test
https://github.com/lora-aprs/LoRa_APRS_Tracker/blob/master/src/configuration.cpp#L73
https://github.com/josefmtd/lora-aprs
https://on5vl.org/lora-aprs-le-guide-pratique/
Použití knihovny WiFi ve verzi 2.0.0 v adresáři: /home/dan/Arduino/hardware/espressif/esp32/libraries/WiFi
Použití knihovny EEPROM ve verzi 2.0.0 v adresáři: /home/dan/Arduino/hardware/espressif/esp32/libraries/EEPROM
Použití knihovny Ethernet ve verzi 2.0.0 v adresáři: /home/dan/Arduino/hardware/espressif/esp32/libraries/Ethernet
Použití knihovny ESPmDNS ve verzi 2.0.0 v adresáři: /home/dan/Arduino/hardware/espressif/esp32/libraries/ESPmDNS
Použití knihovny ArduinoOTA ve verzi 2.0.0 v adresáři: /home/dan/Arduino/hardware/espressif/esp32/libraries/ArduinoOTA
Použití knihovny Update ve verzi 2.0.0 v adresáři: /home/dan/Arduino/hardware/espressif/esp32/libraries/Update
Použití knihovny AsyncTCP ve verzi 1.1.1 v adresáři: /home/dan/Arduino/libraries/AsyncTCP
Použití knihovny ESPAsyncWebServer ve verzi 1.2.3 v adresáři: /home/dan/Arduino/libraries/ESPAsyncWebServer
Použití knihovny FS ve verzi 2.0.0 v adresáři: /home/dan/Arduino/hardware/espressif/esp32/libraries/FS
Použití knihovny AsyncElegantOTA ve verzi 2.2.5 v adresáři: /home/dan/Arduino/libraries/AsyncElegantOTA
Použití knihovny PubSubClient ve verzi 2.8 v adresáři: /home/dan/Arduino/libraries/PubSubClient
Použití knihovny Wire ve verzi 2.0.0 v adresáři: /home/dan/Arduino/hardware/espressif/esp32/libraries/Wire
Použití knihovny Adafruit_Unified_Sensor ve verzi 1.1.2 v adresáři: /home/dan/Arduino/libraries/Adafruit_Unified_Sensor
Použití knihovny Adafruit_BMP280_Library ve verzi 2.5.0 v adresáři: /home/dan/Arduino/libraries/Adafruit_BMP280_Library
Použití knihovny Adafruit_BusIO ve verzi 1.9.8 v adresáři: /home/dan/Arduino/libraries/Adafruit_BusIO
Použití knihovny SPI ve verzi 2.0.0 v adresáři: /home/dan/Arduino/hardware/espressif/esp32/libraries/SPI
Použití knihovny Adafruit_HTU21DF_Library ve verzi 1.0.4 v adresáři: /home/dan/Arduino/libraries/Adafruit_HTU21DF_Library
Použití knihovny SD_MMC ve verzi 2.0.0 v adresáři: /home/dan/Arduino/hardware/espressif/esp32/libraries/SD_MMC
Použití knihovny OneWire ve verzi 2.3.6 v adresáři: /home/dan/Arduino/libraries/OneWire
Použití knihovny DallasTemperature ve verzi 3.9.0 v adresáři: /home/dan/Arduino/libraries/DallasTemperature
*/
//-------------------------------------------------------------------------------------------------------
const char* REV = "20240211";
#define HWREVsw 8 // software PCB version [7-8]
// #define AJAX // enable ajax web server
// #define WINDY // upload to windy.com (not work, probably low memory)
// #define M_DNS // not work, but ArduinoOTA.setHostname() rewrite hostname
#define OTAWEB // enable upload firmware via web
#define DS18B20 // external 1wire Temperature sensor
#define BMP280 // pressure I2C sensor
#define HTU21D // humidity I2C sensor
// #define SHT21 // humidity I2C sensor
// #define SHT // general SHT sensors I2C sensor https://github.com/Sensirion/arduino-sht/blob/master/examples/multiple-sht-sensors/multiple-sht-sensors.ino
// #define RF69_EXTERNAL_SENSOR // RX temp and humidity radio sensor RF69
#define ETHERNET // Enable ESP32 ethernet (DHCP IPv4)
#define ETH_ADDR 0
#define ETH_TYPE ETH_PHY_LAN8720
#if HWREVsw==8
#define ETH_POWER 0 // #define ETH_PHY_POWER 0 ./Arduino/hardware/espressif/esp32/variants/esp32-poe/pins_arduino.h
#endif
#if HWREVsw==7
#define ETH_POWER 12 // mosfet on VDDIO
#endif
#define ETH_MDC 23 // MDC pin17
#define ETH_MDIO 18 // MDIO pin16
#define ETH_CLK ETH_CLOCK_GPIO17_OUT // CLKIN pin5 | settings for ESP32 GATEWAY rev f-g
String MACString;
char MACchar[18];
// #define ETH_CLK ETH_CLOCK_GPIO0_OUT // settings for ESP32 GATEWAY rev c and older
// ETH.begin(ETH_ADDR, ETH_POWER, ETH_MDC, ETH_MDIO, ETH_TYPE, ETH_CLK);
// #define WIFI // Enable ESP32 WIFI (DHCP IPv4) - NOT TESTED
const char* ssid = "";
const char* password = "";
const float FunelDiaInCM = 10.0; // cm funnel diameter
unsigned char HWREVpcb = 0; // PCB version must be compatible with HWREVsw
//-------------------------------------------------------------------------------------------------------
// unsigned long TimerTemp;
// interrupts
#include "esp_attr.h"
// values
const int keyNumber = 1;
char key[100];
String YOUR_CALL = "";
/*
1mm rain = 15,7cm^2/10 = 1,57ml <- by rain funnel radius
10ml = 11,5 pulses = 0,87ml/pulse <- constanta tilting measuring cup
*/
// float mmInPulse = 0.87/(3.14*(FunelDiaInCM/2)*(FunelDiaInCM/2)/10); // calculate rain mm, in one pulse
// float mmInPulse = 0.2 ; // callibration rain 17,7-20,2 mm with 95 pulse
float mmInPulse = 0; // 0.65 ; // measure 3.8mm, reference 12.2mm
long MeasureTimer[2]={2800000,300000}; // millis,timer (5 min)
long RainTimer[2]={0,5000}; // <---------------- rain timing
int RainCount;
String RainCountDayOfMonth;
bool RainStatus;
int WindDir = 0;
int WindDirShift = 0;
float RainTodayMM = 0;
float WindSpeedAvgMPS = 0;
float WindSpeedMaxPeriodMPS = 0;
float PressureHPA = 0;
float TemperatureCelsius = 0;
float HumidityRelPercent = 0;
float DewPointCelsius = 0;
float PressureHPaBMP280 = 0;
float TemperatureCelsiusBMP280 = 0;
float HumidityRelPercentHTU21D = 0;
float DewPointCelsiusHTU21D = 0;
float TemperatureCelsiusDS18B20 = 0;
long RpmTimer[2]={0,3000};
long RpmPulse = 987654321;
long PeriodMinRpmPulse = 987654321;
String WindSpeedMaxPeriodUTC;
long MinRpmPulse;
String MinRpmPulseTimestamp;
// unsigned int RpmSpeed = 0;
unsigned long RpmAverage[2]={1,0}; // counter,sum time
bool RpmInterrupt = false;
float TempCal=0;
int SpeedAlertLimit_ms = 0;
int SpeedAlert_ms = 3000;
// bool NeedSpeedAlert_ms = false;
long AlertTimer[2]={0,60000};
// |alert...........|alert........... everry max 1 minutes and with publish max value in period
// 73 seconds WDT (WatchDogTimer)
#include <esp_task_wdt.h>
#define WDT_TIMEOUT 73
long WdtTimer=0;
byte InputByte[21];
// #define Ser2net // Serial to ip proxy - DISABLE if board revision 0.3 or lower
#define EnableOTA // Enable flashing ESP32 Over The Air
// int NumberOfEncoderOutputs = 8; // 2-16
byte NET_ID = 0x00; // Unique ID number [0-F] hex format
int EnableSerialDebug = 0;
long FreneticModeTimer ;
#define HTTP_SERVER_PORT 80 // Web server port
int IncomingSwitchUdpPort;
#define ShiftOut // Enable ShiftOut register
#define UdpAnswer // Send UDP answer confirm packet
int BroadcastPort; // destination broadcast packet port
// bool EnableGroupPrefix = 0; // enable multi controller control
// bool EnableGroupButton = 0; // group to one from
// unsigned int GroupButton[8]={1,2,3,4,5,6,7,8};
byte DetectedRemoteSw[16][4];
unsigned int DetectedRemoteSwPort[16];
const int SERIAL_BAUDRATE = 115200; // serial debug baudrate
int SERIAL1_BAUDRATE; // serial1 to IP baudrate
int incomingByte = 0; // for incoming serial data
int i = 0;
#include <WiFi.h>
// mDNS
#if defined(M_DNS) && defined(ETHERNET)
#include <ESPmDNS.h>
#endif
#include <WiFiUdp.h>
#include "EEPROM.h"
#define EEPROM_SIZE 368 /* up to 512
0|Byte 1|128
1|Char 1|A
2|UChar 1|255
3|Short 2|-32768
5|UShort 2|65535
7|Int 4|-2147483648
11|Uint 4|4294967295
15|Long 4|-2147483648
19|Ulong 4|4294967295
23|Long64 8|0x00FFFF8000FF4180
31|Ulong64 8|0x00FFFF8000FF4180
39|Float 4|1234.1234
43|Double 8|123456789.12345679
51|Bool 1|1
0 -listen source
1 -net ID
2-3 - TempCal Short
4 - HWREVpcb UChar
5 - mmInPulse Short
-13
14-17 - SERIAL1_BAUDRATE
18-21 - SerialServerIPport
22-25 - IncomingSwitchUdpPort
26-29 - RebootWatchdog
30-33 - OutputWatchdog
34 - Bank0 storage
35 - Bank1 storage
36 - Bank2 storage
37-40 - Authorised telnet client IP
41-140 - Authorised telnet client key
141-160 - YOUR_CALL
161-164 - MQTT broker IP
165-168? - MQTT broker port 4
169-172 - MinRpmPulse 4
173-198 - MinRpmPulseTimestamp
199 - APRS ON/OFF;
200-203 - APRS server IP
204-207 - APRS server port 4
208-212 - APRS password
213-230 - APRS coordinate
231-234 - Altitude 4
// 232 - SpeedAlert 4
235-238 - SpeedAlert 4
// 233 - DS18B20 on/off 1
239 - DS18B20 on/off 1
240-243 - WindDirShift 4
244-367 - 123 char API key windy.com
!! Increment EEPROM_SIZE #define !!
*/
int Altitude = 0;
bool needEEPROMcommit = false;
unsigned int RebootWatchdog;
unsigned int OutputWatchdog;
unsigned long WatchdogTimer=0;
#if defined(WINDY)
// #include <HTTPClient.h>
#include <WiFiClientSecure.h>
const char* server = "stations.windy.com"; // Server URL
WiFiClientSecure client;
// ISRG Root X1 root .pem certificate for windy.com valid to Mon, 04 Jun 2035 11:04:38 GMT
const char* rootCACertificate = \
"-----BEGIN CERTIFICATE-----\n" \
"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\n" \
"-----END CERTIFICATE-----\n";
#endif
#if defined(AJAX)
#include <WebServer.h>
#include "index.h" //Web page header file
#include "index-cal.h" //Web page header file
WebServer ajaxserver(HTTP_SERVER_PORT+9);
#endif
WiFiServer server1(HTTP_SERVER_PORT);
WiFiServer server2(HTTP_SERVER_PORT+8);
bool DHCP_ENABLE = 1;
// Client variables
char linebuf[80];
int charcount=0;
//Are we currently connected?
boolean connected = false;
//The udp library class
WiFiUDP UdpCommand;
uint8_t buffer[50] = "";
unsigned char packetBuffer[10];
int UDPpacketSize;
byte TxUdpBuffer[8];
#include <ETH.h>
static bool eth_connected = false;
IPAddress RemoteSwIP(0, 0, 0, 0); // remote UDP IP switch - set from UDP DetectRemote array
int RemoteSwPort = 0; // remote UDP IP switch port
String HTTP_req;
#if defined(EnableOTA)
#include <ESPmDNS.h>
#include <ArduinoOTA.h>
#endif
#if defined(OTAWEB)
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <AsyncElegantOTA.h>
AsyncWebServer OTAserver(82);
#endif
#define MQTT // Enable MQTT debug
#if defined(MQTT)
#include <PubSubClient.h>
// #include "PubSubClient.h" // lokalni verze s upravou #define MQTT_MAX_PACKET_SIZE 128
// WiFiClient esp32Client;
// PubSubClient mqttClient(esp32Client);
WiFiClient espClient;
PubSubClient mqttClient(espClient);
// PubSubClient mqttClient(ethClient);
// PubSubClient mqttClient(server, 1883, callback, ethClient);
long lastMqttReconnectAttempt = 0;
#endif
boolean MQTT_ENABLE = 1; // enable public to MQTT broker
IPAddress mqtt_server_ip(0, 0, 0, 0);
// byte BrokerIpArray[2][4]{
// // {192,168,1,200}, // MQTT broker remoteqth.com
// {54,38,157,134}, // MQTT broker remoteqth.com
// };
// IPAddress server(10, 24, 213, 92); // MQTT broker
int MQTT_PORT; // MQTT broker PORT
// int MQTT_PORT_Array[2] = {
// 1883,
// 1883
// }; // MQTT broker PORT
boolean MQTT_LOGIN = 0; // enable MQTT broker login
// char MQTT_USER= 'login'; // MQTT broker user login
// char MQTT_PASS= 'passwd'; // MQTT broker password
const int MqttBuferSize = 1000; // 1000
char mqttTX[MqttBuferSize];
char mqttPath[MqttBuferSize];
// char mqttTX[100];
// char mqttPath[100];
long MqttStatusTimer[2]{1500,1000};
long HeartBeatTimer[2]={0,1000};
// WX pinout
const int ShiftInDataPin = 34; // to rev0.3 13
const int ShiftInLatchPin = 4;
const int ShiftInClockPin = 5;
bool rxShiftInRead;
byte ShiftOutByte[3];
// https://randomnerdtutorials.com/esp32-i2c-communication-arduino-ide/
#include <Wire.h>
#define I2C_SDA 33
#define I2C_SCL 32
#if defined(BMP280)||defined(HTU21D)||defined(SHT21)
// #include <SPI.h>
#include <Adafruit_Sensor.h>
TwoWire I2Cone = TwoWire(0);
#endif
#if defined(BMP280)
#include <Adafruit_BMP280.h>
Adafruit_BMP280 bmp(&I2Cone); // use I2C interface
Adafruit_Sensor *bmp_temp = bmp.getTemperatureSensor();
Adafruit_Sensor *bmp_pressure = bmp.getPressureSensor();
bool BMP280enable;
#endif
#if defined(HTU21D)
#include "Adafruit_HTU21DF.h"
Adafruit_HTU21DF htu = Adafruit_HTU21DF();
bool HTU21Denable;
#endif
#if defined(SHT21)
#include "SHT2x.h"
SHT2x internal;
// SHT2x external;
#endif
#if defined(SHT)
#include "SHTSensor.h"
// Sensor with normal i2c address
// Sensor 1 with address pin pulled to GND
SHTSensor sht1(SHTSensor::SHT3X);
// Sensor with alternative i2c address
// Sensor 2 with address pin pulled to Vdd
// SHTSensor sht2(SHTSensor::SHT3X_ALT);
#endif
// https://github.com/PaulStoffregen/RadioHead
#if defined(RF69_EXTERNAL_SENSOR)
bool RF69enable;
#include <SPI.h>
#include <RH_RF69.h>
// Change to 434.0 or other frequency, must match RX's freq!
#define RF69_FREQ 434.0
#define RFM69_RST -1 // same as LED
#define RFM69_CS 0 // "B"
#define RFM69_INT 16 // "A"
// Singleton instance of the radio driver
RH_RF69 rf69(RFM69_CS, RFM69_INT);
int16_t packetnum = 0; // packet counter, we increment per xmission
String received_data;
float humidity, temp_f, temp_f2, temp_PT100, temp_dallas;
String temp_radio;
String humidity_radio;
String vbat_radio;
uint8_t buf[RH_RF69_MAX_MESSAGE_LEN];
uint8_t len = sizeof(buf);
#endif
const int RpmPin = 39;
#if HWREVsw==8
const int RainPin = 36;
#endif
#if HWREVsw==7
const int Rain1Pin = 36;
const int Rain2Pin = 35;
#endif
// const int EnablePin = 13;
// const int ButtonPin = 34;
#if defined(Ser2net)
#define RX1 3
#define TX1 1
HardwareSerial Serial_one(1);
#endif
const int MappingRow = 5;
const long mapping[MappingRow][2] = { // ms > m/s
{987654321,0},
{120,1},
{50,2},
{2,4},
{1,200},
};
// WX end
// SD
// #define ETH_CLK_MODE ETH_CLOCK_GPIO17_OUT
// #define ETH_PHY_POWER 12
// #include "FS.h"
// #include "SD_MMC.h"
// ntp
#include "time.h"
const char* ntpServer = "pool.ntp.org";
// const char* ntpServer = "tik.cesnet.cz";
// const char* ntpServer = "time.google.com";
const long gmtOffset_sec = 0;
const int daylightOffset_sec = 0;
#define MAX_SRV_CLIENTS 1
int SerialServerIPport;
// WiFiServer SerialServer(SerialServerIPport);
WiFiServer SerialServer;
WiFiClient SerialServerClients[MAX_SRV_CLIENTS];
int TelnetServerIPport = 23;
WiFiServer TelnetServer;
WiFiClient TelnetServerClients[MAX_SRV_CLIENTS];
IPAddress TelnetServerClientAuth;
bool TelnetAuthorized = false;
int TelnetAuthStep=0;
int TelnetAuthStepFails=0;
int TelnetLoginFails=0;
long TelnetLoginFailsBanTimer[2]={0,600000};
int RandomNumber;
bool FirstListCommands=true;
int CompareInt;
// APRS
WiFiClient AprsClient;
boolean AprsON = false;
uint16_t AprsPort;
IPAddress aprs_server_ip(0, 0, 0, 0);
String AprsPassword;
String AprsCoordinates;
// DS18B20
#if defined(DS18B20)
// #include <OneWire.h>
// #include <DallasTemperature.h>
// // const int DsPin = 3;
// // OneWire ds(DsPin);
// // DallasTemperature sensors(&ds);
// const int oneWireBus = 13;
// OneWire oneWire(oneWireBus);
// DallasTemperature sensors(&oneWire);
bool ExtTemp = true;
// Include the libraries we need
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is plugged into port 2 on the Arduino
#if HWREVsw==8
#define ONE_WIRE_BUS 2
#endif
#if HWREVsw==7
#define ONE_WIRE_BUS 13
#endif
#define TEMPERATURE_PRECISION 10 // 9: ±0,5°C | 10: ±0,25°C | 11: ±0,125°C
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
// arrays to hold device addresses
DeviceAddress insideThermometer, outsideThermometer;
// Assign address manually. The addresses below will need to be changed
// to valid device addresses on your bus. Device address can be retrieved
// by using either oneWire.search(deviceAddress) or individually via
// sensors.getAddress(deviceAddress, index)
// DeviceAddress insideThermometer = { 0x28, 0x1D, 0x39, 0x31, 0x2, 0x0, 0x0, 0xF0 };
// DeviceAddress outsideThermometer = { 0x28, 0x3F, 0x1C, 0x31, 0x2, 0x0, 0x0, 0x2 };
#endif
//-------------------------------------------------------------------------------------------------------
void setup() {
// for (int i = 0; i < 8; i++) {
// pinMode(TestPin[i], INPUT);
// }
#if HWREVsw==8
pinMode(RainPin, INPUT);
#endif
#if HWREVsw==7
pinMode(Rain1Pin, INPUT);
pinMode(Rain2Pin, INPUT);
#endif
pinMode(RpmPin, INPUT);
// pinMode(EnablePin, OUTPUT);
// digitalWrite(EnablePin,1);
// pinMode(ButtonPin, INPUT);
// SHIFT IN
pinMode(ShiftInLatchPin, OUTPUT);
digitalWrite(ShiftInLatchPin,1);
pinMode(ShiftInClockPin, OUTPUT);
pinMode(ShiftInDataPin, INPUT);
Serial.begin(SERIAL_BAUDRATE);
while(!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
#if defined(DS18B20)
sensors.begin();
// locate devices on the bus
Serial.print("DS18B20 found ");
Serial.print(sensors.getDeviceCount(), DEC);
Serial.println(" devices.");
// report parasite power requirements
// Serial.print("Parasite power is: ");
// if (sensors.isParasitePowerMode()) Serial.println("ON");
// else Serial.println("OFF");
// Search for devices on the bus and assign based on an index. Ideally,
// you would do this to initially discover addresses on the bus and then
// use those addresses and manually assign them (see above) once you know
// the devices on your bus (and assuming they don't change).
//
// method 1: by index
if (!sensors.getAddress(insideThermometer, 0)){
ExtTemp = false;
Serial.println("DS18B20 unable to find address for Device 0");
}
// if (!sensors.getAddress(outsideThermometer, 1)) Serial.println("Unable to find address for Device 1");
// method 2: search()
// search() looks for the next device. Returns 1 if a new address has been
// returned. A zero might mean that the bus is shorted, there are no devices,
// or you have already retrieved all of them. It might be a good idea to
// check the CRC to make sure you didn't get garbage. The order is
// deterministic. You will always get the same devices in the same order
//
// Must be called before search()
//oneWire.reset_search();
// assigns the first address found to insideThermometer
//if (!oneWire.search(insideThermometer)) Serial.println("Unable to find address for insideThermometer");
// assigns the seconds address found to outsideThermometer
//if (!oneWire.search(outsideThermometer)) Serial.println("Unable to find address for outsideThermometer");
// show the addresses we found on the bus
Serial.print("DS18B20 device 0 Address: ");
printAddress(insideThermometer);
Serial.println();
// Serial.print("Device 1 Address: ");
// printAddress(outsideThermometer);
// Serial.println();
// set the resolution to 9 bit per device
sensors.setResolution(insideThermometer, TEMPERATURE_PRECISION);
// sensors.setResolution(outsideThermometer, TEMPERATURE_PRECISION);
Serial.print("DS18B20 device 0 Resolution: ");
Serial.print(sensors.getResolution(insideThermometer), DEC);
Serial.println();
// Serial.print("Device 1 Resolution: ");
// Serial.print(sensors.getResolution(outsideThermometer), DEC);
// Serial.println();
#endif
#if defined(BMP280)
// I2Cone.begin(0x76, I2C_SDA, I2C_SCL, 100000); // SDA pin, SCL pin, 100kHz frequency
I2Cone.begin(I2C_SDA, I2C_SCL, (uint32_t)100000); // SDA pin, SCL pin, 100kHz frequency
Serial.print("BMP280 sensor init ");
if(!bmp.begin(0x76)){
Serial.println("failed!");
// while (1) delay(10);
BMP280enable=false;
}else{
Serial.println("OK");
BMP280enable=true;
/* Default settings from datasheet. */
bmp.setSampling(Adafruit_BMP280::MODE_NORMAL, /* Operating Mode. */
Adafruit_BMP280::SAMPLING_X2, /* Temp. oversampling */
Adafruit_BMP280::SAMPLING_X16, /* Pressure oversampling */
Adafruit_BMP280::FILTER_X16, /* Filtering. */
Adafruit_BMP280::STANDBY_MS_500); /* Standby time. */
bmp_temp->printSensorDetails();
}
#endif
#if defined(HTU21D) || defined(SHT)
Wire.begin(I2C_SDA, I2C_SCL);
#endif
#if defined(HTU21D)
Serial.print("HTU21D sensor init ");
if(!htu.begin()){
Serial.println("failed!");
// while (1);
HTU21Denable=false;
}else{
Serial.println("OK");
HTU21Denable=true;
}
#endif
#if defined(SHT21)
Serial.println(__FILE__);
Serial.print("SHT2x_LIB_VERSION: \t");
Serial.println(SHT2x_LIB_VERSION);
internal.begin(&I2Cone);
// external.begin(&I2Ctwo);
uint8_t stat = internal.getStatus();
Serial.print(stat, HEX);
Serial.println();
// stat = external.getStatus();
// Serial.print(stat, HEX);
// Serial.println();
Serial.println();
#endif
#if !defined(BMP280) && !defined(HTU21D)
Wire.begin(I2C_SDA, I2C_SCL);
#endif
// // SD
// if(!SD_MMC.begin()){
// Serial.println("SD card Mount Failed");
// // return;
// }
// Listen source
if (!EEPROM.begin(EEPROM_SIZE)){
if(EnableSerialDebug>0){
Serial.println("failed to initialise EEPROM"); delay(1);
}
}
// 0-listen source
// TxUdpBuffer[2] = EEPROM.read(0);
// if(TxUdpBuffer[2]=='o'||TxUdpBuffer[2]=='r'||TxUdpBuffer[2]=='m'||TxUdpBuffer[2]=='e'){
// // OK
// }else{
TxUdpBuffer[2]='n';
// }
// 1-net ID
NET_ID = EEPROM.read(1);
TxUdpBuffer[0] = NET_ID;
// 2-3 TempCal Short
if(EEPROM.readByte(2)!=255){
TempCal = (float)EEPROM.readShort(2)/100.0;
}else{
TempCal = 0;
}
// 4 HWREVpcb UChar
if(EEPROM.readByte(4)!=255){
HWREVpcb = EEPROM.readUChar(4);
}
// 5 mmInPulse Short
if(EEPROM.readByte(5)!=255){
mmInPulse = (float)EEPROM.readShort(5)/100.0;
}else{
mmInPulse = 0.13;
}
SERIAL1_BAUDRATE=EEPROM.readInt(14);
SerialServerIPport=EEPROM.readInt(18);
IncomingSwitchUdpPort=EEPROM.readInt(22);
BroadcastPort=IncomingSwitchUdpPort;
RebootWatchdog=EEPROM.readUInt(26);
if(RebootWatchdog>10080){
RebootWatchdog=0;
}
OutputWatchdog=EEPROM.readUInt(30);
if(OutputWatchdog>10080){
OutputWatchdog=0;
}
if(RebootWatchdog>0){
ShiftOutByte[0]=EEPROM.readByte(34);
ShiftOutByte[1]=EEPROM.readByte(35);
ShiftOutByte[2]=EEPROM.readByte(36);
}
TelnetServerClientAuth[0]=EEPROM.readByte(37);
TelnetServerClientAuth[1]=EEPROM.readByte(38);
TelnetServerClientAuth[2]=EEPROM.readByte(39);
TelnetServerClientAuth[3]=EEPROM.readByte(40);
// 41-140 key
// if clear, generate
if(EEPROM.readByte(41)==255 && EEPROM.readByte(140)==255){
Serial.println();
Serial.println(" ** GENERATE KEY **");
for(int i=41; i<141; i++){
EEPROM.writeChar(i, RandomChar());
Serial.print("*");
}
EEPROM.commit();
Serial.println();
}
// read
for(int i=41; i<141; i++){
key[i-41] = EEPROM.readChar(i);
}
// YOUR_CALL
// move after ETH init
// MQTT broker IP
for(int i=0; i<4; i++){
mqtt_server_ip[i]=EEPROM.readByte(i+161);
}
MQTT_PORT = EEPROM.readInt(165);
if(mqtt_server_ip[0]==255 && mqtt_server_ip[1]==255 && mqtt_server_ip[2]==255 && mqtt_server_ip[3]==255 && MQTT_PORT==-1){
mqtt_server_ip[0]=54;
mqtt_server_ip[1]=38;
mqtt_server_ip[2]=157;
mqtt_server_ip[3]=134;
MQTT_PORT=1883;
}
// RPM
MinRpmPulse = EEPROM.readLong(169);
if(MinRpmPulse<=0){
MinRpmPulse=987654321;
}
// 173
if(EEPROM.readByte(173)!=255){
MinRpmPulseTimestamp = EEPROM.readString(173);
}
// 199 - APRS ON/OFF;
if(EEPROM.read(199)<2){
AprsON=EEPROM.read(199);
}
// 200-203 - APRS server IP
// 204-207 - APRS server port
for(int i=0; i<4; i++){
aprs_server_ip[i]=EEPROM.readByte(i+200);
}
AprsPort = EEPROM.readInt(204);
// 208-212 - APRS password
for (int i=208; i<213; i++){
if(EEPROM.read(i)!=0xff){
AprsPassword=AprsPassword+char(EEPROM.read(i));
}
}
// 213-230 - APRS coordinate
for (int i=213; i<231; i++){
if(EEPROM.read(i)!=0xff){
AprsCoordinates=AprsCoordinates+char(EEPROM.read(i));
}
}
// 231-234 Altitude
if(EEPROM.readByte(231)!=255){
Altitude = EEPROM.readInt(231);
}
// 235-238 AlertLimit
if(EEPROM.readByte(235)!=255){
SpeedAlertLimit_ms = EEPROM.readInt(235);
}
#if defined(DS18B20)
// 239 - ExtTemp ON/OFF;
if(EEPROM.read(239)<2){
ExtTemp=EEPROM.read(239);
}
#endif
// 240-243 WindDirShift
if(EEPROM.readByte(240)!=255){
WindDirShift = EEPROM.readInt(240);
}
#if defined(WIFI)
if(EnableSerialDebug>0){
Serial.print("WIFI Connecting to ");
Serial.print(ssid);
}
WiFi.begin(ssid, password);
// attempt to connect to Wifi network:
while(WiFi.status() != WL_CONNECTED) {
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
delay(500);
if(EnableSerialDebug>0){
Serial.print(".");
}
}
// LED1status = !LED1status;
// digitalWrite(LED1, LED1status); // signalize wifi connected
if(EnableSerialDebug>0){
Serial.println("");
Serial.println("WIFI connected");
Serial.print("WIFI IP address: ");
Serial.println(WiFi.localIP());
Serial.print("WIFI dBm: ");
Serial.println(WiFi.RSSI());
}
#endif
#if defined(ETHERNET)
// mqtt_server_ip=BrokerIpArray[0];
// MQTT_PORT = MQTT_PORT_Array[0];
WiFi.onEvent(EthEvent);
// ETH.begin();
ETH.begin(ETH_ADDR, ETH_POWER, ETH_MDC, ETH_MDIO, ETH_TYPE, ETH_CLK);
if(DHCP_ENABLE==false){
ETH.config(IPAddress(192, 168, 1, 188), IPAddress(192, 168, 1, 255),IPAddress(255, 255, 255, 0),IPAddress(8, 8, 8, 8));
//config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = (uint32_t)0x00000000, IPAddress dns2 = (uint32_t)0x00000000);
}
// mDNS
#if defined(M_DNS)
// Set up mDNS responder:
// - first argument is the domain name, in this example
// the fully-qualified domain name is "esp32.local"
// - second argument is the IP address to advertise
// we send our IP address on the WiFi network
if (MDNS.begin("wx")) {
Serial.println("mDNS server run");
MDNS.addService("http", "tcp", 80);
MDNS.addService("http", "tcp", 88);
} else {
Serial.println("Error start mDNS server");
}
#endif
#endif
server1.begin();
server2.begin();
UdpCommand.begin(IncomingSwitchUdpPort); // incoming udp port
// chipid=ESP.getEfuseMac();//The chip ID is essentially its MAC address(length: 6 bytes).
// unsigned long long1 = (unsigned long)((chipid & 0xFFFF0000) >> 16 );
// unsigned long long2 = (unsigned long)((chipid & 0x0000FFFF));
// ChipidHex = String(long1, HEX) + String(long2, HEX); // six octets
// YOUR_CALL=ChipidHex;
#if defined(EnableOTA)
// Port defaults to 3232
// ArduinoOTA.setPort(3232);
// Hostname defaults to esp3232-[MAC]
// String StringHostname = "WX-station-"+String(NET_ID, HEX);
String StringHostname = "WX-"+String(YOUR_CALL);
char copy[13];
StringHostname.toCharArray(copy, 13);
ArduinoOTA.setHostname(copy);
ArduinoOTA.setPassword("remoteqth");
// $ echo password | md5sum
// ArduinoOTA.setPasswordHash("5587ba7a03b12a409ee5830cea97e079");
ArduinoOTA
.onStart([]() {
esp_task_wdt_reset();
WdtTimer=millis();
Interrupts(false);
String type;
if (ArduinoOTA.getCommand() == U_FLASH)
type = "sketch";
else // U_SPIFFS
type = "filesystem";
// NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
Serial.println("Start updating " + type);
})
.onEnd([]() {
Serial.println("\nEnd");
Interrupts(true);
})
.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
})
.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
else if (error == OTA_END_ERROR) Serial.println("End Failed");
Interrupts(true);
});
ArduinoOTA.begin();
#endif
#if defined(OTAWEB)
OTAserver.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send(200, "text/plain", "PSE QSY to /update");