forked from ok1hra/IP-rotator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IP-rotator.ino
5243 lines (4820 loc) · 174 KB
/
IP-rotator.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
/*
3D printed IP rotator
----------------------
Compile for HARDWARE ESP32-POE
___ _ ___ _____ _ _
| _ \___ _ __ ___| |_ ___ / _ \_ _| || | __ ___ _ __
| / -_) ' \/ _ \ _/ -_) (_) || | | __ |_/ _/ _ \ ' \
|_|_\___|_|_|_\___/\__\___|\__\_\|_| |_||_(_)__\___/_|_|_|
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/>.
Used MQTT-WALL, credit Adam Hořčica, is under MIT license,
see https://github.com/bastlirna/mqtt-wall/blob/master/license.txt
Contributors: Joerg DL3QQ, Mark G4MEM
MQTT monitor
mosquitto_sub -v -h 192.168.1.200 -t 'OK1HRA/ROT/#'
mosquitto_sub -v -h 54.38.157.134 -t 'OK1HRA/1/ROT/#'
MQTT topic
mosquitto_pub -h 192.168.1.200 -t OK1HRA/ROT/Target -m '10'
mosquitto_pub -h 54.38.157.134 -t BD:2F/ROT/Target -m '10'
Changelog:
+ rotator name
+ watchdog timer, azimuth change
+ stop zone gray color
+ under 11V POE voltage watchdog
+ azimuth target
+ rotator ID for more units mqtt identification
+ calibrate gui set
+ software endstop zone
+ darkzone map, if range < 360
+ watchdog speed limit in gui
+ HW detection show in GUI
+ web online status (timeout 2s)
+ on hover button
+ fix url button
+ diffrent low/high endstop zone
+ control key
+ AC functionality
+ AZ potentiometer
+ LED
+ Supported GS-232 commands: R L A S C Mxxx O F
+ CW/CCW pulse inputs gui
+ serial baudrate set
+ warm up timer for stable value
+ AZtwoWire L=three H=two wire AZ pot
+ AZpreamp L=preamp on H=pre amp off
+ if source AZsource == false && TwoWire == true && CwRaw < 1577 | then show recomended
+ add reverse azimuth button
+ click to map during rotate stoped
+ BRAKE in DC mode support
+ if mqtt_server_ip[0]=0 then disable MQTT
+ Disable PWM from setup gui
+ if PWM in gui OFF, AC-cw out for DC input, if PWM disable and high voltage
+ small stop ramp if(abs(AzimuthTarget-Azimuth)<2)
+ rename PWR to POE on main control page
+ reset button for calibrate settings in setup
+ calibrate control potentiometer on north (show ° in setup gui)
+ DC use brake relay
+ add AzimuthStop mqttpub
+ support HW rev 6 and 7
+ add new settings to setup gui for PWM
- PWM ramp length
- PWM start distance
ToDo
- test
- if stop, after run over target
+ 10 turn pot without preamp
+ test preamp linearity
- save settings (dump eeprom?)
- show target from az pot in map
- disable target in map if status = 0 (not rotate)
- need implement
- CW/CCW pulse functionality
- CW/CCW pulse pulse per grad
- serial protocol easycomm2
- watchdog if eth_connected = false; > 5s, then reconect
- do debugu vypisovat prumer casu behu hlavni smycky v ms
- telnet
- vycistit kod
https://stackoverflow.com/questions/3420975/html5-canvas-zooming
https://codepen.io/chengarda/pen/wRxoyB
IDE 1.8.19
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 WebServer ve verzi 2.0.0 v adresáři: /home/dan/Arduino/hardware/espressif/esp32/libraries/WebServer
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.7 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
*/
//-------------------------------------------------------------------------------------------------------
const char* REV = "20241011";
// #define CN3A // fix ip
float NoEndstopHighZone = 0;
float NoEndstopLowZone = 0;
bool Reverse = false;
bool ReverseAZ = false;
short CcwRaw = 0;
short CwRaw = 0;
short HardwareRev = 99;
unsigned int OneTurnLimitSec = 0;
// set from GUI
String YOUR_CALL = "";
String NET_ID = "";
String RotName = "";
int StartAzimuth = 0; // max CCW limit callibrate in real using
unsigned int MaxRotateDegree = 0;
unsigned int AntRadiationAngle = 10;
String MapUrl = "" ;
//$ /usr/bin/xplanet -window -config ./geoconfig -longitude 13.8 -latitude 50.0 -geometry 600x600 -projection azimuthal -num_times 1 -output ./map.png
//$ /usr/bin/xplanet -window -config ./geoconfig -longitude 13.8 -latitude 50.0 -geometry 600x600 -projection azimuthal -radius 500 -num_times 1 -output ./OK500.png
bool Endstop = false;
bool ACmotor = false;
bool AZsource = false;
short PulsePerDegree = 0;
bool AZtwoWire = false;
bool AZpreamp = false;
bool PWMenable = true;
unsigned int PwmDegree = 0;
unsigned int PwmRampSteps = 0;
unsigned int PwmUpDelay = 3; // [ms]*255steps
unsigned int PwmDwnDelay = 2; // [ms]*255steps
byte dutyCycle = 0;
long StatusWatchdogTimer = 0;
long RotateWatchdogTimer = 0;
int AzimuthWatchdog = 0;
bool ErrorDetect = 0;
long TxMqttAzimuthTimer = 0;
//---------------------------------------------------------
// #define HWREV 8 // PCB version [7-8]
#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
#define ETH_POWER 0 //12 // #define ETH_PHY_POWER 0 ./Arduino/hardware/espressif/esp32/variants/esp32-poe/pins_arduino.h
#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 = "";
//-------------------------------------------------------------------------------------------------------
#include "esp_adc_cal.h"
const int AzimuthPin = 39; // analog
float AzimuthValue = 0.0;
int Azimuth = 0;
int AzimuthTarget = 0;
int Status = 0; // -3 PwmDwnCCW|-2 CCW|-1 PwmUpCCW|0 off|1 PwmUpCW|2 CW|3 PwmDwnCW
const int VoltagePin = 35; // analog
float VoltageValue = 0.0;
const float VoltageLimit = 11.5; // ! also change if( Number(this.responseText)<11.5){ in index.h file | (11.0) Voltage limit below which the control electronics is unstable
const int ReversePin = 16; //
const int PwmPin = 4; //
const int HWidPin = 34; // analog
int HWidValue = 0;
// HardwareRev 1
const int ACcwPin = 2;
const int BrakePin = 33;
// const int CwCcwButtPin = 36; // analog
const int CwInputPin = 36;
const int CcwInputPin = 5;
int CwCcwInputValue = 0;
const int AZmasterPin = 32; // analog
int AZmaster = 142;
int AZmasterValue = 0;
const int LedRPin = 15;
const int LedGPin = 14;
const int LedBPin = 0;
const int AZtwoWirePin = 13;
const int AZpreampPin = 12;
// setting PWM properties
const int PwmFreq = 1000;
const int PwmResolution = 8;
const int mosfetPWMChannel = 0;
const int greenPWMChannel = 2;
// unsigned long TimerTemp;
// interrupts
#include "esp_attr.h"
// values
char key[100];
long MeasureTimer[2]={2800000,300000}; // millis,timer (5 min)
int RainCount;
String RainCountDayOfMonth;
bool RainStatus;
/*
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.2 ; // callibration rain 17,7-20,2 mm with 95 pulse
int WindDir = 0;
int WindDirShift = 0;
long RpmTimer[2]={0,3000};
long RpmPulse = 987654321;
long PeriodMinRpmPulse = 987654321;
String PeriodMinRpmPulseTimestamp;
long MinRpmPulse;
String MinRpmPulseTimestamp;
unsigned int RpmSpeed = 0;
unsigned long RpmAverage[2]={1,0}; // counter,sum time
bool RpmInterrupt = false;
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
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];
int 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>
#include <WiFiUdp.h>
#include "EEPROM.h"
#define EEPROM_SIZE 245 /*
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-1 - NET_ID
2-22 - RotName
23 - StartAzimuth UShort
25 - MaxRotateDegree UShort
27 - AntRadiationAngle UShort
29 - Endstop Bool
30 - ACmotor Bool
31-32 - CcwRaw
33-34 - CwRaw
35 - Reverse
36 - NoEndstopLowZone
37-40 - Authorised telnet client IP
41-140 - Authorised telnet client key
141-160 - YOUR_CALL
161-164 - MQTT broker IP
165-166 - MQTT_PORT
167-168 - none
169-219 - MapUrl
220-221 - OneTurnLimitSec
222 - NoEndstopHighZone
223 - AZsource
224-225 PulsePerDegree
226-227 BaudRate
228 AZtwoWire
229 AZpreamp
230 - ReverseAZ
231 - PWMenable
232 PwmDegree UShort
234 PwmRampSteps UShort
!! Increment EEPROM_SIZE #define !!
*/
int Altitude = 0;
bool needEEPROMcommit = false;
unsigned int RebootWatchdog;
unsigned int OutputWatchdog;
unsigned long WatchdogTimer=0;
//ajax
#include <WebServer.h>
#include "index.h" //Web page header file
#include "index-cal.h" //Web page header file
WebServer ajaxserver(HTTP_SERVER_PORT+8);
WiFiServer server(HTTP_SERVER_PORT);
#if defined(CN3A)
bool DHCP_ENABLE = 0;
#else
bool DHCP_ENABLE = 1;
#endif
// 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};
// Shift register
// CC1 12 CLOCK
// CC2 13 DATA
// SBU1 14 LATCH
// SBU2 15
// const int ShiftOutClockPin = 12;
// const int ShiftOutDataPin = 13;
// const int ShiftOutLatchPin = 14;
// byte ShiftOutByte=0x00;
bool rxShiftInRead;
// 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 HWREV==8
const int RainPin = 36;
#endif
#if HWREV==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 HWREV==8
#define ONE_WIRE_BUS 2
#endif
#if HWREV==7
#define ONE_WIRE_BUS 13
#endif
#define TEMPERATURE_PRECISION 9 // 9-12
// 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() {
pinMode(AzimuthPin, INPUT);
// pinMode(CwCcwButtPin, INPUT);
pinMode(CwInputPin, INPUT);
pinMode(CcwInputPin, INPUT);
pinMode(HWidPin, INPUT);
HWidValue = readADC_Cal(analogRead(HWidPin));
if(HWidValue<=150){
HardwareRev=0;
}else if(HWidValue>150 && HWidValue<=450){
HardwareRev=3; // 319
}else if(HWidValue>450 && HWidValue<=700){
HardwareRev=4; // 604
}else if(HWidValue>700 && HWidValue<=900){
HardwareRev=5; // 807
}else if(HWidValue>900 && HWidValue<=1170){
HardwareRev=6; // 1036
}else if(HWidValue>1170){
HardwareRev=7; // 1304
}
pinMode(VoltagePin, INPUT);
pinMode(ReversePin, OUTPUT);
digitalWrite(ReversePin, LOW);
ledcSetup(mosfetPWMChannel, PwmFreq, PwmResolution);
ledcSetup(greenPWMChannel, PwmFreq, PwmResolution);
ledcAttachPin(PwmPin, mosfetPWMChannel);
ledcAttachPin(LedGPin, greenPWMChannel);
ledcWrite(mosfetPWMChannel, 0);
ledcWrite(greenPWMChannel, 0);
// HardwareRev 1
pinMode(AZmasterPin, INPUT);
pinMode(ACcwPin, OUTPUT);
digitalWrite(ACcwPin, LOW);
pinMode(BrakePin, OUTPUT);
digitalWrite(BrakePin, LOW);
pinMode(LedRPin, OUTPUT);
digitalWrite(LedRPin, LOW);
// pinMode(LedGPin, OUTPUT);
// digitalWrite(LedGPin, HIGH);
pinMode(LedBPin, OUTPUT);
digitalWrite(LedBPin, LOW);
pinMode(AZtwoWirePin, OUTPUT);
digitalWrite(AZtwoWirePin, LOW);
pinMode(AZpreampPin, OUTPUT);
digitalWrite(AZpreampPin, LOW);
// pinMode(ShiftOutClockPin, OUTPUT);
// pinMode(ShiftOutDataPin, OUTPUT);
// pinMode(ShiftOutLatchPin, OUTPUT);
// digitalWrite(ShiftOutLatchPin, HIGH);
// for (int i = 0; i < 8; i++) {
// pinMode(TestPin[i], INPUT);
// }
#if HWREV==8
pinMode(RainPin, INPUT);
#endif
#if HWREV==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);
// pinMode(ShiftInClockPin, OUTPUT);
// pinMode(ShiftInDataPin, INPUT);
Serial.begin(115200); //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-1 net ID
if(EEPROM.read(0)==0xff){
NET_ID="0";
}else{
for (int i=0; i<2; i++){
if(EEPROM.read(i)!=0xff){
NET_ID=NET_ID+char(EEPROM.read(i));
}
}
}
// 2-22 RotName
if(EEPROM.read(2)==0xff){
RotName="Antenna";
}else{
for (int i=2; i<23; i++){
if(EEPROM.read(i)!=0xff){
RotName=RotName+char(EEPROM.read(i));
}
}
}
// 23 StartAzimuth
if(EEPROM.read(23)==0xff){
StartAzimuth=0;
}else{
if(EEPROM.readUShort(23)<359 && EEPROM.readUShort(23)>0){
StartAzimuth = EEPROM.readUShort(23);
}
}
// 25 MaxRotateDegree
if(EEPROM.read(25)==0xff){
MaxRotateDegree=360;
}else{
MaxRotateDegree = EEPROM.readUShort(25);
}
// 27 AntRadiationAngle
if(EEPROM.read(27)==0xff){
AntRadiationAngle=45;
}else{
if(EEPROM.readUShort(27)<180 && EEPROM.readUShort(27)>0){
AntRadiationAngle = EEPROM.readUShort(27);
}else{
AntRadiationAngle=46;
}
}
// 169-219 - MapUrl
if(EEPROM.read(169)==0xff){
MapUrl="https://remoteqth.com/xplanet/OK.png";
}else{
for (int i=169; i<220; i++){
if(EEPROM.read(i)!=0xff){
MapUrl=MapUrl+char(EEPROM.read(i));
}
}
}
// 29 - Endstop
if(EEPROM.read(29)==0xff){
Endstop=false;
}else{
if(EEPROM.readBool(29)==1){
Endstop=true;
}else{
Endstop=false;
}
}
// 30 - ACmotor
if(EEPROM.read(30)==0xff){
ACmotor=false;
}else{
if(EEPROM.readBool(30)==1){
ACmotor=true;
}else{
ACmotor=false;
}
}
// 31-32 CcwRawBaudRate
if(EEPROM.read(31)==0xff){
CcwRaw=392;
}else{
CcwRaw = EEPROM.readUShort(31);
}
// 33-34 CwRaw
if(EEPROM.read(33)==0xff){
CwRaw=3000;
}else{
CwRaw = EEPROM.readUShort(33);
}
// 35 - Reverse
if(EEPROM.read(35)==0xff){
Reverse=false;
}else{
if(EEPROM.readBool(35)==1){
Reverse=true;
}else{
Reverse=false;
}
}
// 36 - NoEndstopLowZone
if(EEPROM.read(36)==0xff){
NoEndstopLowZone = 0.5;
}else{
if(EEPROM.readByte(36)<16){
NoEndstopLowZone = float(EEPROM.readByte(36))/10;
}else{
NoEndstopLowZone = 0.5;
}
}
// NoEndstopHighZone = 3.3 - NoEndstopLowZone;
// NoEndstopLowZone = NoEndstopLowZone;
// 222 - NoEndstopHighZone
if(EEPROM.read(222)==0xff){
NoEndstopHighZone = 2.8;
}else{
if(EEPROM.readByte(222)>15 && EEPROM.readByte(222)<34){
NoEndstopHighZone = float(EEPROM.readByte(222))/10;
}else{
NoEndstopHighZone = 2.8;
}
}
// NoEndstopHighZone = 3.3 - NoEndstopLowZone;
// NoEndstopLowZone = NoEndstopLowZone;
// 223 - AZsource
if(EEPROM.read(223)==0xff){
AZsource=false;
}else{
if(EEPROM.readBool(223)==1){
AZsource=true;
}else{
AZsource=false;
}
}
// 224-225 PulsePerDegree
if(EEPROM.read(224)==0xff){
PulsePerDegree=1;
}else{
PulsePerDegree = EEPROM.readUShort(224);
}
// 226-227 BaudRate
if(EEPROM.read(226)==0xff || EEPROM.readUShort(226) > 9600){
BaudRate=115200;
}else{
BaudRate = EEPROM.readUShort(226);
}
// 228 AZtwoWire
if(EEPROM.read(228)==0xff){
AZtwoWire=false;
}else{
if(EEPROM.readBool(228)==1){
AZtwoWire=true;
}else{
AZtwoWire=false;
}
}
digitalWrite(AZtwoWirePin, AZtwoWire);
// 229 AZpreamp
if(EEPROM.read(229)==0xff){
AZpreamp=false;
}else{
if(EEPROM.readBool(229)==1){
AZpreamp=true;
}else{
AZpreamp=false;
}
}
digitalWrite(AZpreampPin, AZpreamp);
// 230 - ReverseAZ