How to connect Raspberry Pi to Mainflux
Setting up MQTT on headless Raspberry Pi and connecting it to Mainflux platform on DigitalOcean droplet
In this tutorial we will:
- Install Raspbian Strech Lite on headless Raspberry Pi 3 Model B
- Set up WiFi on Raspberry Pi
- Install Mosquitto (MQTT Server) on Raspberry Pi
- Set up Mainflux (IoT Platform) on Digital Ocean droplet
- Connect Raspberry Pi with Mainflux
To follow this tutorial you will need computer (I’m doing it with Windows 7 laptop), Raspberry Pi (I have RPi 3 Model B), microSD card and WiFi connection
Installing Raspbian
If this is the first time you’re using Raspberry Pi, we must install operating system on it. Since we will use headless (without monitor) Raspberry Pi, minimal-lite version of Raspbian will be sufficient. Download Raspbian Strech Lite and download Etcher. Put microSD card in your PC and run Etcher.
Choose Raspbian image (you don’t need to unpack it), choose drive where is your microSD card and click Flash!
Connecting Raspberry Pi
We must connect PC to Raspberry Pi and connect Pi to WiFi. For this task, both our PC and Raspberry Pi must connect on the same WiFi network. While your microSD card is in your PC, create two files in it’s root: wpa_supplicant.conf
andssh
.
In wpa_supplicant.conf
enter this text and edit your details: country code, WiFi name and password:
country=US
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
network={
ssid="WiFiNetworkName"
psk="WiFiPassword"
key_mgmt=WPA-PSK
}
File with namessh
should remain empty and without any extension.
Eject the microSD card from your computer and insert it into your Raspberry Pi. Power up your Pi and after a few moments your Pi should be up and connected to WiFi. But we don’t see that until we connect from PC to Raspberry Pi. For that, we must find out local IP address of Raspberry Pi. One way to find that is to log into your router and find attached devices. Another way is to first find out the local IP address of your PC. If you use Windows, run command prompt (press Win+R and type cmd
) and type ipconfig
Then, we will scan class C subnetwork of that IP address with Advanced IP Scanner (type address of your PC and click “C” button in Advanced IP Scanner). You should see all devices connected to your WiFi and recognize your Raspberry Pi.
On your computer, open up PuTTY and connect to IP address of Pi
Login as pi
with password raspberry
, and finnaly we’re connected to our Raspberry Pi and can install mosquito on it.
Installing MQTT on Raspberry Pi
MQTT is a machine-to-machine messaging protocol, designed to provide lightweight publish/subscribe communication to “Internet of Things” devices.
We will install Mosquitto, popular MQTT server/broker, on Raspbery Pi with this command:
sudo apt-get install mosquitto mosquitto-clients
Let’s test it with two SSH connections to Pi.
In one window we will subscribe to messages on “test/topic” channel:
mosquitto_sub -v -t "test/topic"
In other window we will publish messages on same chanell:
mosquitto_pub -t "test/topic" -m "Hello, World!"
Messages we’re publishing in one terminal window will pop up on other one.
These messages are exchanging on localhost of Raspberry Pi, and our goal is to send them on Mainflux platform in cloud.
Installing Mainflux on DigitalOcean
Mainflux is a modern, scalable, secure, open source and patent-free Internet of Things cloud platform. It accepts connections over multiple network protocols (i.e. HTTP, MQTT, WebSocket), thus making a seamless bridge between them.
We will show you step by step how to install it on DigitalOcean droplet.
Create droplet Ubuntu 16.04.4 x64, lowest resources will be enough for this test (1GB RAM, 1vCPU, 25GB SSD).
Create non-root user with sudo privileges and switch to that user:
adduser ivan
usermod -aG sudo ivan
su ivan
Next, install Docker from the official Docker repository. We only need to run this four commands, if you want to explore details you can look here.
To avoid typing sudo whenever you run the docker command, add your username to the docker group.
sudo usermod -aG docker ivan
To apply the new group membership, you should log out and log in back.
Next, we will install Docker Compose.
First, check here what is latest version and edit command bellow:
And finaly set permissions:sudo chmod +x /usr/local/bin/docker-compose
Now, we are readdy to setup Mainflux. Connect to your droplet (launch PuTTY or console on DigitalOcean dashboard).
Installing and starting the Mainflux platform with additional services involves only these three steps:
That’s it, we have installed and started Mainflux platform with additional services (those services are InfluxDB database, InfluxDB writer service, and Grafana tool).
You can now open the dashboard in your browser on your droplet IP address, don’t forget https://
and ignore security warning.
You can there create users, channel and things on Dasboard or you can do it with curl commands on the server like it’s described in documentation. We will register over dashboard, create two things (named MyThing1 and MyThing2) and connect them to the channel (named MyChann).
Create an authorization token for account you’ve just registered with sending HTTP POST request from your computer. You can use Postman for that, you send request to IP address of your droplet and don’t forget to turn off SSL certification verification (in Postman: Settings -> General). To obtain the authorization token POST to /tokens
email and password of your Mainflux account
Let’s get those things we created on dashboard. Switch to GET method and in header make Authorization key with value of token from previous response. GET from /things
We will use those keys later when we connect Raspberry Pi as these things. With mosquitto service installed on Raspberry Pi we will send SenML messages and Mainflux will store that data in it’s InfluxDB database. So, before connecting and sending messages, let’s set up Grafana , tool we’ll use for displaying data stored in database.
Navigate to your server IP address on port 3001. You should see Grafana login page:
We’re going to use default admin credentials for Grafana: admin/admin
Click to add data source
and you should see the form for adding new data source.
Choose whatever name you want, we’ll use Mainflux. Next, choose InfluxDB from the drop-down as database type.
Database URL is http://mainflux-influxdb:8086
.
If we don’t mention some of the fields, that means those fields are left at its default values. Finally, setup InfluxDB details. Database, user and password all have the same value: mainflux
.
After filling these fields, your form should look like this:
Click save and test. Let’s send some data to Mainflux so we can track them in Grafana.
Connect Raspberry Pi to Mainflux
We have already used mosquitto_pub and mosquitto_sub commands on Raspbery Pi for publishing simple messages and recieving them by subscribing to topics. Messages were sent and recieved on local MQTT server. But now let’s publish messages from Raspbery Pi to Mainflux platform that’s installed on cloud server. So, mosquitto_sub
and mosquitto_pub
will have more options, because we must set hostname (-h
)of our server, username (-u
) and password (-P
) of thing that will Raspbery Pi represent.
First we will subscribe as MyThing1 to the topic channels/1/messages
Channel format is channels/<channel_id>/messages
since we only created one channel (MyChann) his ID is 1. If you created more channels you can get their IDs with Postman with GET request from /channels
Username (-u
) and password (-P
) are thing’s ID and key that we have already GET from /things
with Postman in previous chapter. Hostname (-h
) is IP address of droplet where we installed Mainflux.
Finally, let’s publish something on the same topic, open new terminal window on Raspbbery Pi and now we will represent as MyThing2
We should see the messages that we’re publishing in the terminal where we are subscribed to topics.
For the last test, instead of publishing strings, we will publish messages in SenML format. SenML stands for Sensor Markup Language and represents media type for representing simple sensor measurements and device parameters.
So, instead of -m “Hello, World!”
in last command send:
-m '[{"bn":"name","bu":"A", "n": "voltage", "u": "V", "v": 120.1 }, { "n": "current", "t": -2, "v": 1.5 },{ "n": "current", "t": -1, "v": 1.6 }, { "n": "current", "t": 0, "v": 1.7 }]'
We are simulating device that’s measure voltage and temperature. Keep sending messages like that and change some voltage values for example.
Let’s assure that these values are written in database on Mainflux. Create new dashboard in Grafana:
Add new Graph
Then click on the Panel title and choose to edit panel:
You should be able to set up various settings. First of all, set data source to be the source you’ve just added (in our case, we named it Mainflux). You can execute whatever query you want over messages stored in InfluxDB. For example, we’ll use simple select:
And now we can see graph of voltage values we are sending.
In Grafana you can execute multiple queries, for example you can set another query to be that electric current values we were also sending from Raspbbery Pi.
In this tutorial I wanted to explain how to setup from scratch Raspberry Pi and Mainflux on cloud hosting and send messages between them. I was trying not to skip any step so if you follow this tutorial you can also have this connection. I have posted links in the article where you can explore more about tools and technologies I was using and links to other tutorials that helped me.
Until next time,
Ivan Milošević
About Mainfluix company:
Mainflux is a technology company offering a full-stack open-source, patent-free IoT Platform recognized by Linux Foundation and O’Reilly Media with software and hardware consulting services based on extensive working experience in fortune 500 companies.
Mainflux website: https://www.mainflux.com
Mainflux Github: https://github.com/Mainflux/mainflux