//extern ESP8266WebServer httpServer; static const char html_head_part1[] PROGMEM = "" "" "" "WiFi-PC-Controller - "; static const char html_head_part2[] PROGMEM = "" ""; static const char html_root_script[] PROGMEM = R"( )"; // html_root_script static const char html_confweb_script[] PROGMEM = R"( )"; // html_confweb_script static const char html_confmqtt_script[] PROGMEM = R"( )"; // html_confmqtt_script static const char html_head_part3[] PROGMEM = R"()"; // html_head_part3 static const char html_bodytag_jsinit[] PROGMEM = R"( )"; static const char html_bodytag[] PROGMEM = R"( )"; static const char html_body_part1[] PROGMEM = R"(

WiFi-PC-Controller

)"; // html_body_part1 static const char html_body_part2[] PROGMEM = R"(

)"; // html_body_part2 static const char html_root_body[] PROGMEM = R"(

)"; // html_root_body static const char html_root_body_adminonly[] PROGMEM = R"(

)"; // html_root_body_adminonly static const char html_root_body2[] PROGMEM = R"(


WiFi connected to

MQTT

MQTT reconnects:

Uptime:

)"; // html_root_body2 static const char html_conf_body[] PROGMEM = R"(

)"; // html_conf_body static const char html_confweb_body[] PROGMEM = R"(
 Web Configuration 

Device Name

API Token


HTTP Admin Username *

HTTP Admin Password *

Confirm Password *


Enable User-Authentication * 

HTTP User 1 *

HTTP User 1 Password *


HTTP User 2 *

HTTP User 2 Password *

)"; // html_confweb_body static const char html_confmqtt_body[] PROGMEM = R"(
 MQTT Configuration 

MQTT Server *

MQTT Port *

MQTT User *

MQTT Password *

In Topic *

Out Topic

Out Retain * 

LastWill Topic *

LastWill Qos *

LastWill Retain * 

LastWill Message *

Connect Message *

)"; // html_confmqtt_body static const char html_confsaved_body[] PROGMEM = R"(
Config saved. Restarting...

)"; // html_confsaved_body static const char html_restarting_body[] PROGMEM = R"(
Restarting...

)"; // html_restarting_body static const char html_footer[] PROGMEM = R"(

WiFi-PC-Controller 1.0.0 by Flo Kra
)"; 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(); }