Problems with mqtt

Recurrent H/W and software problems
JosefDuisberg
Posts: 11
Joined: Sun Nov 14, 2021 8:14 am

Problems with mqtt

Post by JosefDuisberg »

Hello,
I have an interesting problem:
I use mqtt and use it to control my home automation - it works wonderfully, but at some point the routine mqtt no longer works.
I think the code is easy:

Code: [Local Link Removed for Guests]

'================================================================================
modulname$ = "/Solarspeicher"                ' Name des Moduls in mqtt
mqttIP$ =    "xxx.xxx.xxx.xxx"
mqttuser$ =  "yyyyyy"
mqttpassw$ = "zzzz"

' Eingänge und Ausgänge setzen
PIN.MODE 3, output               ' GPIO 1      Relais-Ausgang
pin(3) = 0

print mqtt.setup(mqttIP$)                                                ' Adresse des mqtt-Servers im iobroker - Port 1883
print mqtt.connect(mqttuser$, mqttpassw$)                      ' User und Passwort des mqtt-servers im iobroker
print mqtt.publish(modulname$ + "/0-life-state","Start")    ' Lebenszeichen - iobroker sendet 0, System antwortet mit  1 (lebt)
print mqtt.subscribe(modulname$ )                                   ' Befehle abbonieren

print mqtt.subscribe(modulname$ + "/0-life-state")             ' Lebenszeichen - iobroker sendet 0, System antwortet mit  1 (lebt)
print mqtt.subscribe(modulname$ + "/Zusatzlast")


' Eingänge und Ausgänge setzen
PIN.MODE 3, output                                                           ' GPIO 1      Relais-Ausgang
pin(3) = 0

onmqtt mqtt_befehl
OPTION.WDT 125000                                                         ' 125 sek = 2 min watch dog .... danach reboot

wait
 
' ============================== 
mqtt_befehl:
  OPTION.WDTRESET                     ' watch dog timer zurücksetzen
  if mqtt.topic$ = modulname$ + "/0-life-state" and mqtt.message$ = "0" then      
    print mqtt.publish(modulname$ + "/0-life-state",BAS.FILENAME$ + " läuft")        ' Lebenszeichen - iobroker sendet 0, System antwortet mit  1 (lebt)
  end if

  if mqtt.topic$ = modulname$ + "/Zusatzlast" and mqtt.message$ = "1" then 
    pin(3) = 1 
  endif
  if mqtt.topic$ = modulname$ + "/Zusatzlast" and mqtt.message$ = "0" then 
    pin(3) = 0
  endif

return
'================================================================================
Everything works, the code runs ...... but only for a few days, after that communication via mqtt is no longer possible (but the processor is still running - I put additional blinking LEDs in the code .... the LED keeps working)
Is it possible that a stack overflows in the long term when mqtt is integrated?

Does somebody has any idea?

Josef Duisberg
User avatar
PANNO
Posts: 114
Joined: Thu Feb 25, 2021 4:03 am
Has thanked: 119 times
Been thanked: 25 times

Re: Problems with mqtt

Post by PANNO »

Use a watchdog timer to restart the code after
24 h . This will help
User avatar
cicciocb
Site Admin
Posts: 1889
Joined: Mon Feb 03, 2020 1:15 pm
Location: Toulouse
Has thanked: 405 times
Been thanked: 1260 times
Contact:

Re: Problems with mqtt

Post by cicciocb »

[Local Link Removed for Guests] wrote: [Local Link Removed for Guests]Sun Apr 24, 2022 5:18 am Hello,
I have an interesting problem:
I use mqtt and use it to control my home automation - it works wonderfully, but at some point the routine mqtt no longer works.
I think the code is easy:
.................

Everything works, the code runs ...... but only for a few days, after that communication via mqtt is no longer possible (but the processor is still running - I put additional blinking LEDs in the code .... the LED keeps working)
Is it possible that a stack overflows in the long term when mqtt is integrated?

Does somebody has any idea?

Josef Duisberg
Hi Josef,
Are you using the ESP8266 ?
User avatar
Electroguard
Posts: 835
Joined: Mon Feb 08, 2021 6:22 pm
Has thanked: 267 times
Been thanked: 317 times

Re: Problems with mqtt

Post by Electroguard »

Hi, you could help others to help you by letting them know what version of Annex you were using... some versions may have known bugs which may be not present in other versions or devices.

If you are using an esp8266 then you appear to be using the hardware serial RX pin 3 as output to drive a relay, but you are still also sending print output to the hardware serial port, which could cause confusion.
Try using a different pin, eg: pin 2

It might prove a point to temporarily try another type of device if possible... if you are having the problems on an esp8266 then try using an esp32 to see if the problems persist on different hardware and firmware.

I don't know if you are hosting your own MQTT server, if so, presumably you are confident it is already working ok with other devices.

Assuming your home automation has other devices connected, you could try periodically sending the current RAMFREE to be logged elsewhere, which would allow you to spot any memory leakage trends, and similarly keep track of any variables which may be relevant.

I have a similar 'degrading over time' problem with a TFT thermostat which stops accepting browser connections after a few days but still keeps on functioning for days or even weeks after.
If I cannot find the cause of such problems I must work around them, which usually means scheduled the device to reboot during the early hours.
JosefDuisberg
Posts: 11
Joined: Sun Nov 14, 2021 8:14 am

Re: Problems with mqtt

Post by JosefDuisberg »

Thank you for the good information.

I am using version 1.43 and use an ESP-01

The serial interface is not used in my software - changing to other pins (e.g. PIN 2) gives similar results.

A change from ESP8266 to ESP32 is unfortunately not possible (there is a lack of space), but I noticed the same effect with the ESP32.

The reference to watch dog is already implemented. The ESP module is addressed every minute and sends a "life message" from itself.


Josef Duisberg
User avatar
cicciocb
Site Admin
Posts: 1889
Joined: Mon Feb 03, 2020 1:15 pm
Location: Toulouse
Has thanked: 405 times
Been thanked: 1260 times
Contact:

Re: Problems with mqtt

Post by cicciocb »

[Local Link Removed for Guests] wrote: [Local Link Removed for Guests]Mon Apr 25, 2022 7:43 pm Thank you for the good information.

I am using version 1.43 and use an ESP-01

The serial interface is not used in my software - changing to other pins (e.g. PIN 2) gives similar results.

A change from ESP8266 to ESP32 is unfortunately not possible (there is a lack of space), but I noticed the same effect with the ESP32.

The reference to watch dog is already implemented. The ESP module is addressed every minute and sends a "life message" from itself.


Josef Duisberg
HI Josef,
probably the MQTT lose the contact with the server after some time and the connection is not restored automatically.

The latest version of the ESP32 includes a new driver that should work much better than the ESP8266.
So, I think that this problem do not affect the ESP32 (maybe someone could confirm).

In the next version of the ESP8266 I'll include a watchdog for the MQTT connection that will restart the connection automatically if lost.
JosefDuisberg
Posts: 11
Joined: Sun Nov 14, 2021 8:14 am

Re: Problems with mqtt

Post by JosefDuisberg »

do I have a chance to speed this up (donation ;) )
User avatar
PANNO
Posts: 114
Joined: Thu Feb 25, 2021 4:03 am
Has thanked: 119 times
Been thanked: 25 times

Re: Problems with mqtt

Post by PANNO »

:lol: i mean we can’t payout cocciocb but maybe he like some Extra Coffee 😜
User avatar
cicciocb
Site Admin
Posts: 1889
Joined: Mon Feb 03, 2020 1:15 pm
Location: Toulouse
Has thanked: 405 times
Been thanked: 1260 times
Contact:

Re: Problems with mqtt

Post by cicciocb »

[Local Link Removed for Guests] wrote: [Local Link Removed for Guests]Wed May 04, 2022 8:35 am do I have a chance to speed this up (donation ;) )
It's up to you if you want give a donation to the project.

By the way, I'm actually working on the MQTT (and Telegram too) for the ESP8266.
JosefDuisberg
Posts: 11
Joined: Sun Nov 14, 2021 8:14 am

Re: Problems with mqtt

Post by JosefDuisberg »

cicciocb's statement seems to be correct, I built a test system with an ESP32. The new mqtt driver is great - there are no bugs. There was no mqtt disconnection in 2 weeks. I hope that this driver can also be used for the 8266, because I can't use an ESP32 (too little space in my applications). ??
Post Reply