Page 1 of 1

MQTT publishing crashes with esp8266 and not with esp32

Posted: Sun Oct 06, 2019 8:16 am
by jpbihin
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

Re: MQTT publishing crashes with esp8266 and not with esp32

Posted: Sun Oct 06, 2019 5:33 pm
by pythoncoder
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.

Re: MQTT publishing crashes with esp8266 and not with esp32

Posted: Mon Oct 07, 2019 5:25 pm
by jpbihin
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

Re: MQTT publishing crashes with esp8266 and not with esp32

Posted: Tue Oct 08, 2019 5:15 am
by pythoncoder
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.