-
Notifications
You must be signed in to change notification settings - Fork 0
/
bigbutton.ino
1830 lines (1536 loc) · 52.1 KB
/
bigbutton.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
#include <stdio.h>
#include <HTTPClient.h>
#include <WebServer.h>
#define USE_MDNS
#ifdef USE_MDNS
#include <ESPmDNS.h>
#endif
#include <WiFiManager.h>
#define USE_NIMBLE
#include <BleKeyboard.h>
WebServer server(80);
#include <ArduinoJson.h>
WiFiManager wifiManager;
#include "time.h"
#include "SPIFFS.h"
#include <EEPROM.h>
#include "TuyaLocal.hpp"
#define EEPROM_SIZE 100
#define LED_ON_PRESS 0
#define LED_OFF_PRESS 1
#define LED_ON 2
#define LED_OFF 3
#define LED_TOGGLE_PRESS 4
#define LED_SMART 5
#define PRESS_PRINT 0
#define PRESS_PRESSRELEASE 1
#define PRESS_NONE 2
#define SMART_ON 3
#define SMART_OFF 1
#define SMART_TOGGLE 2
#define SMART_ALARM 4
#define SMART_NONE 0
#define NONE 1
#define TUYA_CLOUD 0
#define TUYA_LOCAL 2
#define TASMOTA 3
TaskHandle_t Task1;
/*
Login page
*/
const char* loginIndex =
"<form name='loginForm'>"
"<table width='20%' bgcolor='A09F9F' align='center'>"
"<tr>"
"<td colspan=2>"
"<center><font size=4><b>Big Button Login Page</b></font></center>"
"<br>"
"</td>"
"<br>"
"<br>"
"</tr>"
"<td>Username:</td>"
"<td><input type='text' size=25 name='userid'><br></td>"
"</tr>"
"<br>"
"<br>"
"<tr>"
"<td>Password:</td>"
"<td><input type='Password' size=25 name='pwd'><br></td>"
"<br>"
"<br>"
"</tr>"
"<tr>"
"<td><input type='submit' onclick='check(this.form)' value='Login'></td>"
"</tr>"
"</table>"
"</form>"
"<script>"
"function check(form)"
"{"
"if(form.userid.value=='admin' && form.pwd.value=='admin')"
"{"
"window.open('/serverIndex')"
"}"
"else"
"{"
" alert('Error Password or Username')/*displays error message*/"
"}"
"}"
"</script>";
/*
Server Index Page
*/
const char* serverIndex =
"<script src='https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js'></script>"
"<form method='POST' action='#' enctype='multipart/form-data' id='upload_form'>"
"<input type='file' name='update'>"
"<input type='submit' value='Update'>"
"</form>"
"<div id='prg'>progress: 0%</div>"
"<script>"
"$('form').submit(function(e){"
"e.preventDefault();"
"var form = $('#upload_form')[0];"
"var data = new FormData(form);"
" $.ajax({"
"url: '/update',"
"type: 'POST',"
"data: data,"
"contentType: false,"
"processData:false,"
"xhr: function() {"
"var xhr = new window.XMLHttpRequest();"
"xhr.upload.addEventListener('progress', function(evt) {"
"if (evt.lengthComputable) {"
"var per = evt.loaded / evt.total;"
"$('#prg').html('progress: ' + Math.round(per*100) + '%');"
"}"
"}, false);"
"return xhr;"
"},"
"success:function(d, s) {"
"console.log('success!')"
"},"
"error: function (a, b, c) {"
"}"
"});"
"});"
"</script>";
void(* resetFunc) (void) = 0; //declare reset function @ address 0
const char *settings_file = "/settings.txt";
const char *profiles_file = "/profiles.txt";
const char* ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 0;
const int daylightOffset_sec = 3600;
const char *profiles_key = "profiles";
const char *SELECTED = "selected=\"selected\"";
const char *LED_ON_PRESSED = "led_on_press";
BleKeyboard bleKeyboard("BigButton");
#define TUYA_COMMAND_PORT 6668
#include "TuyaAuth.h"
TuyaAuth *tuya;
//#include <ezButton.h>
#define BUTTON_PIN 15
//ezButton button(BUTTON_PIN); // create ezButton object that attach to pin 7;
#define BUZZER_PIN 12
#define LED_PIN 32
char device_name[20];
int reconnects = 0;
void writeId(const char *id) {
for (int i = 0; i <= strlen(id); i++) {
EEPROM.write(i, id[i]);
}
unsigned long calculatedCrc = eeprom_crc();
EEPROM.put(EEPROM.length() - 4, calculatedCrc);
}
void readId(char *buffer) {
for (int i = 0; i < 20; i++) {
buffer[i] = EEPROM.read(i);
}
}
void init_eeprom() {
unsigned long calculatedCrc = eeprom_crc();
// get stored crc
unsigned long storedCrc;
EEPROM.get(EEPROM.length() - 4, storedCrc);
if (storedCrc != calculatedCrc) {
writeId("bigbutton");
}
}
unsigned long eeprom_crc(void)
{
const unsigned long crc_table[16] =
{
0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
};
unsigned long crc = ~0L;
for (int index = 0 ; index < EEPROM.length() - 4 ; ++index)
{
byte b = EEPROM.read(index);
crc = crc_table[(crc ^ b) & 0x0f] ^ (crc >> 4);
crc = crc_table[(crc ^ (b >> 4)) & 0x0f] ^ (crc >> 4);
crc = ~crc;
}
return crc;
}
bool readFile(String name, String &buffer) {
buffer = "";
File file = SPIFFS.open(name, FILE_READ);
if (file) {
buffer.reserve(file.size());
while (file.available()) {
const char c = file.read();
if (c != '\0') buffer += c;
}
Serial.println("Read file:"); Serial.println(name);
Serial.println(buffer);
return true;
} else {
Serial.print("error reading:"); Serial.println(name);
return false;
}
}
class CButtonProfile {
public:
String name;
int power_off_time;
int press_mode;
int led_mode;
int repeat_time;
int pressrelease_time;
int smart_mode;
String press_string;
String tuya_device_id;
String tuya_api_key;
String tuya_api_client;
String tuya_host;
String smart_ip;
// switch command from cloud
String tuya_switch;
String tuya_localkey;
String tuya_protocol;
bool tuya_localkeyfound;
bool have_status;
bool on;
bool led_on;
bool button_on;
long ms;
int smart;
int tuya_timeout;
bool tuya_error;
String tuya_error_string;
bool tuya_local_connected;
bool alarm_set;
};
#define MAX_PROFILES 10
CButtonProfile profile;
String profiles[MAX_PROFILES];
int attempts = 0;
int local_errors = 0;
int cloud_errors = 0;
int failsafe_errors = 0;
int profile_count = 0;
int cloud_tries = 0;
int local_tries = 0;
int haveProfile(String &name) {
for (int i = 0; i < profile_count; i++) {
if (profiles[i] == name) {
return i;
}
}
return -1;
}
void verifyProfile() {
}
tuyaLocal *tuyaclient = NULL;
void statusTask( void * parameter) {
for (;;) {
if (tuya) {
if (profile.alarm_set && profile.on) {
String tuyaresponse;
if (tuyaclient->getDps( tuyaresponse)) {
Serial.print("TUYA SAYS DPS: " ); Serial.println(tuyaresponse);
DynamicJsonDocument root(1000);
DeserializationError err = deserializeJson(root, tuyaresponse.c_str());
if (!err) {
if (root.containsKey("dps")) {
if (root["dps"].containsKey("1")) {
int nowon = root["dps"]["1"];
profile.on = nowon;
if (nowon != true) {
profile.alarm_set = false;
}
}
} else {
Serial.print("missing dps on:");
}
} else {
Serial.print("unreadable dps repsonse:");
}
} else {
Serial.println(" failed to get dps response");
}
}
if (!profile.tuya_local_connected) {
long s = time(NULL);
if (tuyaclient->connect(10, profile.tuya_timeout)) {
Serial.print("Reestablished local connection: "); Serial.println(time(NULL) - s);
reconnects++;
profile.tuya_local_connected = true;
} else {
//tuyaclient->reconnect();
}
}
}
delay(1000);
}
}
bool local_tuya_switch(bool on) {
local_tries++;
String tuyaresponse ;
String dps = String("\"dps\":{\"1\":") + (on ? String("true") : String("false}")) + String("}");
Serial.println(dps);
if (tuyaclient->setDps(dps, tuyaresponse)) {
Serial.print("TUYA SAYS DPS: " ); Serial.println(tuyaresponse);
DynamicJsonDocument root(1000);
DeserializationError err = deserializeJson(root, tuyaresponse.c_str());
if (!err) {
if (root.containsKey("dps")) {
if (root["dps"].containsKey("1")) {
int nowon = root["dps"]["1"];
if (nowon != on) {
Serial.println("wrong state");
return false;
}
} else {
Serial.print("missing dps on:");
return false;
}
} else {
Serial.print("unreadable dps repsonse:");
return false;
}
} else {
Serial.println(" failed to get dps response");
return false;
}
}
return true;
}
bool cloud_tuya_switch(bool on) {
cloud_tries++;
String command = String( "{\"commands\":[{\"code\":\"") + profile.tuya_switch + String("\",\"value\":") + (on ? String("true") : String("false")) + String("}]}");
Serial.println(command);
if ( tuya->TCommand(profile.tuya_device_id.c_str(), command.c_str())) {
profile.on = true;
return true;
}
return false;
}
bool smart_tuya_switch(bool on) {
attempts++;
if (profile.led_mode == LED_SMART) {
if (profile.on) {
digitalWrite(LED_PIN, LOW);
} else {
digitalWrite(LED_PIN, HIGH);
}
}
if (tuya) {
Serial.print("**** Switch *****"); Serial.println(on);
if (profile.tuya_localkey.length() > 0 && profile.smart == TUYA_LOCAL && profile.tuya_local_connected) {
if (local_tuya_switch(on)) {
profile.on = on;
return true;
} else {
local_errors++;
profile.tuya_local_connected = false;
if ( cloud_tuya_switch(on)) {
profile.on = on;
return true;
} else {
cloud_errors++;
failsafe_errors++;
}
}
} else {
if ( cloud_tuya_switch(on)) {
profile.on = on;
return true;
} else {
cloud_errors++;
if (profile.tuya_localkey.length() > 0 && profile.tuya_local_connected)
{
if (local_tuya_switch(on)) {
profile.on = on;
return true;
} else {
profile.tuya_local_connected = false;
local_errors++;
failsafe_errors++;
}
}else{
failsafe_errors++;
}
}
}
}
return false;
}
bool smart_tasmota_switch(bool on){
local_tries++;
HTTPClient http;
String command = String("http://")+String(profile.smart_ip)+String("/cm?cmnd=Power%20")+(on?String("On"):String("off"));
Serial.println(command);
http.begin(command.c_str());
int err = http.GET();
if (err == 200) {
profile.on = on;
return true;
}
String body = http.getString();
Serial.println(body);
local_errors++;
failsafe_errors++;
return false;
}
bool smart_tasmota_status(bool &on){
HTTPClient http;
String command = String("http://")+String(profile.smart_ip)+String("/cm?cmnd=Power%20");
http.begin(command.c_str());
int err = http.GET();
String body = http.getString();
Serial.println(body);
if (err == 200) {
if (body.indexOf("ON")!=-1){
on = true;
}else{
on = false;
}
return true;
}
return false;
}
bool smart_switch(bool on){
Serial.println(profile.smart);
if (profile.smart == TASMOTA){
return smart_tasmota_switch(on);
}else{
return smart_tuya_switch(on);
}
}
void writeCurrentProfile() {
char buffer[1000];
Serial.print("WriteCurrentProfile: "); Serial.println(profile.name);
Serial.print("Profile index: "); Serial.println(haveProfile(profile.name));
strcpy(buffer, " {\"t_device_id\":\""); strcat(buffer, profile.tuya_device_id.c_str());
strcat(buffer, "\",\"t_protocol\": \"" ); strcat(buffer, String(profile.tuya_protocol).c_str());
strcat(buffer, "\",\"l_mode\": \"" ); strcat(buffer, String(profile.led_mode).c_str());
strcat(buffer, "\",\"p_string\":\""); strcat(buffer, profile.press_string.c_str());
strcat(buffer, "\",\"name\":\""); strcat(buffer, profile.name.c_str());
strcat(buffer, "\",\"t_api_key\": \""); strcat(buffer, profile.tuya_api_key.c_str());
strcat(buffer, "\",\"t_api_client\": \""); strcat(buffer, profile.tuya_api_client.c_str());
strcat(buffer, "\",\"t_host\": \""); strcat(buffer, profile.tuya_host.c_str());
strcat(buffer, "\",\"t_localkey\": \""); strcat(buffer, profile.tuya_localkey.c_str());
strcat(buffer, "\",\"t_ip\": \""); strcat(buffer, profile.smart_ip.c_str());
strcat(buffer, "\",\"t_mode\": \""); strcat(buffer, String(profile.smart_mode).c_str());
strcat(buffer, "\",\"t_smart\": \""); strcat(buffer, String(profile.smart).c_str());
strcat(buffer, "\",\"t_timeout\": \""); strcat(buffer, String(profile.tuya_timeout).c_str());
strcat(buffer, "\",\"poff_time\" :\""); strcat(buffer, String(profile.power_off_time).c_str());
strcat(buffer, "\",\"repeat_time\" :\""); strcat(buffer, String(profile.repeat_time).c_str());
strcat(buffer, "\",\"pressrelease_time\" :\""); strcat(buffer, String(profile.pressrelease_time).c_str());
strcat(buffer, "\",\"alarm_set\" :\""); strcat(buffer, String(profile.alarm_set).c_str());
strcat(buffer, "\",\"p_mode\": \""); strcat(buffer, String(profile.press_mode).c_str());
strcat(buffer, "\"}");
String filename = String("/") + String(haveProfile(profile.name)) + ".js";
SPIFFS.remove(filename);
File file = SPIFFS.open(filename, FILE_WRITE);
int l = strlen(buffer);
Serial.print("Length: "); Serial.println(l);
Serial.print(buffer);
for (int i = 0; i <= l; i++) {
file.write(buffer[i]);
}
file.close();
}
void writeSettings() {
File file = SPIFFS.open(settings_file, FILE_WRITE);
char buffer[3];
sprintf(buffer, "%d", haveProfile(profile.name));
Serial.print("writeSettings: "); Serial.println(buffer);
for (int i = 0; i <= strlen(buffer); i++) {
file.write(buffer[i]);
}
file.write('\0');
file.close();
}
void saveSettings() {
writeProfiles();
writeSettings();
}
void init_default_profile() {
Serial.println("init profile");
profile.name = String("Default");
profile_count = 1;
profiles[0] = profile.name;
saveSettings();
writeCurrentProfile();
}
bool readSettings(String & buffer) {
if (SPIFFS.exists(settings_file)) {
if ( readFile(settings_file, buffer)) {
Serial.print("Profile in settings: ");
Serial.println(buffer);
int profile_number = strtol(buffer.c_str(), NULL, 10);
buffer = String(profile_number);
Serial.print("Read setting: "); Serial.println(buffer);
return true;
}
}
return false;
}
bool readProfile(int profilenumber, String & buffer) {
Serial.print("read profile: "); Serial.print(buffer);
String name = String("/") + String(profilenumber) + ".js";
if (readFile(name, buffer)) {
return true;
}
return false;
}
void installProfile(int profilenumber) {
String buffer;
readProfile(profilenumber, buffer);
DynamicJsonDocument root(4000);
Serial.println("install profile");
DeserializationError err = deserializeJson(root, buffer);
profile.smart = SMART_NONE;
profile.led_mode = LED_ON_PRESS;
profile.smart_mode = NONE;
profile.press_mode = PRESS_NONE;
if (!err) {
if (root.containsKey("name")) {
profile.name = (const char *) root["name"];
} else Serial.println("missing name");
if (root.containsKey("l_mode")) {
profile.led_mode = root["l_mode"];
} else Serial.println("missing l_mode");
if (root.containsKey("t_protocol")) {
profile.tuya_protocol = (const char *)root["t_protocol"];
} else Serial.println("missing p_mode");
if (root.containsKey("p_mode")) {
profile.press_mode = root["p_mode"];
} else Serial.println("missing p_mode");
if (root.containsKey("t_mode")) {
profile.smart_mode = root["t_mode"];
} else Serial.println("missing t_mode");
if (root.containsKey("t_smart")) {
profile.smart = root["t_smart"];
} else Serial.println("missing t_smart");
if (root.containsKey("t_timeout")) {
profile.tuya_timeout = root["t_timeout"];
} else Serial.println("missing t_local");
if (root.containsKey("repeat_time")) {
profile.repeat_time = root["repeat_time"];
} else Serial.println("missing repeat_time");
if (root.containsKey("pressrelease_time")) {
profile.pressrelease_time = root["pressrelease_time"];
} else Serial.println("missing pressrelease_time");
if (root.containsKey("poff_time")) {
profile.power_off_time = root["poff_time"];
} else Serial.println("missing poff_time");
if (root.containsKey("t_device_id")) {
profile.tuya_device_id = (const char *)root["t_device_id"];
} else Serial.println("missing t_device_id");
if (root.containsKey("t_api_client")) {
profile.tuya_api_client = (const char *)root["t_api_client"];
} else Serial.println("missing t_api_client");
if (root.containsKey("t_host")) {
profile.tuya_host = (const char *)root["t_host"];
} else Serial.println("missing t_host");
if (root.containsKey("t_localkey")) {
profile.tuya_localkey = (const char *)root["t_localkey"];
} else Serial.println("missing t_localkey");
if (root.containsKey("t_ip")) {
profile.smart_ip = (const char *)root["t_ip"];
} else Serial.println("missing t_ip");
if (root.containsKey("t_api_key")) {
profile.tuya_api_key = (const char *)root["t_api_key"];
} else Serial.println("missing t_api_key");
if (root.containsKey("p_string")) {
profile.press_string = (const char *)root["p_string"];
} else Serial.println("missing p_string");
if (root.containsKey("t_device_id")) {
profile.tuya_device_id = (const char *)root["t_device_id"];
} else Serial.println("missing t_device_id");
if (root.containsKey("alarm_set")) {
profile.alarm_set = (const char *)root["alarm_set"];
} else Serial.println("missing t_device_id");
if (profile.name != profiles[profilenumber]) {
Serial.println("profile mismatch");
Serial.print("index: "); Serial.println( profiles[profilenumber]);
Serial.print("json: "); Serial.println( profile.name);
profile.name = profiles[profilenumber];
}
} else {
Serial.println("json failed:");
Serial.println(err.f_str());
Serial.println(buffer);
init_default_profile();
}
}
bool writeProfiles() {
auto file = SPIFFS.open(profiles_file, FILE_WRITE);
Serial.println("Write Profiles");
if (file) {
for (int i = 0; i < profile_count; i++) {
Serial.println(profiles[i]);
for (int j = 0; j < profiles[i].length(); j++) {
file.write(profiles[i][j]);
}
file.write('\n');
}
file.close();
return true;
}
return false;
}
bool readProfiles() {
char buffer[500];
profile_count = 0;
auto file = SPIFFS.open(profiles_file, FILE_READ);
if (file) {
int pos = 0;
while (file.available() && profile_count < MAX_PROFILES) {
const char c = file.read();
if (c != '\n' ) {
buffer[pos++] = c;
} else {
buffer[pos++] = '\0';
profiles[profile_count++] = String(buffer);
Serial.println(profiles[profile_count - 1]);
pos = 0;
}
}
Serial.print("Read profiles :"); Serial.println(profile_count);
}
if (profile_count == 0) {
profile_count = 1;
profiles[0] = "Default";
}
return true;
}
const char style_html[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML><html><head>
<style>
.doton {
height: 400px;
width: 400px;
background-color: #f00;
border-radius: 50%;
display: inline-block;
}
.dotoff {
height: 400px;
width: 400px;
background-color: #000;
border-radius: 50%;
display: inline-block;
}
.form-style-2{
max-width: 800px;
padding: 20px 12px 10px 20px;
font: 13px Arial, Helvetica, sans-serif;
}
.form-style-2-heading{
font-weight: bold;
font-style: italic;
border-bottom: 2px solid #ddd;
margin-bottom: 20px;
font-size: 15px;
padding-bottom: 3px;
}
.form-style-2 label{
display: block;
margin: 0px 0px 15px 0px;
}
.form-style-2 label > span{
width: 170px;
font-weight: bold;
float: left;
padding-top: 8px;
padding-right: 5px;
}
.form-style-2 span.required{
color:red;
.form-style-2 input.input-field, .form-style-2 .select-field{
width: 48%;
}
.form-style-2 input.input-field,
.form-style-2 .select-field{
box-sizing: border-box;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
border: 1px solid #C2C2C2;
box-shadow: 1px 1px 4px #EBEBEB;
-moz-box-shadow: 1px 1px 4px #EBEBEB;
-webkit-box-shadow: 1px 1px 4px #EBEBEB;
border-radius: 3px;
-webkit-border-radius: 3px;
-moz-border-radius: 3px;
padding: 7px;
outline: none;
}
.form-style-2 .input-field:focus,
.form-style-2 .select-field:focus{
border: 1px solid #0C0;
}
.form-style-2 input[type=submit],
.form-style-2 input[type=button]{
border: none;
padding: 8px 15px 8px 15px;
background: #FF8500;
color: #fff;
box-shadow: 1px 1px 4px #DADADA;
-moz-box-shadow: 1px 1px 4px #DADADA;
-webkit-box-shadow: 1px 1px 4px #DADADA;
border-radius: 3px;
-webkit-border-radius: 3px;
-moz-border-radius: 3px;
}
.form-style-2 input[type=submit]:hover,
.form-style-2 input[type=button]:hover{
background: #EA7B00;
color: #fff;
}
</style>
)rawliteral";
const char index_html[] PROGMEM = R"rawliteral(
<title>Big Button Setup</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head><body>
%REBOOT%<br />Status: %STATUS%
<div class="form-style-2">
<div class="form-style-2-heading">Provide your information</div>
<form action="/setting" method="POST">
<label for="profile_list"><span>Profile</span>%PROFILE_LIST%</label>
<label for="profile"><span>Edit Profile Name</span><input name="profile" id="profile" value="%PROFILE%" ></label>
<label for="p_string"><span>Bluetooth Press String</span><input name="p_string" id="p_string" value="%P_STRING%" ></label>
<label for="p_mode"><span>Bluetooth Mode</span><select name="p_mode" id="p_mode" >
<option value="print" %PRESSPRINT_S%>Print</option>
<option value="pressrelease" %PRESSRELEASE_S%>Press and Release</option>
<option value="none" %PRESSNONE_S%>None</option>
</select></label>
<label for="pressrelease_time"><span>Bluetooth Press Time (ms)</span><input id="pressrelease_time" type="number" name="pressrelease_time" value="%PRESSRELEASE_TIME%"></label>
<label for="repeat_time"><span>Repeat Time (ms)</span><input id="repeat_time" type="number" name="repeat_time" value="%REPEAT_TIME%"></label>
<label for="l_mode"><span>Led Mode</span><select name="l_mode" id="l_mode" >
<option value="l_on" %L_ON_S%>Always On
<option value="l_off" %L_OFF_S%>Always Off
<option value="l_on_press" %L_ON_PRESS_S%>On when pressed
<option value="l_off_press" %L_OFF_PRESS_S%>Off when pressed
<option value="l_toggle_press" %L_TOGGLE_PRESS_S%>Toggle when pressed
<option value="l_smart" %L_SMART%>On when socket on
</select></label>
<label for="t_mode"><span>Smart Mode</span><select name="t_mode" id="t_mode" >
<option value="t_on" %T_ON_S%>On
<option value="t_off" %T_OFF_S%>Off
<option value="t_toggle" %T_TOGGLE_S%>Toggle
<option value="t_alarm" %T_ALARM_S%>Alarm (stays on until device turned off)
<option value="t_none" %T_NONE_S%>None
</select></label>
<label for="t_smart"><span>Smart Operation</span><select name="t_smart" id="t_smart" >
<option value="t_none" %T_NONE_S%>None
<option value="t_local" %T_LOCAL_S%>Tuya Local
<option value="t_cloud" %T_CLOUD_S%>Tuya Cloud
<option value="t_tasmota" %T_TASMOTA_S%>Tasmota
</select></label>
<label for="t_timeout)"><span>Tuya Local Timeout (ms)</span><input id="t_timeout" type="number" name="t_timeout" value="%T_TIMEOUT%"></label>
<label for="api_host"><span>Tuya API url</span><input id="api_host" type="text" maxlength="100" name="t_host" value="%T_HOST%"></label>
<label for="api_id"><span>API Client Id</span><input id="api_id" type="text" maxlength="20" name="t_api_client" value="%T_CLIENT%"></label>
<label for="api_key"><span>API Secret Key</span><input id="api_key" type="text" maxlength="32" name="t_api_key" value="%T_APIKEY%"></label>
<label for="device_id"><span>Device Id</span><input id="device_id" type="text" maxlength="32" name="t_device_id" value="%T_DEVICE%"></label>
<label for="localkey"><span>Local Key (system will find this)</span><input id="localkey" type="text" maxlength="32" name="t_localkey" value="%T_LOCALKEY%"></label>
<label for="protocol"><span>Protocol (3.3 or 3.1)</span><input id="protocol" type="text" maxlength="32" name="t_protocol" value="%T_PROTOCOL%"></label>
<label for="ip"><span>ip address</span><input id="ip" type="text" maxlength="32" name="t_ip" value="%T_IP%"></label>
<label for="poff"><span>Power Off (M) 0 for never</span><input id="poff" type="text" maxlength="32" name="poff" value="%POWER_OFF%"></label>
<input type='hidden' name='change' value='' />
<input type="submit" name="action" value="Create Profile"><input type="submit" name="action" value="Delete Profile" onclick="return confirm('Are you sure you want to delete this profile?')"><input type="submit" name="action" value="Submit">
</form>
</div>
</body></html>)rawliteral";
const char root_html[] PROGMEM = R"rawliteral(
<title>Big Button</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head><body>
Status: %STATUS%
<div class="form-style-2">
<div class="form-style-2-heading">Use file following links</div>
<p><a href="/setting">Settings - how do you want Big Button to behave?</a></p>
<p><a href="/login">Login - Login to update Big Button with emailed new firmware?</a></p>
<p><a href="http://%DEVICE%/on">on - turn on</a></p>
<p><a href="http://%DEVICE%/off">off - turn off</a></p>
<p><a href="http://%DEVICE%/status">status - see/set on/off</a></p>
<p><a href="http://%DEVICE%/status">toggle - toggle device</a></p>
<p><a href="http://%DEVICE%/test">test - toggle device every 5 seconds to collect reliability data</a></p>
<p><a href="/export">Export - Allows you to save settings to a file</a></p>
</div></div>
</body></html>)rawliteral";
const char status_html[] PROGMEM = R"rawliteral(
<title>Big Button Status</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head><body>
Status: %STATUS%
<div style="text-align:center">
<a href="http://%DEVICE%/toggle"><span class="%STATUS_CLASS%"></span></a>
</div>
</body></html>)rawliteral";
const char test_html[] PROGMEM = R"rawliteral(
<title>Big Button Test</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head><body>
Status: %STATUS%
<div style="text-align:center">
<a href="http://%DEVICE%/test"><span class="%STATUS_CLASS%"></span></a>
</div>
</body></html>)rawliteral";
String ip2Str(IPAddress ip) {
String s = "";
for (int i = 0; i < 4; i++) {
s += i ? "." + String(ip[i]) : String(ip[i]);
}
return s;
}
int freeMemory() {
return ESP.getFreeHeap();
}
String getStatus() {
static String status;
status = ip2Str(WiFi.localIP());
time_t t = time(NULL);
// time will be offset from 0 until we have synced
if (tuya && t < 99999999) {
status += " : Waiting on real time to sync before tuya can connect. This can occasionally take a couple of minutes : ";
} else if (tuya && tuya->isConnected()) {
status += "<span> : Tuya cloud connected : </span>";
if (profile.tuya_error) {
status += String("<span> : ") + profile.tuya_error_string + String(" : </span>");
}
if (profile.tuya_switch == "") {
status += "<span> : Waiting to get switch information : </span>";
}
} else if (tuya) {
status += "<span> : Tuya not connected : </span>";
}
if (tuya ) {
if (profile.tuya_local_connected) {
status += "<span> : Tuya local connected : </span>";
} else {
status += "<span> : Tuya local not connected : </span>";
}
status += "<span> : Attempts :" + String(attempts) + " : </span>";
status += "<span> : Cloud tries :" + String(cloud_tries) + " : </span>";
status += "<span> : Cloud errors :" + String(cloud_errors) + " : </span>";
status += "<span> : real errors :" + String(failsafe_errors) + " : </span>";
status += "<span> : Local reconnects :" + String(reconnects) + " : </span>";
status += "<span> : Alarm On:" + String(profile.alarm_set) + " : </span>";
}
status += "<span> : Local tries :" + String(local_tries) + " : </span>";