You can subscribe to topics and go on to wait for mqtt messages whilst publishing messages when desired but perhaps I did not interpret your post correctly.I got it to work!!! I first made a function to publish a request for data. In the function, I used the connect, publish, and disconnect statements. I called the publish function, then I set up the information to subscribe In the main code. I had to do it in that order. I gave up on using the 2nd core. I was able to get the publish to run in the other core, but every time I need to rerun the program, I had to restart Thonny and power cycle the Pico W. I did put a _thread.exit() statement at the end of the 2nd core thread.
I usually use the excellent mqtt client from Peter Hinch - https://github.com/peterhinch/micropython-mqtt - but I had just had a go with mqtt.simple.py to have a pico on battery power to power up (using a DS3231 rtc) take some temperature reading, send a quick and simple mqtt message and then to power off to await the next awakening
I had not used simple.py to receive messages before as I prefer to use the async mqtt client so I thought I would have a quick go at publishing and subscribing with simple.py just in case I would need it for future use. The program connects to wifi, connects to the mqtt broker (or server), sets up the callbacks and subscribes to a couple of topics. It then awaits for messages to be received whilst periodically publishing an mqtt message. Of course to check the receipt of mqtt messages you will need another mqtt client that can publish messages that the program will pick up.
So my effort is just a quick and very simple effort at using simple.py and put aside in my snippets folder but I put it here in case it helps. If running it for your test then you would need to adjust for wifi connection and mqtt server of course.
Code:
import timefrom mqtt.simple import MQTTClientimport picow_wifi_connect as wcon# globalsbroker = '10.0.1.141' #change as requiredlast_pub = time.time()# Received messages from subscriptions will be delivered to this callbackdef mqtt_msgs(topic, msg): global last_msg_check print((topic, msg)) if topic == 'tests/test1': func1(msg) if topic == 'tests/test2': func2(msg) def func1(payload): print('we have a msg for test/test1 - msg is',payload)def func2(payload): print('we have a msg for test/test2 - msg is',payload) def doing_something(howlong): time.sleep(howlong)def main(): global last_pub # connect to wifi wcon.wlan_connect() # connect to mqtt broker and subscribe c = MQTTClient("mqtt_client", broker) c.set_callback(mqtt_msgs) c.connect() c.subscribe('tests/test1') c.subscribe('tests/test2') # program loop to publish and check for mqtt messages received try: while True: # periodicly publish a mqtt message time_now = time.time() pub_time_diff = time_now - last_pub if pub_time_diff > 10: c.publish('test/Ipublish', 'hello from test_simple') last_pub = time_now # Non-blocking wait for message c.check_msg() # dummy function to block for specified seconds to simulate other stuff # going on in the program doing_something(2) finally: c.disconnect() if __name__ == "__main__": main()Statistics: Posted by SirFico — Wed Apr 23, 2025 12:42 am