Created
January 8, 2022 09:35
-
-
Save hest/e54b740a116a56fa98487df01df890cb to your computer and use it in GitHub Desktop.
MicroPython MQTT subscribe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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