MQTT publishing crashes with esp8266 and not with esp32

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
jpbihin
Posts: 7
Joined: Thu Feb 08, 2018 5:31 pm
Location: Belgium

MQTT publishing crashes with esp8266 and not with esp32

Post by jpbihin » Sun Oct 06, 2019 8:16 am

Hi,
My ESP8266 (nodemcu board controled with REPL) crashes after 5 loops . See the code below.
With an ESP32, the code runs correctly.
More information :
- when I remove line 45 : <time.sleep(1)> , it works
- when I remove the line 25 : < client.subscribe(b"sol") >, its works .

Here is the code :

Code: Select all

import time
import machine
from machine import Pin, ADC
from umqtt.simple import MQTTClient
import sys

server = "farmer.cloudmqtt.com"
client_id = "client_ESP151"

def sub_cb():
	print("cb")

# Connection to MQTT server
def connect_and_subscribe():
	global client_id, server
	client = MQTTClient(client_id, server, port = 16157, user='xxxxx', password='xxxxx')
	client.set_callback(sub_cb)
	client.connect()
	client.subscribe(b"sol")
	print('Connected to MQTT broker')
	return client

# reset ESP8266_151
def restart_and_reconnect():
  print('Failed to connect to MQTT broker. Reconnecting...')
  time.sleep(1)
  machine.reset()

try:
	client = connect_and_subscribe()
except Exception as e:
	print(e)
	restart_and_reconnect()

print ("loop start")
for i in range(0,50) :
	print(i)
	client.publish(b"sol", b"hello" + str(i))
	time.sleep(1)
	
Some body can help me understanding what happens ?

Thanks !
JP

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: MQTT publishing crashes with esp8266 and not with esp32

Post by pythoncoder » Sun Oct 06, 2019 5:33 pm

When you say it "crashes" does it produce a traceback? If so perhaps you could post it. You also might want to try issuing

Code: Select all

client.check_msg()
in your main loop. I think this is necessary to be sure to receive subscriptions.

On a more general note the official MQTT code has issues. This repo asynchronous resilient MQTT has a more robust version, although to run it on the ESP8266 you will need to use frozen bytecode.
Peter Hinch
Index to my micropython libraries.

jpbihin
Posts: 7
Joined: Thu Feb 08, 2018 5:31 pm
Location: Belgium

Re: MQTT publishing crashes with esp8266 and not with esp32

Post by jpbihin » Mon Oct 07, 2019 5:25 pm

Hi !
I added

Code: Select all

def sub_cb(topic_to_esp, message):
with some code and

Code: Select all

client.check_msg()
and it works. ;)
It means that <client.check_msg()> or <client.wait_msg()> are mandatory as soon as I subscribe to a topic. Right ?

I read a lot of your comments on this forum... and I learned a lot... thanks Peter !
JP

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: MQTT publishing crashes with esp8266 and not with esp32

Post by pythoncoder » Tue Oct 08, 2019 5:15 am

It means that <client.check_msg()> or <client.wait_msg()> are mandatory as soon as I subscribe to a topic. Right ?
Yes. You are trying to do two things at once: publish and respond to incoming messages. The official MQTT library cleverly fudges concurrency but the application code has to play its part by regularly running check_msg() which causes the subscribe callback to run if a message is ready.

The "full fat" way to do concurrency is to use uasyncio which I consider to be a vital tool for any serious application, but there is a learning curve and the official MQTT code saves you from this.

As stated above my experience of official MQTT is poor in terms of long term stability.
Peter Hinch
Index to my micropython libraries.

Post Reply