#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
//const char *ssid = "ssid";
//const char *password = "passpass";
const char *ssid = "ssid"; // Сід Wi-Fi
const char *password = "passpass"; // Пасс Wi-Fi
ESP8266WebServer server ( 80 );
IPAddress ip;
char ipH[16];
const int led = 2; // Синій світлодіод (D4)
const int rel = 0; // Реле сидить на 0 (D3)
int curState = 0;
int digState = 0;
float voltage = 0.0;
char svoltage[5] = "";
void handleRoot() {
curState = analogRead(A0); // Знімаю значення з A0 (при моему поділювачеві 220-86 КОм та напрузі 12В значення 730)
digState = digitalRead(4); // Знімаю значення з gpio4 (D2) стан світлодіода PLED+ на материнці
voltage = (float)curState * 12 / 730;
dtostrf(voltage, 2, 1, svoltage);
digitalWrite ( led, 0 );
char temp[800];
int sec = millis() / 1000;
int min = sec / 60;
int hr = min / 60;
snprintf ( temp, 800, "<meta http-equiv='refresh' content='5'>\
<style>body { background-color: #000000; Color: #9ACD32; font-size: 300%; }a.kf {color: #fff; background:#4BD438; padding: .7em 1.5em; outline: none; border-radius:5px;}a.k {color: #fff; background:#D44B38; padding: .7em 1.5em; outline: none; border-radius:5px;}</style>\
<h1>Online: %02d:%02d:%02d</h1>\
<br><a href='/on' class='kf'>On</a><a href='/res' class='k'>Reset</a><a href='/off' class='k'>HardOff</a><br><br><p>State: %s V</p>\
<embed src='%d.svg' width='512' height='512' type='image/svg+xml'>", hr , min % 60, sec % 60, svoltage, digState);
server.send ( 800, "text/html", temp );
delay(100);
digitalWrite ( led, 1 );
}
void handleNotFound() {
digitalWrite ( led, 0 );
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 );
digitalWrite ( led, 1 );
}
void setup ( void ) {
pinMode ( 4, INPUT );
pinMode ( A0, INPUT );
pinMode ( led, OUTPUT );
pinMode ( rel, OUTPUT );
digitalWrite ( led, 1);
digitalWrite ( rel, 0 );
WiFi.hostname("esp8266");
WiFi.mode(WIFI_OFF);
WiFi.begin ( ssid, password );
while ( WiFi.status() != WL_CONNECTED ) {
digitalWrite ( led, 0 );
delay ( 450 );
digitalWrite ( led, 1 );
delay ( 50 );
}
if (!MDNS.begin("esp8266")) {
while (10) {
delay(100);
}
}
MDNS.addService("http", "tcp", 80);
ip = WiFi.localIP();
sprintf(ipH, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
server.on ( "/", handleRoot );
server.on ( "/on", []() {
digitalWrite(rel, 1);
delay(200);
digitalWrite(rel, 0);
String Message = "<meta http-equiv='refresh' content='5;url=http://";
Message += ipH;
Message += "'/><h1>On Done!</h1><br><p>";
Message += "</p>";
Message += "<embed src='ok.svg' width='300' height='300' type='image/svg+xml' >";
server.send ( 200, "text/html", Message );
} );
server.on ( "/off", []() {
String Message = "<meta http-equiv='refresh' content='5;url=http://";
Message += ipH;
Message += "'/><h1>Off Done!</h1><br><p>";
Message += "</p>";
Message += "<embed src='ok.svg' width='300' height='300' type='image/svg+xml' >";
server.send ( 200, "text/html", Message );
digitalWrite(rel, 1);
delay(8000);
digitalWrite(rel, 0);
} );
server.on ( "/res", []() {
String Message = "<meta http-equiv='refresh' content='5;url=http://";
Message += ipH;
Message += "'/><h1>Reset Done!</h1><br><p>";
Message += "</p>";
Message += "<embed src='ok.svg' width='300' height='300' type='image/svg+xml' >";
server.send ( 200, "text/html", Message );
digitalWrite(rel, 1);
delay(8000);
digitalWrite(rel, 0);
delay(3000);
digitalWrite(rel, 1);
delay(200);
digitalWrite(rel, 0);
} );
server.on ( "/ok.svg", []() { server.send ( 200, "image/svg+xml", "<svg version='1.1' xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' width='100%' height='100%' viewBox='0 0 300 300'><circle fill='#009D0D' cx='144' cy='144' r='144'/> <polygon fill='#FFFFFF' points='122.4,222.7 241,104 207,70.2 122.9,154.8 81,112.2 47,146.8'/>Not support inline SVG.</svg>");} );
server.on ( "/1.svg", []() { server.send ( 500, "image/svg+xml", "<svg xmlns='http://www.w3.org/2000/svg' width='100' height='100' viewBox='0 0 200 200'><circle cx='75' cy='75' r='65' fill='#4BD438'/><path d='M75 37a38 38 0 1 0 38 38A38 38 0 0 0 75 37Zm0 72.4A34.5 34.5 0 0 1 67.8 41.3l-3.5 38.4L75.7 76l-2 33.3 16.5-46-9.8 2.4 3.3-24A34.4 34.4 0 0 1 75 109.5Z' fill='#050'/></svg>");} );
server.on ( "/0.svg", []() { server.send ( 500, "image/svg+xml", "<svg xmlns='http://www.w3.org/2000/svg' width='100' height='100' viewBox='0 0 200 200'><circle cx='75' cy='75' r='65' fill='#D44B38'/><path d='M75 37a38 38 0 1 0 38 38A38 38 0 0 0 75 37Zm0 72.4A34.5 34.5 0 0 1 67.8 41.3l-3.5 38.4L75.7 76l-2 33.3 16.5-46-9.8 2.4 3.3-24A34.4 34.4 0 0 1 75 109.5Z' fill='#500'/></svg>");} );
server.onNotFound ( handleNotFound );
server.begin();
}
void loop ( void ) {
server.handleClient();
}