#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>
#include <DNSServer.h>
#include <SmartDelay.h>
#include <uptime.h>
#define ssid_AP "**********************"
#define pswd_AP "**********************"
const char* www_login = "**********************";
const char* www_pswd = "**********************";
ESP8266WebServer server (80);
WiFiManager wm;
SmartDelay delayUptime(5000000UL);
unsigned int eroorAuthoriz = 0;
String TimeUptime = "";
#define host_Name "WAKE-ON-LINE"
#define country "RU"
#define DarkMode true
#define pwr_sw 15
#define pwr_led 4
#define Delay_OnOff 500
#define Delay_Hard 5000
String webPage;
const char webHeader_HTML[] PROGMEM = R"=====(<!DOCTYPE html><html><head><meta http-equiv="content-type" content="text/html; charset=UTF-8"><meta name="viewport" content="width = device-width"><title>Wake-On-Line(ESP-01)</title>)=====";
String webHeader = (String)webHeader_HTML;
const char webBackPage_HTML[] PROGMEM = R"=====(<meta http-equiv="refresh"content="1;URL=/">)=====";
String webBackPage = (String)webBackPage_HTML;
const char webLoading_HTML[] PROGMEM = R"=====(</head><body onmousedown="return false;" onclick="return true;"><br><br><br><br><br><h1> Загрузка...</h1></body></html>)=====";
String webLoading = (String)webLoading_HTML;
const char webControl_on_HTML[] PROGMEM = R"=====(<style>.buttonOFF{display: inline-block;cursor: not-allowed; position: absolute; top: 258px; left: 157px; font-size:20px;text-decoration:none;padding:21px 12px; color:#c0c0c0;background:#d5dce0;border-radius:10px;border:5px solid #354251;} .buttonON{display: inline-block;cursor: pointer; position: absolute; top: 258px; left: 235px; font-size:20px;text-decoration:none;padding:21px 5px; color:#394d5c;background:#d5dce0;border-radius:10px;border:5px solid #354251;} .buttonON:hover{background:#354251;color:#ffffff;border:5px solid #354251;transition: all 0.5s ease;} .buttonReset{display: inline-block;cursor: pointer; position: absolute; top: 60px; left: 25px; font-size:14px;text-decoration:none;padding:12px 30px; color:#394d5c;background:#d5dce0;border-radius:10px;border:5px solid #354251;} .buttonReset:hover{background:#354251;color:#ffffff;border:5px solid #354251;transition: all 0.5s ease;} .buttonSettings{display: inline-block;cursor: pointer; position: absolute; top: 60px; left: 162px; font-size:14px;text-decoration:none;padding:12px 30px; color:#394d5c;background:#d5dce0;border-radius:10px;border:5px solid #354251;} .buttonSettings:hover{background:#354251;color:#ffffff;border:5px solid #354251;transition: all 0.5s ease;} .buttonRefresh{display: inline-block;cursor: pointer; position: absolute; top: 343px; left: 20px; font-size:19px;text-decoration:none;padding:14px 30px; color:#394d5c;background:#d5dce0;border-radius:10px;border:5px solid #354251;} .buttonRefresh:hover{background:#354251;color:#ffffff;border:5px solid #354251;transition: all 0.5s ease;}</style><script>function proverka() {if (confirm("Уверен?")) {return true;} else {return false;}}</script></head><body onmousedown="return false;" onclick="return true;"><h1>Управление PC</h1><br><br><a class="buttonReset" onclick="return proverka()" href="/hard">Reset</a><a class="buttonSettings" href="/settings">Настройки</a><table style="text-align: left; height: 222px; width: 312px;" border="1" cellspacing="2" cellpadding="2"><tbody><tr><td style="font-size: 17pt; text-align: center; width: 80px;">Состояние PC</td><td style="font-size: 20pt; text-align: center; background-color: green; width: 135px;">ON</td></tr><tr><td style="font-size: 20pt; text-align: center; width: 60px;">Система</td><td style="text-align: center; width: 140px;"><a class="buttonOFF">Вкл.</a><a class="buttonON" onclick="return proverka()" href="/off">Выкл.</a></td></tr></tbody></table><a class="buttonRefresh" onClick="window.location.reload()">Обновить состояние PC</a><br><br><br><br>          <a href="/authorization?DISCONNECT=YES">Выйти из системы!</a></body></html>)=====";
String webControl_on = (String)webControl_on_HTML;
const char webControl_off_HTML[] PROGMEM = R"=====(<style>.buttonOFF{display: inline-block;cursor: not-allowed; position: absolute; top: 258px; left: 235px; font-size:20px;text-decoration:none;padding:21px 5px; color:#c0c0c0;background:#d5dce0;border-radius:10px;border:5px solid #354251;} .buttonON{display: inline-block;cursor: pointer; position: absolute; top: 258px; left: 157px; font-size:20px;text-decoration:none;padding:21px 12px; color:#394d5c;background:#d5dce0;border-radius:10px;border:5px solid #354251;} .buttonON:hover{background:#354251;color:#ffffff;border:5px solid #354251;transition: all 0.5s ease;} .buttonReset{display: inline-block;cursor: pointer; position: absolute; top: 60px; left: 25px; font-size:14px;text-decoration:none;padding:12px 30px; color:#394d5c;background:#d5dce0;border-radius:10px;border:5px solid #354251;} .buttonReset:hover{background:#354251;color:#ffffff;border:5px solid #354251;transition: all 0.5s ease;} .buttonSettings{display: inline-block;cursor: pointer; position: absolute; top: 60px; left: 162px; font-size:14px;text-decoration:none;padding:12px 30px; color:#394d5c;background:#d5dce0;border-radius:10px;border:5px solid #354251;} .buttonSettings:hover{background:#354251;color:#ffffff;border:5px solid #354251;transition: all 0.5s ease;} .buttonRefresh{display: inline-block;cursor: pointer; position: absolute; top: 343px; left: 20px; font-size:19px;text-decoration:none;padding:14px 30px; color:#394d5c;background:#d5dce0;border-radius:10px;border:5px solid #354251;} .buttonRefresh:hover{background:#354251;color:#ffffff;border:5px solid #354251;transition: all 0.5s ease;}</style><script>function proverka() {if (confirm("Уверен?")) {return true;} else {return false;}}</script></head><body onmousedown="return false;" onclick="return true;"><h1>Управление PC</h1><br><br><a class="buttonReset" onclick="return proverka()" href="/hard">Reset</a><a class="buttonSettings" href="/settings">Настройки</a><table style="text-align: left; height: 222px; width: 312px;" border="1" cellspacing="2" cellpadding="2"><tbody><tr><td style="font-size: 17pt; text-align: center; width: 80px;">Состояние PC</td><td style="font-size: 20pt; text-align: center; background-color: red; width: 135px;">OFF</td></tr><tr><td style="font-size: 20pt; text-align: center; width: 60px;">Система</td><td style="text-align: center; width: 140px;"><a class="buttonOFF">Выкл.</a><a class="buttonON" onclick="return proverka()" href="/on">Вкл.</a></td></tr></tbody></table><a class="buttonRefresh" onClick="window.location.reload()">Обновить состояние PC</a><br><br><br><br>          <a href="/authorization?DISCONNECT=YES">Выйти из системы!</a></body></html>)=====";
String webControl_off = (String)webControl_off_HTML;
const char webSettings_HTML[] PROGMEM = R"=====(<body><style>.buttonBack{display: inline-block;cursor: pointer; position: absolute; top: 60px; left: 25px; font-size:14px;text-decoration:none;padding:12px 25px; color:#394d5c;background:#d5dce0;border-radius:10px;border:5px solid #354251;} .buttonBack:hover{background:#354251;color:#ffffff;border:5px solid #354251;transition: all 0.5s ease;} .buttonInfo{display: inline-block;cursor: pointer; position: absolute; top: 60px; left: 162px; font-size:14px;text-decoration:none;padding:11px 11px; color:#394d5c;background:#d5dce0;border-radius:10px;border:5px solid #354251;} .buttonInfo:hover{background:#354251;color:#ffffff;border:5px solid #354251;transition: all 0.5s ease;} .buttonWebLoginPasswd{display: inline-block;cursor: pointer; position: absolute; top: 120px; left: 25px; font-size:14px;text-decoration:none;padding:14px 28px; color:#394d5c;background:#d5dce0;border-radius:10px;border:5px solid #354251;} .buttonWebLoginPasswd:hover{background:#354251;color:#ffffff;border:5px solid #354251;transition: all 0.5s ease;} .buttonFirmware{display: inline-block;cursor: pointer; position: absolute; top: 185px; left: 25px; font-size:14px;text-decoration:none;padding:14px 40px; color:#394d5c;background:#d5dce0;border-radius:10px;border:5px solid #354251;} .buttonFirmware:hover{background:#354251;color:#ffffff;border:5px solid #354251;transition: all 0.5s ease;} .buttonReboot{display: inline-block;cursor: pointer; position: absolute; top: 250px; left: 25px; font-size:14px;text-decoration:none;padding:14px 50px; color:#394d5c;background:#d5dce0;border-radius:10px;border:5px solid #354251;} .buttonReboot:hover{background:#354251;color:#ffffff;border:5px solid #354251;transition: all 0.5s ease;} .buttonResWiFi{display: inline-block;cursor: pointer; position: absolute; top: 315px; left: 25px; font-size:14px;text-decoration:none;padding:14px 50px; color:#394d5c;background:#d5dce0;border-radius:10px;border:5px solid #354251;} .buttonResWiFi:hover{background:#354251;color:#ffffff;border:5px solid #354251;transition: all 0.5s ease;}</style><script>function proverka() {if (confirm("Точно желаешь сбросить WiFi пароль?")) {return true;} else {return false;}}</script></head><body onmousedown="return false;" onclick="return true;"><h1> Настройки</h1><br><br><a class="buttonBack" onclick="history.back()">« Назад</a><a class="buttonInfo" href="/ipInform">✈ Информация</a><a class="buttonReboot" href="/rebootESP-01">↺ Перезагрузить ESP-01</a></body></html>)=====";
String webSettings = (String)webSettings_HTML;
const char webInfo1_HTML[] PROGMEM = R"=====(</head><body><h1> Информация</h1><a href="/"><button>⊴ На главную</button></a>  <button onClick="window.location.reload();">Обновить</button><br><br>)=====";
String webInfo1 = (String)webInfo1_HTML;
const char webPageNotFound_HTML[] PROGMEM = R"=====(<body><h1 style="color:#0000FF">Page Not Found!</h1></body></html></body></html>)=====";
String webPageNotFound = (String)webPageNotFound_HTML;
void setup() {
pinMode(pwr_sw, OUTPUT);
pinMode(pwr_led, INPUT);
digitalWrite(pwr_sw, LOW);
WiFi.mode(WIFI_STA);
wm.setDebugOutput(false);
WiFiManagerParameter custom_html("<p>Настройка WEB Авторизации</p>");
WiFiManagerParameter custom_webLogin("login", "Введите Логин:", "", 15);
WiFiManagerParameter custom_webPasswd("pswd", "Введите пароль:", "", 15);
wm.addParameter(&custom_html);
wm.addParameter(&custom_webLogin);
wm.addParameter(&custom_webPasswd);
wm.setDarkMode(DarkMode);
wm.setCountry(country);
wm.setHostname(host_Name);
wm.setConnectRetries(10);
std::vector<const char *> menu = {"wifi", "info", "sep", "erase", "update", "restart"};
wm.setMenu(menu);
wm.autoConnect(ssid_AP, pswd_AP);
server.on("/authorization", PageAuthorization);
server.on("/", PageRoot);
server.on("/on", PageOn);
server.on("/off", PageOff);
server.on("/hard", PageHard);
server.on("/settings", PageSettings);
server.on("/ipInform", PageInfo);
server.on("/rebootESP-01", PageReboot);
server.onNotFound(handleNotFound);
server.collectHeaders("User-Agent", "Cookie");
server.begin();
}
void loop() {
server.handleClient();
uptime::calculateUptime();
if (delayUptime.Now()) {
TimeUptime = (String) uptime::getDays() + ":" + uptime::getHours() + ":" + uptime::getMinutes() + ":" + uptime::getSeconds();
}
}
void pc () {
digitalWrite(pwr_sw, HIGH);
delay(Delay_OnOff);
digitalWrite(pwr_sw, LOW);
}
void pc_hard () {
digitalWrite(pwr_sw, HIGH);
delay(Delay_Hard);
digitalWrite(pwr_sw, LOW);
}
void PageRoot() {
String header;
if (!is_authenticated()) {
server.sendHeader("Location", "/authorization");
server.sendHeader("Cache-Control", "no-cache");
server.send(301);
return;
}
if (digitalRead(pwr_led) == LOW) {
webPage = webHeader + webControl_off;
}
else if (digitalRead(pwr_led) == HIGH) {
webPage = webHeader + webControl_on;
}
server.send(200, "text/html", webPage);
}
void PageOn() {
String header;
if (!is_authenticated()) {
server.sendHeader("Location", "/authorization");
server.sendHeader("Cache-Control", "no-cache");
server.send(301);
return;
}
if (digitalRead(pwr_led) == LOW) {
webPage = webHeader + webBackPage + webLoading;
pc();
}
server.send(200, "text/html", webPage);
}
void PageOff() {
String header;
if (!is_authenticated()) {
server.sendHeader("Location", "/authorization");
server.sendHeader("Cache-Control", "no-cache");
server.send(301);
return;
}
if (digitalRead(pwr_led) == HIGH) {
webPage = webHeader + webBackPage + webLoading;
pc();
}
server.send(200, "text/html", webPage);
}
void PageHard() {
String header;
if (!is_authenticated()) {
server.sendHeader("Location", "/authorization");
server.sendHeader("Cache-Control", "no-cache");
server.send(301);
return;
}
webPage = webHeader + webBackPage + webLoading;
pc_hard();
server.send(200, "text/html", webPage);
}
void PageSettings() {
String header;
if (!is_authenticated()) {
server.sendHeader("Location", "/authorization");
server.sendHeader("Cache-Control", "no-cache");
server.send(301);
return;
}
webPage = webHeader + webSettings;
server.send(200, "text/html", webPage);
}
void PageInfo() {
String header;
if (!is_authenticated()) {
server.sendHeader("Location", "/authorization");
server.sendHeader("Cache-Control", "no-cache");
server.send(301);
return;
}
String FlashChipSpeed = (String)(ESP.getFlashChipSpeed() / 1000000);
String CpuFreqMHz = (String)ESP.getCpuFreqMHz();
String FreeHeap = (String)ESP.getFreeHeap();
String FlashChipSize = (String)ESP.getFlashChipSize();
String FlashChipRealSize = (String)ESP.getFlashChipRealSize();
String FreeSketchSpace = (String)ESP.getFreeSketchSpace();
String HeapFragmentation = (String)ESP.getHeapFragmentation();
String ResetReason = ESP.getResetReason();
String myMAC = WiFi.macAddress();
String myBSSID = WiFi.BSSIDstr();
String ChipId = (String)ESP.getChipId();
String FlashChipId = (String)ESP.getFlashChipId();
String CoreVersion = ESP.getCoreVersion();
String webInfo2 = "<strong style=\"color:#0000FF\">Uptime: </strong>" + TimeUptime + "<br><strong style=\"color:#0000FF\">Частоту FLASH: </strong>" + FlashChipSpeed + " МГц<br><strong style=\"color:#0000FF\">Частоту CPU: </strong>" + CpuFreqMHz + " МГц<br><br><strong style=\"color:#0000FF\">Свободно ОЗУ: </strong>" + FreeHeap + " байт<br><br><strong style=\"color:#0000FF\">Размер FLASH: </strong>" + FlashChipSize + " байт<br><strong style=\"color:#0000FF\">Реальный размер FLASH: </strong>" + FlashChipRealSize + " байт<br><strong style=\"color:#0000FF\">Свободно FLASH: </strong>" + FreeSketchSpace + " байт<br><br><strong style=\"color:#0000FF\">Фрагментация: </strong>" + HeapFragmentation + " % (более 50% ПЛОХО)<br><strong style=\"color:#0000FF\">Неудачных авторизаций: </strong>" + eroorAuthoriz + " раз<br><strong style=\"color:#0000FF\">Причина сброса питания: </strong>" + ResetReason + "<br><br><strong style=\"color:#0000FF\">MAC адрес: </strong>" + myMAC + "<br><strong style=\"color:#0000FF\">BSSID: </strong>" + myBSSID + "<br><strong style=\"color:#0000FF\">HOSTNAME: </strong>" + host_Name + "<br><br><strong style=\"color:#0000FF\">Chip ID ESP8266: </strong>" + ChipId + "<br><strong style=\"color:#0000FF\">Chip ID FLASH: </strong>" + FlashChipId + "<br><strong style=\"color:#0000FF\">Версия платы ESP: </strong>" + CoreVersion + "</body></html>";
webPage = webHeader + webInfo1 + webInfo2;
server.send(200, "text/html", webPage);
}
void PageReboot() {
String header;
if (!is_authenticated()) {
server.sendHeader("Location", "/authorization");
server.sendHeader("Cache-Control", "no-cache");
server.send(301);
return;
}
webPage = webHeader + webBackPage + webLoading;
server.send(200, "text/html", webPage);
server.stop();
delay(300);
ESP.restart();
}
bool is_authenticated() {
if (server.hasHeader("Cookie")) {
String cookie = server.header("Cookie");
if (cookie.indexOf("ESPSESSIONID=1") != -1) {
return true;
}
}
return false;
}
void PageAuthorization() {
String msgError;
if (server.hasHeader("Cookie")) {
String cookie = server.header("Cookie");
}
if (server.hasArg("DISCONNECT")) {
server.sendHeader("Location", "/authorization");
server.sendHeader("Cache-Control", "no-cache");
server.sendHeader("Set-Cookie", "ESPSESSIONID=0");
server.send(301);
return;
}
if (server.hasArg("login") && server.hasArg("pswd")) {
if (server.arg("login") == www_login && server.arg("pswd") == www_pswd) {
server.sendHeader("Location", "/");
server.sendHeader("Cache-Control", "no-cache");
server.sendHeader("Set-Cookie", "ESPSESSIONID=1");
server.send(301);
return;
}
eroorAuthoriz += 1;
msgError = "<font style=\"color:#ff0000\"><strong>Неверный Логин или Пароль!</strong></font>";
}
String content = "</head><body><form action=\"/authorization\" method=\"POST\"><h3>Страница авторизации</h3><label style=\"color:#0000FF\">Логин: </label><input type=\"text\" name=\"login\" placeholder=\"Введите Логин\" required><br><br><label style=\"color:#0000FF\">Пароль: </label><input type=\"password\" name=\"pswd\" placeholder=\"Введите Пароль\" required><br><br><input type=\"submit\" name=\"SUBMIT\" value=\"Войти в систему\"></form>" + msgError + "</body></html>";
webPage = webHeader + content;
server.send(200, "text/html", webPage);
}
void handleNotFound() {
webPage = webHeader + webPageNotFound;
server.send(404, "text/html", webPage);
}