element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Pi IoT
  • Challenges & Projects
  • Design Challenges
  • Pi IoT
  • More
  • Cancel
Pi IoT
Blog [Pi IoT] Smart Competition Home #3: Smarthome I - Connectivity setup: MQTT
  • Blog
  • Forum
  • Documents
  • Polls
  • Files
  • Events
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: clazarom
  • Date Created: 8 Jun 2016 8:48 PM Date Created
  • Views 668 views
  • Likes 5 likes
  • Comments 3 comments
  • mqtt
  • piiot
  • eclipse_paho
  • designchallenge
  • mosquitto
Related
Recommended

[Pi IoT] Smart Competition Home #3: Smarthome I - Connectivity setup: MQTT

clazarom
clazarom
8 Jun 2016

In this entry I discuss the implementation of  MQTT (Message Queue Telemetry Transport) as the connectivity protocol. The Message Queue Telemetry Transport protocol was born as a machine to machine connection protocol for devices where a light implementation is needed. It consumes little bandwidth and its software stack implementation does not require much memory space (perfect for our IoT applications!).image

 

MQTT deploys a publish/subscribe architecture, with a broker in the middle. To provide a multicast to multicast communication, information sources and destination are categorized as publisher and subscriber clients respectively. These clients connect to a central broker, which will manage the income information and distribute it accordingly.

The protocol behavior is orchestrated by the broker. Publishers will connect to the broker and send their data packages. Each of this packages will have a topic (the publisher sets this topic label to identify the type of information), used by the broker to organize them in queues. Subscriber clients can then subscribe to the topics of their interest. New packages arriving to the queue are received by the subscribed clients.

 

In order to work with MQTT, we will make use of two open source projects:

  • Mosquitto – to implement the broker
  • Paho – to implement the clients

 

Communication among broker and clients is achieved using the home WiFi network.

 

MQTT Elements

 

This MQTT architecture has three main elements. Their relationship is shown in the figure bellow:

 

image

 

 

 

 

Raspberry Pi 1 - Publisher

  • Sensor Node
  • Generates data (sensors reading)
  • Sends data to broker

 

 

Raspberry Pi 3 - Broker

  • Central node
  • Receives messages publishers
  • Organizes messages in topic queues

 

 

 

Samartphone - Subscriber

  • User's devices
  • Connected to broker
  • Displays sensors reading

 

 

          Figure 1. MQTT architecture

 

Next sections explain the choice of software and setup for broker and clients

 

image

1) Installation of Mosquitto Broker in the central node of our smart home (Raspberry Pi 3).

Initial setup: Raspberry Pi 3 – Raspbian 8.0 (jessie) / SSH connection enabled

 

A described in its project web “Eclipse MosquittoTm is an open source (EPL/EDL licensed) message broker that implements the MQTT protocol versions 3.1 and 3.1.1. MQTT provides a lightweight method of carrying out messaging using a publish/subscribe model. This makes it suitable for "Internet of Things" messaging such as with low power sensors or mobile devices such as phones, embedded computers or microcontrollers like the Arduino.”

 

I will install the broker in a Raspberry Pi 3, which is already running Raspbian OS. Afterwards, I will configure this broker and test that it is actually running.

 

Install

<OPTIONAL> To do so, you may first need to include the corresponding Debian repository for Mosquitto files, with the following commands:

From Jessie's version on, this step is not needed anymore (as Mosquitto is officially part of the package){thanks rhe123 for the explanation in your comment}. So, if you have an older version of your a apt-get install does not work directly you may want add the repository. Otherwise, next command lines can be skipped

sudo wget http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key
sudo apt-key add mosquitto-repo.gpg.key
cd /etc/apt/sources.list.d/ 
sudo wget http://repo.mosquitto.org/debian/mosquitto-jessie.list
sudo apt-get update

</OPTIONAL>

 

Then, I install Mosquitto broker. Additionally, we will also install the default mqtt clients included in the project:

sudo apt-get install mosquitto mosquitto-clients

 

Configure

Mosquitto allows for most of its parameters to be configured (such as the user name, maximum messages in queue, listener characteristics...). By default, however,  Mosquitto is started without any configuration file (using its default values). I will have to create one mosquitto.conf to be used as a parameter when the broker is started.  A mosquitto.conf can be created in the directory /etc/mosquitto/mosquitto.conf (*NOTE: the name of the file is irrelevant, as long as it is .conf type)

 

Broker can be started with the following command:

sudo /usr/sbin/mosquitto -c etc/mosquitto/mosquitto.conf

 

Mosquitto project has a very detailed explanations for this config file mosquitto-conf-5.html

I also attach a config file (can be found at /usr/share/doc/mosquitto/examples/mosquitto.conf.example). I found it very useful to start changing the parameters.

 

In this project, I modify the following parameters:

  • MQTT general characteristics:
    • user mosquitto
    • persistence true (save messages information)
    • Security:
      • clientid_prefixes secure- (only clients of the type secure-clientname are allowed to connect... better to use something more original than secure- for the prefix). At this first trial, no prefix is included but we implement it later in the project.
      • allow_anonymous false (clients connecting without user name are not allowed)
  • Listener parameters --> it defines the broker's behavior when clients are trying to connect
    • port 1883 (default)
    • max_connections -1 (no limits for the # of connections)
    • Certificate based SSL/TLS support --> include security with a Certificate Authority which generates a certificate for each client. Clients can be required to provide a valid certificate to connect with the broker. (I plan to have this feature enabled, but could not make it work in my first trials so.... no Certificate based SSL/TLS support as of now)

 

Test

Since mosquitto-clients wre also installed, I will use them to test our Mosquitto setup. I open three different terminals, to start the broker and create a subscriber client and a publisher client

1. Start the broker in terminal 1

2. Create an mqtt subscriber client. It will be listening for message with topic 'test/hello' in terminal 2

3. Create an mqtt publisher client. It will send a simple message "hello world!" (original, yeah!) with topic 'test/hello' in terminal 3

 

image

Figure 2. Start MQTT broker with our config file

          

image

Figure 3. Create a subscribe client. It is subscribed to the topic test/hello

image

Figure 4. Create a publisher client. Send the message "Hello world!"

 

(*) Mosquitto broker is automatically started. We stop it first, to then start a broker with the desired configuration

 

We got the message in our subscriber, hurray!!

 

Useful Mosquitto commands

 

STOP BROKER

sudo /etc/init.d/mosquitto stop

 

START BROKER

sudo /usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf

 

SUBSCRIBER CLIENT

mosquitto_sub -h <host> -p <port> -i <user> -d -t what/topic

 

PUBLISHER CLIENT

mosquitto_pub -h <host> -p <port> -i <user> -d -t what/topic -m "new message!"

 

 

MQTT broker (Mosquitto) successfully installed in Raspberry Pi 3

MQTT broker tested with local MQTT clients

 

 

 

2) Paho Clients (Raspberry Pi 1 / Smart phone & Raspberry Pi 3) image

Initial setup: Raspberry Pi 1 - Raspbian / Smartphone - Android  4.2.2/ Raspberry Pi 3 - Raspbian 8.0

 

Our clients, both Publisher and Subscriber, are created using Paho project. Paho provides client implementations of MQTT, in various programming languages. Main advantages we obtained from Paho are compatibility (available for different platforms and programming languages, such as Python and Java) and community support.

We are using Paho to implement three clients: one publisher (in our sensor node) and two subscribers (one in the broker and the other in the smartphone for the user).

 

MQTT Connectivity setup

 

We now have a running broker installed in the Raspberry Pi 3. We write down the values any MQTT client will need to connect to it:

  • Host: local IP address of Raspberry Pi 3
  • Port: 1883
  • Client prefixes (if configured): secure-

We can develop publisher and subscriber clients, connected to our WLAN/LAN.

Attachments:
config_example_mosquitto.txt.zip
  • Sign in to reply

Top Comments

  • clazarom
    clazarom over 9 years ago in reply to rhe123 +1
    Thanks for the correction , I included the changes in the post (also, keen eye with the Wheezy version repo). I should move the Pi's to the most recent version Stretch, but I will stay with Jesse during…
  • rhe123
    rhe123 over 9 years ago in reply to clazarom

    You're welcome. I wouldn't go fully over to Stretch yet, as it's still in testing. Personally I use Jessie and in some cases I take some packages from Stretch (when a newer version is required).

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • clazarom
    clazarom over 9 years ago in reply to rhe123

    Thanks for the correction image , I included the changes in the post (also, keen eye with the Wheezy version repo). I should move the Pi's to the most recent version Stretch, but I will stay with Jesse during the challenge (easy and a bit lazy ^^u) 

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • rhe123
    rhe123 over 9 years ago

    Nice post clazarom, I noticed you're adding the Mosquitto Apt repository. Since Jessie it's not really needed anymore, as it's now also properly in the official Debian repositories. Although that's not the most recent version (Stretch is pretty much up-to-date). Also noticed you used the Wheezy version of the repo instead of Jessie version. You might want to update that image

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube