2018-10-10 21:21:44 +01:00
/*
xdrv_01_webserver . ino - webserver for Sonoff - Tasmota
2019-01-01 12:55:01 +00:00
Copyright ( C ) 2019 Theo Arends
2018-10-10 21:21:44 +01:00
This program is free software : you can redistribute it and / or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
This program is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU General Public License for more details .
You should have received a copy of the GNU General Public License
along with this program . If not , see < http : //www.gnu.org/licenses/>.
*/
# ifdef USE_WEBSERVER
/*********************************************************************************************\
* Web server and WiFi Manager
*
* Enables configuration and reconfiguration of WiFi credentials using a Captive Portal
* Based on source by AlexT ( https : //github.com/tzapu)
\ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2018-11-26 16:00:18 +00:00
# define XDRV_01 1
2018-11-07 09:30:03 +00:00
2018-12-22 15:13:07 +00:00
# ifndef WIFI_SOFT_AP_CHANNEL
2019-03-31 10:59:04 +01:00
# define WIFI_SOFT_AP_CHANNEL 1 // Soft Access Point Channel number between 1 and 11 as used by SmartConfig web GUI
2018-12-22 15:13:07 +00:00
# endif
2019-03-31 10:59:04 +01:00
const uint16_t CHUNKED_BUFFER_SIZE = 400 ; // Chunk buffer size (should be smaller than half mqtt_date size)
const uint16_t HTTP_REFRESH_TIME = 2345 ; // milliseconds
# define HTTP_RESTART_RECONNECT_TIME 9000 // milliseconds
# define HTTP_OTA_RESTART_RECONNECT_TIME 20000 // milliseconds
2018-10-10 21:21:44 +01:00
2018-11-26 16:00:18 +00:00
# include <ESP8266WebServer.h>
# include <DNSServer.h>
2018-11-19 17:07:25 +00:00
2018-10-10 21:21:44 +01:00
# ifdef USE_RF_FLASH
2019-03-26 17:26:50 +00:00
uint8_t * efm8bb1_update = nullptr ;
2018-10-10 21:21:44 +01:00
# endif // USE_RF_FLASH
enum UploadTypes { UPL_TASMOTA , UPL_SETTINGS , UPL_EFM8BB1 } ;
const char HTTP_HEAD [ ] PROGMEM =
" <!DOCTYPE html><html lang= \" " D_HTML_LANGUAGE " \" class= \" \" > "
" <head> "
" <meta charset='utf-8'> "
" <meta name= \" viewport \" content= \" width=device-width,initial-scale=1,user-scalable=no \" /> "
2019-03-04 17:16:07 +00:00
" <title>%s - %s</title> "
2018-10-10 21:21:44 +01:00
" <script> "
2019-02-18 10:35:49 +00:00
" var x=null,lt,to,tp,pc=''; " // x=null allow for abortion
2018-10-10 21:21:44 +01:00
" function eb(s){ "
" return document.getElementById(s); " // Save code space
2018-11-10 16:30:23 +00:00
" } " ;
const char HTTP_SCRIPT_COUNTER [ ] PROGMEM =
2019-02-18 10:35:49 +00:00
" var cn=180; " // seconds
2018-10-10 21:21:44 +01:00
" function u(){ "
" if(cn>=0){ "
" eb('t').innerHTML=' " D_RESTART_IN " '+cn+' " D_SECONDS " '; "
" cn--; "
" setTimeout(u,1000); "
" } "
" } "
2019-03-04 17:16:07 +00:00
" window.onload=u; " ;
2018-11-10 16:30:23 +00:00
const char HTTP_SCRIPT_ROOT [ ] PROGMEM =
2018-10-10 21:21:44 +01:00
" function la(p){ "
" var a=''; "
" if(la.arguments.length==1){ "
" a=p; "
" clearTimeout(lt); "
" } "
2019-03-04 17:16:07 +00:00
" if(x!=null){x.abort();} " // Abort if no response within 2 seconds (happens on restart 1)
2018-10-10 21:21:44 +01:00
" x=new XMLHttpRequest(); "
" x.onreadystatechange=function(){ "
" if(x.readyState==4&&x.status==200){ "
2019-03-04 17:16:07 +00:00
" var s=x.responseText.replace(/{t}/g, \" <table style='width:100%%'> \" ).replace(/{s}/g, \" <tr><th> \" ).replace(/{m}/g, \" </th><td> \" ).replace(/{e}/g, \" </td></tr> \" ).replace(/{c}/g, \" %%'><div style='text-align:center;font-weight: \" ); "
2018-10-10 21:21:44 +01:00
" eb('l1').innerHTML=s; "
" } "
" }; "
2019-03-04 11:36:44 +00:00
" x.open('GET','.?m=1'+a,true); " // ?m related to WebServer->hasArg("m")
2018-10-10 21:21:44 +01:00
" x.send(); "
2019-03-04 17:16:07 +00:00
" lt=setTimeout(la,%d); " // Settings.web_refresh
2018-10-10 21:21:44 +01:00
" } "
" function lb(p){ "
2019-03-01 17:25:46 +00:00
" la('&d='+p); " // &d related to WebGetArg("d", tmp, sizeof(tmp));
2018-10-10 21:21:44 +01:00
" } "
" function lc(p){ "
2019-03-01 17:25:46 +00:00
" la('&t='+p); " // &t related to WebGetArg("t", tmp, sizeof(tmp));
2019-02-17 10:32:53 +00:00
" } "
2019-02-17 13:43:07 +00:00
" window.onload=la(); " ;
2018-11-10 15:45:32 +00:00
2018-11-10 16:30:23 +00:00
const char HTTP_SCRIPT_WIFI [ ] PROGMEM =
" function c(l){ "
" eb('s1').value=l.innerText||l.textContent; "
" eb('p1').focus(); "
" } " ;
2018-10-10 21:21:44 +01:00
2018-11-10 16:30:23 +00:00
const char HTTP_SCRIPT_RELOAD [ ] PROGMEM =
2019-03-04 11:36:44 +00:00
" setTimeout(function(){location.href='.';}, " STR ( HTTP_RESTART_RECONNECT_TIME ) " ); " ;
2018-11-24 10:46:32 +00:00
// Local OTA upgrade requires more time to complete cp: before web ui should be reloaded
const char HTTP_SCRIPT_RELOAD_OTA [ ] PROGMEM =
2019-03-04 11:36:44 +00:00
" setTimeout(function(){location.href='.';}, " STR ( HTTP_OTA_RESTART_RECONNECT_TIME ) " ); " ;
2018-10-10 21:21:44 +01:00
const char HTTP_SCRIPT_CONSOL [ ] PROGMEM =
2019-02-18 10:35:49 +00:00
" var sn=0; " // Scroll position
" var id=0; " // Get most of weblog initially
" function l(p){ " // Console log and command service
2018-10-10 21:21:44 +01:00
" var c,o,t; "
" clearTimeout(lt); "
" o=''; "
" t=eb('t1'); "
" if(p==1){ "
" c=eb('c1'); "
" o='&c1='+encodeURIComponent(c.value); "
" c.value=''; "
" t.scrollTop=sn; "
" } "
2019-02-18 10:35:49 +00:00
" if(t.scrollTop>=sn){ " // User scrolled back so no updates
" if(x!=null){x.abort();} " // Abort if no response within 2 seconds (happens on restart 1)
2018-10-10 21:21:44 +01:00
" x=new XMLHttpRequest(); "
" x.onreadystatechange=function(){ "
" if(x.readyState==4&&x.status==200){ "
" var z,d; "
2019-03-04 17:16:07 +00:00
" d=x.responseText.split(/}1/); " // Field separator
2019-02-07 19:41:38 +00:00
" id=d.shift(); "
" if(d.shift()==0){t.value='';} "
" z=d.shift(); "
" if(z.length>0){t.value+=z;} "
2018-10-10 21:21:44 +01:00
" t.scrollTop=99999; "
" sn=t.scrollTop; "
" } "
" }; "
2019-03-01 17:25:46 +00:00
" x.open('GET','cs?c2='+id+o,true); " // Related to WebServer->hasArg("c2") and WebGetArg("c2", stmp, sizeof(stmp))
2018-10-10 21:21:44 +01:00
" x.send(); "
" } "
2019-03-04 17:16:07 +00:00
" lt=setTimeout(l,%d); "
2018-10-10 21:21:44 +01:00
" return false; "
" } "
2019-03-01 11:08:41 +00:00
" window.onload=l; " ;
2019-02-17 10:32:53 +00:00
2019-02-24 14:05:18 +00:00
const char HTTP_MODULE_TEMPLATE_REPLACE [ ] PROGMEM =
2019-03-04 17:16:07 +00:00
" }2%d'>%s (%d}3 " ; // }2 and }3 are used in below os.replace
2019-02-24 14:05:18 +00:00
2019-02-17 10:32:53 +00:00
const char HTTP_SCRIPT_MODULE_TEMPLATE [ ] PROGMEM =
2018-10-10 21:21:44 +01:00
" var os; "
2019-02-18 10:35:49 +00:00
" function sk(s,g){ " // s = value, g = id and name
2019-03-04 17:16:07 +00:00
" var o=os.replace(/}2/g, \" <option value=' \" ).replace(/}3/g, \" )</option> \" ); "
2018-10-10 21:21:44 +01:00
" eb('g'+g).innerHTML=o; "
2019-02-17 13:43:07 +00:00
" eb('g'+g).value=s; "
2019-02-18 10:35:49 +00:00
" } "
" function ld(u,f){ "
" var x=new XMLHttpRequest(); "
" x.onreadystatechange=function(){ "
" if(this.readyState==4&&this.status==200){ "
" f(this); "
" } "
" }; "
" x.open('GET',u,true); "
" x.send(); "
2019-02-17 10:32:53 +00:00
" } " ;
const char HTTP_SCRIPT_TEMPLATE [ ] PROGMEM =
2019-02-18 10:35:49 +00:00
" var c; " // Need a global for BASE
" function x1(b){ "
2019-02-24 14:05:18 +00:00
" var i,j,g,k,o; "
2019-03-04 17:16:07 +00:00
" o=b.responseText.split(/}1/); " // Field separator
2019-02-24 14:05:18 +00:00
" k=o.shift(); " // Template name
2019-02-18 10:35:49 +00:00
" if(eb('s1').value==''){ "
2019-02-24 14:05:18 +00:00
" eb('s1').value=k; " // Set NAME if not yet set
2019-02-18 10:35:49 +00:00
" } "
2019-02-24 14:05:18 +00:00
" os=o.shift(); " // Complete GPIO sensor list
" g=o.shift().split(','); " // Array separator
2019-02-18 10:35:49 +00:00
" j=0; "
" for(i=0;i<13;i++){ " // Supports 13 GPIOs
" if(6==i){j=9;} "
" if(8==i){j=12;} "
" sk(g[i],j); " // Set GPIO
" j++; "
" } "
2019-02-24 14:05:18 +00:00
" g=o.shift(); "
2019-02-18 14:13:37 +00:00
" for(i=0;i< " STR ( GPIO_FLAG_USED ) " ;i++){ "
2019-02-24 14:05:18 +00:00
" p=(g>>i)&1; "
2019-02-18 10:35:49 +00:00
" eb('c'+i).checked=p; " // Set FLAG checkboxes
" } "
" if( " STR ( USER_MODULE ) " ==c){ "
2019-02-24 14:05:18 +00:00
" g=o.shift(); "
" eb('g99').value=g; " // Set BASE for initial select
2019-02-18 10:35:49 +00:00
" } "
" } "
2019-02-17 10:32:53 +00:00
" function st(t){ "
2019-02-18 10:35:49 +00:00
" c=t; " // Needed for initial BASE select
" var a='tp?t='+t; "
" ld(a,x1); " // ?t related to WebGetArg("t", stemp, sizeof(stemp));
2019-02-17 10:32:53 +00:00
" } "
2019-02-18 10:35:49 +00:00
" function x2(a){ "
" os=a.responseText; "
" sk(17,99); " // 17 = WEMOS
" st( " STR ( USER_MODULE ) " ); "
2018-10-10 21:21:44 +01:00
" } "
2019-02-18 10:35:49 +00:00
" window.onload=ld('tp?m=1',x2); " ; // ?m related to WebServer->hasArg("m")
2019-02-17 10:32:53 +00:00
const char HTTP_SCRIPT_MODULE1 [ ] PROGMEM =
2019-02-18 10:35:49 +00:00
" function x1(a){ "
" os=a.responseText; "
2019-03-04 17:16:07 +00:00
" sk(%d,99); "
2019-02-18 10:35:49 +00:00
" } "
" function x2(b){ "
" os=b.responseText; " ;
2018-10-10 21:21:44 +01:00
const char HTTP_SCRIPT_MODULE2 [ ] PROGMEM =
2019-02-18 10:35:49 +00:00
" } "
" function sl(){ "
" ld('md?m=1',x1); " // ?m related to WebServer->hasArg("m")
" ld('md?g=1',x2); " // ?m related to WebServer->hasArg("m")
2019-02-17 10:32:53 +00:00
" } "
" window.onload=sl; " ;
2018-11-10 16:30:23 +00:00
2018-10-10 21:21:44 +01:00
const char HTTP_SCRIPT_INFO_BEGIN [ ] PROGMEM =
" function i(){ "
" var s,o= \" " ;
const char HTTP_SCRIPT_INFO_END [ ] PROGMEM =
2019-02-18 10:35:49 +00:00
" \" ; " // "}1" and "}2" means do not use "}x" in Information text
2018-10-10 21:21:44 +01:00
" s=o.replace(/}1/g, \" </td></tr><tr><th> \" ).replace(/}2/g, \" </th><td> \" ); "
" eb('i').innerHTML=s; "
" } "
2019-03-04 17:16:07 +00:00
" window.onload=i; " ;
2018-11-10 16:30:23 +00:00
2019-03-04 17:16:07 +00:00
const char HTTP_HEAD_STYLE1 [ ] PROGMEM =
2018-11-10 16:30:23 +00:00
" </script> "
" <style> "
" div,fieldset,input,select{padding:5px;font-size:1em;} "
Add command WebColor
* Add rule Http#Initialized
* Add command WebColor to change non-persistent GUI colors on the fly
Use a rule like:
rule3 on http#initialized do webcolor {"webcolor":["#eeeeee","#181818","#4f4f4f","#000000","#dddddd","#008000","#222222","#ff0000","#008000","#ffffff","#1fa3ec","#0e70a4","#d43535","#931f1f","#47c266","#5aaf6f","#ffffff","#999999","#000000"]} endon
or
rule3 on http#initialized do webcolor {"webcolor":["#eee","#181818","#4f4f4f","#000","#ddd","#008000","#222"]} endon
to make color changes persistent)
2019-04-08 21:37:39 +01:00
" fieldset{background:#%06x;} " // COLOR_FORM, Also update HTTP_TIMER_STYLE
2019-02-13 15:26:16 +00:00
" p{margin:0.5em 0;} "
Add command WebColor
* Add rule Http#Initialized
* Add command WebColor to change non-persistent GUI colors on the fly
Use a rule like:
rule3 on http#initialized do webcolor {"webcolor":["#eeeeee","#181818","#4f4f4f","#000000","#dddddd","#008000","#222222","#ff0000","#008000","#ffffff","#1fa3ec","#0e70a4","#d43535","#931f1f","#47c266","#5aaf6f","#ffffff","#999999","#000000"]} endon
or
rule3 on http#initialized do webcolor {"webcolor":["#eee","#181818","#4f4f4f","#000","#ddd","#008000","#222"]} endon
to make color changes persistent)
2019-04-08 21:37:39 +01:00
" input{width:100%%;box-sizing:border-box;-webkit-box-sizing:border-box;-moz-box-sizing:border-box;background:#%06x;color:#%06x;} " // COLOR_INPUT, COLOR_INPUT_TEXT
2019-02-13 15:05:25 +00:00
" input[type=checkbox],input[type=radio]{width:1em;margin-right:6px;vertical-align:-1px;} "
Add command WebColor
* Add rule Http#Initialized
* Add command WebColor to change non-persistent GUI colors on the fly
Use a rule like:
rule3 on http#initialized do webcolor {"webcolor":["#eeeeee","#181818","#4f4f4f","#000000","#dddddd","#008000","#222222","#ff0000","#008000","#ffffff","#1fa3ec","#0e70a4","#d43535","#931f1f","#47c266","#5aaf6f","#ffffff","#999999","#000000"]} endon
or
rule3 on http#initialized do webcolor {"webcolor":["#eee","#181818","#4f4f4f","#000","#ddd","#008000","#222"]} endon
to make color changes persistent)
2019-04-08 21:37:39 +01:00
" select{width:100%%;background:#%06x;color:#%06x;} " // COLOR_INPUT, COLOR_INPUT_TEXT
" textarea{resize:none;width:98%%;height:318px;padding:5px;overflow:auto;background:#%06x;color:#%06x;} " // COLOR_CONSOLE, COLOR_CONSOLE_TEXT
" body{text-align:center;font-family:verdana;background:#%06x;} " // COLOR_BACKGROUND
2019-03-04 17:16:07 +00:00
" td{padding:0px;} " ;
const char HTTP_HEAD_STYLE2 [ ] PROGMEM =
Add command WebColor
* Add rule Http#Initialized
* Add command WebColor to change non-persistent GUI colors on the fly
Use a rule like:
rule3 on http#initialized do webcolor {"webcolor":["#eeeeee","#181818","#4f4f4f","#000000","#dddddd","#008000","#222222","#ff0000","#008000","#ffffff","#1fa3ec","#0e70a4","#d43535","#931f1f","#47c266","#5aaf6f","#ffffff","#999999","#000000"]} endon
or
rule3 on http#initialized do webcolor {"webcolor":["#eee","#181818","#4f4f4f","#000","#ddd","#008000","#222"]} endon
to make color changes persistent)
2019-04-08 21:37:39 +01:00
" button{border:0;border-radius:0.3rem;background:#%06x;color:#%06x;line-height:2.4rem;font-size:1.2rem;width:100%%;-webkit-transition-duration:0.4s;transition-duration:0.4s;cursor:pointer;} " // COLOR_BUTTON, COLOR_BUTTON_TEXT
" button:hover{background:#%06x;} " // COLOR_BUTTON_HOVER
" .bred{background:#%06x;} " // COLOR_BUTTON_RESET
" .bred:hover{background:#%06x;} " // COLOR_BUTTON_RESET_HOVER
" .bgrn{background:#%06x;} " // COLOR_BUTTON_SAVE
" .bgrn:hover{background:#%06x;} " // COLOR_BUTTON_SAVE_HOVER
2018-11-10 16:30:23 +00:00
" a{text-decoration:none;} "
" .p{float:left;text-align:left;} "
2019-03-04 17:16:07 +00:00
" .q{float:right;text-align:right;} " ;
const char HTTP_HEAD_STYLE3 [ ] PROGMEM =
2018-11-10 16:30:23 +00:00
" </style> "
" </head> "
" <body> "
Add command WebColor
* Add rule Http#Initialized
* Add command WebColor to change non-persistent GUI colors on the fly
Use a rule like:
rule3 on http#initialized do webcolor {"webcolor":["#eeeeee","#181818","#4f4f4f","#000000","#dddddd","#008000","#222222","#ff0000","#008000","#ffffff","#1fa3ec","#0e70a4","#d43535","#931f1f","#47c266","#5aaf6f","#ffffff","#999999","#000000"]} endon
or
rule3 on http#initialized do webcolor {"webcolor":["#eee","#181818","#4f4f4f","#000","#ddd","#008000","#222"]} endon
to make color changes persistent)
2019-04-08 21:37:39 +01:00
" <div style='text-align:left;display:inline-block;color:#%06x;min-width:340px;'> " // COLOR_TEXT
2019-02-08 13:55:45 +00:00
# ifdef FIRMWARE_MINIMAL
Add command WebColor
* Add rule Http#Initialized
* Add command WebColor to change non-persistent GUI colors on the fly
Use a rule like:
rule3 on http#initialized do webcolor {"webcolor":["#eeeeee","#181818","#4f4f4f","#000000","#dddddd","#008000","#222222","#ff0000","#008000","#ffffff","#1fa3ec","#0e70a4","#d43535","#931f1f","#47c266","#5aaf6f","#ffffff","#999999","#000000"]} endon
or
rule3 on http#initialized do webcolor {"webcolor":["#eee","#181818","#4f4f4f","#000","#ddd","#008000","#222"]} endon
to make color changes persistent)
2019-04-08 21:37:39 +01:00
" <div style='text-align:center;color:#%06x;'><h3> " D_MINIMAL_FIRMWARE_PLEASE_UPGRADE " </h3></div> " // COLOR_TEXT_WARNING
2018-11-10 16:30:23 +00:00
# endif
" <div style='text-align:center;'><noscript> " D_NOSCRIPT " <br/></noscript> "
# ifdef LANGUAGE_MODULE_NAME
2019-03-04 17:16:07 +00:00
" <h3> " D_MODULE " %s</h3> "
2018-11-10 16:30:23 +00:00
# else
2019-03-04 17:16:07 +00:00
" <h3>%s " D_MODULE " </h3> "
2018-11-10 16:30:23 +00:00
# endif
2019-03-14 15:50:56 +00:00
" <h2>%s</h2> " ;
2019-03-04 17:16:07 +00:00
2018-10-10 21:21:44 +01:00
const char HTTP_MSG_SLIDER1 [ ] PROGMEM =
" <div><span class='p'> " D_COLDLIGHT " </span><span class='q'> " D_WARMLIGHT " </span></div> "
" <div><input type='range' min='153' max='500' value='%d' onchange='lc(value)'></div> " ;
const char HTTP_MSG_SLIDER2 [ ] PROGMEM =
" <div><span class='p'> " D_DARKLIGHT " </span><span class='q'> " D_BRIGHTLIGHT " </span></div> "
" <div><input type='range' min='1' max='100' value='%d' onchange='lb(value)'></div> " ;
const char HTTP_MSG_RSTRT [ ] PROGMEM =
" <br/><div style='text-align:center;'> " D_DEVICE_WILL_RESTART " </div><br/> " ;
2019-02-13 15:05:25 +00:00
2018-10-10 21:21:44 +01:00
const char HTTP_FORM_LOGIN [ ] PROGMEM =
2019-03-07 17:18:30 +00:00
" <fieldset> "
2018-10-10 21:21:44 +01:00
" <form method='post' action='/'> "
2019-03-07 17:18:30 +00:00
" <p><b> " D_USER " </b><br/><input name='USER1' placeholder=' " D_USER " '></p> "
" <p><b> " D_PASSWORD " </b><br/><input name='PASS1' type='password' placeholder=' " D_PASSWORD " '></p> "
2019-02-13 15:05:25 +00:00
" <br/> "
2019-03-07 17:18:30 +00:00
" <button> " D_OK " </button> "
" </form></fieldset> " ;
2019-02-13 15:05:25 +00:00
2019-02-17 10:32:53 +00:00
const char HTTP_FORM_TEMPLATE [ ] PROGMEM =
" <fieldset><legend><b> " D_TEMPLATE_PARAMETERS " </b></legend> "
2019-03-15 13:10:42 +00:00
" <form method='get' action='tp'> " ;
2019-02-17 10:32:53 +00:00
const char HTTP_FORM_TEMPLATE_FLAG [ ] PROGMEM =
" <p></p> " // Keep close so do not use <br/>
" <fieldset><legend><b> " D_TEMPLATE_FLAGS " </b></legend><p> "
" <input id='c0' name='c0' type='checkbox'><b> " D_ALLOW_ADC0 " </b><br/> "
2019-04-11 15:59:48 +01:00
" <input id='c1' name='c1' type='checkbox'><b> " D_ALLOW_ADC0_TEMP " </b><br/> "
2019-02-17 10:32:53 +00:00
" </p></fieldset> " ;
2018-10-10 21:21:44 +01:00
const char HTTP_FORM_MODULE [ ] PROGMEM =
2019-02-17 10:32:53 +00:00
" <fieldset><legend><b> " D_MODULE_PARAMETERS " </b></legend> "
" <form method='get' action='md'> "
2019-03-04 17:16:07 +00:00
" <p></p><b> " D_MODULE_TYPE " </b> (%s)<br/><select id='g99' name='g99'></select><br/> "
" <br/><table> " ;
2019-01-07 15:33:18 +00:00
2018-10-10 21:21:44 +01:00
const char HTTP_FORM_WIFI [ ] PROGMEM =
2019-02-13 15:05:25 +00:00
" <fieldset><legend><b> " D_WIFI_PARAMETERS " </b></legend> "
" <form method='get' action='wi'> "
2019-03-04 17:16:07 +00:00
" <p><b> " D_AP1_SSID " </b> ( " STA_SSID1 " )<br/><input id='s1' name='s1' placeholder=' " STA_SSID1 " ' value='%s'></p> "
2019-02-16 12:51:43 +00:00
" <p><b> " D_AP1_PASSWORD " </b><br/><input id='p1' name='p1' type='password' placeholder=' " D_AP1_PASSWORD " ' value=' " D_ASTERISK_PWD " '></p> "
2019-03-04 17:16:07 +00:00
" <p><b> " D_AP2_SSID " </b> ( " STA_SSID2 " )<br/><input id='s2' name='s2' placeholder=' " STA_SSID2 " ' value='%s'></p> "
2019-02-16 12:51:43 +00:00
" <p><b> " D_AP2_PASSWORD " </b><br/><input id='p2' name='p2' type='password' placeholder=' " D_AP2_PASSWORD " ' value=' " D_ASTERISK_PWD " '></p> "
2019-03-04 17:16:07 +00:00
" <p><b> " D_HOSTNAME " </b> (%s)<br/><input id='h' name='h' placeholder='%s' value='%s'></p> " ;
2019-02-13 15:05:25 +00:00
2018-10-10 21:21:44 +01:00
const char HTTP_FORM_LOG1 [ ] PROGMEM =
2019-02-13 15:05:25 +00:00
" <fieldset><legend><b> " D_LOGGING_PARAMETERS " </b> "
" </legend><form method='get' action='lg'> " ;
2018-10-10 21:21:44 +01:00
const char HTTP_FORM_LOG2 [ ] PROGMEM =
2019-03-04 17:16:07 +00:00
" <p><b> " D_SYSLOG_HOST " </b> ( " SYS_LOG_HOST " )<br/><input id='lh' name='lh' placeholder=' " SYS_LOG_HOST " ' value='%s'></p> "
" <p><b> " D_SYSLOG_PORT " </b> ( " STR ( SYS_LOG_PORT ) " )<br/><input id='lp' name='lp' placeholder=' " STR ( SYS_LOG_PORT ) " ' value='%d'></p> "
" <p><b> " D_TELEMETRY_PERIOD " </b> ( " STR ( TELE_PERIOD ) " )<br/><input id='lt' name='lt' placeholder=' " STR ( TELE_PERIOD ) " ' value='%d'></p> " ;
2019-02-13 15:05:25 +00:00
2018-10-10 21:21:44 +01:00
const char HTTP_FORM_OTHER [ ] PROGMEM =
2019-02-13 15:05:25 +00:00
" <fieldset><legend><b> " D_OTHER_PARAMETERS " </b></legend> "
" <form method='get' action='co'> "
" <p></p> "
" <fieldset><legend><b> " D_TEMPLATE " </b></legend> "
2019-03-04 17:16:07 +00:00
" <p><input id='t1' name='t1' placeholder=' " D_TEMPLATE " ' value='%s'></p> "
" <p><input id='t2' name='t2' type='checkbox'%s><b> " D_ACTIVATE " </b></p> "
2019-02-13 15:05:25 +00:00
" </fieldset> "
" <br/> "
2019-04-07 15:36:54 +01:00
" <b> " D_WEB_ADMIN_PASSWORD " </b><br/><input id='wp' name='wp' type='password' placeholder=' " D_WEB_ADMIN_PASSWORD " ' value=' " D_ASTERIX " '><br/> "
2019-02-13 15:05:25 +00:00
" <br> "
2019-03-04 17:16:07 +00:00
" <input id='b1' name='b1' type='checkbox'%s><b> " D_MQTT_ENABLE " </b><br/> "
2019-02-13 15:05:25 +00:00
" <br/> " ;
2018-10-10 21:21:44 +01:00
const char HTTP_FORM_END [ ] PROGMEM =
2019-02-13 15:05:25 +00:00
" <br/> "
" <button name='save' type='submit' class='button bgrn'> " D_SAVE " </button> "
" </form></fieldset> " ;
2018-10-10 21:21:44 +01:00
const char HTTP_FORM_RST [ ] PROGMEM =
" <div id='f1' name='f1' style='display:block;'> "
" <fieldset><legend><b> " D_RESTORE_CONFIGURATION " </b></legend> " ;
const char HTTP_FORM_UPG [ ] PROGMEM =
" <div id='f1' name='f1' style='display:block;'> "
" <fieldset><legend><b> " D_UPGRADE_BY_WEBSERVER " </b></legend> "
" <form method='get' action='u1'> "
2019-03-04 17:16:07 +00:00
" <br/><b> " D_OTA_URL " </b><br/><input id='o' name='o' placeholder='OTA_URL' value='%s'><br/> "
2018-10-10 21:21:44 +01:00
" <br/><button type='submit'> " D_START_UPGRADE " </button></form> "
" </fieldset><br/><br/> "
" <fieldset><legend><b> " D_UPGRADE_BY_FILE_UPLOAD " </b></legend> " ;
const char HTTP_FORM_RST_UPG [ ] PROGMEM =
" <form method='post' action='u2' enctype='multipart/form-data'> "
" <br/><input type='file' name='u2'><br/> "
2019-03-04 17:16:07 +00:00
" <br/><button type='submit' onclick='eb( \" f1 \" ).style.display= \" none \" ;eb( \" f2 \" ).style.display= \" block \" ;this.form.submit();'> " D_START " %s</button></form> "
2018-10-10 21:21:44 +01:00
" </fieldset> "
" </div> "
" <div id='f2' name='f2' style='display:none;text-align:center;'><b> " D_UPLOAD_STARTED " ...</b></div> " ;
2019-03-04 17:16:07 +00:00
2018-10-10 21:21:44 +01:00
const char HTTP_FORM_CMND [ ] PROGMEM =
" <br/><textarea readonly id='t1' name='t1' cols='340' wrap='off'></textarea><br/><br/> "
" <form method='get' onsubmit='return l(1);'> "
" <input id='c1' name='c1' placeholder=' " D_ENTER_COMMAND " ' autofocus><br/> "
// "<br/><button type='submit'>Send command</button>"
" </form> " ;
2019-03-04 17:16:07 +00:00
2018-10-10 21:21:44 +01:00
const char HTTP_TABLE100 [ ] PROGMEM =
2019-03-10 14:36:34 +00:00
" <table style='width:100%%'> " ;
2019-03-04 17:16:07 +00:00
2018-10-10 21:21:44 +01:00
const char HTTP_COUNTER [ ] PROGMEM =
" <br/><div id='t' name='t' style='text-align:center;'></div> " ;
2019-03-04 17:16:07 +00:00
2018-10-10 21:21:44 +01:00
const char HTTP_END [ ] PROGMEM =
2019-03-31 10:59:04 +01:00
" <div style='text-align:right;font-size:11px;'><hr/><a href='https://bit.ly/tasmota' target='_blank' style='color:#aaa;'>Sonoff-Tasmota %s " D_BY " Theo Arends</a></div> "
2018-10-10 21:21:44 +01:00
" </div> "
" </body> "
" </html> " ;
2019-03-01 17:25:46 +00:00
const char HTTP_DEVICE_CONTROL [ ] PROGMEM = " <td style='width:%d%%'><button onclick='la( \" &o=%d \" );'>%s%s</button></td> " ; // ?o is related to WebGetArg("o", tmp, sizeof(tmp));
2019-03-16 15:23:41 +00:00
const char HTTP_DEVICE_STATE [ ] PROGMEM = " <td style='width:%d{c}%s;font-size:%dpx'>%s</div></td> " ; // {c} = %'><div style='text-align:center;font-weight:
2018-10-10 21:21:44 +01:00
2019-03-11 09:38:41 +00:00
enum ButtonTitle {
BUTTON_RESTART , BUTTON_RESET_CONFIGURATION ,
BUTTON_MAIN , BUTTON_CONFIGURATION , BUTTON_INFORMATION , BUTTON_FIRMWARE_UPGRADE , BUTTON_CONSOLE ,
BUTTON_MODULE , BUTTON_WIFI , BUTTON_LOGGING , BUTTON_OTHER , BUTTON_TEMPLATE , BUTTON_BACKUP , BUTTON_RESTORE } ;
const char kButtonTitle [ ] PROGMEM =
D_RESTART " | " D_RESET_CONFIGURATION " | "
D_MAIN_MENU " | " D_CONFIGURATION " | " D_INFORMATION " | " D_FIRMWARE_UPGRADE " | " D_CONSOLE " | "
D_CONFIGURE_MODULE " | " D_CONFIGURE_WIFI " | " D_CONFIGURE_LOGGING " | " D_CONFIGURE_OTHER " | " D_CONFIGURE_TEMPLATE " | " D_BACKUP_CONFIGURATION " | " D_RESTORE_CONFIGURATION ;
const char kButtonAction [ ] PROGMEM =
" .|rt| "
" .|cn|in|up|cs| "
" md|wi|lg|co|tp|dl|rs " ;
const char kButtonConfirm [ ] PROGMEM = D_CONFIRM_RESTART " | " D_CONFIRM_RESET_CONFIGURATION ;
2019-02-23 14:29:42 +00:00
enum CTypes { CT_HTML , CT_PLAIN , CT_XML , CT_JSON , CT_STREAM } ;
const char kContentTypes [ ] PROGMEM = " text/html|text/plain|text/xml|application/json|application/octet-stream " ;
2018-10-10 21:21:44 +01:00
2019-03-04 17:16:07 +00:00
const char kLoggingOptions [ ] PROGMEM = D_SERIAL_LOG_LEVEL " | " D_WEB_LOG_LEVEL " | " D_SYS_LOG_LEVEL ;
const char kLoggingLevels [ ] PROGMEM = D_NONE " | " D_ERROR " | " D_INFO " | " D_DEBUG " | " D_MORE_DEBUG ;
const char kEmulationOptions [ ] PROGMEM = D_NONE " | " D_BELKIN_WEMO " | " D_HUE_BRIDGE ;
2019-02-23 12:17:02 +00:00
const char kUploadErrors [ ] PROGMEM =
D_UPLOAD_ERR_1 " | " D_UPLOAD_ERR_2 " | " D_UPLOAD_ERR_3 " | " D_UPLOAD_ERR_4 " | " D_UPLOAD_ERR_5 " | " D_UPLOAD_ERR_6 " | " D_UPLOAD_ERR_7 " | " D_UPLOAD_ERR_8 " | " D_UPLOAD_ERR_9
# ifdef USE_RF_FLASH
" | " D_UPLOAD_ERR_10 " | " D_UPLOAD_ERR_11 " | " D_UPLOAD_ERR_12 " | " D_UPLOAD_ERR_13
# endif
;
2019-03-31 10:59:04 +01:00
const uint16_t DNS_PORT = 53 ;
2019-02-21 16:49:11 +00:00
enum HttpOptions { HTTP_OFF , HTTP_USER , HTTP_ADMIN , HTTP_MANAGER , HTTP_MANAGER_RESET_ONLY } ;
2018-10-10 21:21:44 +01:00
DNSServer * DnsServer ;
ESP8266WebServer * WebServer ;
2019-03-04 17:16:07 +00:00
String chunk_buffer = " " ; // Could be max 2 * CHUNKED_BUFFER_SIZE
2018-10-10 21:21:44 +01:00
int minimum_signal_quality = - 1 ;
2019-02-23 17:38:36 +00:00
bool remove_duplicate_access_points = true ;
2019-03-16 15:23:41 +00:00
bool reset_web_log_flag = false ; // Reset web console log
2018-10-10 21:21:44 +01:00
uint8_t webserver_state = HTTP_OFF ;
uint8_t upload_error = 0 ;
uint8_t upload_file_type ;
uint8_t upload_progress_dot_count ;
uint8_t config_block_count = 0 ;
uint8_t config_xor_on = 0 ;
uint8_t config_xor_on_set = CONFIG_FILE_XOR ;
// Helper function to avoid code duplication (saves 4k Flash)
static void WebGetArg ( const char * arg , char * out , size_t max )
{
String s = WebServer - > arg ( arg ) ;
strlcpy ( out , s . c_str ( ) , max ) ;
// out[max-1] = '\0'; // Ensure terminating NUL
}
2019-02-21 16:49:11 +00:00
static bool WifiIsInManagerMode ( ) {
return ( HTTP_MANAGER = = webserver_state | | HTTP_MANAGER_RESET_ONLY = = webserver_state ) ;
}
2018-10-10 21:21:44 +01:00
void ShowWebSource ( int source )
{
if ( ( source > 0 ) & & ( source < SRC_MAX ) ) {
char stemp1 [ 20 ] ;
2019-03-08 14:15:42 +00:00
AddLog_P2 ( LOG_LEVEL_DEBUG , PSTR ( " SRC: %s from %s " ) , GetTextIndexed ( stemp1 , sizeof ( stemp1 ) , source , kCommandSource ) , WebServer - > client ( ) . remoteIP ( ) . toString ( ) . c_str ( ) ) ;
2018-10-10 21:21:44 +01:00
}
}
void ExecuteWebCommand ( char * svalue , int source )
{
ShowWebSource ( source ) ;
ExecuteCommand ( svalue , SRC_IGNORE ) ;
}
void StartWebserver ( int type , IPAddress ipweb )
{
if ( ! Settings . web_refresh ) { Settings . web_refresh = HTTP_REFRESH_TIME ; }
if ( ! webserver_state ) {
if ( ! WebServer ) {
2019-03-07 17:18:30 +00:00
WebServer = new ESP8266WebServer ( ( HTTP_MANAGER = = type | | HTTP_MANAGER_RESET_ONLY = = type ) ? 80 : WEB_PORT ) ;
2018-10-10 21:21:44 +01:00
WebServer - > on ( " / " , HandleRoot ) ;
WebServer - > onNotFound ( HandleNotFound ) ;
2019-03-07 17:18:30 +00:00
WebServer - > on ( " /up " , HandleUpgradeFirmware ) ;
WebServer - > on ( " /u1 " , HandleUpgradeFirmwareStart ) ; // OTA
WebServer - > on ( " /u2 " , HTTP_POST , HandleUploadDone , HandleUploadLoop ) ;
WebServer - > on ( " /u2 " , HTTP_OPTIONS , HandlePreflightRequest ) ;
WebServer - > on ( " /cs " , HandleConsole ) ;
WebServer - > on ( " /cm " , HandleHttpCommand ) ;
2019-02-08 13:55:45 +00:00
# ifndef FIRMWARE_MINIMAL
2019-03-07 17:18:30 +00:00
WebServer - > on ( " /cn " , HandleConfiguration ) ;
WebServer - > on ( " /md " , HandleModuleConfiguration ) ;
WebServer - > on ( " /wi " , HandleWifiConfiguration ) ;
WebServer - > on ( " /lg " , HandleLoggingConfiguration ) ;
WebServer - > on ( " /tp " , HandleTemplateConfiguration ) ;
WebServer - > on ( " /co " , HandleOtherConfiguration ) ;
WebServer - > on ( " /dl " , HandleBackupConfiguration ) ;
WebServer - > on ( " /rs " , HandleRestoreConfiguration ) ;
2018-10-10 21:21:44 +01:00
WebServer - > on ( " /rt " , HandleResetConfiguration ) ;
2019-03-07 17:18:30 +00:00
WebServer - > on ( " /in " , HandleInformation ) ;
2018-10-10 21:21:44 +01:00
# ifdef USE_EMULATION
2019-03-07 17:18:30 +00:00
HueWemoAddHandlers ( ) ;
2018-10-10 21:21:44 +01:00
# endif // USE_EMULATION
2019-03-07 17:18:30 +00:00
XdrvCall ( FUNC_WEB_ADD_HANDLER ) ;
XsnsCall ( FUNC_WEB_ADD_HANDLER ) ;
2019-02-08 13:55:45 +00:00
# endif // Not FIRMWARE_MINIMAL
2018-10-10 21:21:44 +01:00
}
2019-01-28 13:08:33 +00:00
reset_web_log_flag = false ;
2018-10-10 21:21:44 +01:00
WebServer - > begin ( ) ; // Web server start
}
if ( webserver_state ! = type ) {
2019-03-08 14:15:42 +00:00
AddLog_P2 ( LOG_LEVEL_INFO , PSTR ( D_LOG_HTTP D_WEBSERVER_ACTIVE_ON " %s%s " D_WITH_IP_ADDRESS " %s " ) , my_hostname , ( mdns_begun ) ? " .local " : " " , ipweb . toString ( ) . c_str ( ) ) ;
Add command WebColor
* Add rule Http#Initialized
* Add command WebColor to change non-persistent GUI colors on the fly
Use a rule like:
rule3 on http#initialized do webcolor {"webcolor":["#eeeeee","#181818","#4f4f4f","#000000","#dddddd","#008000","#222222","#ff0000","#008000","#ffffff","#1fa3ec","#0e70a4","#d43535","#931f1f","#47c266","#5aaf6f","#ffffff","#999999","#000000"]} endon
or
rule3 on http#initialized do webcolor {"webcolor":["#eee","#181818","#4f4f4f","#000","#ddd","#008000","#222"]} endon
to make color changes persistent)
2019-04-08 21:37:39 +01:00
rules_flag . http_init = 1 ;
2018-10-10 21:21:44 +01:00
}
if ( type ) { webserver_state = type ; }
}
2018-11-14 13:32:09 +00:00
void StopWebserver ( void )
2018-10-10 21:21:44 +01:00
{
if ( webserver_state ) {
WebServer - > close ( ) ;
webserver_state = HTTP_OFF ;
AddLog_P ( LOG_LEVEL_INFO , PSTR ( D_LOG_HTTP D_WEBSERVER_STOPPED ) ) ;
}
}
2019-02-21 16:49:11 +00:00
void WifiManagerBegin ( bool reset_only )
2018-10-10 21:21:44 +01:00
{
// setup AP
2018-11-04 17:00:07 +00:00
if ( ! global_state . wifi_down ) {
2018-10-10 21:21:44 +01:00
WiFi . mode ( WIFI_AP_STA ) ;
AddLog_P ( LOG_LEVEL_DEBUG , PSTR ( D_LOG_WIFI D_WIFIMANAGER_SET_ACCESSPOINT_AND_STATION ) ) ;
} else {
WiFi . mode ( WIFI_AP ) ;
AddLog_P ( LOG_LEVEL_DEBUG , PSTR ( D_LOG_WIFI D_WIFIMANAGER_SET_ACCESSPOINT ) ) ;
}
StopWebserver ( ) ;
DnsServer = new DNSServer ( ) ;
2018-12-22 15:13:07 +00:00
int channel = WIFI_SOFT_AP_CHANNEL ;
if ( ( channel < 1 ) | | ( channel > 13 ) ) { channel = 1 ; }
2019-03-26 17:26:50 +00:00
WiFi . softAP ( my_hostname , nullptr , channel ) ;
2018-12-22 15:13:07 +00:00
2018-10-10 21:21:44 +01:00
delay ( 500 ) ; // Without delay I've seen the IP address blank
/* Setup the DNS server redirecting all the domains to the apIP */
DnsServer - > setErrorReplyCode ( DNSReplyCode : : NoError ) ;
DnsServer - > start ( DNS_PORT , " * " , WiFi . softAPIP ( ) ) ;
2019-02-21 16:49:11 +00:00
StartWebserver ( ( reset_only ? HTTP_MANAGER_RESET_ONLY : HTTP_MANAGER ) , WiFi . softAPIP ( ) ) ;
2018-10-10 21:21:44 +01:00
}
2018-11-14 13:32:09 +00:00
void PollDnsWebserver ( void )
2018-10-10 21:21:44 +01:00
{
if ( DnsServer ) { DnsServer - > processNextRequest ( ) ; }
if ( WebServer ) { WebServer - > handleClient ( ) ; }
}
/*********************************************************************************************/
bool WebAuthenticate ( void )
{
2019-02-21 16:49:11 +00:00
if ( Settings . web_password [ 0 ] ! = 0 & & HTTP_MANAGER_RESET_ONLY ! = webserver_state ) {
2018-10-10 21:21:44 +01:00
return WebServer - > authenticate ( WEB_USERNAME , Settings . web_password ) ;
2019-03-04 17:16:07 +00:00
} else {
return true ;
2018-10-10 21:21:44 +01:00
}
}
2019-03-04 17:16:07 +00:00
bool HttpCheckPriviledgedAccess ( bool autorequestauth = true )
2018-10-10 21:21:44 +01:00
{
2019-03-04 17:16:07 +00:00
if ( HTTP_USER = = webserver_state ) {
HandleRoot ( ) ;
return false ;
2018-10-10 21:21:44 +01:00
}
2019-03-04 17:16:07 +00:00
if ( autorequestauth & & ! WebAuthenticate ( ) ) {
WebServer - > requestAuthentication ( ) ;
return false ;
}
return true ;
}
2018-10-10 21:21:44 +01:00
2019-03-04 17:16:07 +00:00
void WSHeaderSend ( void )
{
WebServer - > sendHeader ( F ( " Cache-Control " ) , F ( " no-cache, no-store, must-revalidate " ) ) ;
WebServer - > sendHeader ( F ( " Pragma " ) , F ( " no-cache " ) ) ;
WebServer - > sendHeader ( F ( " Expires " ) , F ( " -1 " ) ) ;
# ifndef ARDUINO_ESP8266_RELEASE_2_3_0
WebServer - > sendHeader ( F ( " Access-Control-Allow-Origin " ) , F ( " * " ) ) ;
# endif
}
2019-03-10 14:36:34 +00:00
/**********************************************************************************************
* HTTP Content Page handler
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2019-03-04 17:16:07 +00:00
void WSSend ( int code , int ctype , const String & content )
{
char ct [ 25 ] ; // strlen("application/octet-stream") +1 = Longest Content type string
WebServer - > send ( code , GetTextIndexed ( ct , sizeof ( ct ) , ctype , kContentTypes ) , content ) ;
}
2019-03-10 14:36:34 +00:00
/**********************************************************************************************
* HTTP Content Chunk handler
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2019-03-16 15:23:41 +00:00
void WSContentBegin ( int code , int ctype )
{
WebServer - > client ( ) . flush ( ) ;
WSHeaderSend ( ) ;
# ifdef ARDUINO_ESP8266_RELEASE_2_3_0
WebServer - > sendHeader ( F ( " Accept-Ranges " ) , F ( " none " ) ) ;
WebServer - > sendHeader ( F ( " Transfer-Encoding " ) , F ( " chunked " ) ) ;
# endif
WebServer - > setContentLength ( CONTENT_LENGTH_UNKNOWN ) ;
WSSend ( code , ctype , " " ) ; // Signal start of chunked content
chunk_buffer = " " ;
}
2019-03-10 14:36:34 +00:00
void _WSContentSend ( const String & content ) // Low level sendContent for all core versions
2019-03-04 17:16:07 +00:00
{
size_t len = content . length ( ) ;
# ifdef ARDUINO_ESP8266_RELEASE_2_3_0
const char * footer = " \r \n " ;
char chunk_size [ 11 ] ;
sprintf ( chunk_size , " %x \r \n " , len ) ;
WebServer - > sendContent ( String ( ) + chunk_size + content + footer ) ;
# else
WebServer - > sendContent ( content ) ;
# endif
2019-03-11 09:38:41 +00:00
# ifdef USE_DEBUG_DRIVER
ShowFreeMem ( PSTR ( " WSContentSend " ) ) ;
# endif
2019-03-10 14:36:34 +00:00
// AddLog_P2(LOG_LEVEL_DEBUG, PSTR("HTP: Chunk size %d"), len);
2019-03-04 17:16:07 +00:00
}
void WSContentFlush ( )
{
if ( chunk_buffer . length ( ) > 0 ) {
2019-03-10 14:36:34 +00:00
_WSContentSend ( chunk_buffer ) ; // Flush chunk buffer
2019-03-04 17:16:07 +00:00
chunk_buffer = " " ;
}
}
2019-03-19 16:31:43 +00:00
void _WSContentSendBuffer ( void )
2019-03-04 17:16:07 +00:00
{
2019-03-19 16:31:43 +00:00
int len = strlen ( mqtt_data ) ;
2019-03-04 17:16:07 +00:00
2019-03-10 14:36:34 +00:00
if ( 0 = = len ) { // No content
2019-03-04 17:16:07 +00:00
return ;
}
2019-03-10 14:36:34 +00:00
else if ( len = = sizeof ( mqtt_data ) ) {
AddLog_P ( LOG_LEVEL_INFO , PSTR ( " HTP: Content too large " ) ) ;
}
else if ( len < CHUNKED_BUFFER_SIZE ) { // Append chunk buffer with small content
chunk_buffer + = mqtt_data ;
2019-03-04 17:16:07 +00:00
len = chunk_buffer . length ( ) ;
}
2019-03-10 14:36:34 +00:00
if ( len > = CHUNKED_BUFFER_SIZE ) { // Either content or chunk buffer is oversize
WSContentFlush ( ) ; // Send chunk buffer before possible content oversize
2019-03-04 17:16:07 +00:00
}
2019-03-10 14:36:34 +00:00
if ( strlen ( mqtt_data ) > = CHUNKED_BUFFER_SIZE ) { // Content is oversize
_WSContentSend ( mqtt_data ) ; // Send content
2019-03-04 17:16:07 +00:00
}
}
2019-03-19 16:31:43 +00:00
void WSContentSend_P ( const char * formatP , . . . ) // Content send snprintf_P char data
{
// This uses char strings. Be aware of sending %% if % is needed
va_list arg ;
va_start ( arg , formatP ) ;
vsnprintf_P ( mqtt_data , sizeof ( mqtt_data ) , formatP , arg ) ;
va_end ( arg ) ;
_WSContentSendBuffer ( ) ;
}
void WSContentSend_PD ( const char * formatP , . . . ) // Content send snprintf_P char data checked for decimal separator
{
// This uses char strings. Be aware of sending %% if % is needed
va_list arg ;
va_start ( arg , formatP ) ;
int len = vsnprintf_P ( mqtt_data , sizeof ( mqtt_data ) , formatP , arg ) ;
va_end ( arg ) ;
if ( D_DECIMAL_SEPARATOR [ 0 ] ! = ' . ' ) {
2019-03-19 17:02:38 +00:00
for ( int i = 0 ; i < len ; i + + ) {
2019-03-19 16:31:43 +00:00
if ( ' . ' = = mqtt_data [ i ] ) {
mqtt_data [ i ] = D_DECIMAL_SEPARATOR [ 0 ] ;
}
}
}
_WSContentSendBuffer ( ) ;
}
2019-03-10 14:36:34 +00:00
void WSContentStart_P ( const char * title , bool auth )
2019-03-04 17:16:07 +00:00
{
if ( auth & & ( Settings . web_password [ 0 ] ! = 0 ) & & ! WebServer - > authenticate ( WEB_USERNAME , Settings . web_password ) ) {
return WebServer - > requestAuthentication ( ) ;
}
2019-03-16 15:23:41 +00:00
WSContentBegin ( 200 , CT_HTML ) ;
2019-03-04 17:16:07 +00:00
2019-03-26 17:26:50 +00:00
if ( title ! = nullptr ) {
2019-03-16 15:23:41 +00:00
char ctitle [ strlen_P ( title ) + 1 ] ;
strcpy_P ( ctitle , title ) ; // Get title from flash to RAM
WSContentSend_P ( HTTP_HEAD , Settings . friendlyname [ 0 ] , ctitle ) ;
}
2019-03-04 17:16:07 +00:00
}
2018-10-29 11:21:27 +00:00
2019-03-10 14:36:34 +00:00
void WSContentStart_P ( const char * title )
2019-03-04 17:16:07 +00:00
{
2019-03-10 14:36:34 +00:00
WSContentStart_P ( title , true ) ;
2019-03-04 17:16:07 +00:00
}
Add command WebColor
* Add rule Http#Initialized
* Add command WebColor to change non-persistent GUI colors on the fly
Use a rule like:
rule3 on http#initialized do webcolor {"webcolor":["#eeeeee","#181818","#4f4f4f","#000000","#dddddd","#008000","#222222","#ff0000","#008000","#ffffff","#1fa3ec","#0e70a4","#d43535","#931f1f","#47c266","#5aaf6f","#ffffff","#999999","#000000"]} endon
or
rule3 on http#initialized do webcolor {"webcolor":["#eee","#181818","#4f4f4f","#000","#ddd","#008000","#222"]} endon
to make color changes persistent)
2019-04-08 21:37:39 +01:00
void WSContentSendStyle_P ( const char * formatP , . . . )
2019-03-04 17:16:07 +00:00
{
2019-02-21 16:49:11 +00:00
if ( WifiIsInManagerMode ( ) ) {
2018-10-10 21:21:44 +01:00
if ( WifiConfigCounter ( ) ) {
2019-03-10 14:36:34 +00:00
WSContentSend_P ( HTTP_SCRIPT_COUNTER ) ;
2018-10-10 21:21:44 +01:00
}
}
2019-04-09 12:56:19 +01:00
WSContentSend_P ( HTTP_HEAD_STYLE1 , WebColor ( COL_FORM ) , WebColor ( COL_INPUT ) , WebColor ( COL_INPUT_TEXT ) , WebColor ( COL_INPUT ) , WebColor ( COL_INPUT_TEXT ) , WebColor ( COL_CONSOLE ) , WebColor ( COL_CONSOLE_TEXT ) , WebColor ( COL_BACKGROUND ) ) ;
WSContentSend_P ( HTTP_HEAD_STYLE2 , WebColor ( COL_BUTTON ) , WebColor ( COL_BUTTON_TEXT ) , WebColor ( COL_BUTTON_HOVER ) , WebColor ( COL_BUTTON_RESET ) , WebColor ( COL_BUTTON_RESET_HOVER ) , WebColor ( COL_BUTTON_SAVE ) , WebColor ( COL_BUTTON_SAVE_HOVER ) ) ;
Add command WebColor
* Add rule Http#Initialized
* Add command WebColor to change non-persistent GUI colors on the fly
Use a rule like:
rule3 on http#initialized do webcolor {"webcolor":["#eeeeee","#181818","#4f4f4f","#000000","#dddddd","#008000","#222222","#ff0000","#008000","#ffffff","#1fa3ec","#0e70a4","#d43535","#931f1f","#47c266","#5aaf6f","#ffffff","#999999","#000000"]} endon
or
rule3 on http#initialized do webcolor {"webcolor":["#eee","#181818","#4f4f4f","#000","#ddd","#008000","#222"]} endon
to make color changes persistent)
2019-04-08 21:37:39 +01:00
if ( formatP ! = nullptr ) {
// This uses char strings. Be aware of sending %% if % is needed
va_list arg ;
va_start ( arg , formatP ) ;
vsnprintf_P ( mqtt_data , sizeof ( mqtt_data ) , formatP , arg ) ;
va_end ( arg ) ;
_WSContentSendBuffer ( ) ;
}
2019-04-09 12:56:19 +01:00
WSContentSend_P ( HTTP_HEAD_STYLE3 , WebColor ( COL_TEXT ) ,
Add command WebColor
* Add rule Http#Initialized
* Add command WebColor to change non-persistent GUI colors on the fly
Use a rule like:
rule3 on http#initialized do webcolor {"webcolor":["#eeeeee","#181818","#4f4f4f","#000000","#dddddd","#008000","#222222","#ff0000","#008000","#ffffff","#1fa3ec","#0e70a4","#d43535","#931f1f","#47c266","#5aaf6f","#ffffff","#999999","#000000"]} endon
or
rule3 on http#initialized do webcolor {"webcolor":["#eee","#181818","#4f4f4f","#000","#ddd","#008000","#222"]} endon
to make color changes persistent)
2019-04-08 21:37:39 +01:00
# ifdef FIRMWARE_MINIMAL
2019-04-09 12:56:19 +01:00
WebColor ( COL_TEXT_WARNING ) ,
Add command WebColor
* Add rule Http#Initialized
* Add command WebColor to change non-persistent GUI colors on the fly
Use a rule like:
rule3 on http#initialized do webcolor {"webcolor":["#eeeeee","#181818","#4f4f4f","#000000","#dddddd","#008000","#222222","#ff0000","#008000","#ffffff","#1fa3ec","#0e70a4","#d43535","#931f1f","#47c266","#5aaf6f","#ffffff","#999999","#000000"]} endon
or
rule3 on http#initialized do webcolor {"webcolor":["#eee","#181818","#4f4f4f","#000","#ddd","#008000","#222"]} endon
to make color changes persistent)
2019-04-08 21:37:39 +01:00
# endif
ModuleName ( ) . c_str ( ) , Settings . friendlyname [ 0 ] ) ;
2019-03-14 15:50:56 +00:00
if ( Settings . flag3 . gui_hostname_ip ) {
bool lip = ( static_cast < uint32_t > ( WiFi . localIP ( ) ) ! = 0 ) ;
bool sip = ( static_cast < uint32_t > ( WiFi . softAPIP ( ) ) ! = 0 ) ;
2019-03-16 15:23:41 +00:00
WSContentSend_P ( PSTR ( " <h4>%s%s (%s%s%s)</h4> " ) , // sonoff.local (192.168.2.12, 192.168.4.1)
2019-03-14 15:50:56 +00:00
my_hostname ,
( mdns_begun ) ? " .local " : " " ,
( lip ) ? WiFi . localIP ( ) . toString ( ) . c_str ( ) : " " ,
2019-03-16 15:23:41 +00:00
( lip & & sip ) ? " , " : " " ,
2019-03-14 15:50:56 +00:00
( sip ) ? WiFi . softAPIP ( ) . toString ( ) . c_str ( ) : " " ) ;
}
WSContentSend_P ( PSTR ( " </div> " ) ) ;
2019-03-04 17:16:07 +00:00
}
2018-10-10 21:21:44 +01:00
2019-03-10 14:36:34 +00:00
void WSContentSendStyle ( void )
2019-03-04 17:16:07 +00:00
{
2019-03-26 17:26:50 +00:00
WSContentSendStyle_P ( nullptr ) ;
2018-10-10 21:21:44 +01:00
}
2019-03-11 09:38:41 +00:00
void WSContentButton ( uint8_t title_index )
{
char action [ 4 ] ;
char title [ 32 ] ;
if ( title_index < = BUTTON_RESET_CONFIGURATION ) {
char confirm [ 64 ] ;
WSContentSend_P ( PSTR ( " <p><form action='%s' method='get' onsubmit='return confirm( \" %s \" );'><button name='%s' class='button bred'>%s</button></form></p> " ) ,
GetTextIndexed ( action , sizeof ( action ) , title_index , kButtonAction ) ,
GetTextIndexed ( confirm , sizeof ( confirm ) , title_index , kButtonConfirm ) ,
( ! title_index ) ? " rst " : " non " ,
GetTextIndexed ( title , sizeof ( title ) , title_index , kButtonTitle ) ) ;
} else {
WSContentSend_P ( PSTR ( " <p><form action='%s' method='get'><button>%s</button></form></p> " ) ,
GetTextIndexed ( action , sizeof ( action ) , title_index , kButtonAction ) ,
GetTextIndexed ( title , sizeof ( title ) , title_index , kButtonTitle ) ) ;
}
}
void WSContentSpaceButton ( uint8_t title_index )
{
WSContentSend_P ( PSTR ( " <div></div> " ) ) ; // 5px padding
WSContentButton ( title_index ) ;
}
2019-03-07 17:18:30 +00:00
void WSContentEnd ( void )
2019-03-16 15:23:41 +00:00
{
WSContentFlush ( ) ; // Flush chunk buffer
_WSContentSend ( " " ) ; // Signal end of chunked content
WebServer - > client ( ) . stop ( ) ;
}
void WSContentStop ( void )
2018-10-10 21:21:44 +01:00
{
2019-03-04 17:16:07 +00:00
if ( WifiIsInManagerMode ( ) ) {
if ( WifiConfigCounter ( ) ) {
2019-03-10 14:36:34 +00:00
WSContentSend_P ( HTTP_COUNTER ) ;
2019-03-04 17:16:07 +00:00
}
}
WSContentSend_P ( HTTP_END , my_version ) ;
2019-03-16 15:23:41 +00:00
WSContentEnd ( ) ;
2018-10-10 21:21:44 +01:00
}
2019-03-04 17:16:07 +00:00
/*********************************************************************************************/
2018-10-12 10:42:52 +01:00
void WebRestart ( uint8_t type )
{
// type 0 = restart
// type 1 = restart after config change
// type 2 = restart after config change with possible ip address change too
AddLog_P ( LOG_LEVEL_DEBUG , S_LOG_HTTP , S_RESTART ) ;
2019-03-04 17:16:07 +00:00
bool reset_only = ( HTTP_MANAGER_RESET_ONLY = = webserver_state ) ;
2018-10-12 10:42:52 +01:00
2019-03-10 14:36:34 +00:00
WSContentStart_P ( ( type ) ? S_SAVE_CONFIGURATION : S_RESTART , ! reset_only ) ;
WSContentSend_P ( HTTP_SCRIPT_RELOAD ) ;
2019-03-04 17:16:07 +00:00
WSContentSendStyle ( ) ;
2018-10-12 10:42:52 +01:00
if ( type ) {
2019-03-10 14:36:34 +00:00
WSContentSend_P ( PSTR ( " <div style='text-align:center;'><b> " D_CONFIGURATION_SAVED " </b><br/> " ) ) ;
2018-10-12 10:42:52 +01:00
if ( 2 = = type ) {
2019-03-10 14:36:34 +00:00
WSContentSend_P ( PSTR ( " <br/> " D_TRYING_TO_CONNECT " <br/> " ) ) ;
2018-10-12 10:42:52 +01:00
}
2019-03-10 14:36:34 +00:00
WSContentSend_P ( PSTR ( " </div> " ) ) ;
2018-10-12 10:42:52 +01:00
}
2019-03-10 14:36:34 +00:00
WSContentSend_P ( HTTP_MSG_RSTRT ) ;
2019-02-21 18:45:03 +00:00
if ( HTTP_MANAGER = = webserver_state | | reset_only ) {
2018-10-12 10:42:52 +01:00
webserver_state = HTTP_ADMIN ;
} else {
2019-03-11 09:38:41 +00:00
WSContentSpaceButton ( BUTTON_MAIN ) ;
2018-10-12 10:42:52 +01:00
}
2019-03-16 15:23:41 +00:00
WSContentStop ( ) ;
2018-10-12 10:42:52 +01:00
ShowWebSource ( SRC_WEBGUI ) ;
restart_flag = 2 ;
}
2018-10-10 21:21:44 +01:00
/*********************************************************************************************/
2018-11-14 13:32:09 +00:00
void HandleWifiLogin ( void )
2018-10-10 21:21:44 +01:00
{
2019-03-10 14:36:34 +00:00
WSContentStart_P ( S_CONFIGURE_WIFI , false ) ; // false means show page no matter if the client has or has not credentials
2019-03-04 17:16:07 +00:00
WSContentSendStyle ( ) ;
2019-03-10 14:36:34 +00:00
WSContentSend_P ( HTTP_FORM_LOGIN ) ;
2019-03-07 17:18:30 +00:00
2019-03-07 17:59:24 +00:00
if ( HTTP_MANAGER_RESET_ONLY = = webserver_state ) {
2019-03-11 09:38:41 +00:00
WSContentSpaceButton ( BUTTON_RESTART ) ;
2019-03-07 17:18:30 +00:00
# ifndef FIRMWARE_MINIMAL
2019-03-11 09:38:41 +00:00
WSContentSpaceButton ( BUTTON_RESET_CONFIGURATION ) ;
2019-03-07 17:18:30 +00:00
# endif // FIRMWARE_MINIMAL
}
2019-03-16 15:23:41 +00:00
WSContentStop ( ) ;
2018-10-10 21:21:44 +01:00
}
2018-11-14 13:32:09 +00:00
void HandleRoot ( void )
2018-10-10 21:21:44 +01:00
{
if ( CaptivePortal ( ) ) { return ; } // If captive portal redirect instead of displaying the page.
2019-03-11 09:38:41 +00:00
if ( WebServer - > hasArg ( " rst " ) ) {
2018-10-12 10:42:52 +01:00
WebRestart ( 0 ) ;
return ;
}
2019-02-21 16:49:11 +00:00
if ( WifiIsInManagerMode ( ) ) {
2019-02-08 13:55:45 +00:00
# ifndef FIRMWARE_MINIMAL
2019-02-21 16:49:11 +00:00
if ( ( Settings . web_password [ 0 ] ! = 0 ) & & ! ( WebServer - > hasArg ( " USER1 " ) ) & & ! ( WebServer - > hasArg ( " PASS1 " ) ) & & HTTP_MANAGER_RESET_ONLY ! = webserver_state ) {
2018-10-10 21:21:44 +01:00
HandleWifiLogin ( ) ;
} else {
2019-03-14 15:50:56 +00:00
if ( ! ( Settings . web_password [ 0 ] ! = 0 ) | | ( ( ( WebServer - > arg ( " USER1 " ) = = WEB_USERNAME ) & & ( WebServer - > arg ( " PASS1 " ) = = Settings . web_password ) ) | | HTTP_MANAGER_RESET_ONLY = = webserver_state ) ) {
2018-10-10 21:21:44 +01:00
HandleWifiConfiguration ( ) ;
} else {
// wrong user and pass
HandleWifiLogin ( ) ;
}
}
2019-02-08 13:55:45 +00:00
# endif // Not FIRMWARE_MINIMAL
2019-03-01 17:25:46 +00:00
return ;
}
if ( HandleRootStatusRefresh ( ) ) {
return ;
}
AddLog_P ( LOG_LEVEL_DEBUG , S_LOG_HTTP , S_MAIN_MENU ) ;
char stemp [ 5 ] ;
2019-03-10 14:36:34 +00:00
WSContentStart_P ( S_MAIN_MENU ) ;
2019-03-04 17:16:07 +00:00
WSContentSend_P ( HTTP_SCRIPT_ROOT , Settings . web_refresh ) ;
WSContentSendStyle ( ) ;
2019-03-01 17:25:46 +00:00
2019-03-10 14:36:34 +00:00
WSContentSend_P ( PSTR ( " <div id='l1' name='l1'></div> " ) ) ;
2019-03-01 17:25:46 +00:00
if ( devices_present ) {
if ( light_type ) {
if ( ( LST_COLDWARM = = ( light_type & 7 ) ) | | ( LST_RGBWC = = ( light_type & 7 ) ) ) {
2019-03-04 17:16:07 +00:00
WSContentSend_P ( HTTP_MSG_SLIDER1 , LightGetColorTemp ( ) ) ;
2018-10-10 21:21:44 +01:00
}
2019-03-04 17:16:07 +00:00
WSContentSend_P ( HTTP_MSG_SLIDER2 , Settings . light_dimmer ) ;
2019-03-01 17:25:46 +00:00
}
2019-03-10 14:36:34 +00:00
WSContentSend_P ( HTTP_TABLE100 ) ;
WSContentSend_P ( PSTR ( " <tr> " ) ) ;
2019-03-01 17:25:46 +00:00
if ( SONOFF_IFAN02 = = my_module_type ) {
2019-03-04 17:16:07 +00:00
WSContentSend_P ( HTTP_DEVICE_CONTROL , 36 , 1 , D_BUTTON_TOGGLE , " " ) ;
2019-03-01 17:25:46 +00:00
for ( uint8_t i = 0 ; i < MAX_FAN_SPEED ; i + + ) {
snprintf_P ( stemp , sizeof ( stemp ) , PSTR ( " %d " ) , i ) ;
2019-03-04 17:16:07 +00:00
WSContentSend_P ( HTTP_DEVICE_CONTROL , 16 , i + 2 , stemp , " " ) ;
2019-03-01 17:25:46 +00:00
}
} else {
for ( uint8_t idx = 1 ; idx < = devices_present ; idx + + ) {
snprintf_P ( stemp , sizeof ( stemp ) , PSTR ( " %d " ) , idx ) ;
2019-03-04 17:16:07 +00:00
WSContentSend_P ( HTTP_DEVICE_CONTROL , 100 / devices_present , idx , ( devices_present < 5 ) ? D_BUTTON_TOGGLE : " " , ( devices_present > 1 ) ? stemp : " " ) ;
2018-10-10 21:21:44 +01:00
}
}
2019-03-10 14:36:34 +00:00
WSContentSend_P ( PSTR ( " </tr></table> " ) ) ;
2019-03-01 17:25:46 +00:00
}
if ( SONOFF_BRIDGE = = my_module_type ) {
2019-03-10 14:36:34 +00:00
WSContentSend_P ( HTTP_TABLE100 ) ;
WSContentSend_P ( PSTR ( " <tr> " ) ) ;
2019-03-01 17:25:46 +00:00
uint8_t idx = 0 ;
for ( uint8_t i = 0 ; i < 4 ; i + + ) {
2019-03-10 14:36:34 +00:00
if ( idx > 0 ) { WSContentSend_P ( PSTR ( " </tr><tr> " ) ) ; }
2019-03-01 17:25:46 +00:00
for ( uint8_t j = 0 ; j < 4 ; j + + ) {
idx + + ;
2019-03-04 17:16:07 +00:00
WSContentSend_P ( PSTR ( " <td style='width:25%%'><button onclick='la( \" &k=%d \" );'>%d</button></td> " ) , idx , idx ) ; // &k is related to WebGetArg("k", tmp, sizeof(tmp));
2018-10-10 21:21:44 +01:00
}
}
2019-03-10 14:36:34 +00:00
WSContentSend_P ( PSTR ( " </tr></table> " ) ) ;
2019-03-01 17:25:46 +00:00
}
2018-10-10 21:21:44 +01:00
2019-02-08 13:55:45 +00:00
# ifndef FIRMWARE_MINIMAL
2019-03-01 17:25:46 +00:00
XdrvCall ( FUNC_WEB_ADD_MAIN_BUTTON ) ;
XsnsCall ( FUNC_WEB_ADD_MAIN_BUTTON ) ;
2019-02-08 13:55:45 +00:00
# endif // Not FIRMWARE_MINIMAL
2018-10-11 16:33:07 +01:00
2019-03-01 17:25:46 +00:00
if ( HTTP_ADMIN = = webserver_state ) {
2019-03-11 09:38:41 +00:00
# ifdef FIRMWARE_MINIMAL
WSContentSpaceButton ( BUTTON_FIRMWARE_UPGRADE ) ;
2019-03-07 17:18:30 +00:00
# else
2019-03-11 09:38:41 +00:00
WSContentSpaceButton ( BUTTON_CONFIGURATION ) ;
WSContentButton ( BUTTON_INFORMATION ) ;
WSContentButton ( BUTTON_FIRMWARE_UPGRADE ) ;
2019-03-07 17:18:30 +00:00
# endif // Not FIRMWARE_MINIMAL
2019-03-11 09:38:41 +00:00
WSContentButton ( BUTTON_CONSOLE ) ;
WSContentButton ( BUTTON_RESTART ) ;
2018-10-10 21:21:44 +01:00
}
2019-03-16 15:23:41 +00:00
WSContentStop ( ) ;
2018-10-10 21:21:44 +01:00
}
2019-03-01 17:25:46 +00:00
bool HandleRootStatusRefresh ( void )
2018-10-10 21:21:44 +01:00
{
2019-03-01 17:25:46 +00:00
if ( ! WebAuthenticate ( ) ) {
WebServer - > requestAuthentication ( ) ;
return true ;
}
if ( ! WebServer - > hasArg ( " m " ) ) { // Status refresh requested
return false ;
}
2018-10-10 21:21:44 +01:00
2019-02-23 17:38:36 +00:00
char tmp [ 8 ] ; // WebGetArg numbers only
char svalue [ 32 ] ; // Command and number parameter
2018-10-10 21:21:44 +01:00
2019-02-23 17:38:36 +00:00
WebGetArg ( " o " , tmp , sizeof ( tmp ) ) ; // 1 - 16 Device number for button Toggle or Fanspeed
2018-10-10 21:21:44 +01:00
if ( strlen ( tmp ) ) {
ShowWebSource ( SRC_WEBGUI ) ;
uint8_t device = atoi ( tmp ) ;
2019-02-11 18:21:49 +00:00
if ( SONOFF_IFAN02 = = my_module_type ) {
2018-10-10 21:21:44 +01:00
if ( device < 2 ) {
ExecuteCommandPower ( 1 , POWER_TOGGLE , SRC_IGNORE ) ;
} else {
snprintf_P ( svalue , sizeof ( svalue ) , PSTR ( D_CMND_FANSPEED " %d " ) , device - 2 ) ;
ExecuteCommand ( svalue , SRC_WEBGUI ) ;
}
} else {
ExecuteCommandPower ( device , POWER_TOGGLE , SRC_IGNORE ) ;
}
}
2019-02-23 17:38:36 +00:00
WebGetArg ( " d " , tmp , sizeof ( tmp ) ) ; // 0 - 100 Dimmer value
2018-10-10 21:21:44 +01:00
if ( strlen ( tmp ) ) {
snprintf_P ( svalue , sizeof ( svalue ) , PSTR ( D_CMND_DIMMER " %s " ) , tmp ) ;
ExecuteWebCommand ( svalue , SRC_WEBGUI ) ;
}
2019-02-23 17:38:36 +00:00
WebGetArg ( " t " , tmp , sizeof ( tmp ) ) ; // 153 - 500 Color temperature
2018-10-10 21:21:44 +01:00
if ( strlen ( tmp ) ) {
snprintf_P ( svalue , sizeof ( svalue ) , PSTR ( D_CMND_COLORTEMPERATURE " %s " ) , tmp ) ;
ExecuteWebCommand ( svalue , SRC_WEBGUI ) ;
}
2019-02-23 17:38:36 +00:00
WebGetArg ( " k " , tmp , sizeof ( tmp ) ) ; // 1 - 16 Pre defined RF keys
2018-10-10 21:21:44 +01:00
if ( strlen ( tmp ) ) {
snprintf_P ( svalue , sizeof ( svalue ) , PSTR ( D_CMND_RFKEY " %s " ) , tmp ) ;
ExecuteWebCommand ( svalue , SRC_WEBGUI ) ;
}
2019-03-16 15:23:41 +00:00
WSContentBegin ( 200 , CT_HTML ) ;
2019-03-19 16:31:43 +00:00
WSContentSend_P ( PSTR ( " {t} " ) ) ;
XsnsCall ( FUNC_WEB_SENSOR ) ;
WSContentSend_P ( PSTR ( " </table> " ) ) ;
2018-10-10 21:21:44 +01:00
if ( devices_present ) {
2019-03-16 15:23:41 +00:00
WSContentSend_P ( PSTR ( " {t}<tr> " ) ) ;
2018-10-10 21:21:44 +01:00
uint8_t fsize = ( devices_present < 5 ) ? 70 - ( devices_present * 8 ) : 32 ;
2019-02-11 18:21:49 +00:00
if ( SONOFF_IFAN02 = = my_module_type ) {
2019-03-16 15:23:41 +00:00
WSContentSend_P ( HTTP_DEVICE_STATE , 36 , ( bitRead ( power , 0 ) ) ? " bold " : " normal " , 54 , GetStateText ( bitRead ( power , 0 ) ) ) ;
2018-10-10 21:21:44 +01:00
uint8_t fanspeed = GetFanspeed ( ) ;
snprintf_P ( svalue , sizeof ( svalue ) , PSTR ( " %d " ) , fanspeed ) ;
2019-03-16 15:23:41 +00:00
WSContentSend_P ( HTTP_DEVICE_STATE , 64 , ( fanspeed ) ? " bold " : " normal " , 54 , ( fanspeed ) ? svalue : GetStateText ( 0 ) ) ;
2018-10-10 21:21:44 +01:00
} else {
2019-01-28 13:08:33 +00:00
for ( uint8_t idx = 1 ; idx < = devices_present ; idx + + ) {
2018-10-10 21:21:44 +01:00
snprintf_P ( svalue , sizeof ( svalue ) , PSTR ( " %d " ) , bitRead ( power , idx - 1 ) ) ;
2019-03-16 15:23:41 +00:00
WSContentSend_P ( HTTP_DEVICE_STATE , 100 / devices_present , ( bitRead ( power , idx - 1 ) ) ? " bold " : " normal " , fsize , ( devices_present < 5 ) ? GetStateText ( bitRead ( power , idx - 1 ) ) : svalue ) ;
2018-10-10 21:21:44 +01:00
}
}
2019-03-16 15:23:41 +00:00
WSContentSend_P ( PSTR ( " </tr></table> " ) ) ;
2018-10-10 21:21:44 +01:00
}
2019-03-16 15:23:41 +00:00
WSContentEnd ( ) ;
2019-03-01 17:25:46 +00:00
return true ;
2018-10-10 21:21:44 +01:00
}
/*-------------------------------------------------------------------------------------------*/
2019-02-08 13:55:45 +00:00
# ifndef FIRMWARE_MINIMAL
2018-10-11 07:32:09 +01:00
2018-11-14 13:32:09 +00:00
void HandleConfiguration ( void )
2018-10-10 21:21:44 +01:00
{
2019-01-10 11:57:42 +00:00
if ( ! HttpCheckPriviledgedAccess ( ) ) { return ; }
2018-10-10 21:21:44 +01:00
AddLog_P ( LOG_LEVEL_DEBUG , S_LOG_HTTP , S_CONFIGURATION ) ;
2019-03-10 14:36:34 +00:00
WSContentStart_P ( S_CONFIGURATION ) ;
2019-03-04 17:16:07 +00:00
WSContentSendStyle ( ) ;
2019-03-11 09:38:41 +00:00
WSContentButton ( BUTTON_MODULE ) ;
WSContentButton ( BUTTON_WIFI ) ;
2018-10-10 21:21:44 +01:00
XdrvCall ( FUNC_WEB_ADD_BUTTON ) ;
2018-10-21 15:30:05 +01:00
XsnsCall ( FUNC_WEB_ADD_BUTTON ) ;
2018-10-10 21:21:44 +01:00
2019-03-11 09:38:41 +00:00
WSContentButton ( BUTTON_LOGGING ) ;
WSContentButton ( BUTTON_OTHER ) ;
WSContentButton ( BUTTON_TEMPLATE ) ;
WSContentSpaceButton ( BUTTON_RESET_CONFIGURATION ) ;
WSContentButton ( BUTTON_BACKUP ) ;
WSContentButton ( BUTTON_RESTORE ) ;
WSContentSpaceButton ( BUTTON_MAIN ) ;
2019-03-16 15:23:41 +00:00
WSContentStop ( ) ;
2018-10-10 21:21:44 +01:00
}
/*-------------------------------------------------------------------------------------------*/
2019-02-17 10:32:53 +00:00
void HandleTemplateConfiguration ( void )
{
if ( ! HttpCheckPriviledgedAccess ( ) ) { return ; }
if ( WebServer - > hasArg ( " save " ) ) {
TemplateSaveSettings ( ) ;
2019-02-18 14:13:37 +00:00
WebRestart ( 1 ) ;
2019-02-17 10:32:53 +00:00
return ;
}
2019-02-23 17:38:36 +00:00
char stemp [ 20 ] ; // Template number and Sensor name
2019-02-17 10:32:53 +00:00
if ( WebServer - > hasArg ( " m " ) ) {
2019-03-16 15:23:41 +00:00
WSContentBegin ( 200 , CT_PLAIN ) ;
2019-04-05 14:27:06 +01:00
for ( uint8_t i = 0 ; i < sizeof ( kModuleNiceList ) ; i + + ) { // "}2'%d'>%s (%d)}3" - "}2'0'>Sonoff Basic (1)}3"
2019-02-17 10:32:53 +00:00
uint8_t midx = pgm_read_byte ( kModuleNiceList + i ) ;
2019-03-16 15:23:41 +00:00
WSContentSend_P ( HTTP_MODULE_TEMPLATE_REPLACE , midx , AnyModuleName ( midx ) . c_str ( ) , midx + 1 ) ;
2019-02-17 10:32:53 +00:00
}
2019-03-16 15:23:41 +00:00
WSContentEnd ( ) ;
2019-02-17 10:32:53 +00:00
return ;
}
2019-02-23 17:38:36 +00:00
WebGetArg ( " t " , stemp , sizeof ( stemp ) ) ; // 0 - 69 Template number
2019-02-17 10:32:53 +00:00
if ( strlen ( stemp ) ) {
uint8_t module = atoi ( stemp ) ;
uint8_t module_save = Settings . module ;
Settings . module = module ;
myio cmodule ;
ModuleGpios ( & cmodule ) ;
gpio_flag flag = ModuleFlag ( ) ;
Settings . module = module_save ;
2019-03-16 15:23:41 +00:00
WSContentBegin ( 200 , CT_PLAIN ) ;
WSContentSend_P ( PSTR ( " %s}1 " ) , AnyModuleName ( module ) . c_str ( ) ) ; // NAME: Generic
2019-03-04 17:16:07 +00:00
for ( uint8_t i = 0 ; i < sizeof ( kGpioNiceList ) ; i + + ) { // GPIO: }2'0'>None (0)}3}2'17'>Button1 (17)}3...
2019-02-17 10:32:53 +00:00
if ( 1 = = i ) {
2019-03-16 15:23:41 +00:00
WSContentSend_P ( HTTP_MODULE_TEMPLATE_REPLACE , 255 , D_SENSOR_USER , 255 ) ; // }2'255'>User (255)}3
2019-02-17 10:32:53 +00:00
}
uint8_t midx = pgm_read_byte ( kGpioNiceList + i ) ;
2019-03-16 15:23:41 +00:00
WSContentSend_P ( HTTP_MODULE_TEMPLATE_REPLACE , midx , GetTextIndexed ( stemp , sizeof ( stemp ) , midx , kSensorNames ) , midx ) ;
2019-02-17 10:32:53 +00:00
}
2019-03-16 15:23:41 +00:00
WSContentSend_P ( PSTR ( " }1 " ) ) ; // Field separator
2019-02-17 10:32:53 +00:00
for ( uint8_t i = 0 ; i < sizeof ( cmodule ) ; i + + ) { // 17,148,29,149,7,255,255,255,138,255,139,255,255
2019-02-18 14:13:37 +00:00
if ( ( i < 6 ) | | ( ( i > 8 ) & & ( i ! = 11 ) ) ) { // Ignore flash pins GPIO06, 7, 8 and 11
2019-03-16 15:23:41 +00:00
WSContentSend_P ( PSTR ( " %s%d " ) , ( i > 0 ) ? " , " : " " , cmodule . io [ i ] ) ;
2019-02-17 10:32:53 +00:00
}
}
2019-03-16 15:23:41 +00:00
WSContentSend_P ( PSTR ( " }1%d}1%d " ) , flag , Settings . user_template_base ) ; // FLAG: 1 BASE: 17
WSContentEnd ( ) ;
2019-02-17 10:32:53 +00:00
return ;
}
AddLog_P ( LOG_LEVEL_DEBUG , S_LOG_HTTP , S_CONFIGURE_TEMPLATE ) ;
2019-03-10 14:36:34 +00:00
WSContentStart_P ( S_CONFIGURE_TEMPLATE ) ;
WSContentSend_P ( HTTP_SCRIPT_MODULE_TEMPLATE ) ;
WSContentSend_P ( HTTP_SCRIPT_TEMPLATE ) ;
2019-03-04 17:16:07 +00:00
WSContentSendStyle ( ) ;
2019-03-10 14:36:34 +00:00
WSContentSend_P ( HTTP_FORM_TEMPLATE ) ;
2019-03-15 13:10:42 +00:00
WSContentSend_P ( HTTP_TABLE100 ) ;
WSContentSend_P ( PSTR ( " <tr><td><b> " D_TEMPLATE_NAME " </b></td><td style='width:200px'><input id='s1' name='s1' placeholder=' " D_TEMPLATE_NAME " '></td></tr> "
" <tr><td><b> " D_BASE_TYPE " </b></td><td><select id='g99' name='g99' onchange='st(this.value)'></select></td></tr> "
" </table> "
" <hr/> " ) ) ;
WSContentSend_P ( HTTP_TABLE100 ) ;
2019-02-17 10:32:53 +00:00
for ( uint8_t i = 0 ; i < 17 ; i + + ) {
Add command WebColor
* Add rule Http#Initialized
* Add command WebColor to change non-persistent GUI colors on the fly
Use a rule like:
rule3 on http#initialized do webcolor {"webcolor":["#eeeeee","#181818","#4f4f4f","#000000","#dddddd","#008000","#222222","#ff0000","#008000","#ffffff","#1fa3ec","#0e70a4","#d43535","#931f1f","#47c266","#5aaf6f","#ffffff","#999999","#000000"]} endon
or
rule3 on http#initialized do webcolor {"webcolor":["#eee","#181818","#4f4f4f","#000","#ddd","#008000","#222"]} endon
to make color changes persistent)
2019-04-08 21:37:39 +01:00
if ( ( i < 6 ) | | ( ( i > 8 ) & & ( i ! = 11 ) ) ) { // Ignore flash pins GPIO06, 7, 8 and 11
WSContentSend_P ( PSTR ( " <tr><td><b><font color='#%06x'> " D_GPIO " %d</font></b></td><td%s><select id='g%d' name='g%d'></select></td></tr> " ) ,
2019-04-09 12:56:19 +01:00
( ( 9 = = i ) | | ( 10 = = i ) ) ? WebColor ( COL_TEXT_WARNING ) : WebColor ( COL_TEXT ) , i , ( 0 = = i ) ? " style='width:200px' " : " " , i , i ) ;
2019-02-17 10:32:53 +00:00
}
}
2019-03-10 14:36:34 +00:00
WSContentSend_P ( PSTR ( " </table> " ) ) ;
WSContentSend_P ( HTTP_FORM_TEMPLATE_FLAG ) ;
WSContentSend_P ( HTTP_FORM_END ) ;
2019-03-11 09:38:41 +00:00
WSContentSpaceButton ( BUTTON_CONFIGURATION ) ;
2019-03-16 15:23:41 +00:00
WSContentStop ( ) ;
2019-02-17 10:32:53 +00:00
}
void TemplateSaveSettings ( void )
{
2019-02-24 14:32:54 +00:00
char tmp [ sizeof ( Settings . user_template . name ) ] ; // WebGetArg NAME and GPIO/BASE/FLAG byte value
char webindex [ 5 ] ; // WebGetArg name
char svalue [ 128 ] ; // Template command string
2019-02-17 10:32:53 +00:00
2019-02-24 14:32:54 +00:00
WebGetArg ( " s1 " , tmp , sizeof ( tmp ) ) ; // NAME
2019-02-17 10:32:53 +00:00
snprintf_P ( svalue , sizeof ( svalue ) , PSTR ( D_CMND_TEMPLATE " { \" " D_JSON_NAME " \" : \" %s \" , \" " D_JSON_GPIO " \" :[ " ) , tmp ) ;
uint8_t j = 0 ;
for ( uint8_t i = 0 ; i < sizeof ( Settings . user_template . gp ) ; i + + ) {
if ( 6 = = i ) { j = 9 ; }
if ( 8 = = i ) { j = 12 ; }
2019-02-23 17:38:36 +00:00
snprintf_P ( webindex , sizeof ( webindex ) , PSTR ( " g%d " ) , j ) ;
2019-02-24 14:32:54 +00:00
WebGetArg ( webindex , tmp , sizeof ( tmp ) ) ; // GPIO
2019-02-17 10:32:53 +00:00
uint8_t gpio = atoi ( tmp ) ;
snprintf_P ( svalue , sizeof ( svalue ) , PSTR ( " %s%s%d " ) , svalue , ( i > 0 ) ? " , " : " " , gpio ) ;
j + + ;
}
uint8_t flag = 0 ;
2019-02-18 14:13:37 +00:00
for ( uint8_t i = 0 ; i < GPIO_FLAG_USED ; i + + ) {
2019-02-23 17:38:36 +00:00
snprintf_P ( webindex , sizeof ( webindex ) , PSTR ( " c%d " ) , i ) ;
2019-02-24 14:32:54 +00:00
uint8_t state = WebServer - > hasArg ( webindex ) < < i ; // FLAG
2019-02-17 10:32:53 +00:00
flag + = state ;
}
2019-02-24 14:32:54 +00:00
WebGetArg ( " g99 " , tmp , sizeof ( tmp ) ) ; // BASE
2019-02-17 10:32:53 +00:00
uint8_t base = atoi ( tmp ) + 1 ;
2019-02-24 14:32:54 +00:00
snprintf_P ( svalue , sizeof ( svalue ) , PSTR ( " %s], \" " D_JSON_FLAG " \" :%d, \" " D_JSON_BASE " \" :%d} " ) , svalue , flag , base ) ;
2019-02-17 10:32:53 +00:00
ExecuteWebCommand ( svalue , SRC_WEBGUI ) ;
}
/*-------------------------------------------------------------------------------------------*/
2018-11-14 13:32:09 +00:00
void HandleModuleConfiguration ( void )
2018-10-10 21:21:44 +01:00
{
2019-01-10 11:57:42 +00:00
if ( ! HttpCheckPriviledgedAccess ( ) ) { return ; }
2018-10-10 21:21:44 +01:00
if ( WebServer - > hasArg ( " save " ) ) {
ModuleSaveSettings ( ) ;
2018-10-12 10:42:52 +01:00
WebRestart ( 1 ) ;
2018-10-10 21:21:44 +01:00
return ;
}
2019-02-23 17:38:36 +00:00
char stemp [ 20 ] ; // Sensor name
2018-10-10 21:21:44 +01:00
uint8_t midx ;
2018-12-29 16:19:13 +00:00
myio cmodule ;
ModuleGpios ( & cmodule ) ;
2018-11-10 14:10:58 +00:00
if ( WebServer - > hasArg ( " m " ) ) {
2019-03-16 15:23:41 +00:00
WSContentBegin ( 200 , CT_PLAIN ) ;
2019-02-11 18:21:49 +00:00
uint8_t vidx = 0 ;
2019-04-05 14:27:06 +01:00
for ( uint8_t i = 0 ; i < = sizeof ( kModuleNiceList ) ; i + + ) { // "}2'%d'>%s (%d)}3" - "}2'255'>UserTemplate (0)}3" - "}2'0'>Sonoff Basic (1)}3"
2019-02-11 18:21:49 +00:00
if ( 0 = = i ) {
midx = USER_MODULE ;
vidx = 0 ;
} else {
midx = pgm_read_byte ( kModuleNiceList + i - 1 ) ;
vidx = midx + 1 ;
}
2019-03-16 15:23:41 +00:00
WSContentSend_P ( HTTP_MODULE_TEMPLATE_REPLACE , midx , AnyModuleName ( midx ) . c_str ( ) , vidx ) ;
2019-02-11 18:21:49 +00:00
}
2019-03-16 15:23:41 +00:00
WSContentEnd ( ) ;
2019-02-02 14:16:35 +00:00
return ;
}
if ( WebServer - > hasArg ( " g " ) ) {
2019-03-16 15:23:41 +00:00
WSContentBegin ( 200 , CT_PLAIN ) ;
2019-01-28 13:08:33 +00:00
for ( uint8_t j = 0 ; j < sizeof ( kGpioNiceList ) ; j + + ) {
2018-11-10 14:10:58 +00:00
midx = pgm_read_byte ( kGpioNiceList + j ) ;
2018-12-29 16:19:13 +00:00
if ( ! GetUsedInModule ( midx , cmodule . io ) ) {
2019-03-16 15:23:41 +00:00
WSContentSend_P ( HTTP_MODULE_TEMPLATE_REPLACE , midx , GetTextIndexed ( stemp , sizeof ( stemp ) , midx , kSensorNames ) , midx ) ;
2018-11-10 14:10:58 +00:00
}
}
2019-03-16 15:23:41 +00:00
WSContentEnd ( ) ;
2018-11-10 14:10:58 +00:00
return ;
2018-10-10 21:21:44 +01:00
}
2018-11-10 14:10:58 +00:00
AddLog_P ( LOG_LEVEL_DEBUG , S_LOG_HTTP , S_CONFIGURE_MODULE ) ;
2019-03-10 14:36:34 +00:00
WSContentStart_P ( S_CONFIGURE_MODULE ) ;
WSContentSend_P ( HTTP_SCRIPT_MODULE_TEMPLATE ) ;
2019-03-04 17:16:07 +00:00
WSContentSend_P ( HTTP_SCRIPT_MODULE1 , Settings . module ) ;
2019-01-28 13:08:33 +00:00
for ( uint8_t i = 0 ; i < sizeof ( cmodule ) ; i + + ) {
2019-02-11 18:21:49 +00:00
if ( ValidGPIO ( i , cmodule . io [ i ] ) ) {
2019-03-04 17:16:07 +00:00
WSContentSend_P ( PSTR ( " sk(%d,%d); " ) , my_module . io [ i ] , i ) ; // g0 - g16
2018-10-10 21:21:44 +01:00
}
}
2019-03-10 14:36:34 +00:00
WSContentSend_P ( HTTP_SCRIPT_MODULE2 ) ;
2019-03-04 17:16:07 +00:00
WSContentSendStyle ( ) ;
WSContentSend_P ( HTTP_FORM_MODULE , AnyModuleName ( MODULE ) . c_str ( ) ) ;
2019-01-28 13:08:33 +00:00
for ( uint8_t i = 0 ; i < sizeof ( cmodule ) ; i + + ) {
2019-02-11 18:21:49 +00:00
if ( ValidGPIO ( i , cmodule . io [ i ] ) ) {
2018-10-10 21:21:44 +01:00
snprintf_P ( stemp , 3 , PINS_WEMOS + i * 2 ) ;
Add command WebColor
* Add rule Http#Initialized
* Add command WebColor to change non-persistent GUI colors on the fly
Use a rule like:
rule3 on http#initialized do webcolor {"webcolor":["#eeeeee","#181818","#4f4f4f","#000000","#dddddd","#008000","#222222","#ff0000","#008000","#ffffff","#1fa3ec","#0e70a4","#d43535","#931f1f","#47c266","#5aaf6f","#ffffff","#999999","#000000"]} endon
or
rule3 on http#initialized do webcolor {"webcolor":["#eee","#181818","#4f4f4f","#000","#ddd","#008000","#222"]} endon
to make color changes persistent)
2019-04-08 21:37:39 +01:00
char sesp8285 [ 40 ] ;
2019-04-09 12:56:19 +01:00
snprintf_P ( sesp8285 , sizeof ( sesp8285 ) , PSTR ( " <font color='#%06x'>ESP8285</font> " ) , WebColor ( COL_TEXT_WARNING ) ) ;
2019-03-04 17:16:07 +00:00
WSContentSend_P ( PSTR ( " <tr><td style='width:190px'>%s <b> " D_GPIO " %d</b> %s</td><td style='width:176px'><select id='g%d' name='g%d'></select></td></tr> " ) ,
Add command WebColor
* Add rule Http#Initialized
* Add command WebColor to change non-persistent GUI colors on the fly
Use a rule like:
rule3 on http#initialized do webcolor {"webcolor":["#eeeeee","#181818","#4f4f4f","#000000","#dddddd","#008000","#222222","#ff0000","#008000","#ffffff","#1fa3ec","#0e70a4","#d43535","#931f1f","#47c266","#5aaf6f","#ffffff","#999999","#000000"]} endon
or
rule3 on http#initialized do webcolor {"webcolor":["#eee","#181818","#4f4f4f","#000","#ddd","#008000","#222"]} endon
to make color changes persistent)
2019-04-08 21:37:39 +01:00
( WEMOS = = my_module_type ) ? stemp : " " , i , ( 0 = = i ) ? D_SENSOR_BUTTON " 1 " : ( 1 = = i ) ? D_SERIAL_OUT : ( 3 = = i ) ? D_SERIAL_IN : ( ( 9 = = i ) | | ( 10 = = i ) ) ? sesp8285 : ( 12 = = i ) ? D_SENSOR_RELAY " 1 " : ( 13 = = i ) ? D_SENSOR_LED " 1i " : ( 14 = = i ) ? D_SENSOR : " " , i , i ) ;
2018-10-10 21:21:44 +01:00
}
}
2019-03-10 14:36:34 +00:00
WSContentSend_P ( PSTR ( " </table> " ) ) ;
WSContentSend_P ( HTTP_FORM_END ) ;
2019-03-11 09:38:41 +00:00
WSContentSpaceButton ( BUTTON_CONFIGURATION ) ;
2019-03-16 15:23:41 +00:00
WSContentStop ( ) ;
2018-10-10 21:21:44 +01:00
}
2018-11-14 13:32:09 +00:00
void ModuleSaveSettings ( void )
2018-10-10 21:21:44 +01:00
{
2019-02-23 17:38:36 +00:00
char tmp [ 8 ] ; // WebGetArg numbers only
char webindex [ 5 ] ; // WebGetArg name
2018-10-10 21:21:44 +01:00
WebGetArg ( " g99 " , tmp , sizeof ( tmp ) ) ;
2019-01-28 13:08:33 +00:00
uint8_t new_module = ( ! strlen ( tmp ) ) ? MODULE : atoi ( tmp ) ;
2018-10-10 21:21:44 +01:00
Settings . last_module = Settings . module ;
Settings . module = new_module ;
2019-02-11 18:21:49 +00:00
SetModuleType ( ) ;
2018-12-29 16:19:13 +00:00
myio cmodule ;
ModuleGpios ( & cmodule ) ;
2018-10-10 21:21:44 +01:00
String gpios = " " ;
2019-01-28 13:08:33 +00:00
for ( uint8_t i = 0 ; i < sizeof ( cmodule ) ; i + + ) {
2018-10-10 21:21:44 +01:00
if ( Settings . last_module ! = new_module ) {
2019-02-11 18:21:49 +00:00
Settings . my_gp . io [ i ] = GPIO_NONE ;
2018-10-10 21:21:44 +01:00
} else {
2019-02-11 18:21:49 +00:00
if ( ValidGPIO ( i , cmodule . io [ i ] ) ) {
2019-02-23 17:38:36 +00:00
snprintf_P ( webindex , sizeof ( webindex ) , PSTR ( " g%d " ) , i ) ;
WebGetArg ( webindex , tmp , sizeof ( tmp ) ) ;
2018-10-10 21:21:44 +01:00
Settings . my_gp . io [ i ] = ( ! strlen ( tmp ) ) ? 0 : atoi ( tmp ) ;
gpios + = F ( " , " D_GPIO ) ; gpios + = String ( i ) ; gpios + = F ( " " ) ; gpios + = String ( Settings . my_gp . io [ i ] ) ;
}
}
}
2019-03-08 14:15:42 +00:00
AddLog_P2 ( LOG_LEVEL_INFO , PSTR ( D_LOG_MODULE " %s " D_CMND_MODULE " %s " ) , ModuleName ( ) . c_str ( ) , gpios . c_str ( ) ) ;
2018-10-10 21:21:44 +01:00
}
/*-------------------------------------------------------------------------------------------*/
String htmlEscape ( String s )
{
2019-02-23 17:38:36 +00:00
s . replace ( " & " , " & " ) ;
s . replace ( " < " , " < " ) ;
s . replace ( " > " , " > " ) ;
s . replace ( " \" " , " " " ) ;
s . replace ( " ' " , " ' " ) ;
s . replace ( " / " , " / " ) ;
return s ;
2018-10-10 21:21:44 +01:00
}
2018-11-14 13:32:09 +00:00
void HandleWifiConfiguration ( void )
2018-10-10 21:21:44 +01:00
{
2019-03-07 17:18:30 +00:00
if ( ! HttpCheckPriviledgedAccess ( ! WifiIsInManagerMode ( ) ) ) { return ; }
2018-10-10 21:21:44 +01:00
AddLog_P ( LOG_LEVEL_DEBUG , S_LOG_HTTP , S_CONFIGURE_WIFI ) ;
2019-02-21 16:49:11 +00:00
if ( WebServer - > hasArg ( " save " ) & & HTTP_MANAGER_RESET_ONLY ! = webserver_state ) {
2018-10-10 21:21:44 +01:00
WifiSaveSettings ( ) ;
2018-10-12 10:42:52 +01:00
WebRestart ( 2 ) ;
2018-10-10 21:21:44 +01:00
return ;
}
2019-03-10 14:36:34 +00:00
WSContentStart_P ( S_CONFIGURE_WIFI , ! WifiIsInManagerMode ( ) ) ;
WSContentSend_P ( HTTP_SCRIPT_WIFI ) ;
2019-03-04 17:16:07 +00:00
WSContentSendStyle ( ) ;
2018-10-10 21:21:44 +01:00
2019-02-23 17:38:36 +00:00
if ( HTTP_MANAGER_RESET_ONLY ! = webserver_state ) {
2019-02-21 16:49:11 +00:00
if ( WebServer - > hasArg ( " scan " ) ) {
2019-02-23 17:38:36 +00:00
# ifdef USE_EMULATION
2019-02-21 16:49:11 +00:00
UdpDisconnect ( ) ;
2019-02-23 17:38:36 +00:00
# endif // USE_EMULATION
2019-02-21 16:49:11 +00:00
int n = WiFi . scanNetworks ( ) ;
AddLog_P ( LOG_LEVEL_DEBUG , PSTR ( D_LOG_WIFI D_SCAN_DONE ) ) ;
if ( 0 = = n ) {
AddLog_P ( LOG_LEVEL_DEBUG , S_LOG_WIFI , S_NO_NETWORKS_FOUND ) ;
2019-03-10 14:36:34 +00:00
WSContentSend_P ( S_NO_NETWORKS_FOUND ) ;
WSContentSend_P ( PSTR ( " . " D_REFRESH_TO_SCAN_AGAIN " . " ) ) ;
2019-02-21 16:49:11 +00:00
} else {
//sort networks
int indices [ n ] ;
for ( int i = 0 ; i < n ; i + + ) {
indices [ i ] = i ;
2018-10-10 21:21:44 +01:00
}
2019-02-21 16:49:11 +00:00
// RSSI SORT
2018-10-10 21:21:44 +01:00
for ( int i = 0 ; i < n ; i + + ) {
for ( int j = i + 1 ; j < n ; j + + ) {
2019-02-21 16:49:11 +00:00
if ( WiFi . RSSI ( indices [ j ] ) > WiFi . RSSI ( indices [ i ] ) ) {
std : : swap ( indices [ i ] , indices [ j ] ) ;
2018-10-10 21:21:44 +01:00
}
}
}
2019-02-21 16:49:11 +00:00
// remove duplicates ( must be RSSI sorted )
if ( remove_duplicate_access_points ) {
String cssid ;
for ( int i = 0 ; i < n ; i + + ) {
if ( - 1 = = indices [ i ] ) { continue ; }
cssid = WiFi . SSID ( indices [ i ] ) ;
for ( int j = i + 1 ; j < n ; j + + ) {
if ( cssid = = WiFi . SSID ( indices [ j ] ) ) {
2019-03-08 14:15:42 +00:00
AddLog_P2 ( LOG_LEVEL_DEBUG , PSTR ( D_LOG_WIFI D_DUPLICATE_ACCESSPOINT " %s " ) , WiFi . SSID ( indices [ j ] ) . c_str ( ) ) ;
2019-02-21 16:49:11 +00:00
indices [ j ] = - 1 ; // set dup aps to index -1
}
}
}
2018-10-10 21:21:44 +01:00
}
2019-02-21 16:49:11 +00:00
//display networks in page
for ( int i = 0 ; i < n ; i + + ) {
if ( - 1 = = indices [ i ] ) { continue ; } // skip dups
2019-03-08 14:15:42 +00:00
AddLog_P2 ( LOG_LEVEL_DEBUG , PSTR ( D_LOG_WIFI D_SSID " %s, " D_BSSID " %s, " D_CHANNEL " %d, " D_RSSI " %d " ) , WiFi . SSID ( indices [ i ] ) . c_str ( ) , WiFi . BSSIDstr ( indices [ i ] ) . c_str ( ) , WiFi . channel ( indices [ i ] ) , WiFi . RSSI ( indices [ i ] ) ) ;
2019-02-21 16:49:11 +00:00
int quality = WifiGetRssiAsQuality ( WiFi . RSSI ( indices [ i ] ) ) ;
if ( minimum_signal_quality = = - 1 | | minimum_signal_quality < quality ) {
uint8_t auth = WiFi . encryptionType ( indices [ i ] ) ;
2019-03-10 14:36:34 +00:00
WSContentSend_P ( PSTR ( " <div><a href='#p' onclick='c(this)'>%s</a> (%d) <span class='q'>%s %d%%</span></div> " ) ,
htmlEscape ( WiFi . SSID ( indices [ i ] ) ) . c_str ( ) ,
WiFi . channel ( indices [ i ] ) ,
( ENC_TYPE_WEP = = auth ) ? D_WEP : ( ENC_TYPE_TKIP = = auth ) ? D_WPA_PSK : ( ENC_TYPE_CCMP = = auth ) ? D_WPA2_PSK : ( ENC_TYPE_AUTO = = auth ) ? D_AUTO : " " ,
quality
) ;
2019-02-21 16:49:11 +00:00
delay ( 0 ) ;
} else {
AddLog_P ( LOG_LEVEL_DEBUG , PSTR ( D_LOG_WIFI D_SKIPPING_LOW_QUALITY ) ) ;
}
}
2019-03-10 14:36:34 +00:00
WSContentSend_P ( PSTR ( " <br/> " ) ) ;
2018-10-10 21:21:44 +01:00
}
2019-02-21 16:49:11 +00:00
} else {
2019-03-10 14:36:34 +00:00
WSContentSend_P ( PSTR ( " <div><a href='/wi?scan='> " D_SCAN_FOR_WIFI_NETWORKS " </a></div><br/> " ) ) ;
2018-10-10 21:21:44 +01:00
}
2019-03-04 17:16:07 +00:00
// As WIFI_HOSTNAME may contain %s-%04d it cannot be part of HTTP_FORM_WIFI where it will exception
WSContentSend_P ( HTTP_FORM_WIFI , Settings . sta_ssid [ 0 ] , Settings . sta_ssid [ 1 ] , WIFI_HOSTNAME , WIFI_HOSTNAME , Settings . hostname ) ;
2019-03-10 14:36:34 +00:00
WSContentSend_P ( HTTP_FORM_END ) ;
2019-02-21 16:49:11 +00:00
}
2019-03-04 17:16:07 +00:00
2019-02-21 16:49:11 +00:00
if ( WifiIsInManagerMode ( ) ) {
2019-03-11 09:38:41 +00:00
WSContentSpaceButton ( BUTTON_RESTART ) ;
2019-02-23 17:38:36 +00:00
# ifndef FIRMWARE_MINIMAL
2019-03-11 09:38:41 +00:00
WSContentSpaceButton ( BUTTON_RESET_CONFIGURATION ) ;
2019-03-04 17:16:07 +00:00
# endif // FIRMWARE_MINIMAL
2018-10-10 21:21:44 +01:00
} else {
2019-03-11 09:38:41 +00:00
WSContentSpaceButton ( BUTTON_CONFIGURATION ) ;
2018-10-10 21:21:44 +01:00
}
2019-03-16 15:23:41 +00:00
WSContentStop ( ) ;
2018-10-10 21:21:44 +01:00
}
2018-11-14 13:32:09 +00:00
void WifiSaveSettings ( void )
2018-10-10 21:21:44 +01:00
{
2019-02-23 17:38:36 +00:00
char tmp [ sizeof ( Settings . sta_pwd [ 0 ] ) ] ; // Max length is currently 65
2018-10-10 21:21:44 +01:00
WebGetArg ( " h " , tmp , sizeof ( tmp ) ) ;
strlcpy ( Settings . hostname , ( ! strlen ( tmp ) ) ? WIFI_HOSTNAME : tmp , sizeof ( Settings . hostname ) ) ;
2019-03-26 17:26:50 +00:00
if ( strstr ( Settings . hostname , " % " ) ! = nullptr ) {
2018-10-10 21:21:44 +01:00
strlcpy ( Settings . hostname , WIFI_HOSTNAME , sizeof ( Settings . hostname ) ) ;
}
WebGetArg ( " s1 " , tmp , sizeof ( tmp ) ) ;
strlcpy ( Settings . sta_ssid [ 0 ] , ( ! strlen ( tmp ) ) ? STA_SSID1 : tmp , sizeof ( Settings . sta_ssid [ 0 ] ) ) ;
WebGetArg ( " s2 " , tmp , sizeof ( tmp ) ) ;
strlcpy ( Settings . sta_ssid [ 1 ] , ( ! strlen ( tmp ) ) ? STA_SSID2 : tmp , sizeof ( Settings . sta_ssid [ 1 ] ) ) ;
WebGetArg ( " p1 " , tmp , sizeof ( tmp ) ) ;
2019-02-21 14:58:37 +00:00
strlcpy ( Settings . sta_pwd [ 0 ] , ( ! strlen ( tmp ) ) ? " " : ( strlen ( tmp ) < 5 ) ? Settings . sta_pwd [ 0 ] : tmp , sizeof ( Settings . sta_pwd [ 0 ] ) ) ;
2018-10-10 21:21:44 +01:00
WebGetArg ( " p2 " , tmp , sizeof ( tmp ) ) ;
2019-02-21 14:58:37 +00:00
strlcpy ( Settings . sta_pwd [ 1 ] , ( ! strlen ( tmp ) ) ? " " : ( strlen ( tmp ) < 5 ) ? Settings . sta_pwd [ 1 ] : tmp , sizeof ( Settings . sta_pwd [ 1 ] ) ) ;
2019-03-08 14:15:42 +00:00
AddLog_P2 ( LOG_LEVEL_INFO , PSTR ( D_LOG_WIFI D_CMND_HOSTNAME " %s, " D_CMND_SSID " 1 %s, " D_CMND_SSID " 2 %s " ) , Settings . hostname , Settings . sta_ssid [ 0 ] , Settings . sta_ssid [ 1 ] ) ;
2018-10-10 21:21:44 +01:00
}
/*-------------------------------------------------------------------------------------------*/
2018-11-14 13:32:09 +00:00
void HandleLoggingConfiguration ( void )
2018-10-10 21:21:44 +01:00
{
2019-01-10 11:57:42 +00:00
if ( ! HttpCheckPriviledgedAccess ( ) ) { return ; }
2018-10-10 21:21:44 +01:00
AddLog_P ( LOG_LEVEL_DEBUG , S_LOG_HTTP , S_CONFIGURE_LOGGING ) ;
if ( WebServer - > hasArg ( " save " ) ) {
LoggingSaveSettings ( ) ;
HandleConfiguration ( ) ;
return ;
}
2019-03-10 14:36:34 +00:00
WSContentStart_P ( S_CONFIGURE_LOGGING ) ;
2019-03-04 17:16:07 +00:00
WSContentSendStyle ( ) ;
2019-03-10 14:36:34 +00:00
WSContentSend_P ( HTTP_FORM_LOG1 ) ;
2019-03-04 17:16:07 +00:00
char stemp1 [ 32 ] ;
char stemp2 [ 32 ] ;
uint8_t dlevel [ 3 ] = { LOG_LEVEL_INFO , LOG_LEVEL_INFO , LOG_LEVEL_NONE } ;
2019-01-28 13:08:33 +00:00
for ( uint8_t idx = 0 ; idx < 3 ; idx + + ) {
2019-03-04 17:16:07 +00:00
uint8_t llevel = ( 0 = = idx ) ? Settings . seriallog_level : ( 1 = = idx ) ? Settings . weblog_level : Settings . syslog_level ;
WSContentSend_P ( PSTR ( " <p><b>%s</b> (%s)<br/><select id='l%d' name='l%d'> " ) ,
GetTextIndexed ( stemp1 , sizeof ( stemp1 ) , idx , kLoggingOptions ) ,
GetTextIndexed ( stemp2 , sizeof ( stemp2 ) , dlevel [ idx ] , kLoggingLevels ) ,
idx , idx ) ;
for ( uint8_t i = LOG_LEVEL_NONE ; i < LOG_LEVEL_ALL ; i + + ) {
WSContentSend_P ( PSTR ( " <option%s value='%d'>%d %s</option> " ) ,
( i = = llevel ) ? " selected " : " " , i , i ,
GetTextIndexed ( stemp1 , sizeof ( stemp1 ) , i , kLoggingLevels ) ) ;
2018-10-10 21:21:44 +01:00
}
2019-03-10 14:36:34 +00:00
WSContentSend_P ( PSTR ( " </select></p> " ) ) ;
2018-10-10 21:21:44 +01:00
}
2019-03-04 17:16:07 +00:00
WSContentSend_P ( HTTP_FORM_LOG2 , Settings . syslog_host , Settings . syslog_port , Settings . tele_period ) ;
2019-03-10 14:36:34 +00:00
WSContentSend_P ( HTTP_FORM_END ) ;
2019-03-11 09:38:41 +00:00
WSContentSpaceButton ( BUTTON_CONFIGURATION ) ;
2019-03-16 15:23:41 +00:00
WSContentStop ( ) ;
2018-10-10 21:21:44 +01:00
}
2018-11-14 13:32:09 +00:00
void LoggingSaveSettings ( void )
2018-10-10 21:21:44 +01:00
{
2019-02-23 17:38:36 +00:00
char tmp [ sizeof ( Settings . syslog_host ) ] ; // Max length is currently 33
2018-10-10 21:21:44 +01:00
2019-03-04 17:16:07 +00:00
WebGetArg ( " l0 " , tmp , sizeof ( tmp ) ) ;
2018-10-10 21:21:44 +01:00
Settings . seriallog_level = ( ! strlen ( tmp ) ) ? SERIAL_LOG_LEVEL : atoi ( tmp ) ;
2019-03-04 17:16:07 +00:00
WebGetArg ( " l1 " , tmp , sizeof ( tmp ) ) ;
2018-10-10 21:21:44 +01:00
Settings . weblog_level = ( ! strlen ( tmp ) ) ? WEB_LOG_LEVEL : atoi ( tmp ) ;
2019-03-04 17:16:07 +00:00
WebGetArg ( " l2 " , tmp , sizeof ( tmp ) ) ;
2018-10-10 21:21:44 +01:00
Settings . syslog_level = ( ! strlen ( tmp ) ) ? SYS_LOG_LEVEL : atoi ( tmp ) ;
syslog_level = Settings . syslog_level ;
syslog_timer = 0 ;
WebGetArg ( " lh " , tmp , sizeof ( tmp ) ) ;
strlcpy ( Settings . syslog_host , ( ! strlen ( tmp ) ) ? SYS_LOG_HOST : tmp , sizeof ( Settings . syslog_host ) ) ;
WebGetArg ( " lp " , tmp , sizeof ( tmp ) ) ;
Settings . syslog_port = ( ! strlen ( tmp ) ) ? SYS_LOG_PORT : atoi ( tmp ) ;
WebGetArg ( " lt " , tmp , sizeof ( tmp ) ) ;
Settings . tele_period = ( ! strlen ( tmp ) ) ? TELE_PERIOD : atoi ( tmp ) ;
if ( ( Settings . tele_period > 0 ) & & ( Settings . tele_period < 10 ) ) {
Settings . tele_period = 10 ; // Do not allow periods < 10 seconds
}
2019-03-08 14:15:42 +00:00
AddLog_P2 ( LOG_LEVEL_INFO , PSTR ( D_LOG_LOG D_CMND_SERIALLOG " %d, " D_CMND_WEBLOG " %d, " D_CMND_SYSLOG " %d, " D_CMND_LOGHOST " %s, " D_CMND_LOGPORT " %d, " D_CMND_TELEPERIOD " %d " ) ,
2018-10-10 21:21:44 +01:00
Settings . seriallog_level , Settings . weblog_level , Settings . syslog_level , Settings . syslog_host , Settings . syslog_port , Settings . tele_period ) ;
}
/*-------------------------------------------------------------------------------------------*/
2018-11-14 13:32:09 +00:00
void HandleOtherConfiguration ( void )
2018-10-10 21:21:44 +01:00
{
2019-01-10 11:57:42 +00:00
if ( ! HttpCheckPriviledgedAccess ( ) ) { return ; }
2018-10-10 21:21:44 +01:00
AddLog_P ( LOG_LEVEL_DEBUG , S_LOG_HTTP , S_CONFIGURE_OTHER ) ;
if ( WebServer - > hasArg ( " save " ) ) {
OtherSaveSettings ( ) ;
2018-10-12 10:42:52 +01:00
WebRestart ( 1 ) ;
2018-10-10 21:21:44 +01:00
return ;
}
2019-03-10 14:36:34 +00:00
WSContentStart_P ( S_CONFIGURE_OTHER ) ;
2019-03-04 17:16:07 +00:00
WSContentSendStyle ( ) ;
2018-10-10 21:21:44 +01:00
2019-02-12 10:55:47 +00:00
TemplateJson ( ) ;
2019-03-04 17:16:07 +00:00
char stemp [ strlen ( mqtt_data ) + 1 ] ;
strlcpy ( stemp , mqtt_data , sizeof ( stemp ) ) ; // Get JSON template
WSContentSend_P ( HTTP_FORM_OTHER , stemp , ( USER_MODULE = = Settings . module ) ? " checked disabled " : " " , ( Settings . flag . mqtt_enabled ) ? " checked " : " " ) ;
2019-01-07 15:33:18 +00:00
2018-10-10 21:21:44 +01:00
uint8_t maxfn = ( devices_present > MAX_FRIENDLYNAMES ) ? MAX_FRIENDLYNAMES : ( ! devices_present ) ? 1 : devices_present ;
2019-02-11 18:21:49 +00:00
if ( SONOFF_IFAN02 = = my_module_type ) { maxfn = 1 ; }
2019-01-28 13:08:33 +00:00
for ( uint8_t i = 0 ; i < maxfn ; i + + ) {
2019-03-04 17:16:07 +00:00
snprintf_P ( stemp , sizeof ( stemp ) , PSTR ( " %d " ) , i + 1 ) ;
WSContentSend_P ( PSTR ( " <b> " D_FRIENDLY_NAME " %d</b> ( " FRIENDLY_NAME " %s)<br/><input id='a%d' name='a%d' placeholder=' " FRIENDLY_NAME " %s' value='%s'><p></p> " ) ,
i + 1 ,
( i ) ? stemp : " " ,
i , i ,
( i ) ? stemp : " " ,
Settings . friendlyname [ i ] ) ;
2018-10-10 21:21:44 +01:00
}
2019-02-13 15:05:25 +00:00
2018-10-10 21:21:44 +01:00
# ifdef USE_EMULATION
2019-03-10 14:36:34 +00:00
WSContentSend_P ( PSTR ( " <p></p><fieldset><legend><b> " D_EMULATION " </b></legend><p> " ) ) ; // Keep close to Friendlynames so do not use <br/>
2019-01-28 13:08:33 +00:00
for ( uint8_t i = 0 ; i < EMUL_MAX ; i + + ) {
2019-03-04 17:16:07 +00:00
WSContentSend_P ( PSTR ( " <input id='r%d' name='b2' type='radio' value='%d'%s><b>%s</b> %s<br/> " ) , // Different id only used for labels
i , i ,
( i = = Settings . flag2 . emulation ) ? " checked " : " " ,
GetTextIndexed ( stemp , sizeof ( stemp ) , i , kEmulationOptions ) ,
( i = = EMUL_NONE ) ? " " : ( i = = EMUL_WEMO ) ? D_SINGLE_DEVICE : D_MULTI_DEVICE ) ;
2018-10-10 21:21:44 +01:00
}
2019-03-10 14:36:34 +00:00
WSContentSend_P ( PSTR ( " </p></fieldset> " ) ) ;
2018-10-10 21:21:44 +01:00
# endif // USE_EMULATION
2019-02-13 15:05:25 +00:00
2019-03-10 14:36:34 +00:00
WSContentSend_P ( HTTP_FORM_END ) ;
2019-03-11 09:38:41 +00:00
WSContentSpaceButton ( BUTTON_CONFIGURATION ) ;
2019-03-16 15:23:41 +00:00
WSContentStop ( ) ;
2018-10-10 21:21:44 +01:00
}
2018-11-14 13:32:09 +00:00
void OtherSaveSettings ( void )
2018-10-10 21:21:44 +01:00
{
2019-02-12 10:55:47 +00:00
char tmp [ 128 ] ;
2019-02-23 17:38:36 +00:00
char webindex [ 5 ] ;
char friendlyname [ sizeof ( Settings . friendlyname [ 0 ] ) ] ;
2018-10-10 21:21:44 +01:00
2019-04-07 15:36:54 +01:00
WebGetArg ( " wp " , tmp , sizeof ( tmp ) ) ;
2018-10-10 21:21:44 +01:00
strlcpy ( Settings . web_password , ( ! strlen ( tmp ) ) ? " " : ( strchr ( tmp , ' * ' ) ) ? Settings . web_password : tmp , sizeof ( Settings . web_password ) ) ;
Settings . flag . mqtt_enabled = WebServer - > hasArg ( " b1 " ) ;
# ifdef USE_EMULATION
WebGetArg ( " b2 " , tmp , sizeof ( tmp ) ) ;
Settings . flag2 . emulation = ( ! strlen ( tmp ) ) ? 0 : atoi ( tmp ) ;
# endif // USE_EMULATION
snprintf_P ( log_data , sizeof ( log_data ) , PSTR ( D_LOG_OTHER D_MQTT_ENABLE " %s, " D_CMND_EMULATION " %d, " D_CMND_FRIENDLYNAME ) , GetStateText ( Settings . flag . mqtt_enabled ) , Settings . flag2 . emulation ) ;
2019-01-28 13:08:33 +00:00
for ( uint8_t i = 0 ; i < MAX_FRIENDLYNAMES ; i + + ) {
2019-03-04 17:16:07 +00:00
snprintf_P ( webindex , sizeof ( webindex ) , PSTR ( " a%d " ) , i ) ;
2019-02-23 17:38:36 +00:00
WebGetArg ( webindex , tmp , sizeof ( tmp ) ) ;
snprintf_P ( friendlyname , sizeof ( friendlyname ) , PSTR ( FRIENDLY_NAME " %d " ) , i + 1 ) ;
strlcpy ( Settings . friendlyname [ i ] , ( ! strlen ( tmp ) ) ? ( i ) ? friendlyname : FRIENDLY_NAME : tmp , sizeof ( Settings . friendlyname [ i ] ) ) ;
2018-10-10 21:21:44 +01:00
snprintf_P ( log_data , sizeof ( log_data ) , PSTR ( " %s%s %s " ) , log_data , ( i ) ? " , " : " " , Settings . friendlyname [ i ] ) ;
}
AddLog ( LOG_LEVEL_INFO ) ;
2019-02-12 10:55:47 +00:00
WebGetArg ( " t1 " , tmp , sizeof ( tmp ) ) ;
if ( strlen ( tmp ) ) { // {"NAME":"12345678901234","GPIO":[255,255,255,255,255,255,255,255,255,255,255,255,255],"FLAG":255,"BASE":255}
char svalue [ 128 ] ;
snprintf_P ( svalue , sizeof ( svalue ) , PSTR ( D_CMND_TEMPLATE " %s " ) , tmp ) ;
ExecuteWebCommand ( svalue , SRC_WEBGUI ) ;
2019-02-13 15:05:25 +00:00
if ( WebServer - > hasArg ( " t2 " ) ) {
snprintf_P ( svalue , sizeof ( svalue ) , PSTR ( D_CMND_MODULE " 0 " ) ) ;
ExecuteWebCommand ( svalue , SRC_WEBGUI ) ;
}
2019-02-12 10:55:47 +00:00
}
2018-10-10 21:21:44 +01:00
}
/*-------------------------------------------------------------------------------------------*/
2018-11-14 13:32:09 +00:00
void HandleBackupConfiguration ( void )
2018-10-10 21:21:44 +01:00
{
2019-01-10 11:57:42 +00:00
if ( ! HttpCheckPriviledgedAccess ( ) ) { return ; }
2018-10-10 21:21:44 +01:00
AddLog_P ( LOG_LEVEL_DEBUG , PSTR ( D_LOG_HTTP D_BACKUP_CONFIGURATION ) ) ;
if ( ! SettingsBufferAlloc ( ) ) { return ; }
WiFiClient myClient = WebServer - > client ( ) ;
WebServer - > setContentLength ( sizeof ( Settings ) ) ;
char attachment [ 100 ] ;
char friendlyname [ sizeof ( Settings . friendlyname [ 0 ] ) ] ;
snprintf_P ( attachment , sizeof ( attachment ) , PSTR ( " attachment; filename=Config_%s_%s.dmp " ) , NoAlNumToUnderscore ( friendlyname , Settings . friendlyname [ 0 ] ) , my_version ) ;
WebServer - > sendHeader ( F ( " Content-Disposition " ) , attachment ) ;
2019-02-23 14:29:42 +00:00
WSSend ( 200 , CT_STREAM , " " ) ;
2018-10-10 21:21:44 +01:00
uint16_t cfg_crc = Settings . cfg_crc ;
Settings . cfg_crc = GetSettingsCrc ( ) ; // Calculate crc (again) as it might be wrong when savedata = 0 (#3918)
memcpy ( settings_buffer , & Settings , sizeof ( Settings ) ) ;
if ( config_xor_on_set ) {
for ( uint16_t i = 2 ; i < sizeof ( Settings ) ; i + + ) {
settings_buffer [ i ] ^ = ( config_xor_on_set + i ) ;
}
}
# ifdef ARDUINO_ESP8266_RELEASE_2_3_0
size_t written = myClient . write ( ( const char * ) settings_buffer , sizeof ( Settings ) ) ;
if ( written < sizeof ( Settings ) ) { // https://github.com/esp8266/Arduino/issues/3218
myClient . write ( ( const char * ) settings_buffer + written , sizeof ( Settings ) - written ) ;
}
# else
myClient . write ( ( const char * ) settings_buffer , sizeof ( Settings ) ) ;
# endif
SettingsBufferFree ( ) ;
Settings . cfg_crc = cfg_crc ; // Restore crc in case savedata = 0 to make sure settings will be noted as changed
}
/*-------------------------------------------------------------------------------------------*/
2018-11-14 13:32:09 +00:00
void HandleResetConfiguration ( void )
2018-10-10 21:21:44 +01:00
{
2019-03-07 17:18:30 +00:00
if ( ! HttpCheckPriviledgedAccess ( ! WifiIsInManagerMode ( ) ) ) { return ; }
2018-10-10 21:21:44 +01:00
AddLog_P ( LOG_LEVEL_DEBUG , S_LOG_HTTP , S_RESET_CONFIGURATION ) ;
2019-03-10 14:36:34 +00:00
WSContentStart_P ( S_RESET_CONFIGURATION , ! WifiIsInManagerMode ( ) ) ;
2019-03-04 17:16:07 +00:00
WSContentSendStyle ( ) ;
2019-03-10 14:36:34 +00:00
WSContentSend_P ( PSTR ( " <div style='text-align:center;'> " D_CONFIGURATION_RESET " </div> " ) ) ;
WSContentSend_P ( HTTP_MSG_RSTRT ) ;
2019-03-11 09:38:41 +00:00
WSContentSpaceButton ( BUTTON_MAIN ) ;
2019-03-16 15:23:41 +00:00
WSContentStop ( ) ;
2018-10-10 21:21:44 +01:00
2019-02-23 17:38:36 +00:00
char command [ CMDSZ ] ;
snprintf_P ( command , sizeof ( command ) , PSTR ( D_CMND_RESET " 1 " ) ) ;
ExecuteWebCommand ( command , SRC_WEBGUI ) ;
2018-10-10 21:21:44 +01:00
}
2018-11-14 13:32:09 +00:00
void HandleRestoreConfiguration ( void )
2018-10-10 21:21:44 +01:00
{
2019-01-10 11:57:42 +00:00
if ( ! HttpCheckPriviledgedAccess ( ) ) { return ; }
2018-10-10 21:21:44 +01:00
AddLog_P ( LOG_LEVEL_DEBUG , S_LOG_HTTP , S_RESTORE_CONFIGURATION ) ;
2019-03-10 14:36:34 +00:00
WSContentStart_P ( S_RESTORE_CONFIGURATION ) ;
2019-03-04 17:16:07 +00:00
WSContentSendStyle ( ) ;
2019-03-10 14:36:34 +00:00
WSContentSend_P ( HTTP_FORM_RST ) ;
2019-03-04 17:16:07 +00:00
WSContentSend_P ( HTTP_FORM_RST_UPG , D_RESTORE ) ;
2019-03-11 09:38:41 +00:00
WSContentSpaceButton ( BUTTON_CONFIGURATION ) ;
2019-03-16 15:23:41 +00:00
WSContentStop ( ) ;
2018-10-10 21:21:44 +01:00
upload_error = 0 ;
upload_file_type = UPL_SETTINGS ;
}
/*-------------------------------------------------------------------------------------------*/
2018-11-14 13:32:09 +00:00
void HandleInformation ( void )
2018-10-10 21:21:44 +01:00
{
2019-01-10 11:57:42 +00:00
if ( ! HttpCheckPriviledgedAccess ( ) ) { return ; }
2018-10-10 21:21:44 +01:00
AddLog_P ( LOG_LEVEL_DEBUG , S_LOG_HTTP , S_INFORMATION ) ;
char stopic [ TOPSZ ] ;
int freeMem = ESP . getFreeHeap ( ) ;
2019-03-10 14:36:34 +00:00
WSContentStart_P ( S_INFORMATION ) ;
2018-10-10 21:21:44 +01:00
// Save 1k of code space replacing table html with javascript replace codes
// }1 = </td></tr><tr><th>
// }2 = </th><td>
2019-03-10 14:36:34 +00:00
WSContentSend_P ( HTTP_SCRIPT_INFO_BEGIN ) ;
2019-03-04 17:16:07 +00:00
WSContentSend_P ( PSTR ( " <table style='width:100%%'><tr><th> " ) ) ;
WSContentSend_P ( PSTR ( D_PROGRAM_VERSION " }2%s%s " ) , my_version , my_image ) ;
WSContentSend_P ( PSTR ( " }1 " D_BUILD_DATE_AND_TIME " }2%s " ) , GetBuildDateAndTime ( ) . c_str ( ) ) ;
WSContentSend_P ( PSTR ( " }1 " D_CORE_AND_SDK_VERSION " }2 " ARDUINO_ESP8266_RELEASE " /%s " ) , ESP . getSdkVersion ( ) ) ;
WSContentSend_P ( PSTR ( " }1 " D_UPTIME " }2%s " ) , GetUptime ( ) . c_str ( ) ) ;
WSContentSend_P ( PSTR ( " }1 " D_FLASH_WRITE_COUNT " }2%d at 0x%X " ) , Settings . save_flag , GetSettingsAddress ( ) ) ;
WSContentSend_P ( PSTR ( " }1 " D_BOOT_COUNT " }2%d " ) , Settings . bootcount ) ;
WSContentSend_P ( PSTR ( " }1 " D_RESTART_REASON " }2%s " ) , GetResetReason ( ) . c_str ( ) ) ;
2018-10-10 21:21:44 +01:00
uint8_t maxfn = ( devices_present > MAX_FRIENDLYNAMES ) ? MAX_FRIENDLYNAMES : devices_present ;
2019-02-11 18:21:49 +00:00
if ( SONOFF_IFAN02 = = my_module_type ) { maxfn = 1 ; }
2019-01-28 13:08:33 +00:00
for ( uint8_t i = 0 ; i < maxfn ; i + + ) {
2019-03-04 17:16:07 +00:00
WSContentSend_P ( PSTR ( " }1 " D_FRIENDLY_NAME " %d}2%s " ) , i + 1 , Settings . friendlyname [ i ] ) ;
2018-10-10 21:21:44 +01:00
}
2019-03-04 17:16:07 +00:00
WSContentSend_P ( PSTR ( " }1}2 " ) ) ; // Empty line
WSContentSend_P ( PSTR ( " }1 " D_AP " %d " D_SSID " ( " D_RSSI " )}2%s (%d%%) " ) , Settings . sta_active + 1 , Settings . sta_ssid [ Settings . sta_active ] , WifiGetRssiAsQuality ( WiFi . RSSI ( ) ) ) ;
WSContentSend_P ( PSTR ( " }1 " D_HOSTNAME " }2%s%s " ) , my_hostname , ( mdns_begun ) ? " .local " : " " ) ;
2018-10-10 21:21:44 +01:00
if ( static_cast < uint32_t > ( WiFi . localIP ( ) ) ! = 0 ) {
2019-03-04 17:16:07 +00:00
WSContentSend_P ( PSTR ( " }1 " D_IP_ADDRESS " }2%s " ) , WiFi . localIP ( ) . toString ( ) . c_str ( ) ) ;
WSContentSend_P ( PSTR ( " }1 " D_GATEWAY " }2%s " ) , IPAddress ( Settings . ip_address [ 1 ] ) . toString ( ) . c_str ( ) ) ;
WSContentSend_P ( PSTR ( " }1 " D_SUBNET_MASK " }2%s " ) , IPAddress ( Settings . ip_address [ 2 ] ) . toString ( ) . c_str ( ) ) ;
WSContentSend_P ( PSTR ( " }1 " D_DNS_SERVER " }2%s " ) , IPAddress ( Settings . ip_address [ 3 ] ) . toString ( ) . c_str ( ) ) ;
WSContentSend_P ( PSTR ( " }1 " D_MAC_ADDRESS " }2%s " ) , WiFi . macAddress ( ) . c_str ( ) ) ;
2018-10-10 21:21:44 +01:00
}
if ( static_cast < uint32_t > ( WiFi . softAPIP ( ) ) ! = 0 ) {
2019-03-04 17:16:07 +00:00
WSContentSend_P ( PSTR ( " }1 " D_IP_ADDRESS " }2%s " ) , WiFi . softAPIP ( ) . toString ( ) . c_str ( ) ) ;
WSContentSend_P ( PSTR ( " }1 " D_GATEWAY " }2%s " ) , WiFi . softAPIP ( ) . toString ( ) . c_str ( ) ) ;
WSContentSend_P ( PSTR ( " }1 " D_MAC_ADDRESS " }2%s " ) , WiFi . softAPmacAddress ( ) . c_str ( ) ) ;
2018-10-10 21:21:44 +01:00
}
2019-03-04 17:16:07 +00:00
WSContentSend_P ( PSTR ( " }1}2 " ) ) ; // Empty line
2018-10-10 21:21:44 +01:00
if ( Settings . flag . mqtt_enabled ) {
2019-03-04 17:16:07 +00:00
WSContentSend_P ( PSTR ( " }1 " D_MQTT_HOST " }2%s " ) , Settings . mqtt_host ) ;
WSContentSend_P ( PSTR ( " }1 " D_MQTT_PORT " }2%d " ) , Settings . mqtt_port ) ;
WSContentSend_P ( PSTR ( " }1 " D_MQTT_USER " }2%s " ) , Settings . mqtt_user ) ;
WSContentSend_P ( PSTR ( " }1 " D_MQTT_CLIENT " }2%s " ) , mqtt_client ) ;
WSContentSend_P ( PSTR ( " }1 " D_MQTT_TOPIC " }2%s " ) , Settings . mqtt_topic ) ;
WSContentSend_P ( PSTR ( " }1 " D_MQTT_GROUP_TOPIC " }2%s " ) , Settings . mqtt_grptopic ) ;
WSContentSend_P ( PSTR ( " }1 " D_MQTT_FULL_TOPIC " }2%s " ) , GetTopic_P ( stopic , CMND , mqtt_topic , " " ) ) ;
WSContentSend_P ( PSTR ( " }1 " D_MQTT " " D_FALLBACK_TOPIC " }2%s " ) , GetFallbackTopic_P ( stopic , CMND , " " ) ) ;
2018-10-10 21:21:44 +01:00
} else {
2019-03-04 17:16:07 +00:00
WSContentSend_P ( PSTR ( " }1 " D_MQTT " }2 " D_DISABLED ) ) ;
2018-10-10 21:21:44 +01:00
}
2019-03-04 17:16:07 +00:00
WSContentSend_P ( PSTR ( " }1}2 " ) ) ; // Empty line
2018-10-10 21:21:44 +01:00
# ifdef USE_EMULATION
2019-03-04 17:16:07 +00:00
WSContentSend_P ( PSTR ( " }1 " D_EMULATION " }2%s " ) , GetTextIndexed ( stopic , sizeof ( stopic ) , Settings . flag2 . emulation , kEmulationOptions ) ) ;
2018-10-10 21:21:44 +01:00
# else
2019-03-04 17:16:07 +00:00
WSContentSend_P ( PSTR ( " }1 " D_EMULATION " }2 " D_DISABLED ) ) ;
2018-10-10 21:21:44 +01:00
# endif // USE_EMULATION
2019-03-04 17:16:07 +00:00
2018-10-10 21:21:44 +01:00
# ifdef USE_DISCOVERY
2019-03-04 17:16:07 +00:00
WSContentSend_P ( PSTR ( " }1 " D_MDNS_DISCOVERY " }2%s " ) , ( Settings . flag3 . mdns_enabled ) ? D_ENABLED : D_DISABLED ) ;
2019-01-03 17:07:03 +00:00
if ( Settings . flag3 . mdns_enabled ) {
2018-10-10 21:21:44 +01:00
# ifdef WEBSERVER_ADVERTISE
2019-03-04 17:16:07 +00:00
WSContentSend_P ( PSTR ( " }1 " D_MDNS_ADVERTISE " }2 " D_WEB_SERVER ) ) ;
2018-10-10 21:21:44 +01:00
# else
2019-03-04 17:16:07 +00:00
WSContentSend_P ( PSTR ( " }1 " D_MDNS_ADVERTISE " }2 " D_DISABLED ) ) ;
2018-10-10 21:21:44 +01:00
# endif // WEBSERVER_ADVERTISE
2019-01-03 17:07:03 +00:00
}
2018-10-10 21:21:44 +01:00
# else
2019-03-04 17:16:07 +00:00
WSContentSend_P ( PSTR ( " }1 " D_MDNS_DISCOVERY " }2 " D_DISABLED ) ) ;
2018-10-10 21:21:44 +01:00
# endif // USE_DISCOVERY
2019-03-04 17:16:07 +00:00
WSContentSend_P ( PSTR ( " }1}2 " ) ) ; // Empty line
WSContentSend_P ( PSTR ( " }1 " D_ESP_CHIP_ID " }2%d " ) , ESP . getChipId ( ) ) ;
WSContentSend_P ( PSTR ( " }1 " D_FLASH_CHIP_ID " }20x%06X " ) , ESP . getFlashChipId ( ) ) ;
WSContentSend_P ( PSTR ( " }1 " D_FLASH_CHIP_SIZE " }2%dkB " ) , ESP . getFlashChipRealSize ( ) / 1024 ) ;
WSContentSend_P ( PSTR ( " }1 " D_PROGRAM_FLASH_SIZE " }2%dkB " ) , ESP . getFlashChipSize ( ) / 1024 ) ;
WSContentSend_P ( PSTR ( " }1 " D_PROGRAM_SIZE " }2%dkB " ) , ESP . getSketchSize ( ) / 1024 ) ;
WSContentSend_P ( PSTR ( " }1 " D_FREE_PROGRAM_SPACE " }2%dkB " ) , ESP . getFreeSketchSpace ( ) / 1024 ) ;
WSContentSend_P ( PSTR ( " }1 " D_FREE_MEMORY " }2%dkB " ) , freeMem / 1024 ) ;
WSContentSend_P ( PSTR ( " </td></tr></table> " ) ) ;
2019-03-10 14:36:34 +00:00
WSContentSend_P ( HTTP_SCRIPT_INFO_END ) ;
2019-03-04 17:16:07 +00:00
WSContentSendStyle ( ) ;
2019-03-10 14:36:34 +00:00
// WSContentSend_P(PSTR("<fieldset><legend><b> Information </b></legend>"));
2019-03-04 17:16:07 +00:00
WSContentSend_P ( PSTR ( " <style>td{padding:0px 5px;}</style> "
" <div id='i' name='i'></div> " ) ) ;
2019-03-10 14:36:34 +00:00
// WSContentSend_P(PSTR("</fieldset>"));
2019-03-11 09:38:41 +00:00
WSContentSpaceButton ( BUTTON_MAIN ) ;
2019-03-16 15:23:41 +00:00
WSContentStop ( ) ;
2018-10-10 21:21:44 +01:00
}
2019-02-08 13:55:45 +00:00
# endif // Not FIRMWARE_MINIMAL
2018-10-10 21:21:44 +01:00
/*-------------------------------------------------------------------------------------------*/
2018-11-14 13:32:09 +00:00
void HandleUpgradeFirmware ( void )
2018-10-10 21:21:44 +01:00
{
2019-01-10 11:57:42 +00:00
if ( ! HttpCheckPriviledgedAccess ( ) ) { return ; }
2018-10-10 21:21:44 +01:00
AddLog_P ( LOG_LEVEL_DEBUG , S_LOG_HTTP , S_FIRMWARE_UPGRADE ) ;
2019-03-10 14:36:34 +00:00
WSContentStart_P ( S_FIRMWARE_UPGRADE ) ;
2019-03-04 17:16:07 +00:00
WSContentSendStyle ( ) ;
WSContentSend_P ( HTTP_FORM_UPG , Settings . ota_url ) ;
WSContentSend_P ( HTTP_FORM_RST_UPG , D_UPGRADE ) ;
2019-03-11 09:38:41 +00:00
WSContentSpaceButton ( BUTTON_MAIN ) ;
2019-03-16 15:23:41 +00:00
WSContentStop ( ) ;
2018-10-10 21:21:44 +01:00
upload_error = 0 ;
upload_file_type = UPL_TASMOTA ;
}
2018-11-14 13:32:09 +00:00
void HandleUpgradeFirmwareStart ( void )
2018-10-10 21:21:44 +01:00
{
2019-01-10 11:57:42 +00:00
if ( ! HttpCheckPriviledgedAccess ( ) ) { return ; }
2019-02-23 17:38:36 +00:00
char command [ sizeof ( Settings . ota_url ) + 10 ] ; // OtaUrl
2018-10-10 21:21:44 +01:00
AddLog_P ( LOG_LEVEL_DEBUG , PSTR ( D_LOG_HTTP D_UPGRADE_STARTED ) ) ;
WifiConfigCounter ( ) ;
2019-02-23 17:38:36 +00:00
char otaurl [ sizeof ( Settings . ota_url ) ] ;
WebGetArg ( " o " , otaurl , sizeof ( otaurl ) ) ;
if ( strlen ( otaurl ) ) {
snprintf_P ( command , sizeof ( command ) , PSTR ( D_CMND_OTAURL " %s " ) , otaurl ) ;
ExecuteWebCommand ( command , SRC_WEBGUI ) ;
2018-10-10 21:21:44 +01:00
}
2019-03-10 14:36:34 +00:00
WSContentStart_P ( S_INFORMATION ) ;
WSContentSend_P ( HTTP_SCRIPT_RELOAD_OTA ) ;
2019-03-04 17:16:07 +00:00
WSContentSendStyle ( ) ;
2019-03-10 14:36:34 +00:00
WSContentSend_P ( PSTR ( " <div style='text-align:center;'><b> " D_UPGRADE_STARTED " ...</b></div> " ) ) ;
WSContentSend_P ( HTTP_MSG_RSTRT ) ;
2019-03-11 09:38:41 +00:00
WSContentSpaceButton ( BUTTON_MAIN ) ;
2019-03-16 15:23:41 +00:00
WSContentStop ( ) ;
2018-10-10 21:21:44 +01:00
2019-02-23 17:38:36 +00:00
snprintf_P ( command , sizeof ( command ) , PSTR ( D_CMND_UPGRADE " 1 " ) ) ;
ExecuteWebCommand ( command , SRC_WEBGUI ) ;
2018-10-10 21:21:44 +01:00
}
2018-11-14 13:32:09 +00:00
void HandleUploadDone ( void )
2018-10-10 21:21:44 +01:00
{
2019-01-10 11:57:42 +00:00
if ( ! HttpCheckPriviledgedAccess ( ) ) { return ; }
2018-10-10 21:21:44 +01:00
AddLog_P ( LOG_LEVEL_DEBUG , PSTR ( D_LOG_HTTP D_UPLOAD_DONE ) ) ;
char error [ 100 ] ;
WifiConfigCounter ( ) ;
restart_flag = 0 ;
MqttRetryCounter ( 0 ) ;
2019-03-10 14:36:34 +00:00
WSContentStart_P ( S_INFORMATION ) ;
2019-03-04 11:36:44 +00:00
if ( ! upload_error ) {
2019-03-10 14:36:34 +00:00
WSContentSend_P ( HTTP_SCRIPT_RELOAD_OTA ) ; // Refesh main web ui after OTA upgrade
2019-03-04 11:36:44 +00:00
}
2019-03-04 17:16:07 +00:00
WSContentSendStyle ( ) ;
Add command WebColor
* Add rule Http#Initialized
* Add command WebColor to change non-persistent GUI colors on the fly
Use a rule like:
rule3 on http#initialized do webcolor {"webcolor":["#eeeeee","#181818","#4f4f4f","#000000","#dddddd","#008000","#222222","#ff0000","#008000","#ffffff","#1fa3ec","#0e70a4","#d43535","#931f1f","#47c266","#5aaf6f","#ffffff","#999999","#000000"]} endon
or
rule3 on http#initialized do webcolor {"webcolor":["#eee","#181818","#4f4f4f","#000","#ddd","#008000","#222"]} endon
to make color changes persistent)
2019-04-08 21:37:39 +01:00
WSContentSend_P ( PSTR ( " <div style='text-align:center;'><b> " D_UPLOAD " <font color='# " ) ) ;
2018-10-10 21:21:44 +01:00
if ( upload_error ) {
Add command WebColor
* Add rule Http#Initialized
* Add command WebColor to change non-persistent GUI colors on the fly
Use a rule like:
rule3 on http#initialized do webcolor {"webcolor":["#eeeeee","#181818","#4f4f4f","#000000","#dddddd","#008000","#222222","#ff0000","#008000","#ffffff","#1fa3ec","#0e70a4","#d43535","#931f1f","#47c266","#5aaf6f","#ffffff","#999999","#000000"]} endon
or
rule3 on http#initialized do webcolor {"webcolor":["#eee","#181818","#4f4f4f","#000","#ddd","#008000","#222"]} endon
to make color changes persistent)
2019-04-08 21:37:39 +01:00
// WSContentSend_P(PSTR(COLOR_TEXT_WARNING "'>" D_FAILED "</font></b><br/><br/>"));
2019-04-09 12:56:19 +01:00
WSContentSend_P ( PSTR ( " %06x'> " D_FAILED " </font></b><br/><br/> " ) , WebColor ( COL_TEXT_WARNING ) ) ;
2018-10-10 21:21:44 +01:00
# ifdef USE_RF_FLASH
2019-02-23 12:17:02 +00:00
if ( upload_error < 14 ) {
# else
if ( upload_error < 10 ) {
2018-10-10 21:21:44 +01:00
# endif
2019-02-23 12:17:02 +00:00
GetTextIndexed ( error , sizeof ( error ) , upload_error - 1 , kUploadErrors ) ;
} else {
snprintf_P ( error , sizeof ( error ) , PSTR ( D_UPLOAD_ERROR_CODE " %d " ) , upload_error ) ;
2018-10-10 21:21:44 +01:00
}
2019-03-10 14:36:34 +00:00
WSContentSend_P ( error ) ;
2019-03-08 14:15:42 +00:00
AddLog_P2 ( LOG_LEVEL_DEBUG , PSTR ( D_UPLOAD " : %s " ) , error ) ;
2018-10-10 21:21:44 +01:00
stop_flash_rotate = Settings . flag . stop_flash_rotate ;
} else {
2019-04-09 12:56:19 +01:00
WSContentSend_P ( PSTR ( " %06x'> " D_SUCCESSFUL " </font></b><br/> " ) , WebColor ( COL_TEXT_SUCCESS ) ) ;
2019-03-10 14:36:34 +00:00
WSContentSend_P ( HTTP_MSG_RSTRT ) ;
2018-10-10 21:21:44 +01:00
ShowWebSource ( SRC_WEBGUI ) ;
restart_flag = 2 ; // Always restart to re-enable disabled features during update
}
SettingsBufferFree ( ) ;
2019-03-10 14:36:34 +00:00
WSContentSend_P ( PSTR ( " </div><br/> " ) ) ;
2019-03-11 09:38:41 +00:00
WSContentSpaceButton ( BUTTON_MAIN ) ;
2019-03-16 15:23:41 +00:00
WSContentStop ( ) ;
2018-10-10 21:21:44 +01:00
}
2018-11-14 13:32:09 +00:00
void HandleUploadLoop ( void )
2018-10-10 21:21:44 +01:00
{
// Based on ESP8266HTTPUpdateServer.cpp uses ESP8266WebServer Parsing.cpp and Cores Updater.cpp (Update)
2019-01-28 13:08:33 +00:00
bool _serialoutput = ( LOG_LEVEL_DEBUG < = seriallog_level ) ;
2018-10-10 21:21:44 +01:00
if ( HTTP_USER = = webserver_state ) { return ; }
if ( upload_error ) {
if ( UPL_TASMOTA = = upload_file_type ) { Update . end ( ) ; }
return ;
}
HTTPUpload & upload = WebServer - > upload ( ) ;
if ( UPLOAD_FILE_START = = upload . status ) {
restart_flag = 60 ;
if ( 0 = = upload . filename . c_str ( ) [ 0 ] ) {
upload_error = 1 ; // No file selected
return ;
}
SettingsSave ( 1 ) ; // Free flash for upload
2019-03-08 14:15:42 +00:00
AddLog_P2 ( LOG_LEVEL_INFO , PSTR ( D_LOG_UPLOAD D_FILE " %s ... " ) , upload . filename . c_str ( ) ) ;
2018-10-10 21:21:44 +01:00
if ( UPL_SETTINGS = = upload_file_type ) {
if ( ! SettingsBufferAlloc ( ) ) {
upload_error = 2 ; // Not enough space
return ;
}
} else {
MqttRetryCounter ( 60 ) ;
# ifdef USE_EMULATION
UdpDisconnect ( ) ;
# endif // USE_EMULATION
# ifdef USE_ARILUX_RF
AriluxRfDisable ( ) ; // Prevent restart exception on Arilux Interrupt routine
# endif // USE_ARILUX_RF
if ( Settings . flag . mqtt_enabled ) MqttDisconnect ( ) ;
uint32_t maxSketchSpace = ( ESP . getFreeSketchSpace ( ) - 0x1000 ) & 0xFFFFF000 ;
if ( ! Update . begin ( maxSketchSpace ) ) { //start with max available size
// if (_serialoutput) Update.printError(Serial);
// if (Update.getError() == UPDATE_ERROR_BOOTSTRAP) {
// if (_serialoutput) Serial.println("Device still in UART update mode, perform powercycle");
// }
upload_error = 2 ; // Not enough space
return ;
}
}
upload_progress_dot_count = 0 ;
} else if ( ! upload_error & & ( UPLOAD_FILE_WRITE = = upload . status ) ) {
if ( 0 = = upload . totalSize ) {
if ( UPL_SETTINGS = = upload_file_type ) {
config_block_count = 0 ;
}
else {
# ifdef USE_RF_FLASH
2019-02-11 18:21:49 +00:00
if ( ( SONOFF_BRIDGE = = my_module_type ) & & ( upload . buf [ 0 ] = = ' : ' ) ) { // Check if this is a RF bridge FW file
2018-10-10 21:21:44 +01:00
Update . end ( ) ; // End esp8266 update session
upload_file_type = UPL_EFM8BB1 ;
upload_error = SnfBrUpdateInit ( ) ;
if ( upload_error ! = 0 ) { return ; }
} else
# endif // USE_RF_FLASH
{
if ( upload . buf [ 0 ] ! = 0xE9 ) {
upload_error = 3 ; // Magic byte is not 0xE9
return ;
}
uint32_t bin_flash_size = ESP . magicFlashChipSize ( ( upload . buf [ 3 ] & 0xf0 ) > > 4 ) ;
if ( bin_flash_size > ESP . getFlashChipRealSize ( ) ) {
upload_error = 4 ; // Program flash size is larger than real flash size
return ;
}
2018-11-15 13:55:45 +00:00
// upload.buf[2] = 3; // Force DOUT - ESP8285
2018-10-10 21:21:44 +01:00
}
}
}
if ( UPL_SETTINGS = = upload_file_type ) {
if ( ! upload_error ) {
if ( upload . currentSize > ( sizeof ( Settings ) - ( config_block_count * HTTP_UPLOAD_BUFLEN ) ) ) {
upload_error = 9 ; // File too large
return ;
}
memcpy ( settings_buffer + ( config_block_count * HTTP_UPLOAD_BUFLEN ) , upload . buf , upload . currentSize ) ;
config_block_count + + ;
}
}
# ifdef USE_RF_FLASH
else if ( UPL_EFM8BB1 = = upload_file_type ) {
2019-03-26 17:26:50 +00:00
if ( efm8bb1_update ! = nullptr ) { // We have carry over data since last write, i. e. a start but not an end
2018-10-10 21:21:44 +01:00
ssize_t result = rf_glue_remnant_with_new_data_and_write ( efm8bb1_update , upload . buf , upload . currentSize ) ;
free ( efm8bb1_update ) ;
2019-03-26 17:26:50 +00:00
efm8bb1_update = nullptr ;
2018-10-10 21:21:44 +01:00
if ( result ! = 0 ) {
upload_error = abs ( result ) ; // 2 = Not enough space, 8 = File invalid
return ;
}
}
ssize_t result = rf_search_and_write ( upload . buf , upload . currentSize ) ;
if ( result < 0 ) {
upload_error = abs ( result ) ;
return ;
} else if ( result > 0 ) {
2018-12-24 17:14:25 +00:00
if ( ( size_t ) result > upload . currentSize ) {
2018-10-10 21:21:44 +01:00
// Offset is larger than the buffer supplied, this should not happen
upload_error = 9 ; // File too large - Failed to decode RF firmware
return ;
}
// A remnant has been detected, allocate data for it plus a null termination byte
size_t remnant_sz = upload . currentSize - result ;
efm8bb1_update = ( uint8_t * ) malloc ( remnant_sz + 1 ) ;
2019-03-26 17:26:50 +00:00
if ( efm8bb1_update = = nullptr ) {
2018-10-10 21:21:44 +01:00
upload_error = 2 ; // Not enough space - Unable to allocate memory to store new RF firmware
return ;
}
memcpy ( efm8bb1_update , upload . buf + result , remnant_sz ) ;
// Add null termination at the end of of remnant buffer
efm8bb1_update [ remnant_sz ] = ' \0 ' ;
}
}
# endif // USE_RF_FLASH
else { // firmware
if ( ! upload_error & & ( Update . write ( upload . buf , upload . currentSize ) ! = upload . currentSize ) ) {
upload_error = 5 ; // Upload buffer miscompare
return ;
}
if ( _serialoutput ) {
Serial . printf ( " . " ) ;
upload_progress_dot_count + + ;
if ( ! ( upload_progress_dot_count % 80 ) ) { Serial . println ( ) ; }
}
}
} else if ( ! upload_error & & ( UPLOAD_FILE_END = = upload . status ) ) {
if ( _serialoutput & & ( upload_progress_dot_count % 80 ) ) {
Serial . println ( ) ;
}
if ( UPL_SETTINGS = = upload_file_type ) {
if ( config_xor_on_set ) {
for ( uint16_t i = 2 ; i < sizeof ( Settings ) ; i + + ) {
settings_buffer [ i ] ^ = ( config_xor_on_set + i ) ;
}
}
bool valid_settings = false ;
unsigned long buffer_version = settings_buffer [ 11 ] < < 24 | settings_buffer [ 10 ] < < 16 | settings_buffer [ 9 ] < < 8 | settings_buffer [ 8 ] ;
if ( buffer_version > 0x06000000 ) {
uint16_t buffer_size = settings_buffer [ 3 ] < < 8 | settings_buffer [ 2 ] ;
uint16_t buffer_crc = settings_buffer [ 15 ] < < 8 | settings_buffer [ 14 ] ;
uint16_t crc = 0 ;
for ( uint16_t i = 0 ; i < buffer_size ; i + + ) {
if ( ( i < 14 ) | | ( i > 15 ) ) { crc + = settings_buffer [ i ] * ( i + 1 ) ; } // Skip crc
}
valid_settings = ( buffer_crc = = crc ) ;
} else {
valid_settings = ( settings_buffer [ 0 ] = = CONFIG_FILE_SIGN ) ;
}
if ( valid_settings ) {
SettingsDefaultSet2 ( ) ;
memcpy ( ( char * ) & Settings + 16 , settings_buffer + 16 , sizeof ( Settings ) - 16 ) ;
Settings . version = buffer_version ; // Restore version and auto upgrade after restart
SettingsBufferFree ( ) ;
} else {
upload_error = 8 ; // File invalid
return ;
}
}
# ifdef USE_RF_FLASH
else if ( UPL_EFM8BB1 = = upload_file_type ) {
// RF FW flash done
upload_file_type = UPL_TASMOTA ;
}
# endif // USE_RF_FLASH
else {
if ( ! Update . end ( true ) ) { // true to set the size to the current progress
if ( _serialoutput ) { Update . printError ( Serial ) ; }
upload_error = 6 ; // Upload failed. Enable logging 3
return ;
}
}
if ( ! upload_error ) {
2019-03-08 14:15:42 +00:00
AddLog_P2 ( LOG_LEVEL_INFO , PSTR ( D_LOG_UPLOAD D_SUCCESSFUL " %u bytes. " D_RESTARTING ) , upload . totalSize ) ;
2018-10-10 21:21:44 +01:00
}
} else if ( UPLOAD_FILE_ABORTED = = upload . status ) {
restart_flag = 0 ;
MqttRetryCounter ( 0 ) ;
upload_error = 7 ; // Upload aborted
if ( UPL_TASMOTA = = upload_file_type ) { Update . end ( ) ; }
}
delay ( 0 ) ;
}
/*-------------------------------------------------------------------------------------------*/
2018-11-14 13:32:09 +00:00
void HandlePreflightRequest ( void )
2018-10-10 21:21:44 +01:00
{
WebServer - > sendHeader ( F ( " Access-Control-Allow-Origin " ) , F ( " * " ) ) ;
WebServer - > sendHeader ( F ( " Access-Control-Allow-Methods " ) , F ( " GET, POST " ) ) ;
WebServer - > sendHeader ( F ( " Access-Control-Allow-Headers " ) , F ( " authorization " ) ) ;
2019-02-23 14:29:42 +00:00
WSSend ( 200 , CT_HTML , " " ) ;
2018-10-10 21:21:44 +01:00
}
/*-------------------------------------------------------------------------------------------*/
2018-11-14 13:32:09 +00:00
void HandleHttpCommand ( void )
2018-10-10 21:21:44 +01:00
{
2019-01-10 11:57:42 +00:00
if ( ! HttpCheckPriviledgedAccess ( false ) ) { return ; }
2018-10-10 21:21:44 +01:00
AddLog_P ( LOG_LEVEL_DEBUG , PSTR ( D_LOG_HTTP D_COMMAND ) ) ;
uint8_t valid = 1 ;
if ( Settings . web_password [ 0 ] ! = 0 ) {
2019-02-23 17:38:36 +00:00
char tmp1 [ sizeof ( Settings . web_password ) ] ;
2018-10-10 21:21:44 +01:00
WebGetArg ( " user " , tmp1 , sizeof ( tmp1 ) ) ;
2019-02-23 17:38:36 +00:00
char tmp2 [ sizeof ( Settings . web_password ) ] ;
2018-10-10 21:21:44 +01:00
WebGetArg ( " password " , tmp2 , sizeof ( tmp2 ) ) ;
if ( ! ( ! strcmp ( tmp1 , WEB_USERNAME ) & & ! strcmp ( tmp2 , Settings . web_password ) ) ) { valid = 0 ; }
}
2019-03-16 15:23:41 +00:00
WSContentBegin ( 200 , CT_JSON ) ;
2018-10-10 21:21:44 +01:00
if ( valid ) {
2019-01-28 13:08:33 +00:00
uint8_t curridx = web_log_index ;
2019-02-22 11:04:05 +00:00
String svalue = WebServer - > arg ( " cmnd " ) ;
if ( svalue . length ( ) & & ( svalue . length ( ) < INPUT_BUFFER_SIZE ) ) {
2019-02-23 11:48:39 +00:00
ExecuteWebCommand ( ( char * ) svalue . c_str ( ) , SRC_WEBCOMMAND ) ;
2018-10-10 21:21:44 +01:00
if ( web_log_index ! = curridx ) {
2019-01-28 13:08:33 +00:00
uint8_t counter = curridx ;
2019-03-16 15:23:41 +00:00
WSContentSend_P ( PSTR ( " { " ) ) ;
bool cflg = false ;
2018-10-10 21:21:44 +01:00
do {
char * tmp ;
size_t len ;
GetLog ( counter , & tmp , & len ) ;
if ( len ) {
// [14:49:36 MQTT: stat/wemos5/RESULT = {"POWER":"OFF"}] > [{"POWER":"OFF"}]
char * JSON = ( char * ) memchr ( tmp , ' { ' , len ) ;
if ( JSON ) { // Is it a JSON message (and not only [15:26:08 MQT: stat/wemos5/POWER = O])
size_t JSONlen = len - ( JSON - tmp ) ;
2019-02-21 13:31:31 +00:00
if ( JSONlen > sizeof ( mqtt_data ) ) { JSONlen = sizeof ( mqtt_data ) ; }
2019-03-16 15:23:41 +00:00
char stemp [ JSONlen ] ;
strlcpy ( stemp , JSON + 1 , JSONlen - 2 ) ;
WSContentSend_P ( PSTR ( " %s%s " ) , ( cflg ) ? " , " : " " , stemp ) ;
cflg = true ;
2018-10-10 21:21:44 +01:00
}
}
counter + + ;
if ( ! counter ) counter + + ; // Skip 0 as it is not allowed
} while ( counter ! = web_log_index ) ;
2019-03-16 15:23:41 +00:00
WSContentSend_P ( PSTR ( " } " ) ) ;
2018-10-10 21:21:44 +01:00
} else {
2019-03-16 15:23:41 +00:00
WSContentSend_P ( PSTR ( " { \" " D_RSLT_WARNING " \" : \" " D_ENABLE_WEBLOG_FOR_RESPONSE " \" } " ) ) ;
2018-10-10 21:21:44 +01:00
}
} else {
2019-03-16 15:23:41 +00:00
WSContentSend_P ( PSTR ( " { \" " D_RSLT_WARNING " \" : \" " D_ENTER_COMMAND " cmnd= \" } " ) ) ;
2018-10-10 21:21:44 +01:00
}
} else {
2019-03-16 15:23:41 +00:00
WSContentSend_P ( PSTR ( " { \" " D_RSLT_WARNING " \" : \" " D_NEED_USER_AND_PASSWORD " \" } " ) ) ;
2018-10-10 21:21:44 +01:00
}
2019-03-16 15:23:41 +00:00
WSContentEnd ( ) ;
2018-10-10 21:21:44 +01:00
}
/*-------------------------------------------------------------------------------------------*/
2018-11-14 13:32:09 +00:00
void HandleConsole ( void )
2018-10-10 21:21:44 +01:00
{
2019-01-10 11:57:42 +00:00
if ( ! HttpCheckPriviledgedAccess ( ) ) { return ; }
2019-03-01 17:25:46 +00:00
if ( WebServer - > hasArg ( " c2 " ) ) { // Console refresh requested
HandleConsoleRefresh ( ) ;
return ;
}
2018-10-10 21:21:44 +01:00
AddLog_P ( LOG_LEVEL_DEBUG , S_LOG_HTTP , S_CONSOLE ) ;
2019-03-10 14:36:34 +00:00
WSContentStart_P ( S_CONSOLE ) ;
2019-03-04 17:16:07 +00:00
WSContentSend_P ( HTTP_SCRIPT_CONSOL , Settings . web_refresh ) ;
WSContentSendStyle ( ) ;
2019-03-10 14:36:34 +00:00
WSContentSend_P ( HTTP_FORM_CMND ) ;
2019-03-11 09:38:41 +00:00
WSContentSpaceButton ( BUTTON_MAIN ) ;
2019-03-16 15:23:41 +00:00
WSContentStop ( ) ;
2018-10-10 21:21:44 +01:00
}
2019-03-01 17:25:46 +00:00
void HandleConsoleRefresh ( void )
2018-10-10 21:21:44 +01:00
{
2019-01-28 13:08:33 +00:00
bool cflg = true ;
uint8_t counter = 0 ; // Initial start, should never be 0 again
2018-10-10 21:21:44 +01:00
2019-02-22 11:04:05 +00:00
String svalue = WebServer - > arg ( " c1 " ) ;
if ( svalue . length ( ) & & ( svalue . length ( ) < INPUT_BUFFER_SIZE ) ) {
2019-03-08 14:15:42 +00:00
AddLog_P2 ( LOG_LEVEL_INFO , PSTR ( D_LOG_COMMAND " %s " ) , svalue . c_str ( ) ) ;
2019-02-22 11:04:05 +00:00
ExecuteWebCommand ( ( char * ) svalue . c_str ( ) , SRC_WEBCONSOLE ) ;
2018-10-10 21:21:44 +01:00
}
2019-02-23 17:38:36 +00:00
char stmp [ 8 ] ;
2019-02-22 11:04:05 +00:00
WebGetArg ( " c2 " , stmp , sizeof ( stmp ) ) ;
if ( strlen ( stmp ) ) { counter = atoi ( stmp ) ; }
2018-10-10 21:21:44 +01:00
2019-03-16 15:23:41 +00:00
WSContentBegin ( 200 , CT_PLAIN ) ;
WSContentSend_P ( PSTR ( " %d}1%d}1 " ) , web_log_index , reset_web_log_flag ) ;
2018-10-10 21:21:44 +01:00
if ( ! reset_web_log_flag ) {
counter = 0 ;
2019-01-28 13:08:33 +00:00
reset_web_log_flag = true ;
2018-10-10 21:21:44 +01:00
}
if ( counter ! = web_log_index ) {
if ( ! counter ) {
counter = web_log_index ;
2019-01-28 13:08:33 +00:00
cflg = false ;
2018-10-10 21:21:44 +01:00
}
do {
char * tmp ;
size_t len ;
GetLog ( counter , & tmp , & len ) ;
if ( len ) {
2019-02-21 13:31:31 +00:00
if ( len > sizeof ( mqtt_data ) - 2 ) { len = sizeof ( mqtt_data ) ; }
2019-03-16 15:23:41 +00:00
char stemp [ len + 1 ] ;
strlcpy ( stemp , tmp , len ) ;
WSContentSend_P ( PSTR ( " %s%s " ) , ( cflg ) ? " \n " : " " , stemp ) ;
cflg = true ;
2018-10-10 21:21:44 +01:00
}
counter + + ;
2019-03-16 15:23:41 +00:00
if ( ! counter ) { counter + + ; } // Skip log index 0 as it is not allowed
2018-10-10 21:21:44 +01:00
} while ( counter ! = web_log_index ) ;
}
2019-03-16 15:23:41 +00:00
WSContentSend_P ( PSTR ( " }1 " ) ) ;
WSContentEnd ( ) ;
2018-10-10 21:21:44 +01:00
}
/********************************************************************************************/
2018-11-14 13:32:09 +00:00
void HandleNotFound ( void )
2018-10-10 21:21:44 +01:00
{
2019-03-08 14:15:42 +00:00
// AddLog_P2(LOG_LEVEL_DEBUG, PSTR(D_LOG_HTTP "Not fount (%s)"), WebServer->uri().c_str());
2018-10-10 21:21:44 +01:00
if ( CaptivePortal ( ) ) { return ; } // If captive portal redirect instead of displaying the error page.
# ifdef USE_EMULATION
String path = WebServer - > uri ( ) ;
if ( ( EMUL_HUE = = Settings . flag2 . emulation ) & & ( path . startsWith ( " /api " ) ) ) {
HandleHueApi ( & path ) ;
} else
# endif // USE_EMULATION
{
2019-03-23 16:00:59 +00:00
WSContentBegin ( 404 , CT_PLAIN ) ;
WSContentSend_P ( PSTR ( D_FILE_NOT_FOUND " \n \n URI: %s \n Method: %s \n Arguments: %d \n " ) , WebServer - > uri ( ) . c_str ( ) , ( WebServer - > method ( ) = = HTTP_GET ) ? " GET " : " POST " , WebServer - > args ( ) ) ;
2018-10-10 21:21:44 +01:00
for ( uint8_t i = 0 ; i < WebServer - > args ( ) ; i + + ) {
2019-03-23 16:00:59 +00:00
WSContentSend_P ( PSTR ( " %s: %s \n " ) , WebServer - > argName ( i ) . c_str ( ) , WebServer - > arg ( i ) . c_str ( ) ) ;
2018-10-10 21:21:44 +01:00
}
2019-03-23 16:00:59 +00:00
WSContentEnd ( ) ;
2018-10-10 21:21:44 +01:00
}
}
/* Redirect to captive portal if we got a request for another domain. Return true in that case so the page handler do not try to handle the request again. */
2019-01-28 13:08:33 +00:00
bool CaptivePortal ( void )
2018-10-10 21:21:44 +01:00
{
2019-04-02 19:26:30 +01:00
// Possible hostHeader: connectivitycheck.gstatic.com or 192.168.4.1
2019-03-31 16:57:28 +01:00
if ( ( WifiIsInManagerMode ( ) ) & & ! ValidIpAddress ( WebServer - > hostHeader ( ) . c_str ( ) ) ) {
2018-10-10 21:21:44 +01:00
AddLog_P ( LOG_LEVEL_DEBUG , PSTR ( D_LOG_HTTP D_REDIRECTED ) ) ;
WebServer - > sendHeader ( F ( " Location " ) , String ( " http:// " ) + WebServer - > client ( ) . localIP ( ) . toString ( ) , true ) ;
2019-02-23 14:29:42 +00:00
WSSend ( 302 , CT_PLAIN , " " ) ; // Empty content inhibits Content-length header so we have to close the socket ourselves.
2018-10-10 21:21:44 +01:00
WebServer - > client ( ) . stop ( ) ; // Stop is needed because we sent no content length
return true ;
}
return false ;
}
/*********************************************************************************************/
String UrlEncode ( const String & text )
{
const char hex [ ] = " 0123456789ABCDEF " ;
String encoded = " " ;
int len = text . length ( ) ;
int i = 0 ;
while ( i < len ) {
char decodedChar = text . charAt ( i + + ) ;
/*
if ( ( ' a ' < = decodedChar & & decodedChar < = ' z ' ) | |
( ' A ' < = decodedChar & & decodedChar < = ' Z ' ) | |
( ' 0 ' < = decodedChar & & decodedChar < = ' 9 ' ) | |
( ' = ' = = decodedChar ) ) {
encoded + = decodedChar ;
} else {
encoded + = ' % ' ;
encoded + = hex [ decodedChar > > 4 ] ;
encoded + = hex [ decodedChar & 0xF ] ;
}
*/
if ( ' ' = = decodedChar ) {
encoded + = ' % ' ;
encoded + = hex [ decodedChar > > 4 ] ;
encoded + = hex [ decodedChar & 0xF ] ;
} else {
encoded + = decodedChar ;
}
}
return encoded ;
}
int WebSend ( char * buffer )
{
2019-02-19 11:51:38 +00:00
// [sonoff] POWER1 ON --> Sends http://sonoff/cm?cmnd=POWER1 ON
// [192.168.178.86:80,admin:joker] POWER1 ON --> Sends http://hostname:80/cm?user=admin&password=joker&cmnd=POWER1 ON
// [sonoff] /any/link/starting/with/a/slash.php?log=123 --> Sends http://sonoff/any/link/starting/with/a/slash.php?log=123
// [sonoff,admin:joker] /any/link/starting/with/a/slash.php?log=123 --> Sends http://sonoff/any/link/starting/with/a/slash.php?log=123
2018-10-10 21:21:44 +01:00
char * host ;
char * user ;
char * password ;
char * command ;
int status = 1 ; // Wrong parameters
2018-11-12 11:33:49 +00:00
// buffer = | [ 192.168.178.86 : 80 , admin : joker ] POWER1 ON |
host = strtok_r ( buffer , " ] " , & command ) ; // host = | [ 192.168.178.86 : 80 , admin : joker |, command = | POWER1 ON |
2018-10-10 21:21:44 +01:00
if ( host & & command ) {
2019-02-23 11:48:39 +00:00
RemoveSpace ( host ) ; // host = |[192.168.178.86:80,admin:joker|
host + + ; // host = |192.168.178.86:80,admin:joker| - Skip [
host = strtok_r ( host , " , " , & user ) ; // host = |192.168.178.86:80|, user = |admin:joker|
2019-02-22 14:19:59 +00:00
String url = F ( " http:// " ) ; // url = |http://|
2019-02-23 11:48:39 +00:00
url + = host ; // url = |http://192.168.178.86:80|
2019-02-22 11:04:05 +00:00
2018-11-12 11:33:49 +00:00
command = Trim ( command ) ; // command = |POWER1 ON| or |/any/link/starting/with/a/slash.php?log=123|
2018-11-12 10:13:05 +00:00
if ( command [ 0 ] ! = ' / ' ) {
2019-02-19 11:51:38 +00:00
url + = F ( " /cm? " ) ; // url = |http://192.168.178.86/cm?|
2019-02-23 11:48:39 +00:00
if ( user ) {
user = strtok_r ( user , " : " , & password ) ; // user = |admin|, password = |joker|
if ( user & & password ) {
char userpass [ 128 ] ;
snprintf_P ( userpass , sizeof ( userpass ) , PSTR ( " user=%s&password=%s& " ) , user , password ) ;
url + = userpass ; // url = |http://192.168.178.86/cm?user=admin&password=joker&|
}
2018-11-12 10:13:05 +00:00
}
2019-02-19 11:51:38 +00:00
url + = F ( " cmnd= " ) ; // url = |http://192.168.178.86/cm?cmnd=| or |http://192.168.178.86/cm?user=admin&password=joker&cmnd=|
2018-10-10 21:21:44 +01:00
}
2019-02-19 11:51:38 +00:00
url + = command ; // url = |http://192.168.178.86/cm?cmnd=POWER1 ON|
2018-10-10 21:21:44 +01:00
2019-03-08 14:15:42 +00:00
//AddLog_P2(LOG_LEVEL_DEBUG, PSTR("DBG: Uri |%s|"), url.c_str());
2018-10-10 21:21:44 +01:00
2019-02-21 08:48:58 +00:00
# if defined(ARDUINO_ESP8266_RELEASE_2_3_0) || defined(ARDUINO_ESP8266_RELEASE_2_4_0) || defined(ARDUINO_ESP8266_RELEASE_2_4_1) || defined(ARDUINO_ESP8266_RELEASE_2_4_2)
2019-02-19 11:51:38 +00:00
HTTPClient http ;
if ( http . begin ( UrlEncode ( url ) ) ) { // UrlEncode(url) = |http://192.168.178.86/cm?cmnd=POWER1%20ON|
2019-02-21 08:48:58 +00:00
# else
WiFiClient http_client ;
HTTPClient http ;
2019-02-21 13:56:39 +00:00
if ( http . begin ( http_client , UrlEncode ( url ) ) ) { // UrlEncode(url) = |http://192.168.178.86/cm?cmnd=POWER1%20ON|
2019-02-21 08:48:58 +00:00
# endif
2019-02-19 11:51:38 +00:00
int http_code = http . GET ( ) ; // Start connection and send HTTP header
if ( http_code > 0 ) { // http_code will be negative on error
if ( http_code = = HTTP_CODE_OK | | http_code = = HTTP_CODE_MOVED_PERMANENTLY ) {
/*
// Return received data to the user - Adds 900+ bytes to the code
String result = http . getString ( ) ; // File found at server - may need lot of ram or trigger out of memory!
uint16_t j = 0 ;
for ( uint16_t i = 0 ; i < result . length ( ) ; i + + ) {
char text = result . charAt ( i ) ;
if ( text > 31 ) { // Remove control characters like linefeed
2019-02-21 08:48:58 +00:00
mqtt_data [ j + + ] = text ;
2019-02-19 11:51:38 +00:00
if ( j = = sizeof ( mqtt_data ) - 2 ) { break ; }
}
}
mqtt_data [ j ] = ' \0 ' ;
MqttPublishPrefixTopic_P ( RESULT_OR_STAT , PSTR ( D_CMND_WEBSEND ) ) ;
*/
}
2018-10-10 21:21:44 +01:00
status = 0 ; // No error - Done
} else {
status = 2 ; // Connection failed
}
2019-02-21 08:48:58 +00:00
http . end ( ) ; // Clean up connection data
2018-10-10 21:21:44 +01:00
} else {
2019-02-19 11:51:38 +00:00
status = 3 ; // Host not found or connection error
2018-10-10 21:21:44 +01:00
}
}
return status ;
}
/*********************************************************************************************/
Add command WebColor
* Add rule Http#Initialized
* Add command WebColor to change non-persistent GUI colors on the fly
Use a rule like:
rule3 on http#initialized do webcolor {"webcolor":["#eeeeee","#181818","#4f4f4f","#000000","#dddddd","#008000","#222222","#ff0000","#008000","#ffffff","#1fa3ec","#0e70a4","#d43535","#931f1f","#47c266","#5aaf6f","#ffffff","#999999","#000000"]} endon
or
rule3 on http#initialized do webcolor {"webcolor":["#eee","#181818","#4f4f4f","#000","#ddd","#008000","#222"]} endon
to make color changes persistent)
2019-04-08 21:37:39 +01:00
bool JsonWebColor ( const char * dataBuf )
{
// Default (light)
// {"WebColor":["#000000","#ffffff","#f2f2f2","#000000","#ffffff","#000000","#ffffff","#ff0000","#008000","#ffffff","#1fa3ec","#0e70a4","#d43535","#931f1f","#47c266","#5aaf6f","#ffffff","#999999","#000000"]}
// Alternative (Dark)
// {"webcolor":["#eeeeee","#181818","#4f4f4f","#000000","#dddddd","#008000","#222222","#ff0000","#008000","#ffffff","#1fa3ec","#0e70a4","#d43535","#931f1f","#47c266","#5aaf6f","#ffffff","#999999","#000000"]}
char dataBufLc [ strlen ( dataBuf ) + 1 ] ;
LowerCase ( dataBufLc , dataBuf ) ;
RemoveSpace ( dataBufLc ) ;
if ( strlen ( dataBufLc ) < 9 ) { return false ; } // Workaround exception if empty JSON like {} - Needs checks
StaticJsonBuffer < 450 > jb ; // 421 from https://arduinojson.org/v5/assistant/
JsonObject & obj = jb . parseObject ( dataBufLc ) ;
if ( ! obj . success ( ) ) { return false ; }
char parm_lc [ 10 ] ;
if ( obj [ LowerCase ( parm_lc , D_CMND_WEBCOLOR ) ] . success ( ) ) {
for ( uint8_t i = 0 ; i < COL_LAST ; i + + ) {
const char * color = obj [ parm_lc ] [ i ] ;
if ( color ! = nullptr ) {
2019-04-09 12:56:19 +01:00
WebHexCode ( i , color ) ;
Add command WebColor
* Add rule Http#Initialized
* Add command WebColor to change non-persistent GUI colors on the fly
Use a rule like:
rule3 on http#initialized do webcolor {"webcolor":["#eeeeee","#181818","#4f4f4f","#000000","#dddddd","#008000","#222222","#ff0000","#008000","#ffffff","#1fa3ec","#0e70a4","#d43535","#931f1f","#47c266","#5aaf6f","#ffffff","#999999","#000000"]} endon
or
rule3 on http#initialized do webcolor {"webcolor":["#eee","#181818","#4f4f4f","#000","#ddd","#008000","#222"]} endon
to make color changes persistent)
2019-04-08 21:37:39 +01:00
}
}
}
return true ;
}
enum WebCommands { CMND_WEBSERVER , CMND_WEBPASSWORD , CMND_WEBLOG , CMND_WEBREFRESH , CMND_WEBSEND , CMND_WEBCOLOR , CMND_EMULATION } ;
const char kWebCommands [ ] PROGMEM = D_CMND_WEBSERVER " | " D_CMND_WEBPASSWORD " | " D_CMND_WEBLOG " | " D_CMND_WEBREFRESH " | " D_CMND_WEBSEND " | " D_CMND_WEBCOLOR " | " D_CMND_EMULATION ;
2018-10-10 21:21:44 +01:00
const char kWebSendStatus [ ] PROGMEM = D_JSON_DONE " | " D_JSON_WRONG_PARAMETERS " | " D_JSON_CONNECT_FAILED " | " D_JSON_HOST_NOT_FOUND ;
2018-11-14 13:32:09 +00:00
bool WebCommand ( void )
2018-10-10 21:21:44 +01:00
{
char command [ CMDSZ ] ;
bool serviced = true ;
int command_code = GetCommandCode ( command , sizeof ( command ) , XdrvMailbox . topic , kWebCommands ) ;
if ( - 1 = = command_code ) {
serviced = false ; // Unknown command
}
if ( CMND_WEBSERVER = = command_code ) {
if ( ( XdrvMailbox . payload > = 0 ) & & ( XdrvMailbox . payload < = 2 ) ) { Settings . webserver = XdrvMailbox . payload ; }
if ( Settings . webserver ) {
2019-03-23 16:00:59 +00:00
Response_P ( PSTR ( " { \" " D_CMND_WEBSERVER " \" : \" " D_JSON_ACTIVE_FOR " %s " D_JSON_ON_DEVICE " %s " D_JSON_WITH_IP_ADDRESS " %s \" } " ) ,
2018-10-10 21:21:44 +01:00
( 2 = = Settings . webserver ) ? D_ADMIN : D_USER , my_hostname , WiFi . localIP ( ) . toString ( ) . c_str ( ) ) ;
} else {
2019-03-23 16:00:59 +00:00
Response_P ( S_JSON_COMMAND_SVALUE , command , GetStateText ( 0 ) ) ;
2018-10-10 21:21:44 +01:00
}
}
else if ( CMND_WEBPASSWORD = = command_code ) {
if ( ( XdrvMailbox . data_len > 0 ) & & ( XdrvMailbox . data_len < sizeof ( Settings . web_password ) ) ) {
strlcpy ( Settings . web_password , ( SC_CLEAR = = Shortcut ( XdrvMailbox . data ) ) ? " " : ( SC_DEFAULT = = Shortcut ( XdrvMailbox . data ) ) ? WEB_PASSWORD : XdrvMailbox . data , sizeof ( Settings . web_password ) ) ;
2019-03-23 16:00:59 +00:00
Response_P ( S_JSON_COMMAND_SVALUE , command , Settings . web_password ) ;
2018-10-10 21:21:44 +01:00
} else {
2019-03-23 16:00:59 +00:00
Response_P ( S_JSON_COMMAND_ASTERIX , command ) ;
2018-10-10 21:21:44 +01:00
}
}
else if ( CMND_WEBLOG = = command_code ) {
if ( ( XdrvMailbox . payload > = LOG_LEVEL_NONE ) & & ( XdrvMailbox . payload < = LOG_LEVEL_ALL ) ) { Settings . weblog_level = XdrvMailbox . payload ; }
2019-03-23 16:00:59 +00:00
Response_P ( S_JSON_COMMAND_NVALUE , command , Settings . weblog_level ) ;
2018-10-10 21:21:44 +01:00
}
else if ( CMND_WEBREFRESH = = command_code ) {
if ( ( XdrvMailbox . payload > 999 ) & & ( XdrvMailbox . payload < = 10000 ) ) { Settings . web_refresh = XdrvMailbox . payload ; }
2019-03-23 16:00:59 +00:00
Response_P ( S_JSON_COMMAND_NVALUE , command , Settings . web_refresh ) ;
2018-10-10 21:21:44 +01:00
}
else if ( CMND_WEBSEND = = command_code ) {
if ( XdrvMailbox . data_len > 0 ) {
uint8_t result = WebSend ( XdrvMailbox . data ) ;
char stemp1 [ 20 ] ;
2019-03-23 16:00:59 +00:00
Response_P ( S_JSON_COMMAND_SVALUE , command , GetTextIndexed ( stemp1 , sizeof ( stemp1 ) , result , kWebSendStatus ) ) ;
2018-10-10 21:21:44 +01:00
}
}
Add command WebColor
* Add rule Http#Initialized
* Add command WebColor to change non-persistent GUI colors on the fly
Use a rule like:
rule3 on http#initialized do webcolor {"webcolor":["#eeeeee","#181818","#4f4f4f","#000000","#dddddd","#008000","#222222","#ff0000","#008000","#ffffff","#1fa3ec","#0e70a4","#d43535","#931f1f","#47c266","#5aaf6f","#ffffff","#999999","#000000"]} endon
or
rule3 on http#initialized do webcolor {"webcolor":["#eee","#181818","#4f4f4f","#000","#ddd","#008000","#222"]} endon
to make color changes persistent)
2019-04-08 21:37:39 +01:00
else if ( CMND_WEBCOLOR = = command_code ) {
if ( XdrvMailbox . data_len > 0 ) {
if ( strstr ( XdrvMailbox . data , " { " ) = = nullptr ) { // If no JSON it must be parameter
if ( ( XdrvMailbox . data_len > 3 ) & & ( XdrvMailbox . index > 0 ) & & ( XdrvMailbox . index < = COL_LAST ) ) {
2019-04-09 12:56:19 +01:00
WebHexCode ( XdrvMailbox . index - 1 , XdrvMailbox . data ) ;
Add command WebColor
* Add rule Http#Initialized
* Add command WebColor to change non-persistent GUI colors on the fly
Use a rule like:
rule3 on http#initialized do webcolor {"webcolor":["#eeeeee","#181818","#4f4f4f","#000000","#dddddd","#008000","#222222","#ff0000","#008000","#ffffff","#1fa3ec","#0e70a4","#d43535","#931f1f","#47c266","#5aaf6f","#ffffff","#999999","#000000"]} endon
or
rule3 on http#initialized do webcolor {"webcolor":["#eee","#181818","#4f4f4f","#000","#ddd","#008000","#222"]} endon
to make color changes persistent)
2019-04-08 21:37:39 +01:00
}
else if ( 0 = = XdrvMailbox . payload ) {
2019-04-09 12:56:19 +01:00
SettingsDefaultWebColor ( ) ;
Add command WebColor
* Add rule Http#Initialized
* Add command WebColor to change non-persistent GUI colors on the fly
Use a rule like:
rule3 on http#initialized do webcolor {"webcolor":["#eeeeee","#181818","#4f4f4f","#000000","#dddddd","#008000","#222222","#ff0000","#008000","#ffffff","#1fa3ec","#0e70a4","#d43535","#931f1f","#47c266","#5aaf6f","#ffffff","#999999","#000000"]} endon
or
rule3 on http#initialized do webcolor {"webcolor":["#eee","#181818","#4f4f4f","#000","#ddd","#008000","#222"]} endon
to make color changes persistent)
2019-04-08 21:37:39 +01:00
}
}
else {
JsonWebColor ( XdrvMailbox . data ) ;
}
}
Response_P ( PSTR ( " { \" " D_CMND_WEBCOLOR " \" :[ " ) ) ;
for ( uint8_t i = 0 ; i < COL_LAST ; i + + ) {
if ( i ) { ResponseAppend_P ( PSTR ( " , " ) ) ; }
2019-04-09 12:56:19 +01:00
ResponseAppend_P ( PSTR ( " \" #%06x \" " ) , WebColor ( i ) ) ;
Add command WebColor
* Add rule Http#Initialized
* Add command WebColor to change non-persistent GUI colors on the fly
Use a rule like:
rule3 on http#initialized do webcolor {"webcolor":["#eeeeee","#181818","#4f4f4f","#000000","#dddddd","#008000","#222222","#ff0000","#008000","#ffffff","#1fa3ec","#0e70a4","#d43535","#931f1f","#47c266","#5aaf6f","#ffffff","#999999","#000000"]} endon
or
rule3 on http#initialized do webcolor {"webcolor":["#eee","#181818","#4f4f4f","#000","#ddd","#008000","#222"]} endon
to make color changes persistent)
2019-04-08 21:37:39 +01:00
}
ResponseAppend_P ( PSTR ( " ]} " ) ) ;
}
2018-10-10 21:21:44 +01:00
# ifdef USE_EMULATION
else if ( CMND_EMULATION = = command_code ) {
if ( ( XdrvMailbox . payload > = EMUL_NONE ) & & ( XdrvMailbox . payload < EMUL_MAX ) ) {
Settings . flag2 . emulation = XdrvMailbox . payload ;
restart_flag = 2 ;
}
2019-03-23 16:00:59 +00:00
Response_P ( S_JSON_COMMAND_NVALUE , command , Settings . flag2 . emulation ) ;
2018-10-10 21:21:44 +01:00
}
# endif // USE_EMULATION
else serviced = false ; // Unknown command
return serviced ;
}
/*********************************************************************************************\
* Interface
\ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2019-01-28 13:08:33 +00:00
bool Xdrv01 ( uint8_t function )
2018-10-10 21:21:44 +01:00
{
2019-01-28 13:08:33 +00:00
bool result = false ;
2018-10-10 21:21:44 +01:00
switch ( function ) {
case FUNC_LOOP :
2018-11-26 16:00:18 +00:00
PollDnsWebserver ( ) ;
2018-10-10 21:21:44 +01:00
# ifdef USE_EMULATION
2019-03-30 12:03:45 +00:00
if ( Settings . flag2 . emulation ) { PollUdp ( ) ; }
2018-10-10 21:21:44 +01:00
# endif // USE_EMULATION
break ;
case FUNC_COMMAND :
result = WebCommand ( ) ;
break ;
}
return result ;
}
# endif // USE_WEBSERVER