(Skip to installation part at the bottom is you just want to install)
Building block of an Iot system (Network protocol)
There are many many protocols used in Iot systems but one of the most popular and free protocol is Iot protocol for communications is Mqtt. This is the protocol which I used in my projects.
In this post I will share:
- What is Mqtt?
- What is Mosquitto (Mqtt Broker)?
- How to install Mosquitto with websockets enabled on raspberry pi.
What is Mqtt?
MQTT (formerly MQ Telemetry Transport) is a publish-subscribe based "light weight" messaging protocol for use on top of the TCP/IP protocol. The publish-subscribe messaging pattern requires a message broker. In this post, the Mqtt broker I describe is Mosquitto.
What is Mosquitto?
(Taken from Mosquitto website) Eclipse Mosquitto™ 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.
Basically, it is a broker for Mqtt protocol. Mqtt requires a broker as it uses a server client architecture unlike data distribution service(DDS) which does not require a broker.
Mqtt Server client architecture
In the figure below you can see how an Iot system works using Mqtt protocol. Each client can be a sensor, computer, user interface, etc. In addition there can be multiple publishers and subscribers for each topic.
|
Mqtt server client architecture |
How to install Mosquitto on Raspberry Pi/Raspberry Pi 2
1. Download and unzip source tar file from http://mosquitto.org/download/. We need to build from source codes because we have to enable websockets ourselves.
- Right click mosquitto-1.4.9.tar.gz on the page and click copy link
- In raspberry pi console type:
- cd
- mkdir mosquitto
- cd mosquitto
- wget http://mosquitto.org/files/source/mosquitto-1.4.9.tar.gz
- tar -xvzf mosquitto-1.4.9.tar.gz
2. Install some mosquitto dependencies before building mosquitto
- In raspberry pi console type:
- sudo apt-get install libssl-dev
- sudo apt-get install libc-ares-dev
- sudo apt-get install cmake
- sudo apt-get install uuid-dev
- sudo apt-get install daemon
- sudo apt-get install libwebsockets-dev
3. Enable websocket
- In raspberry pi console type:
- cd
- cd mosquitto
- cd mosquitto-1.4.9 (whichever folder you unzipped to in (1))
- edit config.mk changing the line:
- WITH_WEBSOCKETS:=no to WITH_WEBSOCKETS:=yes
- save
4. Build and install mosquitto
- In raspberry pi console type:
- cd
- cd mosquitto
- cd mosquitto-1.4.9 (whichever folder you unzipped to in (1))
- sudo make clean
- sudo make
- sudo make install
- Note: If you want to uninstall type sudo make uninstall
5. Now you have installed mosquitto but have not configured or started it. To configure:
- In raspberry pi console type:
- cd /etc/mosquitto
- cp mosquitto.conf.example mosquitto.conf
- Edit mosquitto.conf to include the lines
- listener 1883
- listener 9001 127.0.0.1
- protocol websockets
- Here we set port 1883 to use tcp protocol and port 9001 to use websockets
- save
6. Add mosquitto user to rpi
- In raspberry pi console type:
- Follow the instructions to add a user
7. Now you are done installing and configuring. We want to create make mosquitto as a service so that it starts on boot up and is controllable through systemctl.
- Modify the lines in /etc/mosquitto/mosquitto.conf
- #pid_file to pid_file /var/run/mosquitto.pid
- #user mosquitto to user mosquitto
- Create a .service file /etc/systemd/system/mosquitto.service with this content
[Unit]
Description=Mosquitto MQTT Broker daemon
ConditionPathExists=/etc/mosquitto/mosquitto.conf
Requires=network.target
[Service]
Type=simple
ExecStartPre=/bin/rm -f /var/run/mosquitto.pid
ExecStart=/usr/local/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf -d
ExecReload=/bin/kill -HUP $MAINPID
PIDFile=/run/mosquitto.pid
Restart=on-failure
[Install]
WantedBy=multi-user.target
- To start mosquitto on boot up
- sudo systemctl enable mosquitto or mosquitto.service
- To disable auto startup
- sudo systemctl disable mosquitto or mosquitto.service
- Now you can do
- sudo systemctl start mosquitto
- sudo systemctl status mosquitto
- sudo systemctl stop mosquitto
8. To check whether mosquitto is running
- type ps -ef | grep mosq && netstat -tln | grep 1883