-
-
Notifications
You must be signed in to change notification settings - Fork 476
/
Copy pathcommand.cpp
2189 lines (2115 loc) · 95.7 KB
/
command.cpp
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
/*
command.cpp - ESP3D configuration class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "config.h"
#include "command.h"
#include "wificonf.h"
#include "webinterface.h"
#ifndef FS_NO_GLOBALS
#define FS_NO_GLOBALS
#endif
#include <FS.h>
#if defined(ARDUINO_ARCH_ESP32)
#include "SPIFFS.h"
#define MAX_GPIO 37
int ChannelAttached2Pin[16]= {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};
#else
#define MAX_GPIO 16
#endif
#ifdef TIMESTAMP_FEATURE
#include <time.h>
#endif
#ifdef ESP_OLED_FEATURE
#include "esp_oled.h"
#endif
#ifdef DHT_FEATURE
#include "DHTesp.h"
extern DHTesp dht;
#endif
#ifdef NOTIFICATION_FEATURE
#include "notifications_service.h"
#endif
String COMMAND::buffer_serial;
String COMMAND::buffer_tcp;
#define ERROR_CMD_MSG (output == WEB_PIPE)?F("Error: Wrong Command"):F("Cmd Error")
#define INCORRECT_CMD_MSG (output == WEB_PIPE)?F("Error: Incorrect Command"):F("Incorrect Cmd")
#define OK_CMD_MSG (output == WEB_PIPE)?F("ok"):F("Cmd Ok")
extern uint8_t Checksum(const char * line, uint16_t lineSize);
extern bool sendLine2Serial (String & line, int32_t linenb, int32_t* newlinenb);
const char * encodeString(const char * s){
static String tmp;
tmp = s;
while(tmp.indexOf("'")!=-1)tmp.replace("'", "'");
while(tmp.indexOf("\"")!=-1)tmp.replace("\"", """);
if (tmp =="") tmp=" ";
return tmp.c_str();
}
bool isValidNumber(String str)
{
if(!(str.charAt(0) == '+' || str.charAt(0) == '-' || isDigit(str.charAt(0)))) return false;
for(byte i=1;i<str.length();i++)
{
if(!(isDigit(str.charAt(i)) || str.charAt(i) == '.')) return false;
}
return true;
}
String COMMAND::get_param (String & cmd_params, const char * id, bool withspace)
{
static String parameter;
String sid = id;
int start;
int end = -1;
parameter = "";
//if no id it means it is first part of cmd
if (strlen (id) == 0) {
start = 0;
}
//else find id position
else {
start = cmd_params.indexOf (id);
}
//if no id found and not first part leave
if (start == -1 ) {
return parameter;
}
if (start >0){
if (cmd_params[start-1]!=' ') return parameter;
}
//password and SSID can have space so handle it
//if no space expected use space as delimiter
if (!withspace) {
end = cmd_params.indexOf (" ", start);
}
#ifdef AUTHENTICATION_FEATURE
//if space expected only one parameter but additional password may be present
else if (sid != " pwd=") {
end = cmd_params.indexOf (" pwd=", start);
}
#endif
//if no end found - take all
if (end == -1) {
end = cmd_params.length();
}
//extract parameter
parameter = cmd_params.substring (start + strlen (id), end);
//be sure no extra space
parameter.trim();
return parameter;
}
#ifdef AUTHENTICATION_FEATURE
//check admin password
bool COMMAND::isadmin (String & cmd_params)
{
String adminpassword;
String sadminPassword;
if (!CONFIG::read_string (EP_ADMIN_PWD, sadminPassword, MAX_LOCAL_PASSWORD_LENGTH) ) {
LOG ("ERROR getting admin\r\n")
sadminPassword = FPSTR (DEFAULT_ADMIN_PWD);
}
adminpassword = get_param (cmd_params, "pwd=", true);
if (!sadminPassword.equals (adminpassword) ) {
LOG("Not identified from command line\r\n")
return false;
} else {
return true;
}
}
//check user password - admin password is also valid
bool COMMAND::isuser (String & cmd_params)
{
String userpassword;
String suserPassword;
if (!CONFIG::read_string (EP_USER_PWD, suserPassword, MAX_LOCAL_PASSWORD_LENGTH) ) {
LOG ("ERROR getting user\r\n")
suserPassword = FPSTR (DEFAULT_USER_PWD);
}
userpassword = get_param (cmd_params, "pwd=", true);
//it is not user password
if (!suserPassword.equals (userpassword) ) {
//check admin password
return COMMAND::isadmin (cmd_params);
} else {
return true;
}
}
#endif
bool COMMAND::execute_command (int cmd, String cmd_params, tpipe output, level_authenticate_type auth_level, ESPResponseStream *espresponse)
{
bool response = true;
level_authenticate_type auth_type = auth_level;
(void)auth_type; //avoid warning if updater only
#ifdef AUTHENTICATION_FEATURE
if (isadmin(cmd_params)) {
auth_type = LEVEL_ADMIN;
LOG("you are Admin\r\n");
}
if (isuser (cmd_params) && (auth_type != LEVEL_ADMIN) ) {
auth_type = LEVEL_USER;
LOG("you are User\r\n");
}
#ifdef DEBUG_ESP3D
if ( auth_type == LEVEL_ADMIN) {
LOG("admin identified\r\n");
} else {
if( auth_type == LEVEL_USER) {
LOG("user identified\r\n");
} else {
LOG("guest identified\r\n");
}
}
#endif
#endif
//manage parameters
byte mode = 254;
String parameter;
LOG ("Execute Command\r\n")
switch (cmd) {
//STA SSID
//[ESP100]<SSID>[pwd=<admin password>]
case 100:
parameter = get_param (cmd_params, "", true);
if (!CONFIG::isSSIDValid (parameter.c_str() ) ) {
ESPCOM::println (INCORRECT_CMD_MSG, output, espresponse);
}
#ifdef AUTHENTICATION_FEATURE
if (auth_type != LEVEL_ADMIN) {
ESPCOM::println (INCORRECT_CMD_MSG, output, espresponse);
} else
#endif
if (!CONFIG::write_string (EP_STA_SSID, parameter.c_str() ) ) {
ESPCOM::println (ERROR_CMD_MSG, output, espresponse);
response = false;
} else {
ESPCOM::println (OK_CMD_MSG, output, espresponse);
}
break;
//STA Password
//[ESP101]<Password>[pwd=<admin password>]
case 101:
parameter = get_param (cmd_params, "", true);
if (!CONFIG::isPasswordValid (parameter.c_str() ) ) {
ESPCOM::println (INCORRECT_CMD_MSG, output, espresponse);
response = false;
}
#ifdef AUTHENTICATION_FEATURE
if (auth_type != LEVEL_ADMIN) {
ESPCOM::println (INCORRECT_CMD_MSG, output, espresponse);
response = false;
} else
#endif
if (!CONFIG::write_string (EP_STA_PASSWORD, parameter.c_str() ) ) {
ESPCOM::println (ERROR_CMD_MSG, output, espresponse);
response = false;
} else {
ESPCOM::println (OK_CMD_MSG, output, espresponse);
}
break;
//Hostname
//[ESP102]<hostname>[pwd=<admin password>]
case 102:
parameter = get_param (cmd_params, "", true);
if (!CONFIG::isHostnameValid (parameter.c_str() ) ) {
ESPCOM::println (INCORRECT_CMD_MSG, output, espresponse);
response = false;
}
#ifdef AUTHENTICATION_FEATURE
if (auth_type != LEVEL_ADMIN) {
ESPCOM::println (INCORRECT_CMD_MSG, output, espresponse);
response = false;
} else
#endif
if (!CONFIG::write_string (EP_HOSTNAME, parameter.c_str() ) ) {
ESPCOM::println (ERROR_CMD_MSG, output, espresponse);
response = false;
} else {
ESPCOM::println (OK_CMD_MSG, output, espresponse);
}
break;
//Wifi mode (STA/AP)
//[ESP103]<mode>[pwd=<admin password>]
case 103:
parameter = get_param (cmd_params, "", true);
if (parameter == "STA") {
mode = CLIENT_MODE;
} else if (parameter == "AP") {
mode = AP_MODE;
} else {
ESPCOM::println (INCORRECT_CMD_MSG, output, espresponse);
response = false;
}
if ( (mode == CLIENT_MODE) || (mode == AP_MODE) ) {
#ifdef AUTHENTICATION_FEATURE
if (auth_type != LEVEL_ADMIN) {
ESPCOM::println (INCORRECT_CMD_MSG, output, espresponse);
response = false;
} else
#endif
if (!CONFIG::write_byte (EP_WIFI_MODE, mode) ) {
ESPCOM::println (ERROR_CMD_MSG, output, espresponse);
response = false;
} else {
ESPCOM::println (OK_CMD_MSG, output, espresponse);
}
}
break;
//STA IP mode (DHCP/STATIC)
//[ESP104]<mode>[pwd=<admin password>]
case 104:
parameter = get_param (cmd_params, "", true);
if (parameter == "STATIC") {
mode = STATIC_IP_MODE;
} else if (parameter == "DHCP") {
mode = DHCP_MODE;
} else {
ESPCOM::println (INCORRECT_CMD_MSG, output, espresponse);
response = false;
}
if ( (mode == STATIC_IP_MODE) || (mode == DHCP_MODE) ) {
#ifdef AUTHENTICATION_FEATURE
if (auth_type != LEVEL_ADMIN) {
ESPCOM::println (INCORRECT_CMD_MSG, output, espresponse);
response = false;
} else
#endif
if (!CONFIG::write_byte (EP_STA_IP_MODE, mode) ) {
ESPCOM::println (ERROR_CMD_MSG, output, espresponse);
response = false;
} else {
ESPCOM::println (OK_CMD_MSG, output, espresponse);
}
}
break;
#ifndef USE_AS_UPDATER_ONLY
//AP SSID
//[ESP105]<SSID>[pwd=<admin password>]
case 105:
parameter = get_param (cmd_params, "", true);
if (!CONFIG::isSSIDValid (parameter.c_str() ) ) {
ESPCOM::println (INCORRECT_CMD_MSG, output, espresponse);
response = false;
}
#ifdef AUTHENTICATION_FEATURE
if (auth_type != LEVEL_ADMIN) {
ESPCOM::println (INCORRECT_CMD_MSG, output, espresponse);
response = false;
} else
#endif
if (!CONFIG::write_string (EP_AP_SSID, parameter.c_str() ) ) {
ESPCOM::println (ERROR_CMD_MSG, output, espresponse);
response = false;
} else {
ESPCOM::println (OK_CMD_MSG, output, espresponse);
}
break;
//AP Password
//[ESP106]<Password>[pwd=<admin password>]
case 106:
parameter = get_param (cmd_params, "", true);
if (!CONFIG::isPasswordValid (parameter.c_str() ) ) {
ESPCOM::println (INCORRECT_CMD_MSG, output, espresponse);
response = false;
}
#ifdef AUTHENTICATION_FEATURE
if (auth_type != LEVEL_ADMIN) {
ESPCOM::println (INCORRECT_CMD_MSG, output, espresponse);
response = false;
} else
#endif
if (!CONFIG::write_string (EP_AP_PASSWORD, parameter.c_str() ) ) {
ESPCOM::println (ERROR_CMD_MSG, output, espresponse);
response = false;
} else {
ESPCOM::println (OK_CMD_MSG, output, espresponse);
}
break;
//AP IP mode (DHCP/STATIC)
//[ESP107]<mode>[pwd=<admin password>]
case 107:
parameter = get_param (cmd_params, "", true);
if (parameter == "STATIC") {
mode = STATIC_IP_MODE;
} else if (parameter == "DHCP") {
mode = DHCP_MODE;
} else {
ESPCOM::println (INCORRECT_CMD_MSG, output, espresponse);
response = false;
}
if ( (mode == STATIC_IP_MODE) || (mode == DHCP_MODE) ) {
#ifdef AUTHENTICATION_FEATURE
if (auth_type != LEVEL_ADMIN) {
ESPCOM::println (INCORRECT_CMD_MSG, output, espresponse);
response = false;
} else
#endif
if (!CONFIG::write_byte (EP_AP_IP_MODE, mode) ) {
ESPCOM::println (ERROR_CMD_MSG, output, espresponse);
response = false;
} else {
ESPCOM::println (OK_CMD_MSG, output, espresponse);
}
}
break;
// Set wifi on/off
//[ESP110]<state>[pwd=<admin password>]
case 110:
parameter = get_param (cmd_params, "", true);
if (parameter == "ON") {
mode = 1;
} else if (parameter == "OFF") {
mode = 0;
} else if (parameter == "RESTART") {
mode = 2;
} else {
ESPCOM::println (INCORRECT_CMD_MSG, output, espresponse);
response = false;
}
if (response) {
#ifdef AUTHENTICATION_FEATURE
if (auth_type != LEVEL_ADMIN) {
ESPCOM::println (INCORRECT_CMD_MSG, output, espresponse);
response = false;
} else
#endif
if (mode == 0) {
if ((WiFi.getMode() != WIFI_OFF) || wifi_config.WiFi_on) {
//disable wifi
ESPCOM::println ("Disabling Wifi", output, espresponse);
#ifdef ESP_OLED_FEATURE
OLED_DISPLAY::display_signal(-1);
OLED_DISPLAY::setCursor(0, 0);
ESPCOM::print("", OLED_PIPE);
OLED_DISPLAY::setCursor(0, 16);
ESPCOM::print("", OLED_PIPE);
OLED_DISPLAY::setCursor(0, 48);
ESPCOM::print("Wifi disabled", OLED_PIPE);
#endif
WiFi.disconnect(true);
WiFi.enableSTA (false);
WiFi.enableAP (false);
WiFi.mode (WIFI_OFF);
wifi_config.WiFi_on = false;
wifi_config.Disable_servers();
return response;
} else {
ESPCOM::println ("Wifi already off", output, espresponse);
}
} else if (mode == 1) { //restart device is the best way to start everything clean
if ((WiFi.getMode() == WIFI_OFF) || !wifi_config.WiFi_on) {
ESPCOM::println ("Enabling Wifi", output, espresponse);
web_interface->restartmodule = true;
} else {
ESPCOM::println ("Wifi already on", output, espresponse);
}
} else { //restart wifi and restart is the best way to start everything clean
ESPCOM::println ("Enabling Wifi", output, espresponse);
web_interface->restartmodule = true;
}
}
break;
//Get current IP
//[ESP111]<header answer>
case 111: {
String currentIP ;
if (WiFi.getMode() == WIFI_STA) {
currentIP = WiFi.localIP().toString();
} else {
currentIP = WiFi.softAPIP().toString();
}
ESPCOM::print (cmd_params, output, espresponse);
ESPCOM::println (currentIP, output, espresponse);
LOG (cmd_params)
LOG (currentIP)
LOG ("\r\n")
}
break;
//Get hostname
//[ESP112]<header answer>
case 112: {
String shost ;
if (!CONFIG::read_string (EP_HOSTNAME, shost, MAX_HOSTNAME_LENGTH) ) {
shost = wifi_config.get_default_hostname();
}
ESPCOM::print (cmd_params, output, espresponse);
ESPCOM::println (shost, output, espresponse);
LOG (cmd_params)
LOG (shost)
LOG ("\r\n")
}
break;
#if defined(TIMESTAMP_FEATURE)
//restart time client
case 114: {
CONFIG::init_time_client();
ESPCOM::println (OK_CMD_MSG, output, espresponse);
LOG ("restart time client\r\n")
}
break;
//get time client
case 115: {
struct tm tmstruct;
time_t now;
String stmp = "";
time(&now);
localtime_r(&now, &tmstruct);
stmp = String((tmstruct.tm_year)+1900) + "-";
if (((tmstruct.tm_mon)+1) < 10) {
stmp +="0";
}
stmp += String(( tmstruct.tm_mon)+1) + "-";
if (tmstruct.tm_mday < 10) {
stmp +="0";
}
stmp += String(tmstruct.tm_mday) + " ";
if (tmstruct.tm_hour < 10) {
stmp +="0";
}
stmp += String(tmstruct.tm_hour) + ":";
if (tmstruct.tm_min < 10) {
stmp +="0";
}
stmp += String(tmstruct.tm_min) + ":";
if (tmstruct.tm_sec < 10) {
stmp +="0";
}
stmp += String(tmstruct.tm_sec);
ESPCOM::println(stmp.c_str(), output, espresponse);
}
break;
#endif
#ifdef DIRECT_PIN_FEATURE
//Get/Set pin value
//[ESP201]P<pin> V<value> [PULLUP=YES RAW=YES ANALOG=NO ANALOG_RANGE=255 CLEARCHANNELS=NO]pwd=<admin password>
case 201:
parameter = get_param (cmd_params, "", true);
#ifdef AUTHENTICATION_FEATURE
if (auth_type == LEVEL_GUEST) {
ESPCOM::println (INCORRECT_CMD_MSG, output, espresponse);
response = false;
} else
#endif
{
//check if have pin
parameter = get_param (cmd_params, "P", false);
LOG ("Pin:")
LOG (parameter)
LOG ("\r\n")
if (parameter == "") {
ESPCOM::println (INCORRECT_CMD_MSG, output, espresponse);
response = false;
} else {
int pin = parameter.toInt();
//check pin is valid
if ((pin >= 0) && (pin <= MAX_GPIO) && isValidNumber(parameter)) {
//check if analog or digital
bool isdigital = true;
parameter = get_param (cmd_params, "ANALOG=", false);
if (parameter == "YES") {
LOG ("Set as analog\r\n")
isdigital=false;
#ifdef ARDUINO_ARCH_ESP32
parameter = get_param (cmd_params, "CLEARCHANNELS=", false);
if (parameter == "YES") {
for (uint8_t p = 0; p < 16; p++) {
if(ChannelAttached2Pin[p] != -1) {
ledcDetachPin(ChannelAttached2Pin[p]);
ChannelAttached2Pin[p] = -1;
}
}
}
#endif
}
//check if is set or get
parameter = get_param (cmd_params, "V", false);
//it is a get
if (parameter == "") {
int value = 0;
if(isdigital) {
//this is to not set pin mode
parameter = get_param (cmd_params, "RAW=", false);
if (parameter == "NO") {
parameter = get_param (cmd_params, "PULLUP=", false);
if (parameter == "NO") {
LOG ("Set as input\r\n")
pinMode (pin, INPUT);
} else {
//GPIO16 is different than others
if (pin < MAX_GPIO) {
LOG ("Set as input pull up\r\n")
pinMode (pin, INPUT_PULLUP);
}
#ifdef ARDUINO_ARCH_ESP8266
else {
LOG ("Set as input pull down 16\r\n")
pinMode (pin, INPUT_PULLDOWN_16);
}
#endif
}
}
value = digitalRead (pin);
} else {
#ifdef ARDUINO_ARCH_ESP8266 //only one ADC on ESP8266 A0
value = analogRead (A0);
#else
value = analogRead (pin);
#endif
}
LOG ("Read:");
LOG (String (value).c_str() )
LOG ("\n");
ESPCOM::println (String (value).c_str(), output, espresponse);
} else {
//it is a set
int value = parameter.toInt();
if (!isValidNumber(parameter)){
ESPCOM::println (INCORRECT_CMD_MSG, output, espresponse);
response = false;
break;
}
if (isdigital) {
//verify it is a 0 or a 1
if ( (value == 0) || (value == 1) ) {
pinMode (pin, OUTPUT);
LOG ("Set:")
LOG (String ( (value == 0) ? LOW : HIGH) )
LOG ("\r\n")
digitalWrite (pin, (value == 0) ? LOW : HIGH);
ESPCOM::println (OK_CMD_MSG, output, espresponse);
} else {
ESPCOM::println (INCORRECT_CMD_MSG, output, espresponse);
response = false;
}
} else {
int analog_range= 255;
parameter = get_param (cmd_params, "ANALOG_RANGE=", false);
if (parameter.length() > 0) {
analog_range = parameter.toInt();
}
LOG ("Range ")
LOG(String (analog_range).c_str() )
LOG ("\r\n")
if ( (value >= 0) || (value <= analog_range+1) ) {
LOG ("Set:")
LOG (String ( value) )
LOG ("\r\n")
#ifdef ARDUINO_ARCH_ESP8266
analogWriteRange(analog_range);
pinMode(pin, OUTPUT);
analogWrite(pin, value);
#else
int channel = -1;
for (uint8_t p = 0; p < 16; p++) {
if(ChannelAttached2Pin[p] == pin) {
channel = p;
}
}
if (channel==-1) {
for (uint8_t p = 0; p < 16; p++) {
if(ChannelAttached2Pin[p] == -1) {
channel = p;
ChannelAttached2Pin[p] = pin;
p = 16;
}
}
}
uint8_t resolution = 0;
analog_range++;
switch(analog_range) {
case 8191:
resolution=13;
break;
case 1024:
resolution=10;
break;
case 2047:
resolution=11;
break;
case 4095:
resolution=12;
break;
default:
resolution=8;
analog_range = 255;
break;
}
if ((channel==-1) || (value > (analog_range-1))) {
ESPCOM::println (INCORRECT_CMD_MSG, output, espresponse);
return false;
}
ledcSetup(channel, 1000, resolution);
ledcAttachPin(pin, channel);
ledcWrite(channel, value);
#endif
} else {
ESPCOM::println (INCORRECT_CMD_MSG, output, espresponse);
response = false;
}
}
}
} else {
ESPCOM::println (INCORRECT_CMD_MSG, output, espresponse);
response = false;
}
}
}
break;
#endif
#ifdef ESP_OLED_FEATURE
//Output to oled
//[ESP210]<Text>
case 210: {
parameter = get_param (cmd_params, "C=", false);
int c = parameter.toInt();
parameter = get_param (cmd_params, "L=", false);
int l = parameter.toInt();
parameter = get_param (cmd_params, "T=", true);
OLED_DISPLAY::setCursor(c, l);
ESPCOM::print(parameter.c_str(), OLED_PIPE);
ESPCOM::println (OK_CMD_MSG, output, espresponse);
}
break;
//Output to oled line 1
//[ESP211]<Text>
case 211: {
parameter = get_param (cmd_params, "", true);
OLED_DISPLAY::setCursor(0, 0);
ESPCOM::print(parameter.c_str(), OLED_PIPE);
ESPCOM::println (OK_CMD_MSG, output, espresponse);
}
break;
//Output to oled line 2
//[ESP212]<Text>
case 212: {
parameter = get_param (cmd_params, "", true);
OLED_DISPLAY::setCursor(0, 16);
ESPCOM::print(parameter.c_str(), OLED_PIPE);
ESPCOM::println (OK_CMD_MSG, output, espresponse);
}
break;
//Output to oled line 3
//[ESP213]<Text>
case 213: {
parameter = get_param (cmd_params, "", true);
OLED_DISPLAY::setCursor(0, 32);
ESPCOM::print(parameter.c_str(), OLED_PIPE);
ESPCOM::println (OK_CMD_MSG, output, espresponse);
}
break;
//Output to oled line 4
//[ESP214]<Text>
case 214: {
parameter = get_param (cmd_params, "", true);
OLED_DISPLAY::setCursor(0, 48);
ESPCOM::print(parameter.c_str(), OLED_PIPE);
ESPCOM::println (OK_CMD_MSG, output, espresponse);
}
break;
#endif
//Command delay
case 290: {
parameter = get_param (cmd_params, "", true);
#ifdef AUTHENTICATION_FEATURE
if (auth_type == LEVEL_GUEST) {
ESPCOM::println (INCORRECT_CMD_MSG, output, espresponse);
response = false;
} else
#endif
{
if (parameter.length() != 0) {
ESPCOM::println ("Pause", output, espresponse);
CONFIG::wait(parameter.toInt());
}
ESPCOM::println (OK_CMD_MSG, output, espresponse);
}
}
break;
//display ESP3D EEPROM version detected
case 300: {
uint8_t v = CONFIG::get_EEPROM_version();
ESPCOM::println (String(v).c_str(), output, espresponse);
}
break;
//Get full EEPROM settings content
//[ESP400]
case 400: {
char sbuf[MAX_DATA_LENGTH + 1];
uint8_t ipbuf[4];
byte bbuf = 0;
int ibuf = 0;
parameter = get_param (cmd_params, "", true);
//Start JSON
ESPCOM::println (F ("{\"EEPROM\":["), output, espresponse);
if (cmd_params == "network" || cmd_params == "") {
//1- Baud Rate
ESPCOM::print (F ("{\"F\":\"network\",\"P\":\""), output, espresponse);
ESPCOM::print ( (const char *) CONFIG::intTostr (EP_BAUD_RATE), output, espresponse);
ESPCOM::print (F ("\",\"T\":\"I\",\"V\":\""), output, espresponse);
if (!CONFIG::read_buffer (EP_BAUD_RATE, (byte *) &ibuf, INTEGER_LENGTH) ) {
ESPCOM::print ("???", output, espresponse);
} else {
ESPCOM::print ( (const char *) CONFIG::intTostr (ibuf), output, espresponse);
}
ESPCOM::print (F ("\",\"H\":\"Baud Rate\",\"O\":[{\"9600\":\"9600\"},{\"19200\":\"19200\"},{\"38400\":\"38400\"},{\"57600\":\"57600\"},{\"115200\":\"115200\"},{\"230400\":\"230400\"},{\"250000\":\"250000\"},{\"500000\":\"500000\"},{\"921600\":\"921600\"}]}"), output, espresponse);
ESPCOM::println (F (","), output, espresponse);
//2-Sleep Mode
ESPCOM::print (F ("{\"F\":\"network\",\"P\":\""), output, espresponse);
ESPCOM::print ( (const char *) CONFIG::intTostr (EP_SLEEP_MODE), output, espresponse);
ESPCOM::print (F ("\",\"T\":\"B\",\"V\":\""), output, espresponse);
if (!CONFIG::read_byte (EP_SLEEP_MODE, &bbuf ) ) {
ESPCOM::print ("???", output, espresponse);
} else {
ESPCOM::print ( (const char *) CONFIG::intTostr (bbuf), output, espresponse);
}
ESPCOM::print (F ("\",\"H\":\"Sleep Mode\",\"O\":[{\"None\":\""), output, espresponse);
ESPCOM::print ( (const char *) CONFIG::intTostr (WIFI_NONE_SLEEP), output, espresponse);
ESPCOM::print (F ("\"},{\"Light\":\""), output, espresponse);
ESPCOM::print ( (const char *) CONFIG::intTostr (WIFI_LIGHT_SLEEP), output, espresponse);
ESPCOM::print (F ("\"},{\"Modem\":\""), output, espresponse);
ESPCOM::print ( (const char *) CONFIG::intTostr (WIFI_MODEM_SLEEP), output, espresponse);
ESPCOM::print (F ("\"}]}"), output, espresponse);
ESPCOM::println (F (","), output, espresponse);
//3-Web Port
ESPCOM::print (F ("{\"F\":\"network\",\"P\":\""), output, espresponse);
ESPCOM::print ( (const char *) CONFIG::intTostr (EP_WEB_PORT), output, espresponse);
ESPCOM::print (F ("\",\"T\":\"I\",\"V\":\""), output, espresponse);
if (!CONFIG::read_buffer (EP_WEB_PORT, (byte *) &ibuf, INTEGER_LENGTH) ) {
ESPCOM::print ("???", output, espresponse);
} else {
ESPCOM::print ( (const char *) CONFIG::intTostr (ibuf), output, espresponse);
}
ESPCOM::print (F ("\",\"H\":\"Web Port\",\"S\":\""), output, espresponse);
ESPCOM::print ( (const char *) CONFIG::intTostr (DEFAULT_MAX_WEB_PORT), output, espresponse);
ESPCOM::print (F ("\",\"M\":\""), output, espresponse);
ESPCOM::print ( (const char *) CONFIG::intTostr (DEFAULT_MIN_WEB_PORT), output, espresponse);
ESPCOM::print (F ("\"}"), output, espresponse);
ESPCOM::println (F (","), output, espresponse);
//4-Data Port
ESPCOM::print (F ("{\"F\":\"network\",\"P\":\""), output, espresponse);
ESPCOM::print ( (const char *) CONFIG::intTostr (EP_DATA_PORT), output, espresponse);
ESPCOM::print (F ("\",\"T\":\"I\",\"V\":\""), output, espresponse);
if (!CONFIG::read_buffer (EP_DATA_PORT, (byte *) &ibuf, INTEGER_LENGTH) ) {
ESPCOM::print ("???", output, espresponse);
} else {
ESPCOM::print ( (const char *) CONFIG::intTostr (ibuf), output, espresponse);
}
ESPCOM::print (F ("\",\"H\":\"Data Port\",\"S\":\""), output, espresponse);
ESPCOM::print ( (const char *) CONFIG::intTostr (DEFAULT_MAX_DATA_PORT), output, espresponse);
ESPCOM::print (F ("\",\"M\":\""), output, espresponse);
ESPCOM::print ( (const char *) CONFIG::intTostr (DEFAULT_MIN_DATA_PORT), output, espresponse);
ESPCOM::print (F ("\"}"), output, espresponse);
ESPCOM::println (F (","), output, espresponse);
#ifdef AUTHENTICATION_FEATURE
//5-Admin password
ESPCOM::print (F ("{\"F\":\"network\",\"P\":\""), output, espresponse);
ESPCOM::print ( (const char *) CONFIG::intTostr (EP_ADMIN_PWD), output, espresponse);
ESPCOM::print (F ("\",\"T\":\"S\",\"V\":\""), output, espresponse);
if (!CONFIG::read_string (EP_ADMIN_PWD, sbuf, MAX_LOCAL_PASSWORD_LENGTH) ) {
ESPCOM::print ("???", output, espresponse);
} else {
ESPCOM::print ("********", output, espresponse);
}
ESPCOM::print (F ("\",\"S\":\""), output, espresponse);
ESPCOM::print ( (const char *) CONFIG::intTostr (MAX_LOCAL_PASSWORD_LENGTH), output, espresponse);
ESPCOM::print (F ("\",\"H\":\"Admin Password\",\"M\":\""), output, espresponse);
ESPCOM::print ( (const char *) CONFIG::intTostr (MIN_LOCAL_PASSWORD_LENGTH), output, espresponse);
ESPCOM::print (F ("\"}"), output, espresponse);
ESPCOM::println (F (","), output, espresponse);
//6-User password
ESPCOM::print (F ("{\"F\":\"network\",\"P\":\""), output, espresponse);
ESPCOM::print ( (const char *) CONFIG::intTostr (EP_USER_PWD), output, espresponse);
ESPCOM::print (F ("\",\"T\":\"S\",\"V\":\""), output, espresponse);
if (!CONFIG::read_string (EP_USER_PWD, sbuf, MAX_LOCAL_PASSWORD_LENGTH) ) {
ESPCOM::print ("???", output, espresponse);
} else {
ESPCOM::print ("********", output, espresponse);
}
ESPCOM::print (F ("\",\"S\":\""), output, espresponse);
ESPCOM::print ( (const char *) CONFIG::intTostr (MAX_LOCAL_PASSWORD_LENGTH), output, espresponse);
ESPCOM::print (F ("\",\"H\":\"User Password\",\"M\":\""), output, espresponse);
ESPCOM::print ( (const char *) CONFIG::intTostr (MIN_LOCAL_PASSWORD_LENGTH), output, espresponse);
ESPCOM::print (F ("\"}"), output, espresponse);
ESPCOM::println (F (","), output, espresponse);
#endif
//7-Hostname
ESPCOM::print (F ("{\"F\":\"network\",\"P\":\""), output, espresponse);
ESPCOM::print ( (const char *) CONFIG::intTostr (EP_HOSTNAME), output, espresponse);
ESPCOM::print (F ("\",\"T\":\"S\",\"V\":\""), output, espresponse);
if (!CONFIG::read_string (EP_HOSTNAME, sbuf, MAX_HOSTNAME_LENGTH) ) {
ESPCOM::print ("???", output, espresponse);
} else {
ESPCOM::print (encodeString(sbuf), output, espresponse);
}
ESPCOM::print (F ("\",\"H\":\"Hostname\" ,\"S\":\""), output, espresponse);
ESPCOM::print ( (const char *) CONFIG::intTostr (MAX_HOSTNAME_LENGTH), output, espresponse);
ESPCOM::print (F ("\", \"M\":\""), output, espresponse);
ESPCOM::print ( (const char *) CONFIG::intTostr (MIN_HOSTNAME_LENGTH), output, espresponse);
ESPCOM::print (F ("\"}"), output, espresponse);
ESPCOM::println (F (","), output, espresponse);
//8-wifi mode
ESPCOM::print (F ("{\"F\":\"network\",\"P\":\""), output, espresponse);
ESPCOM::print ( (const char *) CONFIG::intTostr (EP_WIFI_MODE), output, espresponse);
ESPCOM::print (F ("\",\"T\":\"B\",\"V\":\""), output, espresponse);
if (!CONFIG::read_byte (EP_WIFI_MODE, &bbuf ) ) {
ESPCOM::print ("???", output, espresponse);
} else {
ESPCOM::print ( (const char *) CONFIG::intTostr (bbuf), output, espresponse);
}
ESPCOM::print (F ("\",\"H\":\"Wifi mode\",\"O\":[{\"AP\":\"1\"},{\"STA\":\"2\"}]}"), output, espresponse);
ESPCOM::println (F (","), output, espresponse);
//9-STA SSID
ESPCOM::print (F ("{\"F\":\"network\",\"P\":\""), output, espresponse);
ESPCOM::print ( (const char *) CONFIG::intTostr (EP_STA_SSID), output, espresponse);
ESPCOM::print (F ("\",\"T\":\"S\",\"V\":\""), output, espresponse);
if (!CONFIG::read_string (EP_STA_SSID, sbuf, MAX_SSID_LENGTH) ) {
ESPCOM::print ("???", output, espresponse);
} else {
ESPCOM::print (encodeString(sbuf), output, espresponse);
}
ESPCOM::print (F ("\",\"S\":\""), output, espresponse);
ESPCOM::print ( (const char *) CONFIG::intTostr (MAX_SSID_LENGTH), output, espresponse);
ESPCOM::print (F ("\",\"H\":\"Station SSID\",\"M\":\""), output, espresponse);
ESPCOM::print ( (const char *) CONFIG::intTostr (MIN_SSID_LENGTH), output, espresponse);
ESPCOM::print (F ("\"}"), output, espresponse);
ESPCOM::println (F (","), output, espresponse);
//10-STA password
ESPCOM::print (F ("{\"F\":\"network\",\"P\":\""), output, espresponse);
ESPCOM::print ( (const char *) CONFIG::intTostr (EP_STA_PASSWORD), output, espresponse);
ESPCOM::print (F ("\",\"T\":\"S\",\"V\":\""), output, espresponse);
if (!CONFIG::read_string (EP_STA_PASSWORD, sbuf, MAX_PASSWORD_LENGTH) ) {
ESPCOM::print ("???", output, espresponse);
} else {
ESPCOM::print ("********", output, espresponse);
}
ESPCOM::print (F ("\",\"S\":\""), output, espresponse);
ESPCOM::print ( (const char *) CONFIG::intTostr (MAX_PASSWORD_LENGTH), output, espresponse);
ESPCOM::print (F ("\",\"H\":\"Station Password\",\"M\":\""), output, espresponse);
ESPCOM::print ( (const char *) CONFIG::intTostr (MIN_PASSWORD_LENGTH), output, espresponse);
ESPCOM::print (F ("\"}"), output, espresponse);
ESPCOM::println (F (","), output, espresponse);
//11-Station Network Mode
ESPCOM::print (F ("{\"F\":\"network\",\"P\":\""), output, espresponse);
ESPCOM::print ( (const char *) CONFIG::intTostr (EP_STA_PHY_MODE), output, espresponse);
ESPCOM::print (F ("\",\"T\":\"B\",\"V\":\""), output, espresponse);
if (!CONFIG::read_byte (EP_STA_PHY_MODE, &bbuf ) ) {
ESPCOM::print ("???", output, espresponse);
} else {
ESPCOM::print ( (const char *) CONFIG::intTostr (bbuf), output, espresponse);
}
ESPCOM::print (F ("\",\"H\":\"Station Network Mode\",\"O\":[{\"11b\":\""), output, espresponse);
ESPCOM::print ( (const char *) CONFIG::intTostr (WIFI_PHY_MODE_11B), output, espresponse);
ESPCOM::print (F ("\"},{\"11g\":\""), output, espresponse);
ESPCOM::print ( (const char *) CONFIG::intTostr (WIFI_PHY_MODE_11G), output, espresponse);
ESPCOM::print (F ("\"},{\"11n\":\""), output, espresponse);
ESPCOM::print ( (const char *) CONFIG::intTostr (WIFI_PHY_MODE_11N), output, espresponse);
ESPCOM::print (F ("\"}]}"), output, espresponse);
ESPCOM::println (F (","), output, espresponse);
//12-STA IP mode
ESPCOM::print (F ("{\"F\":\"network\",\"P\":\""), output, espresponse);
ESPCOM::print ( (const char *) CONFIG::intTostr (EP_STA_IP_MODE), output, espresponse);
ESPCOM::print (F ("\",\"T\":\"B\",\"V\":\""), output, espresponse);
if (!CONFIG::read_byte (EP_STA_IP_MODE, &bbuf ) ) {
ESPCOM::print ("???", output, espresponse);
} else {
ESPCOM::print ( (const char *) CONFIG::intTostr (bbuf), output, espresponse);
}
ESPCOM::print (F ("\",\"H\":\"Station IP Mode\",\"O\":[{\"DHCP\":\"1\"},{\"Static\":\"2\"}]}"), output, espresponse);
ESPCOM::println (F (","), output, espresponse);
//13-STA static IP
ESPCOM::print (F ("{\"F\":\"network\",\"P\":\""), output, espresponse);
ESPCOM::print ( (const char *) CONFIG::intTostr (EP_STA_IP_VALUE), output, espresponse);
ESPCOM::print (F ("\",\"T\":\"A\",\"V\":\""), output, espresponse);
if (!CONFIG::read_buffer (EP_STA_IP_VALUE, (byte *) ipbuf, IP_LENGTH) ) {
ESPCOM::print ("???", output, espresponse);
} else {
ESPCOM::print (IPAddress (ipbuf).toString().c_str(), output, espresponse);
}
ESPCOM::print (F ("\",\"H\":\"Station Static IP\"}"), output, espresponse);
ESPCOM::println (F (","), output, espresponse);
//14-STA static Mask
ESPCOM::print (F ("{\"F\":\"network\",\"P\":\""), output, espresponse);
ESPCOM::print ( (const char *) CONFIG::intTostr (EP_STA_MASK_VALUE), output, espresponse);
ESPCOM::print (F ("\",\"T\":\"A\",\"V\":\""), output, espresponse);
if (!CONFIG::read_buffer (EP_STA_MASK_VALUE, (byte *) ipbuf, IP_LENGTH) ) {
ESPCOM::print ("???", output, espresponse);
} else {
ESPCOM::print (IPAddress (ipbuf).toString().c_str(), output, espresponse);
}
ESPCOM::print (F ("\",\"H\":\"Station Static Mask\"}"), output, espresponse);
ESPCOM::println (F (","), output, espresponse);
//15-STA static Gateway
ESPCOM::print (F ("{\"F\":\"network\",\"P\":\""), output, espresponse);
ESPCOM::print ( (const char *) CONFIG::intTostr (EP_STA_GATEWAY_VALUE), output, espresponse);
ESPCOM::print (F ("\",\"T\":\"A\",\"V\":\""), output, espresponse);
if (!CONFIG::read_buffer (EP_STA_GATEWAY_VALUE, (byte *) ipbuf, IP_LENGTH) ) {
ESPCOM::print ("???", output, espresponse);
} else {
ESPCOM::print (IPAddress (ipbuf).toString().c_str(), output, espresponse);
}
ESPCOM::print (F ("\",\"H\":\"Station Static Gateway\"}"), output, espresponse);
ESPCOM::println (F (","), output, espresponse);
//16-AP SSID
ESPCOM::print (F ("{\"F\":\"network\",\"P\":\""), output, espresponse);
ESPCOM::print ( (const char *) CONFIG::intTostr (EP_AP_SSID), output, espresponse);
ESPCOM::print (F ("\",\"T\":\"S\",\"V\":\""), output, espresponse);
if (!CONFIG::read_string (EP_AP_SSID, sbuf, MAX_SSID_LENGTH) ) {