
IoT
Getting Started with IoT Part 1: Message Broker
This is part 1 of a series of tutorials for getting started with IoT (Internet of things). I’m writing these posts to document my adventures developing an Internet of Things project, hoping that they might serve as a guide to others. Throughout the different steps of the project, we will create a network of “Things” that senses the world and acts in real-time. We will be using Raspberry Pis, NodeMCUs, sensors, actuators, and software to create a centralized dashboard where all “Things” can be monitored and controlled.
Here are links to all the things you need to complete Part 1 of this tutorial:
Raspberry PI 3 Model B or Raspberry Pi 3 Complete Starter Kit
For Part 1, we will be setting up a Raspberry Pi to act as a Message Broker. The broker serves as a central server that will handle all messages generated by our IoT devices. This allows the devices to “talk” to each other. For this, we will use Raspberry Pi and MQTT!
MQTT is a technology that fits perfectly with our IoT communication needs:
MQTT is a machine-to-machine (M2M)/”Internet of Things” connectivity protocol. It was designed as an extremely lightweight publish/subscribe messaging transport. It is useful for connections with remote locations where a small code footprint is required and/or network bandwidth is at a premium. – mqtt.org
MQTT uses a “Publish/Subscribe” model.
For our internet of things, we set up ‘channels’. Each channel is similar to a chat room.
We use these channels for publishing messages, then another device listens and reacts to the data published.
For example:
- We create a channel named “temperature”.
- We create a channel named “email-alerts”.
- Sensors around the house post temperature messages to the channel. The message could look like: { time:"12:45 pm", temp:"21" }
- The air conditioner is listening to the "temperature" channel and reacts when the temperature is over 20 degrees. Then it posts to channel "email-alerts" "House Event: Turned AC on temp = 21"
- An email server listens on "email-alerts" and generates an email for the messages received to this channel.
Before we can start, you need to setup your new Raspberry Pi’s SD card with Raspbian following this tutorial.
The first thing we need to do is setup update and install all our dependencies on the Raspberry Pi. I assume you have a working Raspberry Pi and have shell access.
1 2 3 4 5 |
#!/bin/bash apt-get update apt-get upgrade apt-get autoremove apt-get install mosquitto |
This will install mosquitto MQTT on your raspberry pi. Mosquitto MQTT installs 2 new commands to our raspberry pi: mosquitto_sub for subscribing to channels and mosquitto_pub for publishing messages to channels.
To get your brokers local IP, use the command: ifconfig
and search for your interface. Usually wireless interfaces start with the letter w (ex. wlan0) and ethernet interfaces start with the letter
.e
(ex. eth0). The names can change from device to device depending on how you are connecting to the internet. This ip is the <broker-ip
>
Subscribing to a channel:
We now test that mosquitto is working by running a terminal window:
1 |
mosquitto_sub -t "debug/test_channel" -h "<broker-ip>" |
This will subscribe and start listening to the channel: test_channel
from the topic: debug
Publishing to a channel:
Test sending a message to our broker using this on a NEW terminal window:
1 |
mosquitto_pub -t "debug/test_channel" -m "Hello world" -h "<broker-ip>" |
We have now successfully set up an MQTT Broker on a Raspberry Pi. There is more configuration that can be done like setting up users and permissions, but we are keeping it simple for now. In Part 2 (coming soon) we will build a “sensor thing” that will publish messages to our broker.