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:
my router, raspberry, and every other device start up
after a while, raspberry and the other mqtt devices are up and running before the internet connection is established in my router
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?
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);
}
}
}
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.
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.
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.