MQTT dropping on ESP8266

I have been using ESP8266 devices with MQTT for a while now as a platform for sensors and switches. Many of them work fine and are very stable but some of them drop out of the network at random. I mostly have to re-boot the device to get it back. I tried checking everything I could think of (e.g. unique client IDs and making sure my code is solid) but I couldn’t improve them. In the last week I came across a post in an ESP8266 forum which suggested that Wifi type n might be the problem. So I changed my router to a WiFi mix of b/g instead of b/g/n. Remarkably, this has completely cleared the problem. All my devices are now rock solid and I’m getting no failures so far. I though it would be worth mentioning on this forum.

I mostly use Wemos D1 minis but I have had the same problem on other types

I followed up further on the above issue and decided to replace my router with a new Asus RT-AX56U router. I was expecting a big improvement but I had to play around a lot with the settings to get a reliable result. Even then, I found a new issue which I had never seen before. Some of my ESP-8266 devices will connect to the network but will return ‘IP unset’. The device thinks it’s connected but has no IP address. I have had to add additional code to the WiFi connect procedure as follows:

  {
    Serial.print("Connecting to AP");
    int connectAttempts = 0;
    while (WiFi.status() != WL_CONNECTED && connectAttempts < 10) 
      {
         delay(1000);
         Serial.print(".");
         connectAttempts++;
      }
      Serial.println("Connection to AP successful or attempts expired : ");
      Serial.print(WiFi.status());
      Serial.println(WiFi.localIP());
      IPAddress ip;  
      ip = WiFi.localIP();        
      if (!ip)                                                    // if IP is unset (problem with AX mode on
         {                                                         // Asus router if it's enabled - Disable
           delay(30000);                                           // wait 30 seconds
           Serial.println("Restarting ....");
           ESP.restart();                                          // start again
         } 
      Serial.println("WiFi connected");  
  }

Devices will usually connect with an IP Address after a few restarts. I have also had to set the wireless mode on the 2.4GHz radio to ‘Legacy’ and disable any AX feature. I would like to know if anyone elase has seen this problem and maybe come up with a better solution.

I have set the 2.4GHz radio mode back to ‘Auto’ from ‘Legacy’. It seems that ‘Legacy’ setting is not correct / needed. Still need to disable AX.

The saga of the Asus Router (RT-AX56U) finally came to an end. I never got a satisfactory set up, having tried a good few options. Many of my devices, especially the Sonoffs, had a lot of difficulty connecting and would drop out frequently. In any case the issue was taken out of my hands when all the LAN ports failed overnight a few days ago. I tried cooling it off and rebooting etc. and 2 Lan ports would re-appear for a while and then fail again. It’s a common problem on some Asus routers, I’ve discovered. Not good as I have a lot of wired devices. Since it was only a few months old I was able to send it back for a refund. I have replaced it with a TP Link AX1500 and it’s like night and day. It’s been rock solid from the day I set it up. Not a single drop out. The admin software is not as nice as the Asus but it does it’s basic job brilliantly, happy days!