MQTT Publish Subscribe

From Lantronix Developer Wiki
Revision as of 04:48, 11 March 2015 by Ltrxmg42 (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search



The sample application demonstrates how two PWXC HSPA+ devices connected to a cloud service can send/receive messages between each other.

With “paho-mqtt” python packaged installed, HSPA+ can easily connect to a cloud service. The MQTT protocol is a machine-to-machine (M2M) connectivity protocol. With the lightweight publish/subscribe messaging transport, it is very useful for connections when the devices are in remote locations. In this demo, one of the devices has a switch connected to its digital input with DC power while the other one has an LED connected to its relay. By turning the switch on the device to on or off, we can alter the state of LED of the other device.

Mqtt publish subscribe overview.png


  1. Install "paho-mqtt" package according to instructions outlined in the Programmer's Guide
  2. Create a free account at
  3. Upload the scripts shown below to the device using the procedures outlined in the Programmer's Guide
  4. Two PremierWave XC HSPA+ devices as shown in the image above
  5. 7.10 firmware or later with Python

Demo Procedure

  1. Start the MQTT scripts after the device boots up
  2. Logon to the cloud website and monitor the traffic
  3. Turn the switch on the first device to "on" or "off" position
  4. The LED on the second device should turn "on" or "off"

Sample Code

Please note that this sample code uses the Lantronix APIs for hardware access that are present in firmware versions 7.10 and above. To import the APIs, add this to your Python script:

from ltrxlib import *

Device #1

This code snippet polls the state of the digital input and publishes a message whenever the state changes.
The code invokes the methods in the Lantronix Device Abstraction class (LtrxDsal) available inside the "ltrxlib" module.

### device 1
# polling the state of the digital input
# publish a corresponding message when the state is changed.
 def poll_di_status(c):
    print 'Digital Input 1'
    dsal = LtrxDsal()
    global static_state
    static_state  = dsal.readDigitalInput(1)
    while True:
            state = dsal.readDigitalInput(1)
            if state != static_state:
                print 'Digital Input state changed from ' + static_state + ' to ' + state
                if state == 'high':
                    c.publish(PUB_TOPIC, 'RELAY:on', QOS)
                elif state == 'low':
                    c.publish(PUB_TOPIC, 'RELAY:off', QOS)
            static_state = state
        except LtrxDsalError, e:
            print e.errmsg

Device #2

This code snippet parses the received message from the MQTT subscription and set the relay state based on the command.
The code invokes the methods in the Lantronix Device Abstraction class (LtrxDsal) available inside the "ltrxlib" module.

### device 2
# Parse the messages received from device 1.
# Set the relay on or off based on the command.
def on_message(client, userdata, message):
    print("Received message on topic " + str(message.topic) + " (QOS " + str(message.qos) + "): " + str(message.payload))
    msglist = message.payload.split(':')
    if len(msglist) > 1:
        if msglist[0] == 'RELAY':
                dsal = LtrxDsal()
                if msglist[1] == 'on':
                    dsal.setRelay(1, True)
                    print 'Relay 1 is turned on...'
                elif msglist[1] == 'off':
                    dsal.setRelay(1, False)
                    print 'Relay 1 is turned off...'
                print 'Failed to turn on relay 1...'
Personal tools