-
Notifications
You must be signed in to change notification settings - Fork 1
/
Ready Touch example.ino
1608 lines (1389 loc) · 45.5 KB
/
Ready Touch example.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
/*
Name: Factory ready touch.ino
Created: 11/28/2024 5:53:03 AM
Author: WeGoWireless.com
Thank you for supporting my products!
A simple example for WeGoWireless.com Ready Touch 2.8" tft
Adafruit:
"Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing products from Adafruit!"
Use Adafruit Feather ESP32-S3 2MB via board manager or other compatible board.
Download install the below libraries
*/
#include <Arduino.h>
// Voltage regulator power to real time clock DS3231, SD card slot, LCD backlight, DSB18B20, optional SCD41, One Wire DS241
const uint8_t PROGMEM LDO2_EN = 4;
// Reset pin to TFT
const uint8_t PROGMEM RESET = 9;
// TFT 2.8"
const int8_t VSPI_MISO = 37;
const int8_t VSPI_MOSI = 35;
const int8_t VSPI_SCLK = 36;
const int8_t VSPI_TFT_CS = 39;
const int8_t VSPI_TFT_DC = 38;
const int8_t VSPI_RST = -1;
const int8_t VSPI_TOUCH_CS = 40;
const uint8_t PROGMEM TFT_LED = 41;
// i2c
const uint8_t PROGMEM I2C_SDA_0 = 2;
const uint8_t PROGMEM I2C_SCL_0 = 1;
const uint32_t PROGMEM I2C_FREQUENCY = 100000; //standard mode 100k, fast mode 400k
// SD
const uint8_t PROGMEM SD_CS = 21; // SD CS
const uint8_t PROGMEM SD_CD = 44; // card detect
// Piezo
const uint8_t PROGMEM PIEZO_PIN = 14;
// 5v input digital or analog reading resistor divider
const uint8_t PROGMEM SENSE_VBUS_PIN = 15;
// INPUTS resistor divider for analog EOL (End of line security system to detect short, open, closed)
// or simple digital reading
const uint8_t PROGMEM IN0 = 5;
const uint8_t PROGMEM IN1 = 6;
// Button inputs
const uint8_t PROGMEM BTN1 = 47;
const uint8_t PROGMEM BTN2 = 48;
const uint8_t PROGMEM BTN3 = 13;
const uint8_t PROGMEM BTN4 = 12;
// RELAY latching set for min 40 ms hold for on or off
const uint8_t PROGMEM REL1_SET_PIN = 10;
const uint8_t PROGMEM REL1_RESET_PIN = 11;
const uint8_t PROGMEM REL2_SET_PIN = 3;
const uint8_t PROGMEM REL2_RESET_PIN = 8;
const uint8_t PROGMEM REL3_SET_PIN = 16;
const uint8_t PROGMEM REL3_RESET_PIN = 46;
char appVersion[80];
#include <Wire.h> // ESP32 lib
#include <WiFi.h> // ESP32 lib
#include <ESPmDNS.h> // ESP32 lib
#include <WiFiClient.h> // ESP32 lib
#include <WebServer.h> // ESP32 lib
#include <DNSServer.h> // ESP32 lib
#include <ArduinoOTA.h> // ESP32 lib
WebServer server(80);
DNSServer dnsServer; // Create class DNS server, captive portal re-direct
const char* softAP_ssid = "Ready Touch 2.8";
const char* softAP_password = "";
IPAddress apIP(192, 168, 4, 1);
IPAddress apNetMsk(255, 255, 255, 0);
char sta_ssid[31];
char sta_pass[31];
// TFT
#include <Adafruit_ILI9341.h> // https://github.com/adafruit/Adafruit_ILI9341
Adafruit_ILI9341 display_gfx = Adafruit_ILI9341(VSPI_TFT_CS, VSPI_TFT_DC);
#include <Adafruit_GFX.h> // https://github.com/adafruit/Adafruit-GFX-Library
#include "SansSerif_plain_10.h"
#include "SansSerif_plain_16.h"
#include "SansSerif_plain_numbers_90.h"
#include "html.h"
#include "images/wifi_signal_1.svg.gz.h"
#include "images/wifi_signal_2.svg.gz.h"
#include "images/wifi_signal_3.svg.gz.h"
#include "images/wifi_signal_4.svg.gz.h"
#include "images/wifi_signal_5.svg.gz.h"
// Touch screen
#include <XPT2046_Touchscreen.h> // by Paul Stoffregenesp
XPT2046_Touchscreen ts(VSPI_TOUCH_CS);
uint32_t lastTouched = 0;
const uint16_t TS_MINX = 370;
const uint16_t TS_MINY = 470;
const uint16_t TS_MAXX = 3700;
const uint16_t TS_MAXY = 3600;
#include <SD.h> // ESP32 lib
#include <SPI.h> // ESP32 lib
#include <Preferences.h> // ESP32 lib
Preferences Prefs;
// One wire DS248
#include <Adafruit_DS248x.h>
Adafruit_DS248x ds248x;
float tempF;
uint8_t rom[8];
#define DS18B20_FAMILY_CODE 0x28
#define DS18B20_CMD_CONVERT_T 0x44
#define DS18B20_CMD_MATCH_ROM 0x55
#define DS18B20_CMD_READ_SCRATCHPAD 0xBE
#include <RTClib.h> // https://github.com/adafruit/RTClib
RTC_DS3231 rtc;
#include <Adafruit_MAX1704X.h> //download install. Lib manager doesn't report latest https://github.com/adafruit/Adafruit_MAX1704X/tree/main
Adafruit_MAX17048 maxlipo;
bool SD_inserted = false;
bool FoundMax = false;
char wifiScanBuff[33];
bool wifiScanPrint = false;
char scanJson[1565] = "["; // wifi scan for web page 1565 plus /0 = 20 networks if all had 32 char ssid, this is not going to happen.
bool mode_ETH = false;
bool mode_AP = false;
bool mode_STA = false;
float TxPower = 0;
uint8_t sta_authmode = 0;
bool piezo_busy = false;
uint32_t piezo_timer;
const long PIEZO_DURATION = 100;
GFXcanvas1 canvasTime(100, 12);
GFXcanvas16 canvasButton(60, 20);
GFXcanvas16 canvasIn(60, 20);
GFXcanvas16 canvasAnalog(60, 20);
GFXcanvas16 canvasOutput(104, 20);
GFXcanvas16 canvasBattery(85, 22);
GFXcanvas16 canvasRelayButton(70, 25);
GFXcanvas1 canvasTemperature(112, 70);
GFXcanvas1 canvasSD(100, 24);
GFXcanvas1 canvasWiFi(160, 170);
GFXcanvas1 canvasWiFiQuality(50, 12);
struct tft_struct
{
uint8_t tm_sec;
boolean drawTime = false;
uint8_t input_last[8];
float input_voltage_last[2];
float lastBattV = 0;
float Temperature = 0;
int8_t drawSD = 0; // 0=not drawn, 1 mounted, 2 not mounted
int8_t drawQuality = 255;
uint16_t btnRelay1X = 10, btnRelay1Y = 210, btnRelay1W = 50, btnRelay1H = 50;
uint16_t btnRelay2X = 100, btnRelay2Y = 210, btnRelay2W = 50, btnRelay2H = 50;
uint16_t btnRelay3X = 190, btnRelay3Y = 210, btnRelay3W = 50, btnRelay3H = 50;
};
struct tft_struct tftSt;
uint8_t screenRotation = 3; // 3 = landscape sd up
struct power_struct
{
float CellVoltage = 0;
float CellPercent = 0;
float ChargeRate = 0;
};
struct power_struct power_st;
// RELAY
const uint8_t PROGMEM ON = 1;
const uint8_t PROGMEM OFF = 0;
struct relay_struct
{
uint8_t state = OFF;
uint8_t lastState = OFF;
};
struct relay_struct io_relay_struct[3];
uint8_t out_relayGPIOs[6] = { REL1_SET_PIN, REL1_RESET_PIN, REL2_SET_PIN, REL2_RESET_PIN, REL3_SET_PIN, REL3_RESET_PIN }; // Assign each GPIO to a relay
struct wifi_struct
{
char ssid[33];
char rssi[33];
};
struct wifi_struct wifi_st;
// INPUT
const uint8_t PROGMEM IN_DIGITAL = 1;
const uint8_t PROGMEM IN_ANALOG = 2;
// INPUT
const uint8_t PROGMEM IN_UNKNOWN = 0;
const uint8_t PROGMEM IN_CLOSED = 1;
const uint8_t PROGMEM IN_NORMAL = 3;
const uint8_t PROGMEM IN_OPEN = 6;
const uint8_t PROGMEM IN_VIOLATED = 11;
const uint8_t PROGMEM IN_FAULTY = 12;
const uint8_t PROGMEM IN_SHORT = 13;
const uint8_t PROGMEM IN_POWER_LOSS = 14;
const uint32_t TOUCH_UPDATE = 200; // ms, dont set too high as 1000 will not trigger touchscreen
uint32_t Timer_touch;
// INPUT
const long IO_INPUT_UPDATE = 1 * 1000;
uint32_t TimerInput;
const long IO_BUTTON_UPDATE = 400;
uint32_t TimerButton;
const uint32_t WIFI_UPDATE = 5 * 1000;
uint32_t Timer_wifi;
const uint32_t TFT_UPDATE = 1000;
uint32_t Timer_tft;
uint16_t BATTERY_UPDATE = 60 * 1000;
int32_t Timer_battery = millis() - BATTERY_UPDATE;
const uint32_t TEMPERATURE_UPDATE = 60 * 1000;
int32_t TimerTemperature;
const uint32_t START_CONVERSION_UPDATE = 50 * 1000;
uint32_t TimerStartConversion;
void setupPreferences();
void handleRoot();
void handleScan();
void handleSaveNetwork();
void startAP();
void startClient();
void startServer();
void setupOTA();
void scanWiFi(uint16_t x, uint16_t y);
void setupRTC();
void setupMAX();
void setupDS248X();
void SearchDS2484();
float readTemperature(uint8_t* rom);
void Strat_Conversion(uint8_t* rom);
void setupTFT();
void setTouchRotation(uint8_t n);
void drawWiFi(uint16_t x, uint16_t y);
void drawWifiQuality(uint16_t x, uint16_t y);
void drawBattery(uint16_t x, uint16_t y, bool initDraw = false);
void drawTemperatureDetail(uint16_t x, uint16_t y, bool initDraw = false);
void drawSD(uint16_t x, uint16_t y);
void detectButtons(int16_t x, int16_t y);
void drawRedBtn(uint16_t x, uint16_t y);
void drawGreenBtn(uint16_t x, uint16_t y);
void relaySetup();
void setup()
{
Serial.begin(115200);
// Turn on LDO2
pinMode(LDO2_EN, OUTPUT);
digitalWrite(LDO2_EN, HIGH); // startup time 20us no load as per AP2112 spec
delay(50); // Wait for power up
// Set TFT reset pin
pinMode(RESET, OUTPUT);
digitalWrite(RESET, HIGH);
// Set button inputs
pinMode(BTN1, INPUT);
pinMode(BTN2, INPUT);
pinMode(BTN3, INPUT);
pinMode(BTN4, INPUT);
// configure buzzer pins
pinMode(PIEZO_PIN, OUTPUT); // set as outputs
digitalWrite(PIEZO_PIN, 0); // clamp to ground
pinMode(SD_CD, INPUT); // Card Detect
pinMode(TFT_LED, OUTPUT);
// Wire setup
Wire.setPins(I2C_SDA_0, I2C_SCL_0);
get_build_date_time();
setupPreferences();
readPreferences();
startAP();
startClient();
startServer();
setupOTA();
setupRTC();
setupMAX();
setupDS248X();
SearchDS2484();
Strat_Conversion(rom);
TimerTemperature = millis() - TEMPERATURE_UPDATE + 1000; // read temperature 1 second giving time for conversion
setupTFT();
drawBattery(18, 0, true);
drawTemperatureDetail(75, 50, true);
drawSD(220, 0);
drawGreenBtn(tftSt.btnRelay1X, tftSt.btnRelay1Y);
drawGreenBtn(tftSt.btnRelay2X, tftSt.btnRelay2Y);
drawGreenBtn(tftSt.btnRelay3X, tftSt.btnRelay3Y);
relaySetup();
}
void setupPreferences() {
Prefs.begin("Ready_Touch", false); // Open our namespace (or create it if it doesn't exist) in RO mode.
bool tpInit = Prefs.isKey("nvsInit"); // Test for the existence of the "already initialized" key.
if (tpInit == false) { // If tpInit is 'false', the key "nvsInit" does not yet exist therefore this
// must be our first-time run. We need to set up our Preferences namespace keys. So...
Prefs.end();
Prefs.begin("Ready_Touch", false); // reopen it in RW mode.
Prefs.putBool("nvsInit", true); // Create the "already initialized" key and store a value.
Prefs.end(); // Close the namespace in RW mode and...
Prefs.begin("Ready_Touch", true);
}
}
void readPreferences() {
Prefs.getString("sta_ssid", sta_ssid, 30);
Prefs.getString("sta_pass", sta_pass, 30);
}
void writePreferences() {
Prefs.putString("sta_ssid", sta_ssid);
Prefs.putString("sta_pass", sta_pass);
Prefs.end();
readPreferences();
}
void get_build_date_time() {
char s_month[5];
const char date[] = __DATE__;
const char time[] = __TIME__;
int month, day, year, hour, min, sec;
static const char month_names[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
sscanf(date, "%s %d %d", s_month, &day, &year);
sscanf(time, "%d:%d:%d", &hour, &min, &sec);
// add one to month
month = (strstr(month_names, s_month) - month_names) / 3 + 1;
sprintf(appVersion, "%02d/%02d/%04d %02d:%02d:%02d", month, day, year, hour, min, sec);
}
//static void WiFiStationConnected(WiFiEvent_t event, wifi_sta_connected info) {
// sta_authmode = info.authmode;
//}
void startClient() {
if (sizeof(sta_ssid[0]) != 0) {
Serial.println("start client");
WiFi.disconnect(true);
WiFi.hostname("Ready Touch 2.8");
WiFi.enableSTA(true);
WiFi.begin(sta_ssid, sta_pass);
mode_STA = true;
//WiFi.onEvent(WiFiStationConnected, WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_CONNECTED);
}
}
// -------------------------------------------------------------------
// Start Access Point
// Access point is used for wifi network selection
// -------------------------------------------------------------------
void startAP() {
//WiFi.mode(WIFI_AP);
WiFi.enableAP(true);
WiFi.softAPConfig(apIP, apIP, apNetMsk);
WiFi.softAP(softAP_ssid, softAP_password, 1, 0, 4);
mode_AP = true;
// display on webpage
switch (WiFi.getTxPower()) {
case WIFI_POWER_19_5dBm:
TxPower = 19.5;
break;
case WIFI_POWER_19dBm:
TxPower = 19;
break;
case WIFI_POWER_18_5dBm:
TxPower = 18.5;
break;
case WIFI_POWER_17dBm:
TxPower = 17;
break;
case WIFI_POWER_15dBm:
TxPower = 15;
break;
case WIFI_POWER_13dBm:
TxPower = 13;
break;
case WIFI_POWER_11dBm:
TxPower = 11;
break;
case WIFI_POWER_8_5dBm:
TxPower = 8.5;
break;
case WIFI_POWER_7dBm:
TxPower = 7;
break;
case WIFI_POWER_5dBm:
TxPower = 5;
break;
case WIFI_POWER_2dBm:
TxPower = 2;
break;
case WIFI_POWER_MINUS_1dBm:
TxPower = -4;
break;
}
}
void startServer() {
if (MDNS.begin("ReadyTouch")) {
MDNS.addService("http", "tcp", 80);
}
else {
Serial.println("Error setting up MDNS responder!");
}
// by default DNSServer is started serving any "*" domain name. It will reply
// AccessPoint's IP to all DNS request (this is required for Captive Portal detection)
if (dnsServer.start(53, "*", apIP)) {
Serial.println("Started DNS server");
}
else {
Serial.println("Err: Can't start DNS server!");
}
server.enableCORS(true);
server.enableCrossOrigin(true);
server.on("/", HTTP_GET, handleRoot);
server.on("/status", HTTP_GET, handleStatus);
server.on("/scan", HTTP_GET, handleScan);
server.on("/savenetwork", handleSaveNetwork);
server.onNotFound(handleNotFound);
server.begin();
}
// -------------------------------------------------------------------
// Returns web root
// url: /
// -------------------------------------------------------------------
void handleRoot() {
server.send_P(200, "text/html", home);
}
// -------------------------------------------------------------------
// Returns scan json
// url: /scan
// -------------------------------------------------------------------
void handleScan() {
server.send(200, "text/json", scanJson);
}
void returnFail(String msg) {
server.send(500, "text/plain", msg + "\r\n");
}
void returnOK() {
server.send(200, "text/plain", "");
}
void handleSaveNetwork() {
char buff[31];
if (server.args() == 0) {
return returnFail("BAD ARGS");
}
if (server.hasArg("ssid")) {
server.arg("ssid").toCharArray(buff, sizeof(server.arg("ssid"))+1);
strcpy(sta_ssid, buff);
}
if (server.hasArg("pass")) {
server.arg("pass").toCharArray(buff, sizeof(server.arg("pass"))+1);
strcpy(sta_pass, buff);
}
returnOK();
writePreferences();
startClient();
}
void setupOTA() {
ArduinoOTA
.onStart([]() {
String type;
if (ArduinoOTA.getCommand() == U_FLASH) {
type = "sketch";
}
Serial.println("Start updating " + type);
})
.onEnd([]() {
Serial.println("\nEnd");
})
.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");
}
});
ArduinoOTA.setHostname(softAP_ssid);
ArduinoOTA.begin();
}
// -------------------------------------------------------------------
// Returns status json
// url: /status
// -------------------------------------------------------------------
void handleStatus() {
// create our own json documnet
char buff[33]; // max size to hold ssid 32 + /0
char json[500] = "{";
strcat(json, "\"appVersion\":\"");
strcat(json, appVersion);
strcat(json, "\"");
strcat(json, ",\"sta_ssid\":\"");
strcat(json, sta_ssid);
strcat(json, "\"");
strcat(json, ",\"sta_pass\":\"");
strcat(json, sta_pass);
strcat(json, "\"");
strcat(json, ",\"in_0\":");
sprintf(buff, "%.1f", get_input_Analog(IN0));
strcat(json, buff);
strcat(json, ",\"in_1\":");
sprintf(buff, "%.1f", get_input_Analog(IN1));
strcat(json, buff);
strcat(json, ",\"vbus\":");
sprintf(buff, "%.1f", get_input_Analog(SENSE_VBUS_PIN));
strcat(json, buff);
if (mode_ETH) {
strcat(json, ",\"mode\":\"ETH\"");
}
else if (mode_STA && !mode_AP) {
strcat(json, ",\"mode\":\"STA\"");
}
else if (mode_AP && !mode_STA) {
strcat(json, ",\"mode\":\"AP\"");
}
else if (mode_AP && mode_STA) {
strcat(json, ",\"mode\":\"STA+AP\"");
}
else {
strcat(json, ",\"mode\":\"\"");
}
strcat(json, ",\"srssi\":");
sprintf(buff, "%d", WiFi.RSSI());
strcat(json, buff);
strcat(json, ",\"ssid\":\"");
WiFi.SSID().toCharArray(buff, WiFi.SSID().length() + 1);
strcat(json, buff);
strcat(json, "\",\"chan\":");
sprintf(buff, "%d", WiFi.channel());
strcat(json, buff);
strcat(json, ",\"bssid\":\"");
sprintf(buff, "%02X:%02X:%02X:%02X:%02X:%02X", WiFi.BSSID()[0], WiFi.BSSID()[1], WiFi.BSSID()[2], WiFi.BSSID()[3], WiFi.BSSID()[4], WiFi.BSSID()[5]);
strcat(json, buff);
strcat(json, "\",\"txpwr\":");
sprintf(buff, "%d", TxPower);
strcat(json, buff);
//strcat(json, ",\"enc\":");
//sprintf(buff, "%s", sta_authmode);
//strcat(json, buff);
strcat(json, ",\"ip\":\"");
sprintf(buff, "%d.%d.%d.%d", WiFi.localIP()[0], WiFi.localIP()[1], WiFi.localIP()[2], WiFi.localIP()[3]);
strcat(json, buff);
strcat(json, "\",\"gw\":\"");
sprintf(buff, "%d.%d.%d.%d", WiFi.gatewayIP()[0], WiFi.gatewayIP()[1], WiFi.gatewayIP()[2], WiFi.gatewayIP()[3]);
strcat(json, buff);
strcat(json, "\",\"dns\":\"");
sprintf(buff, "%d.%d.%d.%d", WiFi.dnsIP()[0], WiFi.dnsIP()[1], WiFi.dnsIP()[2], WiFi.dnsIP()[3]);
strcat(json, buff);
strcat(json, "\"");
strcat(json, "}");
server.send(200, "text/json", json);
}
void handleNotFound() {
char buff[50];
char message[200] = "Not found\n\nURI: ";
server.uri().toCharArray(buff, server.uri().length() + 1);
strcat(message, buff);
strcat(message, "\nMethod: ");
strcat(message, (server.method() == HTTP_GET) ? "GET" : "POST");
strcat(message, "\nArguments: ");
sprintf(buff, "%d", server.args());
strcat(message, buff);
strcat(message, "\n");
for (uint8_t i = 0; i < server.args(); i++) {
server.argName(i).toCharArray(buff, server.argName(i).length() + 1);
strcat(message, buff);
strcat(message, ": ");
server.arg(i).toCharArray(buff, server.arg(i).length() + 1);
strcat(message, buff);
strcat(message, "\n");
}
server.send(404, "text/plain", message);
}
void setTouchRotation(uint8_t n) {
ts.setRotation(n); // set ts rotation
}
// Make size of files human readable
// source: https://github.com/CelliesProjects/minimalUploadAuthESP32
String file_humanReadableSize(const size_t bytes) {
if (bytes < 1024) return String(bytes) + " B";
else if (bytes < (1024 * 1024)) return String(bytes / 1024.0) + " KB";
else if (bytes < (1024 * 1024 * 1024)) return String(bytes / 1024.0 / 1024.0) + " MB";
else return String(bytes / 1024.0 / 1024.0 / 1024.0) + " GB";
}
void piezo_click() {
if (!piezo_busy) {
piezo_busy = true;
tone(PIEZO_PIN, 4000, PIEZO_DURATION);
piezo_timer = millis();
}
}
// run WiFi scan in non blocking async mode. Check for complete and return results.
void scanWiFi(uint16_t x, uint16_t y) {
char buff[33];
uint8_t canvasLine;
int16_t n;
n = WiFi.scanComplete();
if (n == 0) {
Serial.println("WiFi no networks found");
}
else if (n == WIFI_SCAN_RUNNING) {
if (wifiScanPrint) {
Serial.println("WiFi scan running");
}
}
else if (n == WIFI_SCAN_FAILED) {
Serial.println("WiFi scan failed");
// Delete the scan result to free memory for code below.
WiFi.scanDelete();
// WiFi.scanNetworks start new scan
n = WiFi.scanNetworks(true);
if (n == WIFI_SCAN_RUNNING) {
if (wifiScanPrint) {
Serial.println("WiFi start scan running");
}
}
else if (n == WIFI_SCAN_FAILED) {
Serial.println("WiFi scan start failed");
}
}
else if (n > 0) {
canvasWiFi.fillScreen(ILI9341_BLACK);
if (wifiScanPrint) {
Serial.println("Scan done");
Serial.print(n);
Serial.println(" networks found");
Serial.println("Nr | SSID | RSSI | CH | Encryption");
}
// reinet array
//char scanJson[1565] = "[";
scanJson[0] = '[';
scanJson[1] = (char)0;
canvasLine = 0;
for (int i = 0; i < n; ++i) {
// create scanJson doc for web page wifi scan
//char scanJson[1565] = "[";
if (WiFi.RSSI(i) >= -80) { // only add if dbm strong enough
if (sizeof(scanJson) - strlen(scanJson) >= 52) { // make sure we have room to add to scanJson array
if (i) strcat(scanJson, ",");
strcat(scanJson, "{\"rssi\":");
sprintf(buff, "%d", WiFi.RSSI(i));
strcat(scanJson, buff);
strcat(scanJson, ",\"ssid\":\"");
WiFi.SSID(i).toCharArray(buff, WiFi.SSID(i).length() + 1);
strcat(scanJson, buff);
strcat(scanJson, "\"}");
}
}
// Print SSID and RSSI for each network found
if (wifiScanPrint) {
Serial.printf("%2d", i + 1);
Serial.print(" | ");
Serial.printf("%-32.32s", WiFi.SSID(i).c_str());
Serial.print(" | ");
Serial.printf("%4ld", WiFi.RSSI(i));
Serial.print(" | ");
Serial.printf("%2ld", WiFi.channel(i));
Serial.print(" | ");
switch (WiFi.encryptionType(i)) {
case WIFI_AUTH_OPEN: Serial.print("open"); break;
case WIFI_AUTH_WEP: Serial.print("WEP"); break;
case WIFI_AUTH_WPA_PSK: Serial.print("WPA"); break;
case WIFI_AUTH_WPA2_PSK: Serial.print("WPA2"); break;
case WIFI_AUTH_WPA_WPA2_PSK: Serial.print("WPA+WPA2"); break;
case WIFI_AUTH_WPA2_ENTERPRISE: Serial.print("WPA2-EAP"); break;
case WIFI_AUTH_WPA3_PSK: Serial.print("WPA3"); break;
case WIFI_AUTH_WPA2_WPA3_PSK: Serial.print("WPA2+WPA3"); break;
case WIFI_AUTH_WAPI_PSK: Serial.print("WAPI"); break;
default: Serial.print("unknown");
}
Serial.println();
}
canvasWiFi.setFont(&SansSerif_plain_10);
sprintf(buff, "%d", WiFi.RSSI(i));
canvasWiFi.setCursor(0, canvasLine * 12);
canvasWiFi.print(buff);
canvasWiFi.setFont(&SansSerif_plain_10);
WiFi.SSID(i).toCharArray(buff, WiFi.SSID(i).length() + 1);
canvasWiFi.setCursor(30, canvasLine * 12);
canvasWiFi.print(buff);
canvasLine += 1;
}
strcat(scanJson, "]");
display_gfx.drawBitmap(x, y, canvasWiFi.getBuffer(), 160, 170, ILI9341_WHITE, ILI9341_BLACK);
if (wifiScanPrint) {
Serial.println("");
}
// Delete the scan result to free memory.
WiFi.scanDelete();
// WiFi.scanNetworks start new scan
n = WiFi.scanNetworks(true);
if (n == WIFI_SCAN_RUNNING) {
if (wifiScanPrint) {
Serial.println("WiFi start scan");
}
}
else if (n == WIFI_SCAN_FAILED) {
Serial.println("WiFi scan start failed");
}
}
}
// converts the dBm to a range between 0 and 100%
int8_t getWifiQuality() {
int32_t dbm = WiFi.RSSI();
if (dbm <= -100) {
return 0;
}
else if (dbm >= -50) {
return 100;
}
else {
return 2 * (dbm + 100);
}
}
void drawWifiQuality(uint16_t x, uint16_t y) {
int8_t quality = getWifiQuality();
if (tftSt.drawQuality != quality) {
tftSt.drawQuality = quality;
canvasWiFiQuality.fillScreen(ILI9341_BLACK);
//canvasWiFiQuality.drawRect(0, 0, 48, 10, ILI9341_WHITE); // just for testing the size of our area
for (int8_t i = 0; i < 4; i++) {
for (int8_t j = 0; j < 2 * (i + 1); j++) {
if (quality > i * 25 || j == 0) {
canvasWiFiQuality.drawPixel(39 + 2 * i, 10 - j, ILI9341_WHITE);
}
}
}
canvasWiFiQuality.setFont(&SansSerif_plain_10);
canvasWiFiQuality.setTextColor(ILI9341_WHITE, ILI9341_BLACK);
canvasWiFiQuality.setCursor(0, 12);
canvasWiFiQuality.print(String(quality) + "%");
// 48 x 11 orginal exact fit
// 1x 10font 7h
display_gfx.drawBitmap(x, y, canvasWiFiQuality.getBuffer(), 50, 12, ILI9341_WHITE, ILI9341_BLACK);
}
}
void drawWiFi(uint16_t x, uint16_t y) {
//canvasWiFi.fillScreen(ILI9341_BLACK);
//display_gfx.drawBitmap(x, y, canvasWiFi.getBuffer(), 100, 100, ILI9341_WHITE, ILI9341_BLACK);
}
void drawSD(uint16_t x, uint16_t y) {
if (SD_inserted && tftSt.drawSD == 0 || SD_inserted && tftSt.drawSD == 2) {
canvasSD.fillScreen(ILI9341_BLACK);
canvasSD.drawFastHLine(3, 0, 10, ILI9341_WHITE); // top
canvasSD.drawLine(3, 0, 0, 3, ILI9341_WHITE); // left top short angle
canvasSD.drawFastVLine(0, 3, 7, ILI9341_WHITE); // left short
canvasSD.drawFastVLine(10, 0, 10, ILI9341_WHITE); // right
canvasSD.drawFastHLine(0, 10, 10, ILI9341_WHITE); // bottom
canvasSD.drawFastVLine(4, 1, 3, ILI9341_WHITE); // 3 pins top
canvasSD.drawFastVLine(6, 2, 3, ILI9341_WHITE); // 3 pins top
canvasSD.drawFastVLine(8, 2, 3, ILI9341_WHITE); // 3 pins top
canvasSD.setFont(&SansSerif_plain_10);
canvasSD.setTextColor(ILI9341_WHITE, ILI9341_BLACK);
canvasSD.setCursor(0, 22);
canvasSD.print(file_humanReadableSize(SD.cardSize()));
display_gfx.drawBitmap(x, y, canvasSD.getBuffer(), 100, 24, ILI9341_WHITE, ILI9341_BLACK);
tftSt.drawSD = 1;
}
else if (SD_inserted == false && tftSt.drawSD == 1 || SD_inserted == false && tftSt.drawSD == 0) {
canvasSD.fillScreen(ILI9341_BLACK);
canvasSD.drawFastHLine(3, 0, 10, ILI9341_RED); // top
canvasSD.drawLine(3, 0, 0, 3, ILI9341_RED); // left top short angle
canvasSD.drawFastVLine(0, 3, 7, ILI9341_RED); // left short
canvasSD.drawFastVLine(10, 0, 10, ILI9341_RED); // right
canvasSD.drawFastHLine(0, 10, 10, ILI9341_RED); // bottom
canvasSD.drawFastVLine(4, 1, 3, ILI9341_RED); // 3 pins top
canvasSD.drawFastVLine(6, 2, 3, ILI9341_RED); // 3 pins top
canvasSD.drawFastVLine(8, 2, 3, ILI9341_RED); // 3 pins top
display_gfx.drawBitmap(x, y, canvasSD.getBuffer(), 100, 24, ILI9341_RED, ILI9341_BLACK);
tftSt.drawSD = 2;
}
}
void relaySetup() {
for (uint8_t i = 0; i < sizeof(out_relayGPIOs); i++) {
pinMode(out_relayGPIOs[i], OUTPUT);
digitalWrite(out_relayGPIOs[i], OFF);
}
}
void setupDS248X() {
uint8_t i = 0;
if (!ds248x.begin(&Wire, DS248X_ADDRESS)) {
Serial.println(F("DS248x initialization failed."));
}
else {
while (!ds248x.OneWireReset()) {
if (i > 10) {
Serial.println("Failed to do a 1W reset");
break;
}
if (ds248x.shortDetected()) {
Serial.println("Short detected");
}
if (!ds248x.presencePulseDetected()) {
Serial.println("No presense pulse");
}
Serial.print(F("."));
i++;
delay(500);
}
Serial.println("One Wire bus reset OK");
}
}
void SearchDS2484() {
if (!ds248x.OneWireSearch(rom)) {
Serial.println("No more devices found\n\n");
return;
}
Serial.print("Found device ROM: ");
for (int i = 0; i < 8; i++) {
if (rom[i] < 16) {
Serial.print("0");
}
Serial.print(rom[i], HEX);
Serial.print(" ");
}
Serial.println("");
}
/********************************************************************
* @brief draws the temperature readings
* @param thermostat thermostat number for temperature sensor reading.
* @param x Top left corner horizontal coordinate.
* @param y Top left corner vertical coordinate.
* @return None
*********************************************************************/
void drawTemperatureDetail(uint16_t x, uint16_t y, bool initDraw) {
// only draw if temperature changed by 1 up or down
if (tempF - tftSt.Temperature < 1 || tempF - tftSt.Temperature > 1 || initDraw) {
tftSt.Temperature = tempF;
char charBuf[5];
dtostrf(tempF, 2, 0, charBuf);
canvasTemperature.fillScreen(ILI9341_BLACK);
canvasTemperature.setFont(&SansSerif_plain_90);
canvasTemperature.setCursor(0, 68); // cutting off the bottom
canvasTemperature.print(charBuf);
display_gfx.drawBitmap(x, y, canvasTemperature.getBuffer(), 112, 70, ILI9341_WHITE, ILI9341_BLACK);
}
}
float readTemperature(uint8_t* rom) {
uint8_t i;
// Select the DS18B20 device
ds248x.OneWireReset();
ds248x.OneWireWriteByte(DS18B20_CMD_MATCH_ROM); // Match ROM command
for (i = 0; i < 8; i++) {
ds248x.OneWireWriteByte(rom[i]);
}
ds248x.OneWireWriteByte(DS18B20_CMD_READ_SCRATCHPAD); // Read Scratchpad command
// Read scratchpad
uint8_t data[9];
for (i = 0; i < 9; i++) {
ds248x.OneWireReadByte(&data[i]);
}
// Calculate temperature
//int16_t raw = (data[1] << 8) | data[0];
//float celsius = (float)raw / 16.0;
int16_t fpTemperature = (((int16_t)data[1]) << 11)
| (((int16_t)data[0]) << 3);
return (((float)fpTemperature * 0.0140625f) + 32.0f);
//return celsius;
}
void Strat_Conversion(uint8_t* rom) {
// Select the DS18B20 device
ds248x.OneWireReset();
ds248x.OneWireWriteByte(DS18B20_CMD_MATCH_ROM); // Match ROM command
for (uint8_t i = 0; i < 8; i++) {
ds248x.OneWireWriteByte(rom[i]);
}
// Start temperature conversion
ds248x.OneWireWriteByte(DS18B20_CMD_CONVERT_T); // Convert T command
}
void setupRTC() {
// I2C DS3231 clock
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
}
else {
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));