/*********
Попытка добавить управление освещением
на даче по Wi-Fi
*********/
// Wi-Fi библиотека
#include <ESP8266WiFi.h>
// Настройки Wi-Fi
const char* ssid = "...:::HoMe:::..."; //имя вафли
const char* password = "11111"; //пароль вафли
// сделать порт Веб-сервера 80
WiFiServer server(80);
// Variable to store the HTTP request
String header;
// Auxiliar variables to store the current output state
String VesSvet = "off";
String Parking = "off";
String Dvor = "off";
String Shashlichnya = "off";
// Assign output variables to GPIO pins
const int L_Parking = 16;
const int L_Dvor = 14;
const int L_Shashl = 2;
const int KnopkaAvto = 5;
const int KnopkaDvor = 4;
const int KnopkaShashl = 3;
const int KnopkaParking = 1;
bool z1=0;
bool status_k1=0;
unsigned long time_k1;
bool z2=0;
bool status_k2=0;
unsigned long time_k2;
bool z3=0;
bool status_k3=0;
unsigned long time_k3;
bool k4=0;
bool status_k4=0;
unsigned long time_k4;
unsigned long time_f=1000;
void setup() {
Serial.begin(115200);
// Initialize the output variables as outputs
pinMode(KnopkaAvto, INPUT_PULLUP);
pinMode(KnopkaDvor, INPUT_PULLUP);
pinMode(KnopkaShashl, INPUT_PULLUP);
pinMode(KnopkaParking, INPUT_PULLUP);
pinMode(L_Parking, OUTPUT);
pinMode(L_Dvor, OUTPUT);
pinMode(L_Shashl, OUTPUT);
// Set outputs to LOW
digitalWrite(L_Parking, LOW);
digitalWrite(L_Dvor, LOW);
digitalWrite(L_Shashl, LOW);
// Connect to Wi-Fi network with SSID and password
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// Print local IP address and start web server
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
server.begin();
}
void loop(){
//обработка нажатий кнопок
// первое нажатие кнопки №1
if (digitalRead(KnopkaAvto)==LOW && z1==0 && millis()-time_k1>time_f)
{
VesSvet = "on";
Parking = "on";
Dvor = "on";
Shashlichnya = "on";
z1=1;
z2=1;
z3=1;
time_k1=millis();
digitalWrite(L_Parking, HIGH);
digitalWrite(L_Dvor, HIGH);
digitalWrite(L_Shashl, HIGH);
}
// второе нажатие кнопки №1
if (digitalRead(KnopkaAvto)==LOW && z1==1 && millis()-time_k1>time_f)
{
VesSvet = "off";
Parking = "off";
Dvor = "off";
Shashlichnya = "off";
z1=0;
z2=0;
z3=0;
time_k1=millis();
digitalWrite(L_Parking, LOW);
digitalWrite(L_Dvor, LOW);
digitalWrite(L_Shashl, LOW);
}
WiFiClient client = server.available(); // Listen for incoming clients
if (client) { // If a new client connects,
Serial.println("New Client."); // print a message out in the serial port
String currentLine = ""; // make a String to hold incoming data from the client
while (client.connected()) { // loop while the client's connected
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
header += c;
if (c == '\n') { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Connection: close");
client.println();
// turns the GPIOs on and off
if (header.indexOf("GET /All/on") >= 0) {
Serial.println("All lights is on");
VesSvet = "on";
Parking = "on";
Dvor = "on";
Shashlichnya = "on";
z1=1;
z2=1;
z3=1;
digitalWrite(L_Parking, HIGH);
digitalWrite(L_Dvor, HIGH);
digitalWrite(L_Shashl, HIGH);
} else if (header.indexOf("GET /All/off") >= 0) {
Serial.println("All lights is off");
VesSvet = "off";
Parking = "off";
Dvor = "off";
Shashlichnya = "off";
z1=0;
z2=0;
z3=0;
digitalWrite(L_Parking, LOW);
digitalWrite(L_Dvor, LOW);
digitalWrite(L_Shashl, LOW);
} else if (header.indexOf("GET /4/on") >= 0) {
Serial.println("GPIO 4 on");
Parking = "on";
z1=1;
digitalWrite(L_Parking, HIGH);
} else if (header.indexOf("GET /4/off") >= 0) {
Serial.println("GPIO 4 off");
Parking = "off";
z1=0;
digitalWrite(L_Parking, LOW);
} else if (header.indexOf("GET /14/on") >= 0) {
Serial.println("GPIO 14 on");
Dvor = "on";
z2=1;
digitalWrite(L_Dvor, HIGH);
} else if (header.indexOf("GET /14/off") >= 0) {
Serial.println("GPIO 14 off");
Dvor = "off";
z2=0;
digitalWrite(L_Dvor, LOW);
} else if (header.indexOf("GET /12/on") >= 0) {
Serial.println("L_Shashl on");
Shashlichnya = "on";
z3=1;
digitalWrite(L_Shashl, HIGH);
} else if (header.indexOf("GET /12/off") >= 0) {
Serial.println("L_Shashl off");
Shashlichnya = "off";
z3=0;
digitalWrite(L_Shashl, LOW);
}
// Display the HTML web page
client.println("<!DOCTYPE html><html>");
client.println("<head><meta charset=\"utf-8\" name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
client.println("<link rel=\"icon\" href=\"data:,\">");
// CSS to style the on/off buttons
// Feel free to change the background-color and font-size attributes to fit your preferences
client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");
client.println(".button { background-color: #FF0000; border: none; color: white; padding: 16px 40px;");
client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
client.println(".button2 {background-color: #00FF00;}</style></head>");
// Web Page Heading
client.println("<body><h1>Управление светом</h1>");
// Display current state, and ON/OFF buttons for GPIO 5
client.println("<p>Весь свет - " + VesSvet + "</p>");
// If the VesSvet is off, it displays the ON button
client.println("<p><a href=\"/All/on\"><button class=\"button button2\">ON</button></a><a href=\"/All/off\"><button class=\"button\">OFF</button></a></p>");
client.println("");
// Display current state, and ON/OFF buttons for GPIO 4
client.println("<p>Парковка - " + Parking + "</p>");
// If the Parking is off, it displays the ON button
if (Parking=="off") {
client.println("<p><a href=\"/4/on\"><button class=\"button\">ON</button></a></p>");
} else {
client.println("<p><a href=\"/4/off\"><button class=\"button button2\">OFF</button></a></p>");
}
// Display current state, and ON/OFF buttons for GPIO 14
client.println("<p>Двор - " + Dvor + "</p>");
// If the Dvor is off, it displays the ON button
if (Dvor=="off") {
client.println("<p><a href=\"/14/on\"><button class=\"button\">ON</button></a></p>");
} else {
client.println("<p><a href=\"/14/off\"><button class=\"button button2\">OFF</button></a></p>");
}
// Display current state, and ON/OFF buttons for GPIO 12
client.println("<p>Шашлычная - " + Shashlichnya + "</p>");
// If the Shashlichnya is off, it displays the ON button
if (Shashlichnya=="off") {
client.println("<p><a href=\"/12/on\"><button class=\"button\">ON</button></a></p>");
} else {
client.println("<p><a href=\"/12/off\"><button class=\"button button2\">OFF</button></a></p>");
}
client.println("</body></html>");
// The HTTP response ends with another blank line
client.println();
// Break out of the while loop
break;
} else { // if you got a newline, then clear currentLine
currentLine = "";
}
} else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
}
}
// Clear the header variable
header = "";
// Close the connection
client.stop();
Serial.println("Client disconnected.");
Serial.println("");
}
}