MQTT connection to broker not restored after power loss

Hello,
the problem is that the devices that are connected with mqtt are unavailable after restarting when power goes out in my house.
When the power come back on the following happen:

  1. my router, raspberry, and every other device start up
  2. after a while, raspberry and the other mqtt devices are up and running before the internet connection is established in my router
  3. after that when router is finally connected to the internet, raspberry has an ip address and is fully working and connected to the internet. Also, the other mqtt devices are successfully connected to the WiFi network.

But the problem is that there is no communication between the devices that uses mqtt.
If i restart raspberry everythning works fine again.

Where do you thing is the problem?

I use mqtt binding version 1.x . Do you thing i need to upgrade to version 2? If yes, do i have to make changes to the item files?

Thank you in advance,
Tasos

That has nothing to do with the communication inside your LAN.

The IP address of your pi is given by your router using DCHP. Even before the router has an internet connection.

That will depend on several things

  • Do you devices have static IP? Did you configure your router to assign static IPs to your devices.
  • Did you set-up your pi for static IP?
  • Do your MQTT devices try to reconnect until they get a connection to the broker?
  • Do your devices use an IP in this form in the code xxx.xxx.xxx.xxx for the MQTT broker?
  • What broker do you use? and what IP is the broker on?

You will need to eventually, when the 1.x binding is retired but that’s not going to happen soon.

Hello vzorglub and thank you for your answer.

Yes all of my devices have static IPs and my router assign static IPs

Yes

In my devices (some sonoff and some wemos d1) i use custom firmware.
Here is a part of the code:

void loop() {
if(!client.connected()) {
    reconnect();
  }
  client.loop();
}
void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    // Attempt to connect
    if (client.connect(mqttDeviceID)) { // if client is connected
      client.subscribe(sub_TopicHeater);
      client.subscribe(sub_TopicSystem);
      digitalWrite(LEDPin, !HIGH);
    } 
  }
}

I think so. I use:

const char* mqtt_server = "192.168.1.200";

Mosquitto and 192.168.1.200

Then it’s probably with your broker
Try connecting to it after a power failure. BEFORE rebooting it again. With a utility like mqtt fx or mqtt explorer

If you can connect to your broker then it’s your devices. You code seems ok but you might want to flash one with something like tasmota to see if the problem occurs.

If you can’t connect to your broker after a power cut but you can after a reboot then your power cuts have probably damaged something in the SD card and there is boot/start-up problem.

You MUST protect linux machines from power cuts. If the power cat happens during a read/write of the SD card/Hard drive your system can be bricked. I suggest that you invest in a power bank (The type for mobile phone) to protect your pi. The ESPs will be fine. Make sure that you get one that can be charged and supply power at the same time (Not all can).
Then create a script to ping your router every minute. It it pings OK then the power in on. If no ping then the power is off and start a shut down sudo shutdown -h now to cleanly shutdown linux.

Once the power is back, disconnect the pi from the power bank, connect it again. and off you go again.

1 Like

ok. i will try everything you suggest.
Do you think it is better to transfer the openhab installation to a usb flash drive or leave it in the sd card?

Thank you once again!

No difference. They both use flash memory in more of less the same way.
An SSD or HD would be much better. But a power cut WILL corrupt the files in the same way whether it’s an SSD or an SD.

ok thank you for that.
When i find some decent amount of time to resolve these problems i’ll come back and post!

So i found some time to do some testing. I realize that the problem is with the mqtt v1.3 binding. All devices (sonoffs with custom firmware, sonoffs with tasmota and shellies) are not working properly after power loss.

So i install the mqtt v2.4 binding and i configure only one device (sonoff with tasmota). That device is working as expected after power loss.

So i guess the problem is somewhere in the configuration of mqtt v.1.3. But it doesn’t matter. I will change all my devices to the newest mqtt binding.