122 lines
4.8 KiB
C++
122 lines
4.8 KiB
C++
void handleRoot() {
|
|
server.send(200, "text/html",
|
|
"<head><meta http-equiv=\"refresh\" content=\"" + String(SAMPLING_PERIODE_MS / 1000) + "\" ></head>"
|
|
"<h1>You are connected</h1><br/>"
|
|
"Current temperature " + String(temp, 2) + "C<br/>"
|
|
"Current pressure " + String(pressure, 2) + "mB<br/>"
|
|
"MQTT Status: " + (MQTT_isConnected() ? "Connected" : "Disconnected") + "<br/>"
|
|
"BMP 180 (Temp+Pression) Status: " + (isBMPConnected() ? "Connected" : "Disconnected") + "<br/>"
|
|
"Free space: "+ ESP.getFreeSketchSpace() +"<br/>"
|
|
"Free heap: "+ ESP.getFreeHeap() +"<br/>"
|
|
"<a href=\"/setup\">Setup</a><br/>"
|
|
"<a href=\"/otamode\">OTA mode</a><br/>"
|
|
"<a href=\"/gpio?gpio=2&value=1\">ON</a><br/>"
|
|
"<a href=\"/gpio?gpio=2&value=0\">OFF</a><br/>"
|
|
);
|
|
}
|
|
|
|
void handleSetup() {
|
|
uint8_t mode;
|
|
char *confSsid = "";
|
|
char *confPassword = "";
|
|
char *confHost = "";
|
|
char *mqttServer = "";
|
|
char *mqttUser = "";
|
|
char *mqttPasswd = "";
|
|
int mqttPort = 1883;
|
|
|
|
readEEPROM(mode, &confSsid, &confPassword, &confHost, &mqttServer, &mqttUser, &mqttPasswd, mqttPort);
|
|
|
|
server.send(200, "text/html", "<form action=\"/save\" method=\"get\">"
|
|
"<fieldset>"
|
|
"<legend>Wifi configuration:</legend>"
|
|
"<div><label for=\"ssid\">Wifi SSID :</label> <input type=\"text\" name=\"ssid\" value=\"" + String(confSsid) + "\" /></div>"
|
|
"<div><label for=\"password\">Wifi Password :</label><input type=\"password\" name=\"password\" /> </div>"
|
|
"<div><label for=\"host\">Hostname :</label><input type=\"text\" name=\"host\" value=\"" + String(confHost) + "\" /> </div>"
|
|
"</fieldset>"
|
|
"<fieldset>"
|
|
"<legend>MQTT:</legend>"
|
|
"<div><label for=\"mqttServer\">MQTTServer :</label><input type=\"text\" name=\"mqttServer\" value=\"" + String(mqttServer) + "\" /> </div>"
|
|
"<div><label for=\"mqttUser\">MQTTUsername :</label><input type=\"text\" name=\"mqttUser\" value=\"" + String(mqttUser) + "\" /> </div>"
|
|
"<div><label for=\"mqttPasswd\">MQTTPassword :</label><input type=\"password\" name=\"mqttPasswd\" /> </div>"
|
|
"<div><label for=\"mqttPort\">MQTTPort :</label><input type=\"text\" name=\"mqttPort\" value=\"" + String(mqttPort) + "\" /> </div>"
|
|
"</fieldset>"
|
|
"<div class=\"button\"> <button type=\"submit\">Save</button></div>"
|
|
"</form>");
|
|
}
|
|
|
|
void handleGpio() {
|
|
if (!server.hasArg("gpio") || !server.hasArg("value")) {
|
|
server.send(500, "text/plain", "Bad arguments\r\n");
|
|
return;
|
|
}
|
|
|
|
changeGpioValue(server.arg("gpio").toInt(), server.arg("value").toInt());
|
|
server.send(200, "text/html", "<h1>GPIO" + server.arg("gpio") + " changed to " + server.arg("value") + "</h1>");
|
|
}
|
|
|
|
void handleSave() {
|
|
String password;
|
|
String ssid;
|
|
String hostName;
|
|
String mqttServer;
|
|
String mqttUser;
|
|
String mqttPasswd;
|
|
|
|
if (!server.hasArg("ssid") || !server.hasArg("password") || !server.hasArg("host") || !server.hasArg("mqttServer") || !server.hasArg("mqttUser") || !server.hasArg("mqttPasswd") || !server.hasArg("mqttPort") ) {
|
|
server.send(500, "text/plain", "Bad arguments\r\n");
|
|
return;
|
|
}
|
|
|
|
if (saveConfig(BOOTMODE_NORMAL, server.arg("ssid"), server.arg("password"), server.arg("host"), server.arg("mqttServer"), server.arg("mqttUser"), server.arg("mqttPasswd"), server.arg("mqttPort").toInt()) < 0) {
|
|
server.send(500, "text/plain", "Cannot Save Credentials (Too long ?Contains \";\"?)\r\n");
|
|
return;
|
|
}
|
|
|
|
server.send(200, "text/html", "<h1>Configuration Saved</h1><br/>"
|
|
"<a href=\"/reboot\">Reboot</a><br/>");
|
|
}
|
|
|
|
void handleOTA() {
|
|
Serial.println("Boot mode Set to OTA");
|
|
saveBootMode(BOOTMODE_OTA);
|
|
server.send(200, "text/html", "<h1>OTA Mode set</h1><br/>"
|
|
"You can reboot now");
|
|
}
|
|
|
|
void handleNotFound() {
|
|
String message = "File Not Found\n\n";
|
|
message += "URI: ";
|
|
message += server.uri();
|
|
message += "\nMethod: ";
|
|
message += (server.method() == HTTP_GET) ? "GET" : "POST";
|
|
message += "\nArguments: ";
|
|
message += server.args();
|
|
message += "\n";
|
|
for (uint8_t i = 0; i < server.args(); i++) {
|
|
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
|
|
}
|
|
server.send(404, "text/plain", message);
|
|
}
|
|
|
|
void handleReboot() {
|
|
Serial.println("HTTP request to reboot");
|
|
server.send(200, "text/html", "<h1>Device Reboot</h1><br/>");
|
|
ESP.restart();
|
|
}
|
|
|
|
void setupWebServer(int bootmode) {
|
|
server.on("/", handleRoot);
|
|
server.on("/setup", handleSetup);
|
|
server.on("/save", handleSave);
|
|
server.on("/gpio", handleGpio);
|
|
server.on("/otamode", handleOTA);
|
|
server.on("/reboot", handleReboot);
|
|
|
|
server.onNotFound(handleNotFound);
|
|
server.begin();
|
|
Serial.println("HTTP server started");
|
|
}
|
|
|
|
|