MQTT - nodesign/weio GitHub Wiki

MQTT Support in WeIO

MQTT Client - Paho

Paho is an Eclipse project that implements MQTT client with various language bindings

Support for Paho in WeIO can be added via paho-mqtt Python package (https://pypi.python.org/pypi/paho-mqtt).

  • Install paho-mqtt
root@WEIO:~# pip install paho-mqtt
Downloading/unpacking paho-mqtt
  Downloading paho-mqtt-1.1.tar.gz (41kB): 41kB downloaded
  Running setup.py egg_info for package paho-mqtt
    
Installing collected packages: paho-mqtt
  Running setup.py install for paho-mqtt
    
Successfully installed paho-mqtt
Cleaning up...
root@WEIO:~# 
  • Use following program in WeIO IDE to test:
from weioLib.weio import *

import paho.mqtt.client as mqtt

def setup():
    attach.process(myProcess)
    
def myProcess():
    # The callback for when the client receives a CONNACK response from the server.
    def on_connect(client, userdata, flags, rc):
        print("Connected with result code "+str(rc))

        # Subscribing in on_connect() means that if we lose the connection and
        # reconnect then subscriptions will be renewed.
        client.subscribe("$SYS/#")

    # The callback for when a PUBLISH message is received from the server.
    def on_message(client, userdata, msg):
        print(msg.topic+" "+str(msg.payload))

    client = mqtt.Client()
    client.on_connect = on_connect
    client.on_message = on_message

    client.connect("iot.eclipse.org", 1883, 60)

    # Blocking call that processes network traffic, dispatches callbacks and
    # handles reconnecting.
    # Other loop*() functions are available that give a threaded interface and a
    # manual interface.
    client.loop_forever()

MQTT Broker - Mosquitto

  • Install mosquitto in OpenWrt:
root@WEIO:~# opkg install mosquitto mosquitto-client
Installing mosquitto (1.3.4-1) to root...
Downloading http://downloads.openwrt.org/barrier_breaker/14.07/ar71xx/generic/packages/packages/mosquitto_1.3.4-1_ar71xx.ipk.
Installing mosquitto-client (1.3.4-1) to root...
Downloading http://downloads.openwrt.org/barrier_breaker/14.07/ar71xx/generic/packages/packages/mosquitto-client_1.3.4-1_ar71xx.ipk.
Installing libcares (1.10.0-1) to root...
Downloading http://downloads.openwrt.org/barrier_breaker/14.07/ar71xx/generic/packages/packages/libcares_1.10.0-1_ar71xx.ipk.
Installing libmosquitto (1.3.4-1) to root...
Downloading http://downloads.openwrt.org/barrier_breaker/14.07/ar71xx/generic/packages/packages/libmosquitto_1.3.4-1_ar71xx.ipk.
Configuring libcares.
Configuring libmosquitto.
Configuring mosquitto-client.
Configuring mosquitto.
root@WEIO:~# 
  • Edit /etc/mosquitto/mosquitto.conf at line user:
# When run as root, drop privileges to this user and its primary
# group.
# Leave blank to stay as root, but this is not recommended.
# If run as a non-root user, this setting has no effect.
# Note that on Windows this has no effect and so mosquitto should
# be started by the user you wish it to run as.
#user mosquitto
user root
  • Start mosquitto broker on WeIO board:
root@WEIO:~# mosquitto -c /etc/mosquitto/mosquitto.conf 
1423430188: Warning: Mosquitto should not be run as root/administrator.
1423430188: mosquitto version 1.3.4 (build date 2014-09-23 08:44:39+0200) starting
1423430188: Config loaded from /etc/mosquitto/mosquitto.conf.
1423430188: Opening ipv6 listen socket on port 1883.
1423430188: Warning: Address family not supported by protocol
1423430188: Opening ipv4 listen socket on port 1883.
  • On your PC, start SUB:
drasko@Lenin:~$ mosquitto_sub -h weio.local -t "test"
  • On your PC, from another terminal (or some other PC), PUB something:
drasko@Lenin:~$ mosquitto_pub -h weio.local -t "test" -m "HELLO!"