123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782 |
- //extern ESP8266WebServer httpServer;
- static const char html_head_part1[] PROGMEM =
- "<html><head>"
- "<meta charset='utf-8'>"
- "<meta name='viewport' content='width=device-width,initial-scale=1,user-scalable=no'/>"
- "<title>WiFi-PC-Controller - ";
- static const char html_head_part2[] PROGMEM =
- "</title>"
- "<style>div,fieldset,input,select{padding:5px;font-size:1em;}fieldset{background:#f2f2f2;}p{margin:0.5em 0;}input{width:100%;box-sizing:border-box;-webkit-box-sizing:border-box;-moz-box-sizing:border-box;background:#ffffff;color:#000000;}input[type=checkbox],input[type=radio]{width:1em;margin-right:6px;vertical-align:-1px;}input[type=range]{width:99%;}select{width:100%;background:#ffffff;color:#000000;}textarea{resize:none;width:98%;height:318px;padding:5px;overflow:auto;background:#ffffff;color:#000000;}body{text-align:center;font-family:verdana,sans-serif;background:#ffffff;}td{padding:0px;}button{border:0;border-radius:0.3rem;background:#1fa3ec;color:#ffffff;line-height:2.4rem;font-size:1.2rem;width:100%;-webkit-transition-duration:0.4s;transition-duration:0.4s;cursor:pointer;}button:hover{background:#0e70a4;}.bred{background:#d43535;}.bred:hover{background:#931f1f;}.bgrn{background:#47c266;}.bgrn:hover{background:#5aaf6f;}a{color:#1fa3ec;text-decoration:none;}.p{float:left;text-align:left;}.q{float:right;text-align:right;}.r{border-radius:0.3em;padding:2px;margin:6px 2px;}</style>";
- static const char html_root_script[] PROGMEM = R"(
- <script>
- function g(i) { return document.getElementById(i) };
- var xhttp, updateTime, reqTime, reqFin;
- function sendBtn(btn, conf) {
- var frmn='BtnFrm'+btn;
- var form = g(frmn);
- if(conf !== undefined) {
- if(confirm(conf)) return transmit(form);
- else return false;
- }
- else return transmit(form);
- }
- function transmit(f) {
- if (!xhttp) {
- reqTime = 0;
- reqFin = false;
- xhttp = new XMLHttpRequest();
- xhttp.timeout = 1000;
- xhttp.overrideMimeType("application/json");
- xhttp.open('POST', 'api');
- xhttp.send(f ? (new FormData(f)) : '');
- xhttp.onreadystatechange = function () {
- if (xhttp.readyState === XMLHttpRequest.DONE && xhttp.status === 200) {
- var data = JSON.parse(xhttp.responseText);
- g('PCstate').innerHTML = data.PCstate;
- if(data.PCstate == "ON") {
- g('PCstate').style.fontWeight = "bold";
- }
- else g('PCstate').style.fontWeight = "normal";
- if(data.ssid !== undefined) g('ssid').innerHTML = data.ssid;
- if(data.mqttstate !== undefined) {
- if(data.mqttstate == "CONNECTED" && data.mqtthost !== undefined) {
- g('mqttstate').innerHTML = data.mqttstate + ' to <i>' + data.mqtthost + '</i>';
- }
- else g('mqttstate').innerHTML = data.mqttstate;
- }
- if(data.uptime !== undefined) g('uptime').innerHTML = data.uptime;
- if(data.mqttreconn !== undefined) g('mqttreconn').innerHTML = data.mqttreconn;
- xhttp = null;
- updateTime = 0;
- reqFin = true;
- }
- else {
- if(!reqFin && reqTime > 10) {
- xhttp = null;
- reqFin = true;
- }
- }
- }
- }
- return false;
- }
- function init() {
- transmit();
- }
- setInterval(transmit, 2500);
- </script>
- )"; // html_root_script
- static const char html_confweb_script[] PROGMEM = R"(
- <script>
- function g(i) { return document.getElementById(i) };
- function sp(i){g(i).type=(g(i).type==='text'?'password':'text');}
- var xhttp, reqTime, reqFin;
- function setCbx(el, da) {
- if(da == '1') {
- el.checked = true;
- el.style.visibility = 'visible';
- }
- else {
- el.checked = false;
- el.style.visibility = 'visible';
- }
- }
- function updCbxVal(el) {
- if (el.checked) el.value = '1';
- else {
- el.checked = true;
- el.value = '0';
- el.style.visibility = 'hidden';
- }
- }
-
- function transmit(f) {
- if (!xhttp) {
- reqTime = 0;
- reqFin = false;
- updCbxVal(g('httpAuth'));
- xhttp = new XMLHttpRequest();
- xhttp.timeout = 1000;
- xhttp.overrideMimeType('application/json');
- xhttp.open('POST', 'confdweb');
- xhttp.send(f ? (new FormData(f)) : '');
- xhttp.onreadystatechange = function () {
- if (xhttp.readyState === XMLHttpRequest.DONE && xhttp.status === 200) {
- var data = JSON.parse(xhttp.responseText);
- g('devName').value = data.devName;
- g('apiToken').value = data.apiToken;
- g('httpUA').value = data.httpUA;
- setCbx(g('httpAuth'), data.httpAuth);
- g('httpU1').value = data.httpU1;
- g('httpU2').value = data.httpU2;
- xhttp = null;
- reqFin = true;
- }
- else {
- if(!reqFin && reqTime > 10) {
- xhttp = null;
- reqFin = true;
- }
- }
- }
- }
- return false;
- }
- //transmit();
- function saveConf() {
- updCbxVal(g('httpAuth'));
- if(g('httpPA').value != g('httpPAC').value) {
- alert("Admin password verification failed!");
- }
- else g('frmConf').submit();
- }
- function init() {
- transmit();
- setCbx(g('httpPASet'), 0);
- setCbx(g('httpP1Set'), 0);
- setCbx(g('httpP2Set'), 0);
- }
- setInterval(function () { ++reqTime; }, 1000);
- </script>
- )"; // html_confweb_script
- static const char html_confmqtt_script[] PROGMEM = R"(
- <script>
- function g(i) { return document.getElementById(i) };
- function sp(i){g(i).type=(g(i).type==='text'?'password':'text');}
- var xhttp, reqTime, reqFin;
- function setCbx(el, da) {
- if(da == '1') {
- el.checked = true;
- el.style.visibility = 'visible';
- }
- else {
- el.checked = false;
- el.style.visibility = 'visible';
- }
- }
- function updCbxVal(el) {
- if (el.checked) el.value = '1';
- else {
- el.checked = true;
- el.value = '0';
- el.style.visibility = 'hidden';
- }
- }
-
- function transmit(f) {
- if (!xhttp) {
- reqTime = 0;
- reqFin = false;
- updCbxVal(g('outRet'));
- updCbxVal(g('willRet'));
- xhttp = new XMLHttpRequest();
- xhttp.timeout = 1000;
- xhttp.overrideMimeType('application/json');
- xhttp.open('POST', 'confdmqtt');
- xhttp.send(f ? (new FormData(f)) : '');
- xhttp.onreadystatechange = function () {
- if (xhttp.readyState === XMLHttpRequest.DONE && xhttp.status === 200) {
- var data = JSON.parse(xhttp.responseText);
- g('mqttHost').value = data.mqttHost;
- g('mqttPort').value = data.mqttPort;
- g('mqttUser').value = data.mqttUser;
- g('inTop').value = data.inTop;
- g('outTop').value = data.outTop;
- g('willTop').value = data.willTop;
- g('willQos').value = data.willQos;
- setCbx(g('outRet'), data.outRet);
- setCbx(g('willRet'), data.willRet);
- g('willMsg').value = data.willMsg;
- g('connMsg').value = data.connMsg;
- xhttp = null;
- reqFin = true;
- }
- else {
- if(!reqFin && reqTime > 10) {
- xhttp = null;
- reqFin = true;
- }
- }
- }
- }
- return false;
- }
- //transmit();
- function saveConf() {
- updCbxVal(g('outRet'));
- updCbxVal(g('willRet'));
- g('frmConf').submit();
- }
- function init() {
- transmit();
- setCbx(g('mqttPassSet'), 0);
- }
- setInterval(function () { ++reqTime; }, 1000);
- </script>
- )"; // html_confmqtt_script
- static const char html_head_part3[] PROGMEM =
- R"(</head>)"; // html_head_part3
- static const char html_bodytag_jsinit[] PROGMEM = R"(
- <body onload='init()'>)";
- static const char html_bodytag[] PROGMEM = R"(
- <body>)";
- static const char html_body_part1[] PROGMEM = R"(
- <div style='text-align:left;display:inline-block;color:#000000;min-width:340px;'>
- <div style='text-align:center;color:#000000;'><noscript>Please enable JavaScript<br></noscript>
- <h3>WiFi-PC-Controller</h3>
- <h2>
- )"; // html_body_part1
- static const char html_body_part2[] PROGMEM = R"(</h2>
- </div>)"; // html_body_part2
- static const char html_root_body[] PROGMEM = R"(
- <div id="PCstate" style="text-align:center;font-weight:normal;font-size:50px"></div>
- <p><form id='BtnFrmPwr'><input type='hidden' name='BtnPwr' value='1'><button onclick='return sendBtn("Pwr")'>PC Power-Button</button></form></p>
- <p><form id='BtnFrmPwrH'><input type='hidden' name='BtnPwrH' value='1'><button onclick='return sendBtn("PwrH","Confirm PC Power Hold")' class='button bred'>PC Power-Button HOLD</button></form></p>
- <p><form id='BtnFrmRes'><input type='hidden' name='BtnRes' value='1'><button onclick='return sendBtn("Res", "Confirm PC Reset")' class='button bred'>PC Reset-Button</button></form></p>
- <div></div>
- <div></div>
- )"; // html_root_body
- static const char html_root_body_adminonly[] PROGMEM = R"(
- <p><form action='conf' method='get'><button>Configuration</button></form></p>
- <p><form action='update' method='get'><button>Firmware update</button></form></p>
- )"; // html_root_body_adminonly
- static const char html_root_body2[] PROGMEM = R"(
- <p><form action='/' method='get' onsubmit='return confirm("Confirm Restart");'><button name='restart' class='button bred'>Restart</button></form></p>
- <hr/>
- <p>WiFi connected to <i><span id='ssid'></span></i></p>
- <p>MQTT <span id='mqttstate'></span></p>
- <p>MQTT reconnects: <span id='mqttreconn'></span></p>
- <p>Uptime: <span id='uptime'></span></p>
- )"; // html_root_body2
- static const char html_conf_body[] PROGMEM = R"(
- <p><form action='wifi.htm' method='get'><button>Configure WiFi</button></form></p>
- <p><form action='confweb' method='get'><button>Configure Web</button></form></p>
- <p><form action='confmqtt' method='get'><button>Configure MQTT</button></form></p>
- <div></div>
- <p><form action='/' method='get' onsubmit='return confirm("Confirm Restart");'><button name='restart' class='button bred'>Restart</button></form></p>
- <p><form action='./' method='get'><button>Main Menu</button></form></p>
- )"; // html_conf_body
- static const char html_confweb_body[] PROGMEM = R"(
- <fieldset>
- <legend><b> Web Configuration </b></legend>
- <form id='frmConf' action='setConfWeb' method='POST'>
- <p><b>Device Name</b><br><input type='text' name='devName' id='devName'></p>
- <p><b>API Token</b><br><input type='text' name='apiToken' id='apiToken'></p>
- <div><hr></div>
- <p><b>HTTP Admin Username *</b><br><input type='text' name='httpUA' id='httpUA'></p>
- <p><b>HTTP Admin Password *</b><input type='checkbox' id='httpPASet' name='httpPASet' onclick='sp("httpPA")'><br><input type='password' name='httpPA' id='httpPA'></p>
- <p><b>Confirm Password *</b><br><input type='password' name='httpPAC' id='httpPAC'></p>
- <div><hr></div>
- <p><b>Enable User-Authentication *</b> <input type='checkbox' name='httpAuth' id='httpAuth'></p>
- <p><b>HTTP User 1 *</b><br><input type='text' name='httpU1' id='httpU1'></p>
- <p><b>HTTP User 1 Password *</b><input type='checkbox' id='httpP1Set' name='httpP1Set' onclick='sp("httpP1")'><br><input type='password' name='httpP1' id='httpP1'></p>
- <div><hr></div>
- <p><b>HTTP User 2 *</b><br><input type='text' name='httpU2' id='httpU2'></p>
- <p><b>HTTP User 2 Password *</b><input type='checkbox' id='httpP2Set' name='httpP2Set' onclick='sp("httpP2")'><br><input type='password' name='httpP2' id='httpP2'></p>
- </form>
- <p><button onclick='return saveConf()'>Save</button></p>
- <p><button onclick='return transmit()'>Reload</button></p>
- </fieldset>
- <p><form action='conf' method='get'><button>Configuration</button></form></p>
- <p><form action='/' method='get' onsubmit='return confirm("Confirm Restart");'><button name='restart' class='button bred'>Restart</button></form></p>
- )"; // html_confweb_body
- static const char html_confmqtt_body[] PROGMEM = R"(
- <fieldset>
- <legend><b> MQTT Configuration </b></legend>
- <form id='frmConf' action='setConfMqtt' method='POST'>
- <p><b>MQTT Server *</b><br><input type='text' name='mqttHost' id='mqttHost'></p>
- <p><b>MQTT Port *</b><br><input type='number' name='mqttPort' id='mqttPort'></p>
- <p><b>MQTT User *</b><br><input type='text' name='mqttUser' id='mqttUser'></p>
- <p><b>MQTT Password *</b><input type='checkbox' id='mqttPassSet' name='mqttPassSet' onclick='sp("mqttPass")'><br><input type='password' name='mqttPass' id='mqttPass'></p>
- <p><b>In Topic *</b><br><input type='text' name='inTop' id='inTop'></p>
- <p><b>Out Topic</b><br><input type='text' name='outTop' id='outTop'></p>
- <p><b>Out Retain *</b> <input type='checkbox' name='outRet' id='outRet'></p>
- <p><b>LastWill Topic *</b><br><input type='text' name='willTop' id='willTop'></p>
- <p><b>LastWill Qos *</b><br><select name='willQos' id='willQos'><option>0</option><option>1</option><option selected='selected'>2</option></select></p>
- <p><b>LastWill Retain *</b> <input type='checkbox' name='willRet' id='willRet'></p>
- <p><b>LastWill Message *</b><br><input type='text' name='willMsg' id='willMsg'></p>
- <p><b>Connect Message *</b><br><input type='text' name='connMsg' id='connMsg'></p>
- </form>
- <p><button onclick='return saveConf()'>Save</button></p>
- <p><button onclick='return transmit()'>Reload</button></p>
- </fieldset>
- <p><form action='conf' method='get'><button>Configuration</button></form></p>
- <p><form action='/' method='get' onsubmit='return confirm("Confirm Restart");'><button name='restart' class='button bred'>Restart</button></form></p>
- )"; // html_confmqtt_body
- static const char html_confsaved_body[] PROGMEM = R"(
- <script>setTimeout(function(){window.location.href = '.';}, 7000);</script>
- <div>Config saved. Restarting...</div>
- <p><form action='./' method='get'><button>Main Menu</button></form></p>
- )"; // html_confsaved_body
- static const char html_restarting_body[] PROGMEM = R"(
- <script>setTimeout(function(){window.location.href = '.';}, 7000);</script>
- <div>Restarting...</div>
- <p><form action='./' method='get'><button>Main Menu</button></form></p>
- )"; // html_restarting_body
- static const char html_footer[] PROGMEM = R"(
- <div style='text-align:right;font-size:11px;'><hr/><a href='https://git.flokra.at/WiFiPCController' target='_blank' style='color:#aaa;'>WiFi-PC-Controller 1.0.0 by Flo Kra</a></div>
- </div></body></html>
- )";
- void httpServerHandleRoot() {
- //httpServer.send_P(200, "text/html", httpRoot);
- httpServer.setContentLength(CONTENT_LENGTH_UNKNOWN); //Enable Chunked Transfer
- httpServer.send(200, "text/html", html_head_part1);
- httpServer.sendContent(deviceName);
- httpServer.sendContent_P(html_head_part2);
- httpServer.sendContent_P(html_root_script);
- httpServer.sendContent_P(html_head_part3);
- httpServer.sendContent_P(html_bodytag_jsinit);
- httpServer.sendContent_P(html_body_part1);
- httpServer.sendContent(deviceName);
- httpServer.sendContent_P(html_body_part2);
- httpServer.sendContent_P(html_root_body);
- if(httpIsAuthenticatedAdmin()) httpServer.sendContent_P(html_root_body_adminonly);
-
- httpServer.sendContent_P(html_root_body2);
- httpServer.sendContent_P(html_footer);
- httpServer.sendContent("");
- httpServer.client().stop();
- }
- void httpServerHandleConfPage() {
- //httpServer.send_P(200, "text/html", httpConfPage);
- httpServer.setContentLength(CONTENT_LENGTH_UNKNOWN); //Enable Chunked Transfer
- httpServer.send(200, "text/html", html_head_part1);
- httpServer.sendContent(deviceName);
- httpServer.sendContent_P(html_head_part2);
- //httpServer.sendContent_P(html_conf_script);
- httpServer.sendContent_P(html_head_part3);
- httpServer.sendContent_P(html_bodytag);
- httpServer.sendContent_P(html_body_part1);
- httpServer.sendContent(deviceName);
- httpServer.sendContent_P(html_body_part2);
- httpServer.sendContent_P(html_conf_body);
- httpServer.sendContent_P(html_footer);
- httpServer.sendContent("");
- httpServer.client().stop();
- }
- void httpServerHandleConfWebPage() {
- //httpServer.send_P(200, "text/html", httpConfPage);
- httpServer.setContentLength(CONTENT_LENGTH_UNKNOWN); //Enable Chunked Transfer
- httpServer.send(200, "text/html", html_head_part1);
- httpServer.sendContent(deviceName);
- httpServer.sendContent_P(html_head_part2);
- httpServer.sendContent_P(html_confweb_script);
- httpServer.sendContent_P(html_head_part3);
- httpServer.sendContent_P(html_bodytag_jsinit);
- httpServer.sendContent_P(html_body_part1);
- httpServer.sendContent(deviceName);
- httpServer.sendContent_P(html_body_part2);
- httpServer.sendContent_P(html_confweb_body);
- httpServer.sendContent_P(html_footer);
- httpServer.sendContent("");
- httpServer.client().stop();
- }
- void httpServerHandleConfMqttPage() {
- //httpServer.send_P(200, "text/html", httpConfPage);
- httpServer.setContentLength(CONTENT_LENGTH_UNKNOWN); //Enable Chunked Transfer
- httpServer.send(200, "text/html", html_head_part1);
- httpServer.sendContent(deviceName);
- httpServer.sendContent_P(html_head_part2);
- httpServer.sendContent_P(html_confmqtt_script);
- httpServer.sendContent_P(html_head_part3);
- httpServer.sendContent_P(html_bodytag_jsinit);
- httpServer.sendContent_P(html_body_part1);
- httpServer.sendContent(deviceName);
- httpServer.sendContent_P(html_body_part2);
- httpServer.sendContent_P(html_confmqtt_body);
- httpServer.sendContent_P(html_footer);
- httpServer.sendContent("");
- httpServer.client().stop();
- }
- void httpServerHandleNotFound() {
- // if (strlen(http_user) > 0 && strlen(http_pass) > 0) {
- // if (!httpServer.authenticate(http_user, http_pass))
- // return httpServer.requestAuthentication();
- httpServer.send(404, "text/plain", "");
- //}
- }
- boolean httpIsAuthenticated() {
- if(http_user_auth) {
- boolean auth=false;
- if ((strlen(http_user) > 0 && strlen(http_pass) > 0) || (strlen(http_user1) > 0 && strlen(http_pass1) > 0) || (strlen(http_user2) > 0 && strlen(http_pass2) > 0)) auth=true;
- if (auth) {
- if (!httpServer.authenticate(http_user, http_pass) && !httpServer.authenticate(http_user1, http_pass1) && !httpServer.authenticate(http_user2, http_pass2)) return false;
- else return true;
- }
- else return true;
- }
- else return true;
- }
- boolean httpIsAuthenticatedAdmin() {
- boolean auth=false;
- if ((strlen(http_user) > 0 && strlen(http_pass) > 0)) auth=true;
- if(auth) {
- if (!httpServer.authenticate(http_user, http_pass)) return false;
- else return true;
- }
- else return true;
- }
- void httpSendUnauthorized() {
- httpServer.send (401, "text/plain", "UNAUTHORIZED");
- }
- void httpSend200OK() {
- httpServer.send (200, "text/plain", "OK");
- }
- boolean httpCheckToken() {
- if (http_token[0] != '\0') { // dont accept empty token
- if (httpServer.hasArg("token")) {
- char buf[20];
- httpServer.arg("token").toCharArray(buf, 20);
- if (strcmp(buf, http_token) == 0) return true;
- else return false;
- }
- }
- }
- void httpServerInit() {
- httpServer.on("/delconf", []() {
- Serial.println("httpServer.on /delconf");
- if ( httpIsAuthenticatedAdmin() || (!httpIsAuthenticatedAdmin() && httpCheckToken()) ) {
- deleteConfig();
- }
- else {
- httpSendUnauthorized();
- }
- });
- httpServer.on("/api", []() {
- if ( httpIsAuthenticated() || (!httpIsAuthenticated() && httpCheckToken()) ) {
- //Serial.println("httpServer.on /api");
- if (httpServer.hasArg("BtnPwr")) {
- Serial.println("web BtnPwr");
- PC_pwrSwitchShort();
- }
- if (httpServer.hasArg("BtnPwrH")) {
- Serial.println("web BtnPwrH");
- PC_pwrSwitchHold();
- }
- if (httpServer.hasArg("BtnRes")) {
- Serial.println("web BtnRes");
- PC_resSwitch();
- }
- yield();
- //build json object of program data
- StaticJsonBuffer<200> jsonBuffer;
- JsonObject &json = jsonBuffer.createObject();
- json["ssid"] = WiFi.SSID();
- json["devname"] = deviceName;
- json["uptime"] = uptimeStr;
- json["freeheap"] = ESP.getFreeHeap();
- //if(mqttclient.state() == 0) json["mqttstate"] = "connected";
- //else json["mqttstate"] = mqttclient.state();
- if(mqttclient.state() == -4) json["mqttstate"] = "CONNECTION_TIMEOUT";
- else if(mqttclient.state() == -3) json["mqttstate"] = "CONNECTION_LOST";
- else if(mqttclient.state() == -2) json["mqttstate"] = "CONNECT_FAILED";
- else if(mqttclient.state() == -1) json["mqttstate"] = "DISCONNECTED";
- else if(mqttclient.state() == 0) json["mqttstate"] = "CONNECTED";
- else if(mqttclient.state() == 1) json["mqttstate"] = "CONNECT_BAD_PROTOCOL";
- else if(mqttclient.state() == 2) json["mqttstate"] = "CONNECT_BAD_CLIENT_ID";
- else if(mqttclient.state() == 3) json["mqttstate"] = "CONNECT_UNAVAILABLE";
- else if(mqttclient.state() == 4) json["mqttstate"] = "CONNECT_BAD_CREDENTIALS";
- else if(mqttclient.state() == 5) json["mqttstate"] = "CONNECT_UNAUTHORIZED";
-
- json["mqtthost"] = mqtt_server;
-
- json["mqttreconn"] = mqttReconnects - 1;
-
- if (PCstate == 0) json["PCstate"] = "OFF";
- else if (PCstate == 1) json["PCstate"] = "ON";
- else if (PCstate == 2) json["PCstate"] = "SLEEP";
- else json["PCstate"] = "unknown";
- yield();
- char jsonchar[200];
- json.printTo(jsonchar); //print to char array, takes more memory but sends in one piece
- httpServer.send(200, "application/json", jsonchar);
- }
- else {
- httpSendUnauthorized();
- }
- }); //httpServer.on /api
- // httpServer.on("/restart", []() {
- // if ( httpIsAuthenticated() || (!httpIsAuthenticated() && httpCheckToken()) ) {
- // //Serial.println("web triggered restart");
- // ESP.restart();
- // }
- // else httpSendUnauthorized();
- // });
- httpServer.on("/mqttReconnect", []() {
- if ( httpIsAuthenticated() || (!httpIsAuthenticated() && httpCheckToken()) ) {
- //Serial.println("web triggered mqttReconnect");
- mqttReconnect();
- httpServer.sendHeader("Location", "/", true);
- httpServer.send(303);
- }
- else httpSendUnauthorized();
- });
- httpServer.on("/confdweb", []() {
- if (!httpIsAuthenticatedAdmin()) httpSendUnauthorized();
- else {
- Serial.println("httpServer.on /confdata");
- //build json object of program data
- StaticJsonBuffer<1000> jsonBuffer;
- JsonObject &json = jsonBuffer.createObject();
- json["devName"] = deviceName;
- json["apiToken"] = http_token;
- json["httpUA"] = http_user;
- //json["httpPA"] = http_pass;
-
- if(http_user_auth) json["httpAuth"] = "1";
- else json["httpAuth"] = "0";
-
- json["httpU1"] = http_user1;
- //json["httpP1"] = http_pass1;
- json["httpU2"] = http_user2;
- //json["httpP2"] = http_pass2;
- yield();
- char jsonchar[1000];
- json.printTo(jsonchar); //print to char array, takes more memory but sends in one piece
- httpServer.send(200, "application/json", jsonchar);
- }
- }); //httpServer.on /confdweb
- httpServer.on("/confdmqtt", []() {
- if (!httpIsAuthenticatedAdmin()) httpSendUnauthorized();
- else {
- Serial.println("httpServer.on /confdata");
- //build json object of program data
- StaticJsonBuffer<1000> jsonBuffer;
- JsonObject &json = jsonBuffer.createObject();
- json["mqttHost"] = mqtt_server;
- json["mqttPort"] = mqtt_port;
- json["mqttUser"] = mqtt_user;
- //json["mqttPass"] = mqtt_pass;
- json["inTop"] = mqtt_topic_in;
- json["outTop"] = mqtt_topic_out;
-
- if(mqtt_outRetain) json["outRet"] = "1";
- else json["outRet"] = "0";
-
- json["willTop"] = mqtt_willTopic;
- json["willQos"] = mqtt_willQos;
- if(mqtt_willRetain) json["willRet"] = "1";
- else json["willRet"] = "0";
-
- json["willMsg"] = mqtt_willMsg;
- json["connMsg"] = mqtt_connMsg;
- yield();
- char jsonchar[1000];
- json.printTo(jsonchar); //print to char array, takes more memory but sends in one piece
- httpServer.send(200, "application/json", jsonchar);
- }
- }); //httpServer.on /confdmqtt
- httpServer.on("/setConfWeb", []() {
- if (!httpIsAuthenticatedAdmin()) httpSendUnauthorized();
- else {
- Serial.println("httpServer.on /setConfWeb");
- bool httpPASet, httpP1Set, httpP2Set;
- httpPASet = false;
- httpP1Set = false;
- httpP2Set = false;
- if(httpServer.hasArg("httpPASet")) httpPASet = true;
- if(httpServer.hasArg("httpP1Set")) httpP1Set = true;
- if(httpServer.hasArg("httpP2Set")) httpP2Set = true;
- for (int i = 0; i < httpServer.args(); i++) {
- char bufName[20];
- char bufValue[101];
- httpServer.argName(i).toCharArray(bufName, 20);
- httpServer.arg(i).toCharArray(bufValue, 101);
-
- if (strlen(bufName) > 0) {
- Serial.print("web update ");
- Serial.print(bufName);
- Serial.print(" = ");
- Serial.println(bufValue);
- if(strcmp(bufName, "httpUA") == 0 || strcmp(bufName, "httpPA") == 0) {
- if(httpPASet) setConfig(bufName, bufValue);
- }
- else if(strcmp(bufName, "httpU1") == 0 || strcmp(bufName, "httpP1") == 0) {
- if(httpP1Set) setConfig(bufName, bufValue);
- }
- else if(strcmp(bufName, "httpU2") == 0 || strcmp(bufName, "httpP2") == 0) {
- if(httpP2Set) setConfig(bufName, bufValue);
- }
- else if(strcmp(bufName, "httpPASet") != 0 && strcmp(bufName, "httpP1Set") != 0 && strcmp(bufName, "httpP2Set") != 0) setConfig(bufName, bufValue);
- //else setConfig(bufName, bufValue);
- }
- saveConfigWebToFlash = true; // will be saved in next loop()
- Serial.println("web triggered saveConfigWebToFlash");
- }
- yield();
- saveConfigWeb();
-
- httpServer.setContentLength(CONTENT_LENGTH_UNKNOWN); //Enable Chunked Transfer
- httpServer.send(200, "text/html", html_head_part1);
- httpServer.sendContent(deviceName);
- httpServer.sendContent_P(html_head_part2);
- httpServer.sendContent_P(html_head_part3);
- httpServer.sendContent_P(html_bodytag_jsinit);
- httpServer.sendContent_P(html_body_part1);
- httpServer.sendContent(deviceName);
- httpServer.sendContent_P(html_body_part2);
- httpServer.sendContent_P(html_confsaved_body);
- httpServer.sendContent_P(html_footer);
- httpServer.sendContent("");
- httpServer.client().stop();
- yield();
- ESP.restart();
- }
- }); //httpServer.on /setConfWeb
- httpServer.on("/setConfMqtt", []() {
- if (!httpIsAuthenticatedAdmin()) httpSendUnauthorized();
- else {
- Serial.println("httpServer.on /setConfMqtt");
- bool mqttPassSet;
- mqttPassSet = false;
- if(httpServer.hasArg("mqttPassSet")) mqttPassSet = true;
- for (int i = 0; i < httpServer.args(); i++) {
- char bufName[20];
- char bufValue[101];
- httpServer.argName(i).toCharArray(bufName, 20);
- httpServer.arg(i).toCharArray(bufValue, 101);
-
- if (strlen(bufName) > 0) {
- Serial.print("web update ");
- Serial.print(bufName);
- Serial.print(" = ");
- Serial.println(bufValue);
- if(strcmp(bufName, "mqttUser") == 0 || strcmp(bufName, "mqttPass") == 0) {
- if(mqttPassSet) setConfig(bufName, bufValue);
- }
- else setConfig(bufName, bufValue);
- }
- saveConfigMqttToFlash = true; // will be saved in next loop()
- Serial.println("web triggered saveConfigMqttToFlash");
- }
- yield();
- saveConfigMqtt();
-
- httpServer.setContentLength(CONTENT_LENGTH_UNKNOWN); //Enable Chunked Transfer
- httpServer.send(200, "text/html", html_head_part1);
- httpServer.sendContent(deviceName);
- httpServer.sendContent_P(html_head_part2);
- httpServer.sendContent_P(html_head_part3);
- httpServer.sendContent_P(html_bodytag);
- httpServer.sendContent_P(html_body_part1);
- httpServer.sendContent(deviceName);
- httpServer.sendContent_P(html_body_part2);
- httpServer.sendContent_P(html_confsaved_body);
- httpServer.sendContent_P(html_footer);
- httpServer.sendContent("");
- httpServer.client().stop();
- yield();
- ESP.restart();
- }
- }); //httpServer.on /setConfMqtt
-
- httpServer.on("/", []() {
- if (httpServer.hasArg("restart")) {
- httpServer.setContentLength(CONTENT_LENGTH_UNKNOWN); //Enable Chunked Transfer
- httpServer.send(200, "text/html", html_head_part1);
- httpServer.sendContent(deviceName);
- httpServer.sendContent_P(html_head_part2);
- httpServer.sendContent_P(html_head_part3);
- httpServer.sendContent_P(html_bodytag);
- httpServer.sendContent_P(html_body_part1);
- httpServer.sendContent(deviceName);
- httpServer.sendContent_P(html_body_part2);
- httpServer.sendContent_P(html_restarting_body);
- httpServer.sendContent_P(html_footer);
- httpServer.sendContent("");
- httpServer.client().stop();
- yield();
- ESP.restart();
- }
- else if (!httpIsAuthenticated()) return httpServer.requestAuthentication();
- else {
- httpServerHandleRoot();
- }
- });
- httpServer.on("/conf", []() {
- if (!httpIsAuthenticatedAdmin()) return httpServer.requestAuthentication();
- else {
- httpServerHandleConfPage();
- }
- });
- httpServer.on("/confweb", []() {
- if (!httpIsAuthenticatedAdmin()) return httpServer.requestAuthentication();
- else {
- httpServerHandleConfWebPage();
- }
- });
- httpServer.on("/confmqtt", []() {
- if (!httpIsAuthenticatedAdmin()) return httpServer.requestAuthentication();
- else {
- httpServerHandleConfMqttPage();
- }
- });
- httpServer.onNotFound([]() {
- httpServerHandleNotFound();
- }); //httpServer.onNotFound
-
- // HTTP Updater at /update
- httpUpdater.setup(&httpServer, "/update", http_user, http_pass);
- httpServer.begin();
- }
|