<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[MQTT cannot subscribe to adafruit]]></title><description><![CDATA[<p dir="auto">I have tried out the MQTT example with IO Adafruit platform following this example<br />
<a href="https://www.hackster.io/bucknalla/mqtt-micropython-044e77" target="_blank" rel="noopener noreferrer nofollow">https://www.hackster.io/bucknalla/mqtt-micropython-044e77</a><br />
and the code I used is from Github.</p>
<p dir="auto">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.</p>
<p dir="auto">the coding for <a href="http://mqtt.py" target="_blank" rel="noopener noreferrer nofollow">mqtt.py</a> follow exactly same as the code in github, sometimes I get the error &quot;subscribe callback is not set&quot;</p>
<p dir="auto"><a href="http://mqtt.py" target="_blank" rel="noopener noreferrer nofollow">mqtt.py</a></p>
<pre><code>def subscribe(self, topic, qos=0):
        assert self.cb is not None, &quot;Subscribe callback is not set&quot;
        pkt = bytearray(b&quot;\x82\0\0\0&quot;)
        self.pid += 1
        struct.pack_into(&quot;!BH&quot;, pkt, 1, 2 + 2 + len(topic) + 1, self.pid)
        #print(hex(len(pkt)), hexlify(pkt, &quot;:&quot;))
        self.sock.write(pkt)
        self._send_str(topic)
        self.sock.write(qos.to_bytes(1, &quot;little&quot;))
        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])
                return
</code></pre>
<p dir="auto"><a href="http://main.py" target="_blank" rel="noopener noreferrer nofollow">main.py</a> coding from alex's corner<br />
<a href="https://www.youtube.com/watch?v=3NDPSbr5J14&amp;t=997s" target="_blank" rel="noopener noreferrer nofollow">https://www.youtube.com/watch?v=3NDPSbr5J14&amp;t=997s</a></p>
<pre><code>def settimeout(duration): 
   pass

def sub_cb(topic, msg): 
   print(msg)

client = MQTTClient(client_id=&quot;xxx&quot;, server=&quot;io.adafruit.com&quot;,user=&quot;xxx&quot;,
password=&quot;xxx&quot;, port=1883) 
client.settimeout=settimeout
client.set_callback(sub_cb)

client.connect()
print(&quot;connected to broker\n&quot;)
client.publish(topic=&quot;vickyho/feeds/lights&quot;,msg=&quot;False&quot;)
print(&quot;Published!&quot;)
time.sleep(10)
client.subscribe(topic=&quot;vickyho/feeds/lights&quot;)
print(&quot;Subscribed!&quot;)

while True:
     client.check_msg()
     if client.subscribe(topic=&quot;vickyho/feeds/digital&quot;,msg=&quot;ON&quot;)  
          pycom.rgbled(0xff00)
          print(&quot;Led ON&quot;)
          time.sleep(3)

     if client.subscribe(topic=&quot;vickyho/feeds/digital&quot;,msg=&quot;OFF&quot;)
          pycom.heartbeat(False)  
          pycom.rgbled(0x0000)
          print(&quot;Led OFF&quot;)
          time.sleep(3)

</code></pre>
<p dir="auto">here is the error:</p>
<pre><code>  File &quot;main.py&quot;, line 40
SyntaxError: invalid syntax
</code></pre>
<p dir="auto">line 40 is at pycom.rgbled(0xff00)</p>
<p dir="auto">Can anyone help? Im new to python language so I dont know how to code. Thanks!</p>
]]></description><link>https://forum.pycom.io/topic/2993/mqtt-cannot-subscribe-to-adafruit</link><generator>RSS for Node</generator><lastBuildDate>Tue, 14 Apr 2026 15:51:10 GMT</lastBuildDate><atom:link href="https://forum.pycom.io/topic/2993.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 08 Apr 2018 04:19:16 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to MQTT cannot subscribe to adafruit on Sun, 08 Apr 2018 04:21:33 GMT]]></title><description><![CDATA[<p dir="auto">I have tried out the MQTT example with IO Adafruit platform following this example<br />
<a href="https://www.hackster.io/bucknalla/mqtt-micropython-044e77" target="_blank" rel="noopener noreferrer nofollow">https://www.hackster.io/bucknalla/mqtt-micropython-044e77</a><br />
and the code I used is from Github.</p>
<p dir="auto">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.</p>
<p dir="auto">the coding for <a href="http://mqtt.py" target="_blank" rel="noopener noreferrer nofollow">mqtt.py</a> follow exactly same as the code in github, sometimes I get the error &quot;subscribe callback is not set&quot;</p>
<p dir="auto"><a href="http://mqtt.py" target="_blank" rel="noopener noreferrer nofollow">mqtt.py</a></p>
<pre><code>def subscribe(self, topic, qos=0):
        assert self.cb is not None, &quot;Subscribe callback is not set&quot;
        pkt = bytearray(b&quot;\x82\0\0\0&quot;)
        self.pid += 1
        struct.pack_into(&quot;!BH&quot;, pkt, 1, 2 + 2 + len(topic) + 1, self.pid)
        #print(hex(len(pkt)), hexlify(pkt, &quot;:&quot;))
        self.sock.write(pkt)
        self._send_str(topic)
        self.sock.write(qos.to_bytes(1, &quot;little&quot;))
        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])
                return
</code></pre>
<p dir="auto"><a href="http://main.py" target="_blank" rel="noopener noreferrer nofollow">main.py</a> coding from alex's corner<br />
<a href="https://www.youtube.com/watch?v=3NDPSbr5J14&amp;t=997s" target="_blank" rel="noopener noreferrer nofollow">https://www.youtube.com/watch?v=3NDPSbr5J14&amp;t=997s</a></p>
<pre><code>def settimeout(duration): 
   pass

def sub_cb(topic, msg): 
   print(msg)

client = MQTTClient(client_id=&quot;xxx&quot;, server=&quot;io.adafruit.com&quot;,user=&quot;xxx&quot;,
password=&quot;xxx&quot;, port=1883) 
client.settimeout=settimeout
client.set_callback(sub_cb)

client.connect()
print(&quot;connected to broker\n&quot;)
client.publish(topic=&quot;vickyho/feeds/lights&quot;,msg=&quot;False&quot;)
print(&quot;Published!&quot;)
time.sleep(10)
client.subscribe(topic=&quot;vickyho/feeds/lights&quot;)
print(&quot;Subscribed!&quot;)

while True:
     client.check_msg()
     if client.subscribe(topic=&quot;vickyho/feeds/digital&quot;,msg=&quot;ON&quot;)  
          pycom.rgbled(0xff00)
          print(&quot;Led ON&quot;)
          time.sleep(3)

     if client.subscribe(topic=&quot;vickyho/feeds/digital&quot;,msg=&quot;OFF&quot;)
          pycom.heartbeat(False)  
          pycom.rgbled(0x0000)
          print(&quot;Led OFF&quot;)
          time.sleep(3)

</code></pre>
<p dir="auto">here is the error:</p>
<pre><code>  File &quot;main.py&quot;, line 40
SyntaxError: invalid syntax
</code></pre>
<p dir="auto">line 40 is at pycom.rgbled(0xff00)</p>
<p dir="auto">Can anyone help? Im new to python language so I dont know how to code. Thanks!</p>
]]></description><link>https://forum.pycom.io/post/18157</link><guid isPermaLink="true">https://forum.pycom.io/post/18157</guid><dc:creator><![CDATA[Vicky_]]></dc:creator><pubDate>Sun, 08 Apr 2018 04:21:33 GMT</pubDate></item><item><title><![CDATA[Reply to MQTT cannot subscribe to adafruit on Mon, 09 Apr 2018 09:17:28 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://forum.pycom.io/uid/1837">@vicky_</a></p>
<p dir="auto">The <a href="http://main.py" target="_blank" rel="noopener noreferrer nofollow">main.py</a> code you posted above is only 31 lines line</p>
]]></description><link>https://forum.pycom.io/post/18193</link><guid isPermaLink="true">https://forum.pycom.io/post/18193</guid><dc:creator><![CDATA[seb]]></dc:creator><pubDate>Mon, 09 Apr 2018 09:17:28 GMT</pubDate></item><item><title><![CDATA[Reply to MQTT cannot subscribe to adafruit on Mon, 09 Apr 2018 11:45:42 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://forum.pycom.io/uid/1881">@seb</a> Hi sorry seb, because the upper part is only wifi configuration so i didnt show it.</p>
<p dir="auto">Here is the whole <a href="http://main.py" target="_blank" rel="noopener noreferrer nofollow">main.py</a></p>
<pre><code>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(&quot;xxx&quot;, auth=(WLAN.WPA2, &quot;xxx&quot;), timeout=5000)

while not wlan.isconnected(): 
     machine.idle()
print(&quot;Connected to Wifi\n&quot;)

def settimeout(duration): 
   pass

def sub_cb(topic, msg): 
   print(msg)

client = MQTTClient(client_id=&quot;xxx&quot;, server=&quot;io.adafruit.com&quot;,user=&quot;vickyho&quot;,
                    password=&quot;xxx&quot;, port=1883) 
client.settimeout=settimeout
client.set_callback(sub_cb)

client.connect()
print(&quot;connected to broker\n&quot;)
client.publish(topic=&quot;vickyho/feeds/lights&quot;,msg=&quot;False&quot;)
print(&quot;Published!&quot;)
time.sleep(10)
client.subscribe(topic=&quot;vickyho/feeds/lights&quot;)
print(&quot;Subscribed!&quot;)

while True:
     client.check_msg()
     if client.subscribe(topic=&quot;vickyho/feeds/digital&quot;,msg=&quot;ON&quot;)  
          pycom.rgbled(0xff00)
          print(&quot;Led ON&quot;)
          time.sleep(3)

     if client.subscribe(topic=&quot;vickyho/feeds/digital&quot;,msg=&quot;OFF&quot;)
          pycom.heartbeat(False)  
          pycom.rgbled(0x0000)
          print(&quot;Led OFF&quot;)
          time.sleep(3)
</code></pre>
<p dir="auto">thanks for your reply btw!</p>
]]></description><link>https://forum.pycom.io/post/18213</link><guid isPermaLink="true">https://forum.pycom.io/post/18213</guid><dc:creator><![CDATA[Vicky_]]></dc:creator><pubDate>Mon, 09 Apr 2018 11:45:42 GMT</pubDate></item></channel></rss>