Issue with MQTT callback

Hi everybody
i am playing with Wio RP2040 board
I use this example : https://wiki.seeedstudio.com/Use_MQTT_to_remotely_light_up_LED_lights/
I can send msg via MQTT to mosquitto on a Raspberry Pi 400.
On the other way, when I send a message from mosquitto, it seems that the callback function is not called, so i can’t on/off the USER LED on the board.


Here is my test program

Programme test MQTT

import network
import mqtt
from machine import Pin, I2C, ADC, UART, SPI, PWM
from time import sleep

N1 = network.WLAN_SPI(network.STA_IF)
N1.active(True)
N1.connect(“Livebox-57A9”,“258xxxxxxxxxx279”)
“”“if N1.isconnected():
print(” ip gateway netmask MAC ssid")
print(N1.ifconfig())
“”"
print (“Connecté au Wi-Fi”)
print(N1.ifconfig()[0])
sleep(1)

led = Pin(13, Pin.OUT)
led.value(0)

SERVER = ‘192.168.1.31’
CLIENT_ID = ‘Wio RP2040_Dev_board’
TOPIC = ‘LED’

def mqtt_callback(topic) :
print(“Received !”)
print(‘topic: {}’.format(topic[0]))
print(‘msg:{}’.format(topic[1]))
if(topic[1] == “off”):
led.value(0)
if(topic[1] == “on”):
led.value(1)

cl = mqtt.MQTTClient(CLIENT_ID, SERVER, mqtt_port = 1883)
cl.connect()

cl.publish(“Wio_RP2040”,“Démarrage Wio”)
cl.subscribe(TOPIC)
cl.set_callback(mqtt_callback)
print(‘Connecté au broker MQTT %s, inscrit au topic %s’ % (SERVER, TOPIC))
print(‘ok’)

compteur = 0
while True:
compteur += 5
cl.wait_msg()
print (“Compteur :”, compteur)
cl.publish(“Wio_RP2040”, str(compteur))
sleep(1)

I verify twice the function name but when i send a message to the topic LED, i never see “Received !” in the shell windows in Thonny IDE.

Maybe somebody can telle me what i made wrong…

have a nice day
François

Hi, I had the same problem. I updated the the board with the latest UF2 file (version V1.15.3) and used check_msg() instead of wait_msg(). That resolved the issue. I was able to send the on and off messages (plain text, not JSON) and the led turned on and off. Hope that helps.

1 Like

Hi
Thank you
I tried check_msg() but it was not available at that time
I think they add it in the last update
I’ll try it !

wait_msg() may work with the latest update. I used check_msg() because I didn’t want my control loop to hang. Let me know if the new update solves your issue.