Created
June 16, 2018 19:54
-
-
Save ishukshin/c867ccd42861f4aa52ef7210c57aba73 to your computer and use it in GitHub Desktop.
Simple webserver at esp8266. Saves value in EEPROM and auto-updates input value in open browsers if changed by any user or the system.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ESP8266WiFi.h> //Содержится в пакете | |
#include <ESP8266WebServer.h> //Содержится в пакете | |
#include <ESP8266SSDP.h> | |
#include <EEPROM.h> | |
// IP адрес устройства | |
IPAddress apIP(192, 168, 4, 1); | |
// Web интерфейс для устройства | |
ESP8266WebServer HTTP(80); | |
// Определяем переменные wifi | |
String _ssid = "home"; // Для хранения SSID | |
String _password = "homehome1"; // Для хранения пароля сети | |
String _ssidAP = "WiFi"; // SSID AP точки доступа | |
String _passwordAP = "homehome2"; // пароль точки доступа | |
String SSDP_Name= "SSDP-test"; | |
String htmlRes; | |
int text; | |
byte b0, b1; | |
int readInput() { | |
return HTTP.arg("text").toInt(); | |
} | |
void saveValue() { | |
text = readInput(); | |
b1 = text % 256; | |
b0 = text >> 8; | |
EEPROM.begin(2); | |
EEPROM.write(0, b0); | |
EEPROM.write(1, b1); | |
EEPROM.commit(); | |
} | |
void handleRoot() { | |
if(readInput() > 0) { | |
saveValue(); | |
HTTP.sendHeader("Location", String("/"), true); | |
HTTP.send(302, "text/plain", ""); | |
} else { | |
HTTP.send(200, "text/html", webPage()); | |
} | |
} | |
void handleUpdate() { | |
HTTP.send(200, "text/plain", String(text)); | |
} | |
String webPage() { | |
String web; | |
web += "<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"/> <meta charset=\"utf-8\"><title>ESP 8266</title><style>button{color:red;padding: 10px 27px;}</style></head>"; | |
web += "<h1 style=\"text-align: center;font-family: Open sans;font-weight: 100;font-size: 20px;\">ESP8266 Web Server</h1><div>"; | |
web += "<script type=\"text/javascript\">" | |
" function clearRes() {\r\n" | |
" setTimeout(function(){document.getElementById('res').innerText='';}, 1000);\r\n" | |
" }\r\n" | |
" function updateValue() {\r\n" | |
" var xmlhttp = new XMLHttpRequest();\r\n" | |
" xmlhttp.onreadystatechange = function() { \r\n" | |
" if (xmlhttp.readyState == XMLHttpRequest.DONE) {\r\n" | |
" if(xmlhttp.status == 200) {\r\n" | |
" var initval = document.getElementById('text').getAttribute('initial')\r\n" | |
" if(parseInt(xmlhttp.responseText) !== parseInt(initval)) { \r\n" | |
" document.getElementById('text').value = xmlhttp.responseText;\r\n" | |
" document.getElementById('text').setAttribute('initial', xmlhttp.responseText);\r\n" | |
" document.getElementById('res').innerText = 'updated';\r\n" | |
" clearRes();\r\n" | |
" }\r\n" | |
" } else { \r\n" | |
" document.getElementById('res').innerText = 'error autoupdating';\r\n" | |
" clearRes();\r\n" | |
" }\r\n" | |
" }\r\n" | |
" }; \r\n" | |
" xmlhttp.open('GET', '/update?_=' + Math.random(), true);\r\n" | |
" xmlhttp.send(); \r\n" | |
" }\r\n" | |
" setInterval(function(){\r\n" | |
" updateValue();\r\n" | |
" }, 2000);\r\n" | |
"</script>\r\n" | |
"<form name='forma4' method=post>\r\n" | |
" <input type='text' id=text name='text' size='5'\r\n" | |
" maxlength='50' autofocus value='"+ String(text)+"' initial='"+ String(text)+"' max='65535'>\r\n" | |
"<div style='font-size:0.8em;'>Save value in the [1, 65535] diap.</div>" | |
"<div style='font-size:0.8em;'>EEPROM bytes: b0=" +String(b0) + ", b1=" + String(b1) + "</div> \r\n" | |
"<div id=res style='font-size:0.7em;'></div>\r\n" | |
"</form>\r\n" | |
"</div>"; | |
return(web); | |
} | |
void setup() { | |
EEPROM.begin(2); | |
b0 = EEPROM.read(0); | |
b1 = EEPROM.read(1); | |
text = (b0 << 8) + b1; | |
//Запускаем WIFI | |
WIFIinit(); | |
HTTP.on("/", handleRoot); | |
HTTP.on("/update", handleUpdate); | |
HTTP.begin(); | |
} | |
void WIFIinit() { | |
WiFi.mode(WIFI_AP); | |
// Задаем настройки сети | |
WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0)); | |
// Включаем WIFI в режиме точки доступа | |
WiFi.softAP(_ssidAP.c_str(), _passwordAP.c_str()); | |
} | |
void loop() { | |
HTTP.handleClient(); | |
delay(1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment