Configuring end-to-end IoT environment on Raspberry Pi
by Ponlakshmi
Over the past few years, MQTT has
begun to establish itself as the most commonly
used messaging protocol for IoT projects. This document provides developers with a complete
guide on how to set up and run an
MQTT Broker on Raspberry
Pi in just a few minutes. You will also see how easily you can add intelligence to your Edge
device using ESP8266. Once you have gained the knowledge of the
MQTT Broker and how to use
it, you will be able to make
your own IoT projects.
Overview:
The steps involved in setting up a MQTT Broker on Raspberry Pi are
Installing MQTT Broker on Raspberry Pi
Configuration of MQTTBroker
Setting up the ESP8266
Flashing ESP8266
Connecting ESP8266 with MQTT Server
MQTT broker User Interface
MQTT
MQTT is a lightweight messaging
protocol that allows you to send arbitrary messages across a
network to any interested device. It uses the publish/subscribe method to exchange data
among clients and the server.
Installing MQTT Broker on Raspberry Pi
We are going to use Bevywise MQTT
Broker
and the Raspberry Pi 3 running the latest version of Raspbian Jessie
in this tutorial.
To install the Bevywise MQTT Broker and its client packages follow the below steps
You can download the broker from
In the download form, provide all your required details, choose the Raspberry OS, and
download the MQTT Broker.
The downloaded file will be in a zip format.
Unzip the downloaded zip folder using the command
unzip Bevywise_MQTT_Route_Raspbian.zip
Once you unzip the file, the Bevywise folder will be created.
Configuration of MQTTBroker on Raspberry Pi
To configure the broker go the Downloads -> Bevywise -> MQTTRoute -> Conf folder.
In the conf folder, there will be two conf files.
broker.conf
data_store.conf
broker.conf
To configure the MQTT broker, use the
broker.conf
file inside the conf folder of the Bevywise package.
By default, the broker will run in non-TLS mode with the
port number 1883,
and the authentication is disabled.
MQTT broker provides an option to enable encrypted data transfer and authentication.
To run your product with SSL/TLS support, change TLS_ENABLED =
TRUE and
change the port number to 8883.
You can also connect your edge devices more securely with MQTT
authentication.
To enable the authentication change AUTHENTICATION_ENABLED =
YES.
By enabling this, you need to provide an MQTT username and
password when the edge device is connected.
The username and the password are available in the Broker User interface.
Once you run the broker, the user interface will appear.
In the User Interface below the “Authentication” tab, there
will be a username and password.
You can use that username and password for a secure device connection.
data_store.conf
SQLite is the default data storage in MQTT broker.
For storing a large amount of data, you can change the storage configuration to
MySQL or MSSQL.
For powerful visualization and analysis of data, you can store the data in Elastic Search or any other
Big Data engine using a custom implementation.
These options configured using the
data_store.conf
file inside the conf folder.
To use MySQL as your data storage change, DB_SERVER= MySQL.
And provide the required username and password for MySQL.
Now the data will be stored inside the data folder in the name of the file specified for
MYSQL_DB.
After the broker configuration, now you can start the MQTT Broker.
To run the MQTTBroker as service, use the Monit version 5.25.
The procedure to set up the monit:
Download the Monit for Raspberry Pi 3, based on your servers.
Extract the archive using tar -xzf < downloaded file>.
Go to monit-5.25.2/bin.
Copy monit to /usr/bin folder by using the command
sudo cp monit /usr/bin
Go to monit-5.25.2/conf.
Using the below command copy the monitrc file to “/etc/”
sudo cp monitrc /etc/.
Monit configuration:
Modify the monit conf file.
Monit comes with its web server running on port 2812.
To configure the web interface, uncomment the section that begins with set httpd port
2812
in /etc/monitrc file.
Then use
sudo vi /etc/monitrc.
And, change the username and password as per your need.
Add Monitoring services:
At the end of the /etc/monitrc file , add the following.
Check process MQTT broker, with pidfile Bevywise/MQTTRoute/Broker.pid.
start program = “Bevywise/MQTTRoute/bin/runbroker.sh” with timeout 2 seconds.
stop program = “Bevywise/MQTTRoute/bin/stopbroker.sh” with timeout 2 seconds.
To reload the configuration changes, run
sudo monit reload
To add the MQTT broker process to monitoring, use
sudo monit start MQTTRoute
Restart the Raspberry Pi.
Now, the broker will start running in the background.
Setting up the ESP8266
The below steps explains how to flash a client library in ESP8266 by using the Arduino IDE.
Step 1: Connect your PC with internet
Connect your PC to the internet in which you are going to flash the ESP8266.
You can connect it using an Ethernet cable or WiFi.
Step 2: Install the Latest Arduino IDE
To install the latest Arduino IDE, navigate to the Arduino the page using the following
link https://www.arduino.cc
Select your operating system and download the latest software.
Step 3: Installing the ESP8266 Board
Once you install the Arduino software, open it.
Navigate to File -> Preferences -> Additional Boards Manager URLs.
In this text box, copy and paste the following link
http://arduino.esp8266.com/stable/package_esp8266com_index.json
Click “ok” to save your changes.
Now open the Board Manager.
Go to Tools -> Board -> Board Manager.
Select the esp8266 by ESP8266 Community package and install
it.
Then choose your ESP8266 board from Tools -> Board -> Generic ESP8266 Module
(ESP8266-01).
Tools -> Board -> NodeMCU 1.0 ESP-12 E Module (ESP8266-12 E & Development Board)
Finally, re-open your Arduino IDE.
Step 4: Install the Device Driver Software – CDM21226
Download driver software for FTDI USB
to TTL Serial Adapter Module from the zip file.
Now unzip downloaded folder.
Run the CDM21226_setup.exe file and install it.
Now, Connect the ESP8266 to your PC’s USB.
Step 5: Download and Install the needed Libraries
Before programming the ESP8266, you need to install the libraries in the Arduino library
manager.
There are quite a several Arduino-compatible MQTT clients available now.
Note : Are you the technology person looking to automate your house on a Raspbery pi box?
Here is your
FOREVER
FREE MQTT Broker with all the widgets and dashboard
needed to monitor and control your MQTT devices.
Cookie Consent
We utilize Cookies to enhance your experience. Your acceptance implies our policy agreement. Your data is
securely handled, and you can learn more about our commitment to data safety in our comprehensive Privacy Policy.