"SUB client" of umqtt simple.py crashes on EVERY message sent by another simple.py "PUB client"

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
dirkpy
Posts: 3
Joined: Fri Jul 19, 2019 2:54 pm

"SUB client" of umqtt simple.py crashes on EVERY message sent by another simple.py "PUB client"

Post by dirkpy » Fri Jul 19, 2019 3:12 pm

I have 2 ESP8266's on my private mosquitto server, one as a pub and one as a sub, using umqtt (simple.py) using the standard github example code (see hereunder)
Issue:
When the ESP-pub publishes a message, the receiving ESP-sub crashes on EVERY message sent by the ESP-pub - REGARDLESS THE TOPIC - on line 172 of simple.py ( if res == b"": raise OSError(-1) )
Other MQTT clients in the network (mosquitto under ubuntu, Paho on different Pi's, Domoticz mqtt client) show and process the published message by the ESP-pub correctly and, the other way around, the ESP-sub shows the messages from all other mqtt publishers in the network without a problem. So the only problem is ANY message sent from an ESP-pub, received by an ESP-sub, regardless the topic or the message format. Very strange....
I tried zillions of configurations with different formats of the topic, message and versions of micropython. It ALWAYS crashes simple.py on line 172.
The mosquitto log on the server doesn't show anything unusual.
Can somebody reproduce this issue or put me on the right (debug-)track? (I DO miss some logging/debug possibilities in umqtt.....)

Environment (very stable):
Private MQTT server mosquitto under Ubuntu 16.04 (running without a glitch for over a year with several Pi's using Paho as MQTT clients)
MicroPython version esp8266-20190529-v1.11 running on two ESP8266's

example code PUB side:

from umqtt.simple import MQTTClient
def main(server="192.168.xx.xx"):
c = MQTTClient("umqtt_client", server)
c.connect()
c.publish(b"foo_topic", b"hello")
c.disconnect()
if __name__ == "__main__":
main()

example code SUB side:

import time
from umqtt.simple import MQTTClient
# Received messages from subscriptions will be delivered to this callback
def sub_cb(topic, msg):
print((topic, msg))
def main(server="192.168.xx.xx"):
c = MQTTClient("umqtt_client", server)
c.set_callback(sub_cb)
c.connect()
c.subscribe(b"foo_topic")
while True:
if True:
# Blocking wait for message
c.wait_msg()
else:
# Non-blocking wait for message
c.check_msg()
# Then need to sleep to avoid 100% CPU usage (in a real
# app other useful actions would be performed instead)
time.sleep(1)
c.disconnect()

if __name__ == "__main__":
main()

the only modification of simple.py is the line with the login credentials:
def __init__(self, client_id, server, port=1883, user="xxxxx", password="yyyyyyy", keepalive=0, ssl=False, ssl_params={}):

The piece of code in simple.py that generates the OSError:
def wait_msg(self):
res = self.sock.read(1)
self.sock.setblocking(True)
if res is None:
return None
if res == b"": <<<<<<<<<<< line 172
raise OSError(-1)
if res == b"\xd0": # PINGRESP
sz = self.sock.read(1)[0]
assert sz == 0
return None

Thanks for your help!
Dirkpy

User avatar
Frida
Posts: 40
Joined: Sat Jan 30, 2016 2:20 pm
Location: Middelfart, Denmark

Re: "SUB client" of umqtt simple.py crashes on EVERY message sent by another simple.py "PUB client"

Post by Frida » Fri Jul 19, 2019 5:01 pm

You must have different client id on every esp for it to work.

Code: Select all

c = MQTTClient("umqtt_client", server) on your PUB
c = MQTTClient("umqtt_client", server) on your SUB
Chance "umqtt_client" to somewhat different on one of your ESP.
Yes Frida is my watchdog!

dirkpy
Posts: 3
Joined: Fri Jul 19, 2019 2:54 pm

Re: "SUB client" of umqtt simple.py crashes on EVERY message sent by another simple.py "PUB client"

Post by dirkpy » Sat Jul 20, 2019 5:24 pm

Thanks very much for your reaction Frida!
I didn't realise that it has to be different client id's. Sounds very logic.
I'll try and let you know.
Dirkpy

dirkpy
Posts: 3
Joined: Fri Jul 19, 2019 2:54 pm

Re: "SUB client" of umqtt simple.py crashes on EVERY message sent by another simple.py "PUB client"

Post by dirkpy » Sun Jul 21, 2019 9:08 pm

Works and what a simple solution!
Thanks again.
Dickpy

Post Reply