Michurinsk
New member
Есть вот такой скетч который управляет реле по mqtt, через физические кнопки и веб интерфейс. Однако с веб интерфейсом проблема, тормозит сильно. В чем моя ошибка?
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
MDNSResponder mdns;
ESP8266WebServer server(80);
const char* ssid = "ssid";
const char* password = "password";
const char* mqtt_server = "ip";
#define RELAY_1 16
#define RELAY_2 2
#define BUTTON_1 4
#define BUTTON_2 5
#define relays_topic "relay/relay_1"
#define relays_topic2 "relay/relay_2"
WiFiClient espClient;
PubSubClient client(espClient);
long last_mls = millis();
long last_mls2 = millis();
long last_mls3 = millis();
boolean Status = true;
boolean Status2 = true;
boolean btnPress = false;
boolean btn2Press = false;
boolean lastbtnStat = false;
boolean lastbtnStat2 = false;
int regim=1;
int regim2=1;
void setup()
{
pinMode(RELAY_1, OUTPUT);
pinMode(RELAY_2, OUTPUT);
pinMode(BUTTON_1, INPUT);
attachInterrupt(BUTTON_1, switch1, CHANGE);
pinMode(BUTTON_2, INPUT);
attachInterrupt(BUTTON_2, switch2, CHANGE);
digitalWrite(RELAY_1, Status);
digitalWrite(RELAY_2, Status2);
Serial.begin(115200);
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
delay(100);
WiFi.begin(ssid, password);
delay(2000);
client.connect("ESP8266Client");
client.subscribe(relays_topic);
client.subscribe(relays_topic2);
}
void callback(char* topic, uint8_t* payload, unsigned int length)
{
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
String strTopic = String(topic); // от них
String strPayload = String((char*)payload);// от них
for (int i = 0; i < length; i++)
{
Serial.print((char)payload);
}
Serial.println();
if (strTopic == "relay/relay_1") //читаем топик 1 реле
{
if ((char)payload[0] == '1')
{
Status = true;
regim = 1;
digitalWrite(RELAY_1, Status);
}
else if ((char)payload[0] == '2')
{
Status = false;
regim = 2;
digitalWrite(RELAY_1, Status);
}
}
if (strTopic == "relay/relay_2") //читаем топик 2 реле
{
if ((char)payload[0] == '1')
{
Status2 = true;
regim2 = 1;
digitalWrite(RELAY_2, Status2);
}
else if ((char)payload[0] == '2')
{
Status2 = false;
regim2 = 2;
digitalWrite(RELAY_2, Status2);
}
}
}
void switch1()
{
Status = digitalRead(RELAY_1);
btnPress = digitalRead(BUTTON_1);
if (btnPress==HIGH&&lastbtnStat==false)
{
//delay(50);
regim++;
lastbtnStat=true;
if(regim>2)
{
regim=1;
}
}
if (btnPress==LOW&&lastbtnStat==true)
{
lastbtnStat=false;
}
if(regim==1) //первый режим
{
Status = true;
digitalWrite(RELAY_1, Status);
}
if(regim==2) //второй режим
{
Status = false;
digitalWrite(RELAY_1, Status);
}
}
void switch2()
{
Status2 = digitalRead(RELAY_2);
btn2Press = digitalRead(BUTTON_2);
if (btn2Press==HIGH&&lastbtnStat2==false)
{
//delay(50);
regim2++;
lastbtnStat2=true;
if(regim2>2)
{
regim2=1;
}
}
if (btn2Press==LOW&&lastbtnStat2==true)
{
lastbtnStat2=false;
}
if(regim2==1)
{
Status2 = true;
digitalWrite(RELAY_2, Status2);
}
if(regim2==2)
{
Status2 = false;
digitalWrite(RELAY_2, Status2);
}
}
void button()
{
server.on("/", [](){
server.send(200, "text/html", webPage());
});
server.on("/socket1On", [](){
digitalWrite(RELAY_1, HIGH);
server.send(200, "text/html", webPage());
delay(100);
});
server.on("/socket1Off", [](){
digitalWrite(RELAY_1, LOW);
server.send(200, "text/html", webPage());
delay(100);
});
server.on("/socket2On", [](){
digitalWrite(RELAY_2, HIGH);
server.send(200, "text/html", webPage());
delay(100);
});
server.on("/socket2Off", [](){
digitalWrite(RELAY_2, LOW);
server.send(200, "text/html", webPage());
delay(100);
});
server.begin();
Serial.println("HTTP server started");
}
void reconnect_server()
{
if (WiFi.status() != WL_CONNECTED)
{
WiFi.begin(ssid, password);
Serial.println("");
Serial.println("WiFi connect...");
} else
if (!client.connected() && WiFi.status() == WL_CONNECTED)
{
if (client.connect("ESP8266Client"))
{
Serial.println("Mosquitto connect...");
client.subscribe(relays_topic);
client.subscribe(relays_topic2);
} else
{
Serial.print("failed connect Mosquitto, rc=");
Serial.print(client.state());
Serial.println("");
}
}
}
void MQTT()
{
if (millis() - last_mls3 > 2000)
{
last_mls3 = millis();
client.publish(relays_topic, String(regim).c_str());
client.publish(relays_topic2, String(regim2).c_str());
}
}
void loop()
{
client.loop();
if (millis() - last_mls > 5000)
{
last_mls = millis();
reconnect_server();
}
MQTT();
button();
{
server.handleClient();
}
}
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 += "<p style=\"text-align: center;margin-top: 0px;margin-bottom: 5px;\">----LED 1----</p>";
if (digitalRead(RELAY_1) == 1)
{
web += "<div style=\"text-align: center;width: 98px;color:white ;padding: 10px 30px;background-color: #43a209;margin: 0 auto;\">ON</div>";
}
else
{
web += "<div style=\"text-align: center;width: 98px;color:white ;padding: 10px 30px;background-color: #ec1212;margin: 0 auto;\">OFF</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>";
web += "<p style=\"text-align: center;margin-top: 0px;margin-bottom: 5px;\">----LED 2----</p>";
if (digitalRead(RELAY_2) == 1)
{
web += "<div style=\"text-align: center;width: 98px;color:white ;padding: 10px 30px;background-color: #43a209;margin: 0 auto;\">ON</div>";
}
else
{
web += "<div style=\"text-align: center;width: 98px;color:white ;padding: 10px 30px;background-color: #ec1212;margin: 0 auto;\">OFF</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>";
web += "<div style=\"text-align:center;margin-top: 20px;\"><a href=\"/\"><button style=\"width:158px;\">REFRESH</button></a></div>";
web += "</div>";
return(web);
}
#include <PubSubClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
MDNSResponder mdns;
ESP8266WebServer server(80);
const char* ssid = "ssid";
const char* password = "password";
const char* mqtt_server = "ip";
#define RELAY_1 16
#define RELAY_2 2
#define BUTTON_1 4
#define BUTTON_2 5
#define relays_topic "relay/relay_1"
#define relays_topic2 "relay/relay_2"
WiFiClient espClient;
PubSubClient client(espClient);
long last_mls = millis();
long last_mls2 = millis();
long last_mls3 = millis();
boolean Status = true;
boolean Status2 = true;
boolean btnPress = false;
boolean btn2Press = false;
boolean lastbtnStat = false;
boolean lastbtnStat2 = false;
int regim=1;
int regim2=1;
void setup()
{
pinMode(RELAY_1, OUTPUT);
pinMode(RELAY_2, OUTPUT);
pinMode(BUTTON_1, INPUT);
attachInterrupt(BUTTON_1, switch1, CHANGE);
pinMode(BUTTON_2, INPUT);
attachInterrupt(BUTTON_2, switch2, CHANGE);
digitalWrite(RELAY_1, Status);
digitalWrite(RELAY_2, Status2);
Serial.begin(115200);
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
delay(100);
WiFi.begin(ssid, password);
delay(2000);
client.connect("ESP8266Client");
client.subscribe(relays_topic);
client.subscribe(relays_topic2);
}
void callback(char* topic, uint8_t* payload, unsigned int length)
{
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
String strTopic = String(topic); // от них
String strPayload = String((char*)payload);// от них
for (int i = 0; i < length; i++)
{
Serial.print((char)payload);
}
Serial.println();
if (strTopic == "relay/relay_1") //читаем топик 1 реле
{
if ((char)payload[0] == '1')
{
Status = true;
regim = 1;
digitalWrite(RELAY_1, Status);
}
else if ((char)payload[0] == '2')
{
Status = false;
regim = 2;
digitalWrite(RELAY_1, Status);
}
}
if (strTopic == "relay/relay_2") //читаем топик 2 реле
{
if ((char)payload[0] == '1')
{
Status2 = true;
regim2 = 1;
digitalWrite(RELAY_2, Status2);
}
else if ((char)payload[0] == '2')
{
Status2 = false;
regim2 = 2;
digitalWrite(RELAY_2, Status2);
}
}
}
void switch1()
{
Status = digitalRead(RELAY_1);
btnPress = digitalRead(BUTTON_1);
if (btnPress==HIGH&&lastbtnStat==false)
{
//delay(50);
regim++;
lastbtnStat=true;
if(regim>2)
{
regim=1;
}
}
if (btnPress==LOW&&lastbtnStat==true)
{
lastbtnStat=false;
}
if(regim==1) //первый режим
{
Status = true;
digitalWrite(RELAY_1, Status);
}
if(regim==2) //второй режим
{
Status = false;
digitalWrite(RELAY_1, Status);
}
}
void switch2()
{
Status2 = digitalRead(RELAY_2);
btn2Press = digitalRead(BUTTON_2);
if (btn2Press==HIGH&&lastbtnStat2==false)
{
//delay(50);
regim2++;
lastbtnStat2=true;
if(regim2>2)
{
regim2=1;
}
}
if (btn2Press==LOW&&lastbtnStat2==true)
{
lastbtnStat2=false;
}
if(regim2==1)
{
Status2 = true;
digitalWrite(RELAY_2, Status2);
}
if(regim2==2)
{
Status2 = false;
digitalWrite(RELAY_2, Status2);
}
}
void button()
{
server.on("/", [](){
server.send(200, "text/html", webPage());
});
server.on("/socket1On", [](){
digitalWrite(RELAY_1, HIGH);
server.send(200, "text/html", webPage());
delay(100);
});
server.on("/socket1Off", [](){
digitalWrite(RELAY_1, LOW);
server.send(200, "text/html", webPage());
delay(100);
});
server.on("/socket2On", [](){
digitalWrite(RELAY_2, HIGH);
server.send(200, "text/html", webPage());
delay(100);
});
server.on("/socket2Off", [](){
digitalWrite(RELAY_2, LOW);
server.send(200, "text/html", webPage());
delay(100);
});
server.begin();
Serial.println("HTTP server started");
}
void reconnect_server()
{
if (WiFi.status() != WL_CONNECTED)
{
WiFi.begin(ssid, password);
Serial.println("");
Serial.println("WiFi connect...");
} else
if (!client.connected() && WiFi.status() == WL_CONNECTED)
{
if (client.connect("ESP8266Client"))
{
Serial.println("Mosquitto connect...");
client.subscribe(relays_topic);
client.subscribe(relays_topic2);
} else
{
Serial.print("failed connect Mosquitto, rc=");
Serial.print(client.state());
Serial.println("");
}
}
}
void MQTT()
{
if (millis() - last_mls3 > 2000)
{
last_mls3 = millis();
client.publish(relays_topic, String(regim).c_str());
client.publish(relays_topic2, String(regim2).c_str());
}
}
void loop()
{
client.loop();
if (millis() - last_mls > 5000)
{
last_mls = millis();
reconnect_server();
}
MQTT();
button();
{
server.handleClient();
}
}
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 += "<p style=\"text-align: center;margin-top: 0px;margin-bottom: 5px;\">----LED 1----</p>";
if (digitalRead(RELAY_1) == 1)
{
web += "<div style=\"text-align: center;width: 98px;color:white ;padding: 10px 30px;background-color: #43a209;margin: 0 auto;\">ON</div>";
}
else
{
web += "<div style=\"text-align: center;width: 98px;color:white ;padding: 10px 30px;background-color: #ec1212;margin: 0 auto;\">OFF</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>";
web += "<p style=\"text-align: center;margin-top: 0px;margin-bottom: 5px;\">----LED 2----</p>";
if (digitalRead(RELAY_2) == 1)
{
web += "<div style=\"text-align: center;width: 98px;color:white ;padding: 10px 30px;background-color: #43a209;margin: 0 auto;\">ON</div>";
}
else
{
web += "<div style=\"text-align: center;width: 98px;color:white ;padding: 10px 30px;background-color: #ec1212;margin: 0 auto;\">OFF</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>";
web += "<div style=\"text-align:center;margin-top: 20px;\"><a href=\"/\"><button style=\"width:158px;\">REFRESH</button></a></div>";
web += "</div>";
return(web);
}