Internet of things (IoT) devices are everywhere these days and are already embedded in our daily life. Anything from Alexa to Google home, you can pick up accompanying smart devices that you can control from these platform. The cost of all of these can rank up. If you are a DIYer, then you are in luck! You can easy build such smart systems with Raspberry Pi and ESP8266. In this post, we are going to show you how to put together a simple MQTT IoT device with Raspberry Pi and ESP8266.
What is MQTT?
MQTT is a connectivity protocol specially designed for machine-to-machine or Internet of Things. It was designed as an extremely lightweight messaging transport. By being lightweight, it makes it useful for connections with remotely located IoT devices where network bandwidth might be at a premium and/or where a small code footprint is required. Almost all IoT platforms support MQTT protocol and there are several implementations for different IoT boards like Arduino, Raspberry and so on.
IoT devices that send data are called publishers and the ones that receives the data are called subscribers. IoT devices can however be both, subscribers and publishers. Relaying the information from a publisher to a subscriber is the job of the MQTT broker. In simple IT terms, the broker is server. The following image illustrates a basic MQTT system.
Messages are published on topics. There is no need to configure a topic, publishing on it is enough. For more detailed information, please visit the Mosquitto Website.
Using a Raspberry Pi as a MQTT Broker
Raspberry Pis are perfect as brokers due to their reasonable performance, connectivity and price, among others. Mosquitto is a lightweight and open source message broker that implements the MQTT protocol. Mosquitto is also suitable for use on all devices from low power single board computers to full servers.
Install Mosquitto on Raspberry Pi running Raspbian Stretch
First of all, install Raspbian on your SD card and then as usual, boot your Pi and so on.
Once the Pi has booted up, open up a terminal window on the Pi or remotely through SSH.
Enter the following commands:
sudo apt-key add mosquitto-repo.gpg.key
sudo wget http://repo.mosquitto.org/debian/mosquitto-stretch.list
sudo apt-get update
sudo dpkg -i libssl1.0.0_1.0.1t-1+deb8u7_armhf.deb
sudo dpkg -i libwebsockets3_1.2.2-1_armhf.deb
You can also install subscribing and publishing capabilities on the broker.
sudo apt-get install mosquitto mosquitto-clients
Once installed, start Mosquitto by typing the following in the terminal:
To test the broker, open a second terminal and subscribe to a topic:
mosquitto_sub -v -t "BTS/topic"
On your original terminal:
mosquitto_pub -t "test/topic" -m "Hello World!"
You should see “Hello World!” in the second terminal.
We used a Raspberry Pi 3.
MQTT ESP8266 IoT publisher
Now we have a broker, let’s start sending; or in MQTT language, let’s publish some data!
In this case, we will be using an ESP8266 as the publisher and will send DS18b20 temperature data to the broker.
First of all, download the PubSub Arduino library either from the Github or in the Arduino library manager.
Connect the DS18B20 sensor as shown in our previous post.
And finally, download the .ino Arduino code from our GitHub; edit the SSID, Raspberry Pi Broker address according to yours and then upload it to your ESP8266.
Testing it all
Once the code is uploaded, the ESP8266 will start sending temperature to the broker (Raspberry Pi).
Download a MQTT client from the App Store/Play Store onto your phone.
In the MQTT client on your phone, subscribe to the topic “BTS” with the server address being of your Raspberry Pi. You should start seeing temperature values in Degrees Celsius on your phone every couple of seconds.
To get this to work outside your home network, you will need to have a DNS or fixed external IP and forward that to your Raspberry Pi.
If you have any questions or comments, send us an email at firstname.lastname@example.org