Issue publishing via mqtt



  • I seem to narrow down the issues I have with the mqtt lib that I found here;

    https://github.com/pycom/pycom-libraries/blob/master/examples/mqtt/mqtt.py

    I am now able to connect and also publish messages, currently to adafruit io every two seconds. The problem with the subscription remains, I would hope someone has a clue.

    My current subscribe method looks liek this

        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, sys.byteorder))
            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
    

    I had to import sys and change the line

    self.sock.write(qos.to_bytes(1))
    

    to

    self.sock.write(qos.to_bytes(1, sys.byteorder))
    

    I otherwise cannot call the qos.to_bytes method as it expects the byteorder on the micropython lopy that I have. Unfortunately I cannot see messages that I send to the topic - I've verified that messages are sent using another mqtt client that subscribed.

    Can anyone help? What is the correct code for subscribing?

    thx
    Sven


 

Hello World?

Pylife on Kickstarter - November 2018








Back Us On Kickstarter >

Pycom on Twitter