[Solved] Micropython and MQTT Libs

micropython
mqtt

#1

Looking for micropython implementation of the losant MQTT libs…
The current lib is not compatible and I am not having luck trying to use umqtt in its place…

thanks
Gavin


#2

Umm not sure I agree.

What platform are you using. In what way is it incompatible ? Are trying to use TLS ?

I have an ESP8266 which working with Losant at home not using TLS . (I prefer to use a local broker for this specific project so didn’t continue further with that path.)

At work I have pycoms (8) on mechanical pumps talking to Losant over LTE-Cat M1 direct. I did want to use TLS so am using the guts of the Pycom AWS library (to get the TLS support)


#3

I snagged an xbee cat1 cellular modem and have it on verizon’s plan for iot… the support micro python right on the device…


#4

Hi Gavin

You would need to post some detail as to what is not working, error etc. And as I asked are you trying to use TLS ?

Then I might be able to help

T


#5

Not using TLS… looks like the umqtt module is having trouble with the length of the password?

File "/flash/lib/umqtt/simple.py", line 97, in connect
IndexError: bytes index out of range

and I see it connect to the broker but it kicks back an index error


#6

so I went another way and found a trimmed down lib for losant for micropython

MP Lib

I am not a strong python programmer and am having troubles getting it to execute a connect the the broker… any direction would be great


#7

Hi

Can you post the simplest example of your code ?

T


#8

Hello Gavin,

I downloaded the library that you mentioned above: micropython and losant example. I was able to successfully connect to my device (pycom GPy) with the Losant Platform using SSL. The updates I made are shown below.

I downloaded the SSL certificate from the Losant Python MQTT library from here and put it in the /flash/cert/ directory that I created. This certificate is not needed but recommended. If you choose to not use it, then remove “ca_certs’:’/flash/cert/RootCA.crt’,” from the call to the MQTTClient constructor shown in the screenshot above.

Here are the values that were added to the call to the MQTTClient constructor in losant_mqtt.py:

  • 60 - this is the keep-alive value in seconds and set to keep the connection with the Losant MQTT broker open
  • True - specifies to use SSL
  • SSL parameters to properly connect to the Losant MQTT broker

The last change I made was setting the port to 0 as shown in the screenshot. With this value at 0 and True specified in the MQTTClient constructor above to use SSL, the umqtt-simple library will set the correct port number.

Hope this helps!
Kevin


#9

That helped so much but I had to change the import json to import ujson in the losant_mqtt lib to get it to work and

        payload = ujson.dumps({"data": state})
        self._mqtt_client.publish(self._state_topic(), payload)

I am using an exbee cellular module on verizon’s thingspace service… so YMMV… :stuck_out_tongue:

Thanks for the help…


#10

also here is the code that works on the module … I am sampling the temperature from the module as a test… trying to code directly on the module for sensors.

   import xbee
        import time
        import network
        import machine
        from losant_mqtt import Device

    # Construct device
    device = Device("id", "key", "secret")

    #def on_command(device, command):
    #    print("Command received.")
    #    print(command["name"])
     #   print(command["payload"])

    # Listen for commands.
    #device.add_event_observer("command", on_command)
    conn = network.Cellular()
    while not conn.isconnected():
    	print("waiting for network connection...")
    time.sleep(10)
    print("network connected")
     

    # Connect to Losant.
    device.connect()

    # Send temperature once every second.
    while True:
        if device.is_connected():

            temp = (xbee.atcmd('TP'))
            device.send_state({"temp": temp})
        time.sleep(1)
    device.close()

#11

Hi Gavin,

Great job getting this to work - I am glad you were able to figure it out! Also, thank you for posting your code in case someone else runs into the same issue!

-Kevin