Go Down

Topic: Wifi permanently reconnecting after executing command via MQTT (Read 552 times) previous topic - next topic

sebasanblas

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.

Code: [Select]
#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");
}

pylon


sebasanblas

Excuse me, I'm very new to this, do you mean the serial monitor output?

pylon

Quote
Excuse me, I'm very new to this, do you mean the serial monitor output?
No, above code tries to connect to a server (MQTT broker, IP 192.168.0.15), that server has a log file and in that log file you usually find the reason for the disconnect.

sebasanblas

Running
Code: [Select]
tail -f /var/log/mosquitto/mosquitto.log on my Rpi - Mosquitto

I obtained

Code: [Select]
1584110499: New connection from 192.168.0.16 on port 1883.
1584110499: New client connected from 192.168.0.16 as ESP8266Client (c1, k15).
1584110801: Saving in-memory database to /var/lib/mosquitto/mosquitto.db.
1584112035: New connection from ::1 on port 1883.
1584112035: New client connected from ::1 as mosqpub|29968-openhab (c1, k60).
1584112035: Client mosqpub|29968-openhab disconnected.
1584112040: New connection from ::1 on port 1883.
1584112040: New client connected from ::1 as mosqpub|29985-openhab (c1, k60).
1584112040: Client mosqpub|29985-openhab disconnected.
1584112056: New connection from ::1 on port 1883.
1584112056: New client connected from ::1 as mosqpub|30074-openhab (c1, k60).
1584112056: Client mosqpub|30074-openhab disconnected.
1584112056: Socket error on client ESP8266Client, disconnecting.
1584112056: New connection from 192.168.0.16 on port 1883.
1584112056: New client connected from 192.168.0.16 as ESP8266Client (c1, k15).
1584112058: New connection from ::1 on port 1883.
1584112058: New client connected from ::1 as mosqpub|30076-openhab (c1, k60).
1584112058: Client mosqpub|30076-openhab disconnected.
1584112081: Client ESP8266Client has exceeded timeout, disconnecting.
1584112081: Socket error on client ESP8266Client, disconnecting.
1584112081: New connection from 192.168.0.16 on port 1883.
1584112081: New client connected from 192.168.0.16 as ESP8266Client (c1, k15).
1584112103: New connection from ::1 on port 1883.
1584112103: New client connected from ::1 as mosqpub|30304-openhab (c1, k60).
1584112103: Client mosqpub|30304-openhab disconnected.
1584112104: New connection from ::1 on port 1883.
1584112104: New client connected from ::1 as mosqpub|30321-openhab (c1, k60).
1584112104: Client mosqpub|30321-openhab disconnected.
1584112127: Client ESP8266Client has exceeded timeout, disconnecting.


Is there a place where I can extract more accurate logs?

pylon

Quote
Is there a place where I can extract more accurate logs?
That's enough.

It seems your WiFi connection to the ESP8266 isn't stable enough. Read out the signal level on the ESP and write that value to the MQTT, I'd expect it to be very low so that it's loosing connection because of the changing noise level.

zoomkat

Auto reconnection after a lost connection may be the default setting on your ESP. The below link has some interesting connection info.

https://arduino-esp8266.readthedocs.io/en/latest/esp8266wifi/station-class.html
Google forum search: Use Google Search box in upper right side of this page.
Why I like my 2005 Rio Yellow Honda S2000  https://www.youtube.com/watch?v=pWjMvrkUqX0

Go Up