[Solved] Micropython and MQTT Libs

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

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)

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

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

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

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

Hi

Can you post the simplest example of your code ?

T

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

1 Like

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…

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()

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

2 Likes

I just went back and made the ssl changes and am now getting this…

Running bytecode…
Raw pin reading: 1171
Temperature: 21 Celsius
Temperature: 70 Fahrenheit
Waiting For Network Connection…
Network Connected
Connecting to Losant as 5c30fba6eaf910000868d41d
Traceback (most recent call last):
File “”, line 30, in
File “/flash/lib/losant_mqtt.py”, line 49, in connect
File “/flash/lib/umqtt/simple.py”, line 61, in connect
TypeError: must use IPPROTO_SEC

Any ideas?