Skip to content

Instantly share code, notes, and snippets.

@hest
Created January 8, 2022 09:35
Show Gist options
  • Save hest/e54b740a116a56fa98487df01df890cb to your computer and use it in GitHub Desktop.
Save hest/e54b740a116a56fa98487df01df890cb to your computer and use it in GitHub Desktop.
MicroPython MQTT subscribe
try:
from umqtt.robust import MQTTClient
except ImportError:
import upip
upip.install(['umqtt.robust', 'umqtt.simple'])
from umqtt.robust import MQTTClient
SERVER = '127.0.0.1'
USER = 'mqtt-test'
PASSWORD = 'password!'
DEBUG = True
def sub_cb(topic, msg):
print((topic, msg))
def main():
c = MQTTClient('client1', SERVER, user=USER, password=PASSWORD)
c.DEBUG = DEBUG
c.set_callback(sub_cb)
if not c.connect(clean_session=False):
c.subscribe(b'test_topic')
while True:
c.wait_msg()
# c.disconnect()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment