Repository navigation
Expand file tree
/
Copy pathWebConfig.cpp
More file actions
909 lines (832 loc) · 49.3 KB
/
Copy pathWebConfig.cpp
File metadata and controls
909 lines (832 loc) · 49.3 KB
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
// WebConfig.cpp — WabbitWeather settings web page (the settings globals live in All_Settings.h)
#include <WebServer.h>
#include <ESPmDNS.h>
#include <LittleFS.h>
#include <WiFi.h>
#include <WiFiManager.h>
#include "WebConfig.h"
#define SETTINGS_LANGUAGE_ONLY // language selection only — see All_Settings.h
#include "All_Settings.h"
#include "Translation.h"
// ── Web server instance ────────────────────────────────────────────────────
static WebServer server(80);
static String mdnsName = "wabbitweather";
// ── Forward declarations of globals from All_Settings.h ───────────────────
extern String units;
extern String latitude;
extern String longitude;
extern int32_t tzOffset;
extern String latitude2;
extern String longitude2;
extern int32_t tzOffset2;
extern bool tzOverride;
extern bool tzOverride2;
extern int32_t tzOverrideOffset;
extern int32_t tzOverrideOffset2;
extern String locationName;
extern String locationName2;
extern String theCityLocation;
extern String theCityLocation2;
extern bool show24Hour;
extern bool autoDimDusk;
extern bool showBarometric;
extern bool pressureSeaLevel;
extern bool showPrecipProbability;
extern bool showUVindex;
extern bool show6hrForecast;
extern bool forecastStartToday;
extern bool invertColours;
extern uint8_t iconSet;
extern int16_t highTempVal;
extern int16_t lowTempVal;
extern uint32_t highTempColour;
extern uint32_t midTempColour;
extern uint32_t lowTempColour;
extern uint8_t highPrecipProb;
extern uint8_t lowPrecipProb;
extern uint32_t highPOPColour;
extern uint32_t midPOPColour;
extern uint32_t lowPOPColour;
extern uint32_t labelColour;
extern uint32_t astrologyColour;
extern uint8_t blDusk;
extern volatile bool configUpdated;
extern uint8_t configChanges;
// ── In WabbitWeather.ino: fetch + cache a location's name (false = 1, true = 2) ──
extern bool lookupCityName(bool alt);
// ── fetchTZOffset declared in NTP_Time.h ──────────────────────────────────
extern int32_t fetchTZOffset(const String& lat, const String& lon, int32_t currentOffset);
// ===========================================================================
// Config file: /config.txt (key=value, one per line, # = comment)
// ===========================================================================
void loadConfig() {
if (!LittleFS.exists(CONFIG_FILE)) {
Serial.println("WebConfig: no config file, writing defaults");
saveConfig();
return;
}
fs::File f = LittleFS.open(CONFIG_FILE, "r");
if (!f) {
Serial.println("WebConfig: failed to open config");
return;
}
while (f.available()) {
String line = f.readStringUntil('\n');
line.trim();
if (line.startsWith("#") || line.length() == 0) continue;
int sep = line.indexOf('=');
if (sep < 0) continue;
String key = line.substring(0, sep);
key.trim();
String value = line.substring(sep + 1);
value.trim();
if (key == "units") units = value;
else if (key == "latitude") latitude = value;
else if (key == "longitude") longitude = value;
else if (key == "tzOffset") tzOffset = value.toInt();
else if (key == "latitude2") latitude2 = value;
else if (key == "longitude2") longitude2 = value;
else if (key == "tzOffset2") tzOffset2 = value.toInt();
else if (key == "tzOverride") tzOverride = (value == "true");
else if (key == "tzOverride2") tzOverride2 = (value == "true");
else if (key == "tzOverrideOffset") tzOverrideOffset = value.toInt();
else if (key == "tzOverrideOffset2") tzOverrideOffset2 = value.toInt();
else if (key == "locationName") locationName = value;
else if (key == "locationName2") locationName2 = value;
else if (key == "show24Hour") show24Hour = (value == "true");
else if (key == "autoDimDusk") autoDimDusk = (value == "true");
else if (key == "showBarometric") showBarometric = (value == "true");
else if (key == "pressureSeaLevel") pressureSeaLevel = (value == "true");
else if (key == "showPrecipProbability") showPrecipProbability = (value == "true");
else if (key == "showUVindex") showUVindex = (value == "true");
else if (key == "show6hrForecast") show6hrForecast = (value == "true");
else if (key == "forecastStartToday") forecastStartToday = (value == "true");
else if (key == "invertColours") invertColours = (value == "true");
else if (key == "iconSet") iconSet = value.toInt();
else if (key == "highTempVal") highTempVal = value.toInt();
else if (key == "lowTempVal") lowTempVal = value.toInt();
else if (key == "highTempColour") highTempColour = colourFromName(value);
else if (key == "midTempColour") midTempColour = colourFromName(value);
else if (key == "lowTempColour") lowTempColour = colourFromName(value);
else if (key == "highPrecipProb") highPrecipProb = value.toInt();
else if (key == "lowPrecipProb") lowPrecipProb = value.toInt();
else if (key == "highPOPColour") highPOPColour = colourFromName(value);
else if (key == "midPOPColour") midPOPColour = colourFromName(value);
else if (key == "lowPOPColour") lowPOPColour = colourFromName(value);
else if (key == "labelColour") labelColour = colourFromName(value);
else if (key == "astrologyColour") astrologyColour = colourFromName(value);
else if (key == "blDusk") blDusk = value.toInt();
}
f.close();
// Serial.println("WebConfig: loaded " CONFIG_FILE);
}
void saveConfig() {
fs::File f = LittleFS.open(CONFIG_FILE, "w");
if (!f) {
Serial.println("WebConfig: failed to save config");
return;
}
f.println("# WabbitWeather configuration — edit via the web page");
f.println("units=" + units);
f.println("latitude=" + latitude);
f.println("longitude=" + longitude);
f.println("tzOffset=" + String(tzOffset));
f.println("latitude2=" + latitude2);
f.println("longitude2=" + longitude2);
f.println("tzOffset2=" + String(tzOffset2));
f.println("tzOverride=" + String(tzOverride ? "true" : "false"));
f.println("tzOverride2=" + String(tzOverride2 ? "true" : "false"));
f.println("tzOverrideOffset=" + String(tzOverrideOffset));
f.println("tzOverrideOffset2=" + String(tzOverrideOffset2));
f.println("locationName=" + locationName);
f.println("locationName2=" + locationName2);
f.println("show24Hour=" + String(show24Hour ? "true" : "false"));
f.println("autoDimDusk=" + String(autoDimDusk ? "true" : "false"));
f.println("showBarometric=" + String(showBarometric ? "true" : "false"));
f.println("pressureSeaLevel=" + String(pressureSeaLevel ? "true" : "false"));
f.println("showPrecipProbability=" + String(showPrecipProbability ? "true" : "false"));
f.println("showUVindex=" + String(showUVindex ? "true" : "false"));
f.println("show6hrForecast=" + String(show6hrForecast ? "true" : "false"));
f.println("forecastStartToday=" + String(forecastStartToday ? "true" : "false"));
f.println("invertColours=" + String(invertColours ? "true" : "false"));
f.println("iconSet=" + String(iconSet));
f.println("highTempVal=" + String(highTempVal));
f.println("lowTempVal=" + String(lowTempVal));
f.println("highTempColour=" + String(nameFromColour(highTempColour)));
f.println("midTempColour=" + String(nameFromColour(midTempColour)));
f.println("lowTempColour=" + String(nameFromColour(lowTempColour)));
f.println("highPrecipProb=" + String(highPrecipProb));
f.println("lowPrecipProb=" + String(lowPrecipProb));
f.println("highPOPColour=" + String(nameFromColour(highPOPColour)));
f.println("midPOPColour=" + String(nameFromColour(midPOPColour)));
f.println("lowPOPColour=" + String(nameFromColour(lowPOPColour)));
f.println("labelColour=" + String(nameFromColour(labelColour)));
f.println("astrologyColour=" + String(nameFromColour(astrologyColour)));
f.println("blDusk=" + String(blDusk));
f.close();
// Serial.println("WebConfig: saved " CONFIG_FILE);
}
// ===========================================================================
// HTML helpers
// ===========================================================================
// Text made safe to put inside a value='...' attribute
static String htmlAttr(const String& s) {
String out;
for (uint16_t i = 0; i < s.length(); i++) {
char c = s[i];
if (c == '&') out += "&";
else if (c == '\'') out += "'";
else if (c == '"') out += """;
else if (c == '<') out += "<";
else out += c;
}
return out;
}
// One location display name row: "TZ1 name on display [ FrostByte Falls ]".
// The box shows what the display shows: the typed name, else the looked-up one.
static String locationNameRow(uint8_t slot, const String& typed, const String& lookedUp) {
String value = typed;
if (value.isEmpty() && lookedUp != "Unknown" && lookedUp != strLocalWeather) value = lookedUp;
String n = String(slot);
return "<div class='row'><label>TZ" + n + " " + String(wcLocName) + "</label>"
"<input type='text' id='name" + n + "' name='locationName" + (slot == 1 ? "" : n) + "'"
" value='" + htmlAttr(value) + "' maxlength='26'></div>";
}
// Every UTC offset in use, in minutes, for the override dropdown
static const int16_t TZ_CHOICES[] = {
-720, -660, -600, -570, -540, -480, -420, -360, -300, -240, -210, -180, -150, -120, -60,
0, 60, 120, 180, 210, 240, 270, 300, 330, 345, 360, 390, 420, 480, 525, 540, 570,
600, 630, 660, 720, 765, 780, 825, 840
};
static const uint8_t TZ_CHOICE_COUNT = sizeof(TZ_CHOICES) / sizeof(TZ_CHOICES[0]);
// "UTC−8" or "UTC+5:30"; full always shows the minutes ("UTC−8:00")
static String utcLabel(int32_t secs, bool full) {
int32_t mins = abs(secs) / 60;
String s = String("UTC") + (secs < 0 ? "−" : "+") + String(mins / 60);
if (full || mins % 60) s += (mins % 60 < 10 ? ":0" : ":") + String(mins % 60);
return s;
}
// One override row: "TZ1 time zone override [x] [UTC−7:00 ▼]", the service's offset below.
// Box off: the list starts at the received offset, so it's one step up or down to fix.
static String tzOverrideRow(uint8_t slot, bool on, int32_t offset, int32_t received) {
String n = String(slot);
String sfx = slot == 1 ? "" : n; // config names: tzOverride, tzOverride2
int32_t shown = on ? offset : received;
String html = "<div class='row'><label>TZ" + n + " " + String(wcTzOverride) + "</label><div>"
"<div style='display:flex;align-items:center;gap:10px'>"
"<label class='toggle' style='width:auto'><input type='checkbox' id='ov" + n + "' name='tzOverride" + sfx + "'"
+ (on ? " checked" : "") + "><span class='slider'></span></label>"
"<select id='ovs" + n + "' name='tzOverrideOffset" + sfx + "'>";
for (uint8_t i = 0; i < TZ_CHOICE_COUNT; i++) {
int32_t secs = TZ_CHOICES[i] * 60;
html += "<option value='" + String(secs) + "'" + (secs == shown ? " selected" : "") + ">"
+ utcLabel(secs, true) + "</option>";
}
html += "</select></div>"
"<div style='font-size:11px;color:var(--muted);margin-top:4px'>" + String(wcTzReceived)
+ ": <span id='r" + n + "'>" + utcLabel(received, false) + "</span></div>"
"</div></div>";
return html;
}
static String colourSelect(const String& name, uint32_t currentValue) {
String curName = "Unknown";
String curCss = "rgb(255,255,255)";
for (uint8_t i = 0; i < TFT_COLOUR_COUNT; i++) {
if (TFT_COLOURS[i].value == currentValue) {
curName = TFT_COLOURS[i].name;
uint32_t c = TFT_COLOURS[i].value;
uint8_t r = ((c >> 11) & 0x1F) << 3;
uint8_t g = ((c >> 5) & 0x3F) << 2;
uint8_t b = ((c >> 0) & 0x1F) << 3;
char css[24];
snprintf(css, sizeof(css), "rgb(%d,%d,%d)", r, g, b);
curCss = String(css);
break;
}
}
String id = "cs_" + name;
String html = "<div class='cs' id='" + id + "'>"
"<input type='hidden' name='"
+ name + "' id='" + id + "_val' value='" + curName + "'>"
"<div class='cs-sel' onclick='csToggle(\""
+ id + "\")'>"
"<div class='cs-swatch' style='background:"
+ curCss + "'></div>"
"<span id='"
+ id + "_lbl'>" + curName + "</span>"
"<span style='margin-left:auto;color:var(--muted)'>▼</span>"
"</div>"
"<div class='cs-drop' id='"
+ id + "_drop'>";
for (uint8_t i = 0; i < TFT_COLOUR_COUNT; i++) {
uint32_t c = TFT_COLOURS[i].value;
uint8_t r = ((c >> 11) & 0x1F) << 3;
uint8_t g = ((c >> 5) & 0x3F) << 2;
uint8_t b = ((c >> 0) & 0x1F) << 3;
bool sel = (TFT_COLOURS[i].value == currentValue);
String cname = TFT_COLOURS[i].name;
String rs = String(r), gs = String(g), bs = String(b);
html += "<div class='cs-opt" + String(sel ? " sel" : "") + "' "
"onclick='csSelect(\""
+ id + "\",\"" + cname + "\"," + ""rgb(" + rs + "," + gs + "," + bs + ")")'>"
"<div class='cs-swatch' style='background:rgb("
+ rs + "," + gs + "," + bs + ")'></div>"
"<span style='color:rgb("
+ rs + "," + gs + "," + bs + ");font-weight:600'>" + cname + "</span>"
"</div>";
}
html += "</div></div>";
return html;
}
static String toggle(const String& name, bool value, const String& label) {
return "<label class='toggle'>"
"<input type='checkbox' name='"
+ name + "'" + (value ? " checked" : "") + ">"
"<span class='slider'></span>"
"<span class='tl'>"
+ label + "</span>"
"</label>";
}
static String section(const String& title) {
return "<h2>" + title + "</h2>";
}
// ===========================================================================
// Full page HTML
// ===========================================================================
// Sends what's built so far and empties it, keeping its memory for the next part
static void sendPart(String& html) {
server.sendContent(html);
html = "";
}
// Sent in parts: the whole page (~70 KB) can be too big for one String, which then silently drops text
static void sendPage(bool saved = false) {
String ip = WiFi.localIP().toString();
String html = F("<!DOCTYPE html><html lang='en'><head>"
"<meta charset='UTF-8'>"
"<meta name='viewport' content='width=device-width,initial-scale=1,maximum-scale=1'>"
"<title>" HOSTNAME "</title>"
"<link rel='stylesheet' href='https://unpkg.com/leaflet@1.9.4/dist/leaflet.css'/>"
"<script src='https://unpkg.com/leaflet@1.9.4/dist/leaflet.js'></script>"
"<style>"
":root{--bg:#121212;--card:#1e1e1e;--bdr:#333;--txt:#e0e0e0;--muted:#888;"
"--accent:#4fc3f7;--danger:#ef5350;}"
"*{box-sizing:border-box;margin:0;padding:0}"
"body{background:var(--bg);color:var(--txt);font-family:sans-serif;font-size:15px}"
"header{background:var(--card);border-bottom:1px solid var(--bdr);padding:14px 20px;"
"display:flex;align-items:center;justify-content:space-between;"
"position:sticky;top:0;z-index:100}"
"header h1{font-size:20px;font-weight:600;color:var(--accent);letter-spacing:1px}"
"header .ip{font-size:11px;color:var(--muted);margin-top:2px}"
".wreset{background:none;border:1px solid var(--danger);color:var(--danger);"
"border-radius:6px;padding:6px 12px;font-size:13px;cursor:pointer}"
".wreset:hover{background:rgba(239,83,80,0.12)}"
"main{max-width:700px;margin:0 auto;padding:20px 16px 50px}"
"h2{font-size:12px;font-weight:600;text-transform:uppercase;letter-spacing:1px;"
"color:var(--accent);margin:28px 0 10px;padding-bottom:6px;border-bottom:1px solid var(--bdr)}"
".card{background:var(--card);border:1px solid var(--bdr);border-radius:10px;"
"padding:16px;display:flex;flex-direction:column;gap:14px}"
".row{display:flex;align-items:center;justify-content:space-between;gap:12px;flex-wrap:wrap}"
".row>label:first-child{font-size:14px;flex:1;min-width:150px}"
"input[type=text],input[type=number]{background:#1a1a1a;border:1px solid var(--bdr);"
"border-radius:6px;color:var(--txt);font-size:14px;padding:7px 10px;width:100%;max-width:210px}"
"select{background:#1a1a1a;border:1px solid var(--bdr);border-radius:6px;"
"color:var(--txt);font-size:14px;padding:7px 10px}"
"input:focus,select:focus{outline:none;border-color:var(--accent)}"
".rg{display:flex;gap:8px}"
".rg label{display:flex;align-items:center;gap:6px;background:#1a1a1a;"
"border:1px solid var(--bdr);border-radius:6px;padding:6px 14px;cursor:pointer;"
"font-size:14px;min-width:unset;flex:unset}"
".rg input{accent-color:var(--accent)}"
".rg label:has(input:checked){border-color:var(--accent);color:var(--accent)}"
".toggle{display:flex;align-items:center;gap:10px;cursor:pointer;width:100%}"
".toggle input{display:none}"
".slider{width:44px;height:24px;background:#444;border-radius:12px;"
"position:relative;flex-shrink:0;transition:background .2s}"
".slider::after{content:'';position:absolute;width:18px;height:18px;background:#fff;"
"border-radius:50%;top:3px;left:3px;transition:left .2s}"
".toggle input:checked+.slider{background:var(--accent)}"
".toggle input:checked+.slider::after{left:23px}"
".tl{font-size:14px}"
"#map{width:100%;height:280px;border-radius:8px;border:1px solid var(--bdr);margin-bottom:10px}"
// TZ row styles
".tzrow{display:flex;align-items:center;gap:8px;padding:3px 0}"
".tzbtn{background:#1a1a1a;border:1px solid var(--bdr);color:var(--muted);"
"border-radius:6px;padding:5px 10px;font-size:13px;font-weight:700;cursor:pointer;min-width:40px}"
".tzbtn.active{border-color:var(--accent);color:var(--accent)}"
".tzcoord{font-size:12px;color:var(--txt);font-family:monospace;"
"background:#111;border:1px solid var(--bdr);border-radius:6px;"
"padding:4px 7px;min-width:82px;text-align:center}"
".tzfetch{font-size:11px;color:var(--muted);padding-left:4px}"
".tzsearch{background:none;border:none;color:var(--accent);font-size:16px;"
"cursor:pointer;padding:2px 6px;line-height:1}"
".tzsearch:hover{color:#fff}"
".srchwrap{margin-top:6px;display:none;max-width:80%}"
".srchinput{width:100%;background:#1a1a1a;border:1px solid var(--accent);"
"border-radius:6px 6px 0 0;color:var(--txt);font-size:16px;"
"padding:8px 12px;outline:none;box-sizing:border-box}"
".srchresults{background:#1e1e1e;border:1px solid #333;border-top:none;"
"border-radius:0 0 6px 6px}"
".srchresult{padding:10px 12px;cursor:pointer;font-size:13px;color:#e0e0e0;"
"border-bottom:1px solid #2a2a2a}"
".srchresult:last-child{border-bottom:none}"
".srchresult:hover{background:#2a2a2a;color:#4fc3f7}"
// end TZ/search styles
".actions{display:flex;gap:12px;margin-top:28px;flex-wrap:wrap}"
".btn{padding:10px 28px;border-radius:8px;font-size:15px;font-weight:600;"
"cursor:pointer;border:2px solid;letter-spacing:.5px}"
".bu{background:var(--accent);border-color:var(--accent);color:#000}"
".bu:hover{opacity:.85}"
".bc{background:transparent;border-color:var(--muted);color:var(--muted)}"
".bc:hover{border-color:var(--txt);color:var(--txt)}"
".mbg{display:none;position:fixed;inset:0;background:rgba(0,0,0,.75);"
"z-index:200;align-items:center;justify-content:center}"
".mbg.open{display:flex}"
".modal{background:var(--card);border:1px solid var(--danger);border-radius:12px;"
"padding:28px 24px;max-width:340px;width:90%;text-align:center}"
".modal h3{color:var(--danger);margin-bottom:12px;font-size:17px}"
".modal p{color:var(--muted);font-size:14px;margin-bottom:20px;line-height:1.5}"
".mbtns{display:flex;gap:10px;justify-content:center}"
".modal.okm{border-color:#4caf50;cursor:pointer}.modal.okm h3{color:#81c784;margin-bottom:20px}"
".mok.okb{background:#4caf50}"
".mok{background:var(--danger);border:none;color:#fff;padding:9px 22px;"
"border-radius:7px;font-size:14px;font-weight:600;cursor:pointer}"
".mno{background:transparent;border:1px solid var(--muted);color:var(--muted);"
"padding:9px 22px;border-radius:7px;font-size:14px;cursor:pointer}"
".cs{position:relative;width:100%;max-width:210px}"
".cs-sel{background:#1a1a1a;border:1px solid var(--bdr);border-radius:6px;"
"color:var(--txt);font-size:14px;padding:7px 10px;cursor:pointer;"
"display:flex;align-items:center;gap:8px;user-select:none}"
".cs-sel:hover{border-color:var(--accent)}"
".cs-swatch{width:14px;height:14px;border-radius:3px;flex-shrink:0;border:1px solid #444}"
".cs-drop{display:none;position:absolute;top:100%;left:0;right:0;"
"background:#1a1a1a;border:1px solid var(--bdr);border-radius:6px;"
"max-height:200px;overflow-y:auto;z-index:999;margin-top:4px}"
".cs-drop.open{display:block}"
".cs-opt{display:flex;align-items:center;gap:8px;padding:7px 10px;"
"cursor:pointer;font-size:14px}"
".cs-opt:hover{background:#2a2a2a}"
".cs-opt.sel{background:#0f3460}"
"@media(max-width:480px){.row{flex-direction:column;align-items:flex-start}"
"input[type=text],input[type=number]{max-width:100%}.cs{max-width:100%}}"
"</style></head><body>");
// ── Header ─────────────────────────────────────────────────────────────────
html += "<header><div><h1>⛈ " + String(HOSTNAME) + "</h1>"
"<div class='ip'>" + ip + " | " + mdnsName + ".local</div></div>"
"<button class='wreset' onclick='document.getElementById(\"wm\").classList.add(\"open\")'>"
+ String(wcWifiResetBtn) + "</button></header>";
// ── WiFi reset modal ────────────────────────────────────────────────────────
html += "<div class='mbg' id='wm'><div class='modal'>"
"<h3>" + String(wcWifiResetTitle) + "</h3>"
"<p>" + String(wcWifiResetBody) + "</p>"
"<div class='mbtns'>"
"<button class='mok' onclick='window.location=\"/wifireset\"'>" + String(wcWifiResetYes) + "</button>"
"<button class='mno' onclick='document.getElementById(\"wm\").classList.remove(\"open\")'>" + String(wcWifiResetCancel) + "</button>"
"</div></div></div>";
html += "<main>";
// Saved popup: a click anywhere closes it. The ?saved=1 comes off the address, so Cancel
// (a reload) or a later refresh doesn't show it again.
if (saved) html += "<div class='mbg open' onclick='this.classList.remove(\"open\")'>"
"<div class='modal okm'><h3>" + String(wcSaved) + "</h3>"
"<div class='mbtns'><button type='button' class='mok okb'>" + String(wcOkBtn) + "</button></div>"
"</div></div>"
"<script>history.replaceState(null,'','/');</script>";
html += "<form method='POST' action='/save'>";
// ── Location ───────────────────────────────────────────────────────────────
sendPart(html);
html += section(wcSecLocation);
html += "<div class='card'>";
html += "<div id='map'></div>";
html += "<div style='display:flex;flex-direction:column;gap:4px'>";
// TZ1 row
html += "<div class='tzrow'>"
"<button type='button' id='btn1' class='tzbtn active' onclick='setActive(1)'>TZ1</button>"
"<span class='tzcoord' id='d1lat'>" + latitude + "</span>"
"<span class='tzcoord' id='d1lon'>" + longitude + "</span>"
"<span class='tzfetch' id='f1'></span>"
"<button type='button' class='tzsearch' onclick='openSearch(1)' title='Search'>🔍</button>"
"<input type='hidden' id='lat' name='latitude' value='" + latitude + "'>"
"<input type='hidden' id='lon' name='longitude' value='" + longitude + "'>"
"<input type='hidden' id='tz1' name='tzOffset' value='" + String(tzOffset) + "'>"
"</div>";
// TZ2 row
html += "<div class='tzrow'>"
"<button type='button' id='btn2' class='tzbtn' onclick='setActive(2)'>TZ2</button>"
"<span class='tzcoord' id='d2lat'>" + latitude2 + "</span>"
"<span class='tzcoord' id='d2lon'>" + longitude2 + "</span>"
"<span class='tzfetch' id='f2'></span>"
"<button type='button' class='tzsearch' onclick='openSearch(2)' title='Search'>🔍</button>"
"<input type='hidden' id='lat2' name='latitude2' value='" + latitude2 + "'>"
"<input type='hidden' id='lon2' name='longitude2' value='" + longitude2 + "'>"
"<input type='hidden' id='tz2' name='tzOffset2' value='" + String(tzOffset2) + "'>"
"</div>";
// Search box — sits outside the map, no Leaflet event conflicts
html += "<div class='srchwrap' id='srchwrap'>"
"<input class='srchinput' id='srchinput' type='text' placeholder='Search address, city, country... (Enter)'"
" onkeydown='if(event.key===\"Enter\"){event.preventDefault();doSearch(this.value.trim());}"
"else if(event.key===\"Escape\")closeSearch()'>"
"<div id='srchresults' class='srchresults'></div>"
"</div>";
html += "</div>"; // end tzrows wrapper
// Optional names for the display, instead of the map's town name
html += locationNameRow(1, locationName, theCityLocation);
html += locationNameRow(2, locationName2, theCityLocation2);
// Fixed offsets for when the service's is wrong (e.g. a place that dropped DST)
html += tzOverrideRow(1, tzOverride, tzOverrideOffset, tzOffset);
html += tzOverrideRow(2, tzOverride2, tzOverrideOffset2, tzOffset2);
html += "</div>"; // end location card
// ── Display ────────────────────────────────────────────────────────────────
sendPart(html);
html += section(wcSecDisplay);
html += "<div class='card'>";
html += "<div class='row'><label>" + String(wcUnitsLabel) + "</label>"
"<div class='rg'>"
"<label><input type='radio' name='units' value='imperial'" + String(units == "imperial" ? " checked" : "") + "> " + String(wcImperial) + "</label>"
"<label><input type='radio' name='units' value='metric'" + String(units == "metric" ? " checked" : "") + "> " + String(wcMetric) + "</label>"
"</div></div>";
html += "<div class='row'>" + toggle("show24Hour", show24Hour, wcClock24) + "</div>";
html += "<div class='row'>" + toggle("autoDimDusk", autoDimDusk, wcAutoDim) + "</div>";
html += "<div class='row'><label>" + String(wcNightBright) + "</label>"
"<div><input type='number' name='blDusk' value='" + String(blDusk) + "' min='0' max='255' style='max-width:90px'>"
"<div style='font-size:11px;color:var(--muted);margin-top:4px'>" + String(wcNightBrightHint) + "</div>"
"</div></div>";
html += "<div class='row'>" + toggle("showBarometric", showBarometric, wcShowBaro) + "</div>";
html += "<div class='row'><label>" + String(wcPressure) + "</label>"
"<div class='rg'>"
"<label><input type='radio' name='pressureSeaLevel' value='1'" + String( pressureSeaLevel ? " checked" : "") + "> " + String(wcPressSeaLevel) + "</label>"
"<label><input type='radio' name='pressureSeaLevel' value='0'" + String(!pressureSeaLevel ? " checked" : "") + "> " + String(wcPressStation) + "</label>"
"</div></div>";
html += "<div class='row'>" + toggle("showPrecipProbability", showPrecipProbability, wcShowPrecip) + "</div>";
html += "<div class='row'>" + toggle("showUVindex", showUVindex, wcShowUV) + "</div>";
html += "<div class='row'>" + toggle("show6hrForecast", show6hrForecast, wcShow6Hr) + "</div>";
html += "<div class='row'><label>" + String(wcForecastStart) + "</label>"
"<div class='rg'>"
"<label><input type='radio' name='forecastStartToday' value='0'" + String(!forecastStartToday ? " checked" : "") + "> " + String(wcForecastTomorrow) + "</label>"
"<label><input type='radio' name='forecastStartToday' value='1'" + String( forecastStartToday ? " checked" : "") + "> " + String(wcForecastToday) + "</label>"
"</div></div>";
html += "<div class='row'><label>" + String(wcIconSet) + "</label>"
"<div class='rg'>"
"<label><input type='radio' name='iconSet' value='0'" + String(iconSet != 1 ? " checked" : "") + "> " + String(wcIconStandard) + "</label>"
"<label><input type='radio' name='iconSet' value='1'" + String(iconSet == 1 ? " checked" : "") + "> " + String(wcIconMeteocons) + "</label>"
"</div></div>";
html += "<div class='row'><div>" + toggle("invertColours", invertColours, wcInvert) +
"<div style='font-size:11px;color:var(--muted);margin:4px 0 0 54px'>" + String(wcInvertHint) + "</div>"
"</div></div>";
html += "<div class='row'><label>" + String(wcWeatherHeadings) + "</label>" + colourSelect("labelColour", labelColour) + "</div>";
html += "<div class='row'><label>" + String(wcAstroHeadings) + "</label>" + colourSelect("astrologyColour", astrologyColour) + "</div>";
html += "</div>";
// ── Temperature colours ────────────────────────────────────────────────────
sendPart(html);
html += section(wcSecTempColours);
html += "<div class='card'>";
html += "<div class='row'><label>" + String(wcHighTempThresh) + "</label>"
"<input type='number' name='highTempVal' value='" + String(highTempVal) + "' min='-99' max='199' style='max-width:90px'></div>";
html += "<div class='row'><label>" + String(wcLowTempThresh) + "</label>"
"<input type='number' name='lowTempVal' value='" + String(lowTempVal) + "' min='-99' max='199' style='max-width:90px'></div>";
html += "<div class='row'><label>" + String(wcHighTempColour) + "</label>" + colourSelect("highTempColour", highTempColour) + "</div>";
html += "<div class='row'><label>" + String(wcMidTempColour) + "</label>" + colourSelect("midTempColour", midTempColour) + "</div>";
html += "<div class='row'><label>" + String(wcLowTempColour) + "</label>" + colourSelect("lowTempColour", lowTempColour) + "</div>";
html += "</div>";
// ── Precipitation colours ──────────────────────────────────────────────────
sendPart(html);
html += section(wcSecPrecipColours);
html += "<div class='card'>";
html += "<div class='row'><label>" + String(wcHighPrecipThresh) + "</label>"
"<input type='number' name='highPrecipProb' value='" + String(highPrecipProb) + "' min='0' max='100' style='max-width:90px'></div>";
html += "<div class='row'><label>" + String(wcLowPrecipThresh) + "</label>"
"<input type='number' name='lowPrecipProb' value='" + String(lowPrecipProb) + "' min='0' max='100' style='max-width:90px'></div>";
html += "<div class='row'><label>" + String(wcHighPOPColour) + "</label>" + colourSelect("highPOPColour", highPOPColour) + "</div>";
html += "<div class='row'><label>" + String(wcMidPOPColour) + "</label>" + colourSelect("midPOPColour", midPOPColour) + "</div>";
html += "<div class='row'><label>" + String(wcLowPOPColour) + "</label>" + colourSelect("lowPOPColour", lowPOPColour) + "</div>";
html += "</div>";
// ── Actions ────────────────────────────────────────────────────────────────
html += "<div class='actions'>"
"<button type='submit' class='btn bu'>" + String(wcSaveBtn) + "</button>"
"<button type='button' class='btn bc' onclick='window.location.reload()'>" + String(wcCancelBtn) + "</button>"
"</div>";
html += "</form>";
sendPart(html);
// ── Map + TZ + Search script ───────────────────────────────────────────────
html += "<script>"
// Active slot: 1 = TZ1, 2 = TZ2
"var active=1;"
// Switch active slot and pan map to that location
"function setActive(n){"
"active=n;"
"document.getElementById('btn1').classList.toggle('active',n===1);"
"document.getElementById('btn2').classList.toggle('active',n===2);"
"var la=parseFloat(document.getElementById(n===1?'lat':'lat2').value);"
"var lo=parseFloat(document.getElementById(n===1?'lon':'lon2').value);"
"if(!isNaN(la)&&!isNaN(lo)){mk.setLatLng([la,lo]);map.setView([la,lo],11);}}"
// Init map centred on TZ1
"var lt=parseFloat(document.getElementById('lat').value)||49.2734;"
"var ln=parseFloat(document.getElementById('lon').value)||-123.1037;"
"var map=L.map('map').setView([lt,ln],11);"
"L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',{"
"attribution:'© OpenStreetMap contributors',maxZoom:18}).addTo(map);"
"var mk=L.marker([lt,ln],{draggable:true}).addTo(map);"
// Nominatim policy: search only on Enter, never repeat the last query
"var searchSlot=1,lastQuery='';"
"function openSearch(slot){"
"searchSlot=slot;setActive(slot);"
"var w=document.getElementById('srchwrap');"
"w.style.display='block';"
"var inp=document.getElementById('srchinput');"
"inp.value='';inp.focus();lastQuery='';"
"document.getElementById('srchresults').innerHTML='';}"
"function closeSearch(){"
"document.getElementById('srchwrap').style.display='none';"
"document.getElementById('srchresults').innerHTML='';}"
"function doSearch(q){"
"if(q.length<2||q===lastQuery)return;"
"lastQuery=q;"
"fetch('https://nominatim.openstreetmap.org/search?format=json&limit=10&q='+encodeURIComponent(q))"
".then(function(r){return r.json();})"
".then(function(data){"
"var div=document.getElementById('srchresults');"
"div.innerHTML='';"
"if(!data.length){"
"div.innerHTML='<div class=\"srchresult\" style=\"color:var(--muted)\">No results</div>';return;}"
"data.forEach(function(item){"
"var d=document.createElement('div');"
"d.className='srchresult';"
"d.textContent=item.display_name;"
"d.onclick=function(){"
"var la=parseFloat(item.lat),lo=parseFloat(item.lon);"
"mk.setLatLng([la,lo]);map.setView([la,lo],12);uc(la,lo);"
"closeSearch();};"
"div.appendChild(d);});}"
").catch(function(){"
"document.getElementById('srchresults').innerHTML="
"'<div class=\"srchresult\" style=\"color:var(--danger)\">Error</div>';lastQuery='';});}"
// Close search when clicking outside
"document.addEventListener('click',function(e){"
"if(!e.target.closest('#srchwrap')&&!e.target.closest('.tzsearch'))closeSearch();});"
// "UTC−8" or "UTC+5:30", as utcLabel() writes it
"function fmtOff(o){"
"var m=Math.abs(o)/60,s='UTC'+(o<0?'\\u2212':'+')+Math.floor(m/60);"
"if(m%60)s+=':'+('0'+m%60).slice(-2);return s;}"
// Fetch TZ offset from timeapi.io; also shown as the override row's "Received"
"function fetchTZ(lat,lon,slot){"
"var fi=document.getElementById('f'+slot);"
"fi.textContent='...';"
"fetch('https://timeapi.io/api/timezone/coordinate?latitude='+lat+'&longitude='+lon)"
".then(function(r){return r.json();})"
".then(function(d){"
"if(d&&d.currentUtcOffset&&d.currentUtcOffset.seconds!==undefined){"
"var off=d.currentUtcOffset.seconds;"
"document.getElementById(slot===1?'tz1':'tz2').value=off;"
"fi.textContent=fmtOff(off);"
"document.getElementById('r'+slot).textContent=fmtOff(off);"
"if(!document.getElementById('ov'+slot).checked)document.getElementById('ovs'+slot).value=off;}" // box off: list follows
"else{fi.textContent='?';}"
"}).catch(function(){fi.textContent='err';});}"
// Town name from OpenStreetMap into the slot's name box, asked the same way as
// the device's own lookup. Waits until the pin has been still for a second
// (Nominatim allows one request a second). On failure the box is left blank,
// so the device looks the name up itself after Save.
"var nameTimer={};"
"function fetchName(lat,lon,slot){"
"var box=document.getElementById('name'+slot);"
"var at=lat+','+lon;"
"box.dataset.at=at;box.value='';box.placeholder='...';"
"clearTimeout(nameTimer[slot]);"
"nameTimer[slot]=setTimeout(function(){"
"fetch('https://nominatim.openstreetmap.org/reverse?format=json&addressdetails=1&zoom=12"
"&accept-language=" NOMINATIM_LANG ",en&lat='+lat+'&lon='+lon)"
".then(function(r){return r.json();})"
".then(function(d){"
"if(box.dataset.at!==at)return;" // the pin moved again since
"var a=d.address||{};"
"var n=a.city||a.town||a.village||a.hamlet||d.name||'';"
"box.value=Array.from(n).slice(0,26).join('');"
"box.placeholder='';})"
".catch(function(){if(box.dataset.at===at)box.placeholder='';});"
"},1000);}"
// Update coords + fetch TZ and name for active slot
"function uc(a,b){"
"b=((b+180)%360+360)%360-180;"
"var latId=active===1?'lat':'lat2';"
"var lonId=active===1?'lon':'lon2';"
"var dlatId=active===1?'d1lat':'d2lat';"
"var dlonId=active===1?'d1lon':'d2lon';"
"var af=a.toFixed(4),bf=b.toFixed(4);"
"document.getElementById(latId).value=af;"
"document.getElementById(lonId).value=bf;"
"document.getElementById(dlatId).textContent=af;"
"document.getElementById(dlonId).textContent=bf;"
"fetchTZ(af,bf,active);fetchName(af,bf,active);}"
"mk.on('dragend',function(e){var p=mk.getLatLng();uc(p.lat,p.lng);});"
"map.on('click',function(e){mk.setLatLng(e.latlng);uc(e.latlng.lat,e.latlng.lng);});"
// A name box still blank on opening (the device couldn't look it up yet):
// fill it from here, TZ2 a second later to keep to Nominatim's pace
"if(!document.getElementById('name1').value)"
"fetchName(document.getElementById('lat').value,document.getElementById('lon').value,1);"
"if(!document.getElementById('name2').value)setTimeout(function(){"
"fetchName(document.getElementById('lat2').value,document.getElementById('lon2').value,2);},1100);"
// Colour selector functions
"function csToggle(id){"
"var d=document.getElementById(id+'_drop');"
"d.classList.toggle('open');"
"document.querySelectorAll('.cs-drop').forEach(function(x){"
"if(x.id!==id+'_drop')x.classList.remove('open');});}"
"function csSelect(id,name,css){"
"document.getElementById(id+'_val').value=name;"
"document.getElementById(id+'_lbl').textContent=name;"
"document.querySelector('#'+id+' .cs-swatch').style.background=css;"
"document.getElementById(id+'_drop').classList.remove('open');"
"document.querySelectorAll('#'+id+' .cs-opt').forEach(function(x){"
"x.classList.remove('sel');"
"if(x.querySelector('span:last-child').textContent===name)x.classList.add('sel');});}"
"document.addEventListener('click',function(e){"
"if(!e.target.closest('.cs')){"
"document.querySelectorAll('.cs-drop').forEach(function(x){"
"x.classList.remove('open');});}});"
"</script>";
// ── Temp thresholds follow the units switch (from the typed value, so °F/°C flips don't drift)
html += "<script>"
"(function(){"
"function cu(){return document.querySelector('input[name=units]:checked').value;}"
"var base={};"
"['highTempVal','lowTempVal'].forEach(function(n){"
"var el=document.querySelector('input[name='+n+']');"
"base[n]={v:+el.value,u:cu()};"
"el.addEventListener('input',function(){base[n]={v:+el.value,u:cu()};});});"
"document.querySelectorAll('input[name=units]').forEach(function(r){"
"r.addEventListener('change',function(){"
"var u=cu();"
"Object.keys(base).forEach(function(n){"
"var b=base[n],v=b.v;"
"if(b.u!==u)v=(u==='metric')?(v-32)*5/9:v*9/5+32;"
"document.querySelector('input[name='+n+']').value=Math.round(v);});});});"
"})();"
"</script>";
html += "</main></body></html>";
sendPart(html);
}
// ===========================================================================
// Request handlers
// ===========================================================================
static void handleRoot() {
bool saved = server.hasArg("saved");
// Boot only looks up the location on screen. Fetch the other name now so both
// boxes open filled; once it's cached (or typed) these return straight away.
lookupCityName(false);
lookupCityName(true);
server.setContentLength(CONTENT_LENGTH_UNKNOWN); // chunked, so the page can go in parts
server.send(200, "text/html", "");
sendPage(saved);
server.sendContent(""); // ends the reply
}
// True if the submitted coordinate differs from the stored one by half a 4-decimal step or more
static bool coordMoved(const String& oldVal, const char* argName) {
if (!server.hasArg(argName)) return false;
return fabs(server.arg(argName).toDouble() - oldVal.toDouble()) > 0.00005;
}
// A submitted display name: one line, no outer spaces, at most 26 letters (as on screen)
static String cleanLocationName(const String& raw) {
String name;
uint8_t letters = 0;
for (uint16_t i = 0; i < raw.length(); i++) {
uint8_t c = raw[i];
if (c < 0x20) continue; // no newlines etc. in the one-line config file
if ((c & 0xC0) != 0x80 && ++letters > 26) break; // count letters, not UTF-8 bytes
name += (char)c;
}
name.trim();
return name;
}
static void handleSave() {
// Work out what changed before anything is overwritten
uint8_t changes = 0;
if (coordMoved(latitude, "latitude") || coordMoved(longitude, "longitude")) changes |= CFG_LOC1_MOVED;
if (coordMoved(latitude2, "latitude2") || coordMoved(longitude2, "longitude2")) changes |= CFG_LOC2_MOVED;
if (server.hasArg("units") && server.arg("units") != units) changes |= CFG_UNITS_CHANGED;
if (server.hasArg("units")) units = server.arg("units");
if (server.hasArg("latitude")) latitude = server.arg("latitude");
if (server.hasArg("longitude")) longitude = server.arg("longitude");
if (server.hasArg("tzOffset")) tzOffset = server.arg("tzOffset").toInt();
if (server.hasArg("latitude2")) latitude2 = server.arg("latitude2");
if (server.hasArg("longitude2")) longitude2 = server.arg("longitude2");
if (server.hasArg("tzOffset2")) tzOffset2 = server.arg("tzOffset2").toInt();
tzOverride = server.hasArg("tzOverride");
tzOverride2 = server.hasArg("tzOverride2");
if (server.hasArg("tzOverrideOffset")) tzOverrideOffset = server.arg("tzOverrideOffset").toInt();
if (server.hasArg("tzOverrideOffset2")) tzOverrideOffset2 = server.arg("tzOverrideOffset2").toInt();
if (server.hasArg("locationName") && server.hasArg("locationName2")) {
String name1 = cleanLocationName(server.arg("locationName"));
String name2 = cleanLocationName(server.arg("locationName2"));
if (name1 != locationName || name2 != locationName2) changes |= CFG_NAMES_CHANGED;
locationName = name1;
locationName2 = name2;
}
show24Hour = server.hasArg("show24Hour");
autoDimDusk = server.hasArg("autoDimDusk");
showBarometric = server.hasArg("showBarometric");
showPrecipProbability = server.hasArg("showPrecipProbability");
showUVindex = server.hasArg("showUVindex");
show6hrForecast = server.hasArg("show6hrForecast");
forecastStartToday = server.hasArg("forecastStartToday") && server.arg("forecastStartToday") == "1";
invertColours = server.hasArg("invertColours");
if (server.hasArg("iconSet")) iconSet = server.arg("iconSet") == "1" ? 1 : 0;
if (server.hasArg("pressureSeaLevel")) pressureSeaLevel = server.arg("pressureSeaLevel") == "1"; // display only — both are always fetched
if (server.hasArg("highTempVal")) highTempVal = server.arg("highTempVal").toInt();
if (server.hasArg("lowTempVal")) lowTempVal = server.arg("lowTempVal").toInt();
if (server.hasArg("highTempColour")) highTempColour = colourFromName(server.arg("highTempColour"));
if (server.hasArg("midTempColour")) midTempColour = colourFromName(server.arg("midTempColour"));
if (server.hasArg("lowTempColour")) lowTempColour = colourFromName(server.arg("lowTempColour"));
if (server.hasArg("highPrecipProb")) highPrecipProb = server.arg("highPrecipProb").toInt();
if (server.hasArg("lowPrecipProb")) lowPrecipProb = server.arg("lowPrecipProb").toInt();
if (server.hasArg("highPOPColour")) highPOPColour = colourFromName(server.arg("highPOPColour"));
if (server.hasArg("midPOPColour")) midPOPColour = colourFromName(server.arg("midPOPColour"));
if (server.hasArg("lowPOPColour")) lowPOPColour = colourFromName(server.arg("lowPOPColour"));
if (server.hasArg("labelColour")) labelColour = colourFromName(server.arg("labelColour"));
if (server.hasArg("astrologyColour")) astrologyColour = colourFromName(server.arg("astrologyColour"));
if (server.hasArg("blDusk")) blDusk = server.arg("blDusk").toInt();
saveConfig();
configChanges |= changes;
configUpdated = true;
server.sendHeader("Location", "/?saved=1");
server.send(303);
}
static void handleWifiReset() {
server.send(200, "text/html",
"<html><body style='background:#121212;color:#e0e0e0;font-family:sans-serif;"
"display:flex;align-items:center;justify-content:center;height:100vh'>"
"<div style='text-align:center'><h2 style='color:#ef5350'>"
+ String(wcWifiResetting) + "</h2><p>" + String(wcWifiResetHotspot) + "</p>"
"</div></body></html>");
delay(1000);
WiFiManager wm;
wm.resetSettings();
ESP.restart();
}
static void handleNotFound() {
server.send(404, "text/plain", "Not found");
}
// ===========================================================================
// Public functions
// ===========================================================================
void startWebConfig(const char* hostName) {
// mDNS names are lowercase letters, digits and hyphens — anything else becomes a hyphen
mdnsName = "";
for (const char* c = hostName; *c; c++) {
char ch = tolower(*c);
mdnsName += (isalnum(ch) || ch == '-') ? ch : '-';
}
if (mdnsName.length() == 0) mdnsName = "wabbitweather";
if (MDNS.begin(mdnsName.c_str())) {
Serial.println("WebConfig: mDNS started — http://" + mdnsName + ".local");
} else {
Serial.println("WebConfig: mDNS failed — use IP");
}
server.on("/", HTTP_GET, handleRoot);
server.on("/save", HTTP_POST, handleSave);
server.on("/wifireset", HTTP_GET, handleWifiReset);
server.onNotFound(handleNotFound);
server.begin();
Serial.print("WebConfig: running at http://");
Serial.println(WiFi.localIP());
}
void handleWebConfig() {
server.handleClient();
}