Hello everyone, I'm running my command to turn on and control my air conditioner but I have a problem. After connecting my ESP8266 I can execute the commands but after a series of executions the system reconnects to the WIFI network. Sometimes the system is not used, thanks to the monitor, I can also see that it reconnects but to a lesser extent. What could be generating this?
At some point I thought it would be some wiring but even disconnecting everything and executing the command posts I also get the reconnections.
#include <Arduino.h>
#include <IRremoteESP8266.h>
#include <IRsend.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <WiFiClient.h>
//Static IP address configuration
IPAddress staticIP(192, 168, 0, 16); //ESP static IP address
IPAddress gateway(192, 168, 0, 1); //IP Address of your WiFi Router (Gateway)
IPAddress subnet(255, 255, 255, 0); //Subnet mask
IPAddress dns(8, 8, 8, 8); //DNS
const char* ssid = "San Blas";
const char* password = "seba2703";
const char* mqtt_server = "192.168.0.15";
const uint16_t kIrLed = 4;
const char* deviceName = "ESP8266-1";
IRsend irsend(kIrLed);
WiFiClient espClient;
PubSubClient client(espClient);
uint16_t TEMP20[199] = {4269, 433, 654, 433, 1732, 433, 1732, 433, 654, 433, 1732, 433, 654, 433, 1732, 433, 1732, 433, 1732, 433};
uint16_t TEMP24[199] = {4269, 4497, 433, 1732, 433, 654, 433, 1732, 433, 1732, 433, 654, 433, 654, 433, 1732, 433, 654, 433, 654, 433, 433};
uint16_t TEMP28[199] = {4269, 4497, 433, 1732, 433, 654, 433, 1732, 433, 1732, 433, 654, 433, 654, 433, 1732, 433, 654, 433, 654, 33, 1732, 433, 654, 433, 654, 433, 1732, 433, 1732, 433, 654, 433, 1732, 433, 654, 433, 654, 433, 654, 433, 1732, 433, 1732, 433, 1732, 433, 1732, 433, 1732, 433, 1732, 433, 1732, 433, 1732, 433, 654, 433, 654, 433, 654, 433, 654, 433, 654, 433, 1732, 433, 654, 433, 654, 433, 654, 433, 1732, 433, 654, 433, 654, 433, 654, 433, 654, 433, 1732, 433, 1732, 433, 1732, 433, 654, 433, 1732, 433, 1732, 433, 1732, 433};
uint16_t Off[199] = {4269, 4492, 433, 1732, 433, 1732, 433, 654, 433, 1732, 433, 1732, 433, 1732, 433, 654, 433, 654, 433, 654, 433, 654, 433, 1732, 433, 654, 433, 654, 433, 1732, 433, 1732, 433, 1732, 433, 654, 433, 654, 433, 654, 433, 654, 433, 654, 433, 654, 433, 654, 433, 654, 433, 1732, 433, 1732, 433, 1732, 433, 1732, 433, 1732, 433};
uint16_t On[199] = {4269, 654, 433, 1732, 433, 1732, 433, 1732, 433, 1732, 433, 1732, 433, 1732, 433, 1732, 433, 1732, 433, 654, 433, 654, 433, 654, 433, 654, 433, 654, 433, 654, 433, 1732, 433, 654, 433, 654, 433, 1732, 433, 654, 433, 654, 433, 654, 433, 1732, 433, 654, 433, 1732, 433, 1732, 433, 654, 433, 1732, 433, 1732, 433, 1732, 433};
void setup() {
Serial.begin(115200);
pinMode(BUILTIN_LED, OUTPUT);
irsend.begin();
/////////// WIFI
WiFi.begin(ssid, password);
Serial.println("");
WiFi.disconnect();
WiFi.hostname(deviceName);
WiFi.config(staticIP, subnet, gateway, dns);
WiFi.begin(ssid, password);
WiFi.mode(WIFI_STA);
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());
/////////// WIFI
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}
void callback(String topic, byte* message, unsigned int length) {
Serial.print("Message arrived on topic: ");
Serial.print(topic);
Serial.print(". Message: ");
String messageTemp;
int msgLen = messageTemp.length();
for (int i = 0; i < length; i++) {
Serial.print((char)message[i]);
messageTemp += (char)message[i];
}
Serial.println();
if(topic=="esp1/habitacion/aire"){
Serial.print("Estado del aire: ");
if(messageTemp == "1"){
irsend.sendRaw(On, 199, 38);
Serial.print("Encendido");
client.publish("esp1/habitacion/aire/state","ON", false);
client.publish("esp1/habitacion/aire/temperatura","",false);
}
else if(messageTemp == "0"){
irsend.sendRaw(Off, 199, 38);
Serial.print("Apagado");
client.publish("esp1/habitacion/aire/state","OFF", false);
client.publish("esp1/habitacion/aire/temperatura","",false);
}
}
else if(topic=="esp1/habitacion/aire/temperatura"){
Serial.print("Temperatura del aire: ");
else if(messageTemp == "20"){
irsend.sendRaw(TEMP20, 199, 38);
}
else if(messageTemp == "24"){
irsend.sendRaw(TEMP24, 199, 38);
}
else if(messageTemp == "28"){
irsend.sendRaw(TEMP28, 199, 38);
}
client.beginPublish("esp1/habitacion/aire/temperatura/state", msgLen, false);
client.print(messageTemp);
client.endPublish();
Serial.print(messageTemp);
Serial.print(" ºC");
}
Serial.println();
}
void reconnect() {
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
if (client.connect("ESP8266Client")) {
Serial.println("connected");
//client.publish("esp1/status","ON");
//Serial.print("Estado ESP1: ON");
Serial.println();
client.subscribe("esp1/habitacion/aire", false);
client.subscribe("esp1/habitacion/aire/temperatura", false);
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}
void loop() {
if (!client.connected()) {
reconnect();
}
if(!client.loop())
client.connect("ESP8266Client");
}