#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
MDNSResponder mdns;
const char* ssid = "********";
const char* password = "********";
byte arduino_mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
IPAddress ip(192,168,1,242);
IPAddress gateway(192,168,1,1);
IPAddress subnet(255,255,255,0);
ESP8266WebServer server(80);
int Relay1 = 2;
int Relay2 = 14;
int But1 = 0;
int But2 = 13;
int flag=0;
int flag1=0;
void setup(void){
pinMode(Relay1, OUTPUT);
digitalWrite(Relay1, LOW);
pinMode(Relay2, OUTPUT);
digitalWrite(Relay2, LOW);
delay(100);
Serial.begin(115200);
WiFi.begin(ssid, password);
WiFi.config(ip, gateway, subnet);
Serial.println("");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
if (mdns.begin("esp8266", WiFi.localIP())) {
Serial.println("MDNS responder started");
}
//+++++++++++++++++++++++ START Relay2 +++++++++++++++++++++++
server.on("/", [](){
server.send(200, "text/html", webPage());
});
server.on("/socket1On", [](){
digitalWrite(Relay1, HIGH);
server.send(200, "text/html", webPage());
delay(100);
});
server.on("/socket1Off", [](){
digitalWrite(Relay1, LOW);
server.send(200, "text/html", webPage());
delay(100);
//+++++++++++++++++++++++ END Relay1 +++++++++++++++++++++++++
//+++++++++++++++++++++++ START Relay2 +++++++++++++++++++++++
});
server.on("/socket2On", [](){
digitalWrite(Relay2, HIGH);
server.send(200, "text/html", webPage());
delay(100);
});
server.on("/socket2Off", [](){
digitalWrite(Relay2, LOW);
server.send(200, "text/html", webPage());
delay(100);
//++++++++++++++++++++++++ END Relay2 +++++++++++++++++++++++
});
server.begin();
Serial.println("HTTP server started");
}
void loop(void){
server.handleClient();
// ++++++++++++++++++++++++ Buttons +++++++++++++++++++++++++++
if(digitalRead(But1)==HIGH&&flag==0)
{
digitalWrite(Relay1,!digitalRead(Relay1));
flag=1;
}
if(digitalRead(But1)==LOW&&flag==1)
{
flag=0;
}
{
if(digitalRead(But2)==HIGH&&flag1==0)
{
digitalWrite(Relay2,!digitalRead(Relay2));
flag1=1;
}
if(digitalRead(But2)==LOW&&flag1==1)
{
flag1=0;
}
// +++++++++++++++++++++++ Buttons +++++++++++++++++++++++
}
}
String webPage()
{
String web;
web += "<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"/> <meta charset=\"utf-8\"><title>Интернет выключатель</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;\">Интернет выключатель</h1><div>";
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++ Relay-1 ++++++++++++++++++++++++++++++++++++++++++++++++++++
web += "<p style=\"text-align: center;margin-top: 0px;margin-bottom: 5px;\">Выключатель 1</p>";
if (digitalRead(Relay1) == 1)
{
web += "<div style=\"text-align: center;width: 98px;color:white ;padding: 10px 30px;background-color: #43a209;margin: 0 auto;\">Включен</div>";
}
else
{
web += "<div style=\"text-align: center;width: 98px;color:white ;padding: 10px 30px;background-color: #ec1212;margin: 0 auto;\">Выключен</div>";
}
web += "<div style=\"text-align: center;margin: 5px 0px;\"> <a href=\"socket1On\"><button>ON</button></a> <a href=\"socket1Off\"><button>OFF</button></a></div>";
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++ Relay-1 ++++++++++++++++++++++++++++++++++++++++++++++++++++
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++ Relay-2 ++++++++++++++++++++++++++++++++++++++++++++++++++++
web += "<p style=\"text-align: center;margin-top: 0px;margin-bottom: 5px;\">Выключатель 2</p>";
if (digitalRead(Relay2) == 1)
{
web += "<div style=\"text-align: center;width: 98px;color:white ;padding: 10px 30px;background-color: #43a209;margin: 0 auto;\">Включен</div>";
}
else
{
web += "<div style=\"text-align: center;width: 98px;color:white ;padding: 10px 30px;background-color: #ec1212;margin: 0 auto;\">Выключен</div>";
}
web += "<div style=\"text-align: center;margin: 5px 0px;\"> <a href=\"socket2On\"><button>ON</button></a> <a href=\"socket2Off\"><button>OFF</button></a></div>";
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++ Relay-2 ++++++++++++++++++++++++++++++++++++++++++++++++++++
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++ REFRESH ++++++++++++++++++++++++++++++++++++++++++++++++++++
web += "<div style=\"text-align:center;margin-top: 20px;\"><a href=\"/\"><button style=\"width:158px;\">Обновить</button></a></div>";
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++ REFRESH ++++++++++++++++++++++++++++++++++++++++++++++++++++
web += "</div>";
return(web);
}