Internet Of Things Communications
  • Cover
  • Summary
  • About This Training
    • Objectives
    • Skills
    • Prerequisites
    • Requirements
  • Once Upon A Time ...
  • Introduction
    • Network Topologies
    • Software Defined Radio
    • Wireshark
    • Sub-1GHz Wireless
  • Connectivity
    • Serial
      • Laboratory
        • Linux and Arduino
    • Telephony
    • WiFi
      • WiFi HaLow
      • WiGig
    • Bluetooth
      • Architecture
        • Bluetooth Specifications
        • Bluetooth Low Energy
        • Bluetooth Smart Mesh
      • Protocol Stack
        • Linux
          • HCITool
          • BlueZ
          • L2Ping
          • SDPTool
          • GATTTool
          • BTMon
          • BTProxy
      • Development Boards
        • Intel Edison
        • Arduino
      • Laboratory
        • Serial Port Protocol
        • Linux and Arduino
        • Nordic
        • Virtual Keyboard
        • Reverse Engineering
        • Bluetooth Low Energy
    • Low-Power Wide-Area Networks
      • LoRa
      • SigFox
    • RFID
    • ZigBee
    • Z-Wave
    • Thread
    • HomeKit
    • Satellite
    • Near Field Communication
    • Neul
    • RF
  • Protocols
    • Web
    • Industrial Protocols
    • MQ Telemetry Transport
      • Mosca
      • Brokers
        • Mosquitto
        • VerneMQ
        • HiveMQ
      • Security
      • Applications
      • Laboratory
    • Advanced Message Queuing Protocol
    • Weave
    • Constrained Application Protocol
    • AllJoyn
    • XMPP
    • 6LoWPAN
    • ModBus
    • Others
    • Lightweight M2M
    • Cap'n Proto
    • IPv6
    • RPL
    • Very Simple Control Protocol
    • NATS
  • Libraries
    • ZeroMQ
  • Wrap-Up
    • Online Training
    • Challenge
  • SandBox
Powered by GitBook
On this page
  • Mosquitto
  • Mosquitto Intel® Edison Setup
  • Mosquitto Intel® Galileo Setup
  • Mosquitto Applications
  • Mosquitto Demo Temperature Gauge
  • Mosquitto MQTT Server/Broker
  • Project
  1. Protocols
  2. MQ Telemetry Transport
  3. Brokers

Mosquitto

PreviousBrokersNextVerneMQ

Last updated 7 years ago

Mosquitto

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.

Mosquitto Intel® Edison Setup

Mosquitto Intel® Galileo Setup

    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

Mosquitto Applications

    root@platform:~# mosquitto
    root@platform:~# mosquitto_sub
    root@platform:~# mosquitto_pub

Mosquitto Demo Temperature Gauge

    root@platform:~# mosquitto_pub -h test.mosquitto.org -t temp/random -m 23.0

Mosquitto MQTT Server/Broker

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

Project

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

Go to and execute

Mosquitto Homepage
Building and running Mosquitto MQTT on Intel Edison
Connecting Sensor Networks and Devices to the Cloud in just minutes: Solution Brief
http://test.mosquitto.org/gauge/
Paho Mqtt