Mosquitto is an open source (BSD licensed) message broker that implements the MQ Telemetry Transport protocol versions 3.1 and 3.1.1. MQTT provides a lightweight method of carrying out messaging using a publish/subscribe model.
MQTT (a.k.a. mosquitto) is perfect for mobile and embedded devices because of its lightweight (in processing, memory management and bandwidth) messaging protocol.
For this protocol, notice that it lacks of encryption in its base, otherwise it would add an important overhead to the connection. Security at the application level requires a lot of work.
- Building and running Mosquitto MQTT on Intel Edison
- Connecting Sensor Networks and Devices to the Cloud in just minutes: Solution Brief
root@platform:~# wget http://mosquitto.org/files/source/mosquitto-1.3.5.tar.gz
root@platform:~# tar xvf mosquitto-1.3.5.tar.gz
root@platform:~# cd mosquitto-1.3.5
root@platform:~# make -j3 WITH_SRV=no
root@platform:~# adduser mosquitto
root@platform:~# cd test/broker
root@platform:~# make -j3 test
root@platform:~# cd ../../
root@platform:~# cp client/mosquitto_pub /usr/bin
root@platform:~# cp client/mosquitto_sub /usr/bin
root@platform:~# cp lib/libmosquitto.so.1 /usr/lib
root@platform:~# cp src/mosquitto /usr/bin
root@platform:~# mosquitto
root@platform:~# mosquitto_sub
root@platform:~# mosquitto_pub
Go to http://test.mosquitto.org/gauge/ and execute
root@platform:~# mosquitto_pub -h test.mosquitto.org -t temp/random -m 23.0
As subscriber
root@platform:~# mosquitto_sub -h test.mosquitto.org -p 1883 -t workshop/communications
As publisher
root@platform:~# mosquitto_pub -h test.mosquitto.org -p 1883 -t workshop/communications -m "Hello Communications Learners!"
As subscriber
root@platform:~# mosquitto_sub -h test.mosquitto.org -p 1883 -t workshop/all
As publisher
root@platform:~# mosquitto_pub -h test.mosquitto.org -p 1883 -t workshop/all -m "Hello All Learners!"
See output for the following command
root@platform:~# mosquitto_sub -h test.mosquitto.org -t "#" -v
Eclipse Paho MQTT Python client library, which implements versions 3.1 and 3.1.1 of the MQTT protocol.
Publish system-wide network I/O statistics through MQTT Protocol using test.mosquitto.org server under "IoT101" topic
root@board:~# vi main.py
#!/usr/bin/python
import paho.mqtt.client as paho
import psutil
import signal
import sys
import time
from threading import Thread
def interruptHandler(signal, frame):
sys.exit(0)
def on_publish(mosq, obj, msg):
pass
def dataNetwork():
netdata = psutil.net_io_counters()
return netdata.packets_sent + netdata.packets_recv
def dataNetworkHandler():
idDevice = "ThisDevice"
mqttclient = paho.Client()
mqttclient.on_publish = on_publish
mqttclient.connect("test.mosquitto.org", 1883, 60)
while True:
packets = dataNetwork()
message = idDevice + " " + str(packets)
print "MQTT dataNetworkHandler " + message
mqttclient.publish("IoTComms/Mqtt", message)
time.sleep(1)
if __name__ == '__main__':
signal.signal(signal.SIGINT, interruptHandler)
threadx = Thread(target=dataNetworkHandler)
threadx.start()
while True:
print "Hello IoT Communications MQTT"
time.sleep(5)
# End of File
root@board:~# python main.py
Listen to those events with mosquitto_pub app or any mqtt cellphone application using a test.mosquitto.org server susbcribing to "IoTComms/#"
root@board:~# mosquitto_sub -h test.mosquitto.org -p 1883 -t IoTComms/#
Implement a threaded subscription function through MQTT Protocol using a test.mosquitto.org server under "IoTComms/Message" topic
root@board:~# vi main.py
#!/usr/bin/python
import paho.mqtt.client as paho
import psutil
import signal
import sys
import time
from threading import Thread
def interruptHandler(signal, frame):
sys.exit(0)
def on_publish(mosq, obj, msg):
pass
def dataNetwork():
netdata = psutil.net_io_counters()
return netdata.packets_sent + netdata.packets_recv
def dataNetworkHandler():
idDevice = "ThisDevice"
mqttclient = paho.Client()
mqttclient.on_publish = on_publish
mqttclient.connect("test.mosquitto.org", 1883, 60)
while True:
packets = dataNetwork()
message = idDevice + " " + str(packets)
print "dataNetworkHandler " + message
mqttclient.publish("IoTComms/Mqtt", message)
time.sleep(1)
def on_message(mosq, obj, msg):
print "MQTT dataMessageHandler %s %s" % (msg.topic, msg.payload)
def dataMessageHandler():
mqttclient = paho.Client()
mqttclient.on_message = on_message
mqttclient.connect("test.mosquitto.org", 1883, 60)
mqttclient.subscribe("IoTComms/Message", 0)
while mqttclient.loop() == 0:
pass
if __name__ == '__main__':
signal.signal(signal.SIGINT, interruptHandler)
threadx = Thread(target=dataNetworkHandler)
threadx.start()
threadx = Thread(target=dataMessageHandler)
threadx.start()
while True:
print "Hello IoT Communications MQTT"
time.sleep(5)
# End of File
root@board:~# python main.py
Confirm you are receiving those events publishing from another device
root@board:~# mosquitto_pub -h test.mosquitto.org -p 1883 -t IoTComms/Message -m Hi