MQTT cannot subscribe to adafruit
-
I have tried out the MQTT example with IO Adafruit platform following this example
https://www.hackster.io/bucknalla/mqtt-micropython-044e77
and the code I used is from Github.I managed to publish and I can see the changes of the button at Adafruit but I cannot subscribe. I want to control the led at lopy to switch on and off at Adafruit.
the coding for mqtt.py follow exactly same as the code in github, sometimes I get the error "subscribe callback is not set"
def subscribe(self, topic, qos=0): assert self.cb is not None, "Subscribe callback is not set" pkt = bytearray(b"\x82\0\0\0") self.pid += 1 struct.pack_into("!BH", pkt, 1, 2 + 2 + len(topic) + 1, self.pid) #print(hex(len(pkt)), hexlify(pkt, ":")) self.sock.write(pkt) self._send_str(topic) self.sock.write(qos.to_bytes(1, "little")) while 1: op = self.wait_msg() if op == 0x90: resp = self.sock.read(4) #print(resp) assert resp[1] == pkt[2] and resp[2] == pkt[3] if resp[3] == 0x80: raise MQTTException(resp[3]) returnmain.py coding from alex's corner
https://www.youtube.com/watch?v=3NDPSbr5J14&t=997sdef settimeout(duration): pass def sub_cb(topic, msg): print(msg) client = MQTTClient(client_id="xxx", server="io.adafruit.com",user="xxx", password="xxx", port=1883) client.settimeout=settimeout client.set_callback(sub_cb) client.connect() print("connected to broker\n") client.publish(topic="vickyho/feeds/lights",msg="False") print("Published!") time.sleep(10) client.subscribe(topic="vickyho/feeds/lights") print("Subscribed!") while True: client.check_msg() if client.subscribe(topic="vickyho/feeds/digital",msg="ON") pycom.rgbled(0xff00) print("Led ON") time.sleep(3) if client.subscribe(topic="vickyho/feeds/digital",msg="OFF") pycom.heartbeat(False) pycom.rgbled(0x0000) print("Led OFF") time.sleep(3)here is the error:
File "main.py", line 40 SyntaxError: invalid syntaxline 40 is at pycom.rgbled(0xff00)
Can anyone help? Im new to python language so I dont know how to code. Thanks!
-
-
@seb Hi sorry seb, because the upper part is only wifi configuration so i didnt show it.
Here is the whole main.py
from network import WLAN from mqtt import MQTTClient import machine import time import pycom #from machine import Pin #pin=Pin('P10',mode=Pin.IN, pull=Pin.PULL_UP) wlan = WLAN(mode=WLAN.STA) wlan.antenna(WLAN.EXT_ANT) wlan.connect("xxx", auth=(WLAN.WPA2, "xxx"), timeout=5000) while not wlan.isconnected(): machine.idle() print("Connected to Wifi\n") def settimeout(duration): pass def sub_cb(topic, msg): print(msg) client = MQTTClient(client_id="xxx", server="io.adafruit.com",user="vickyho", password="xxx", port=1883) client.settimeout=settimeout client.set_callback(sub_cb) client.connect() print("connected to broker\n") client.publish(topic="vickyho/feeds/lights",msg="False") print("Published!") time.sleep(10) client.subscribe(topic="vickyho/feeds/lights") print("Subscribed!") while True: client.check_msg() if client.subscribe(topic="vickyho/feeds/digital",msg="ON") pycom.rgbled(0xff00) print("Led ON") time.sleep(3) if client.subscribe(topic="vickyho/feeds/digital",msg="OFF") pycom.heartbeat(False) pycom.rgbled(0x0000) print("Led OFF") time.sleep(3)thanks for your reply btw!
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login