My intro into IoT – 04

Last time, we finished setting up the ESP8266 module. Now, let’s switch a bit back on what I started the blog, to Raspberry Pi.

Raspberry Pi as hub

Raspberry Pi 3B

The credit-card sized single board computer, Raspberry Pi 3 Model B, is the third-generation Raspberry Pi. Adding keyboard, mouse, display, power supply, SD card with Linux on it, you’ll have a fully  computer, with a 1.2GHz Quad-Core ARM Cortex-A53, 1GB RAM, 4 x USB ports, 10/100 BaseT Ethernet socket, full-size HDMI, 40pin extended GPIO and on-board wireless LAN and Bluetooth.

In my case, that mini-computer works non-stop for 2 years or so, being the smart devices hub.

I have the latest version of the Raspbian operating system, I’ve also installed the PiVPN in order to have access and control my devices from outside of my network.

In the early versions of the devices, I’ve used http requests to communicate with the hub, but later switched using the MQTT protocol – a publish-subscribe based messaging protocol.

Publish–subscribe is a pattern where publishers don’t send the messages to subscribers, set the message payloads into classes without knowing who are the subscribers. In the same way, the subscribers will only receive messages that are of interest, without knowing the publishers of the messages.

Mosquitto software operates as the broker in this system and routes the published data to the appropriate subscribers.

You can install software on the Pi by having a keyboard, mouse and display connected to it and using the terminal OR via ssh.

MQTT setup

After setting up Raspbian and PiVPN, you need to install the Mosquitto Server. The Raspberry apt-get tool (that’s used to install software on the Pi) doesn’t contain the latest version of the Mosquitto software, so we’ll need to update it. Open a terminal to the pi and run the commands

sudo wget
sudo apt-key add mosquitto-repo.gpg.key
cd /etc/apt/sources.list.d/
sudo wget
sudo apt-get update

Next we can install the Mosquitto software:

sudo apt-get install mosquitto mosquitto-clients python-mosquitto
  • mosquitto – is the MQTT broker, the server;
  • mosquitto-clients – command line clients, you’ll need them in debugging;
  • python-mosquitto – the Python language bindings

The broker should automatically start, if not you can start it by running the following command in the terminal

sudo /etc/init.d/mosquitto start

In order to test if all works fine, log to the Pi in two terminals, in one run the MQTT subscriber (mosquitto_sub), to listen to the topic (notice the -t param) “esp8266”:

mosquitto_sub -t esp8266

And in the other terminal, run the publisher (mosquitto_pub) to publish a message (-m param) to the topic “esp8266” ( -t param)

mosquitto_pub -t esp8266 -m "Hello!"

You should see the message sent by the publisher (second terminal) to the first terminal (the subscriber).

Home automation app

First, I’ve started using on the hub (the Raspberry Pi) Domoticz home automation app, with http requests to make the calls and having all the “if-this-then-that” decisions made at the ESP module (eg., things like: if there is movement and the light sensor says it’s dark, turn on the light).

In the end I managed to make it work with the first device, but after working with it for a while, I’ve decided to try something else.

So, I switched to Home Assistant, changed the ESP module code, from http requests to mqtt, moved the comditions and execution logic to the hub – using Home Assistant automations. Seems stable, been using it for a year and a half, with the esp modules and without any issues. Configuring automations and sensors can be achieved using yml files and there is, on the Home Assistant website, a great support how to work with them.

There is also a blog, where you can ask questions or, most of the cases a simple search will help, in case you get stuck.

To read more on the setup, official or third party addons, adding and using themes, different UI you can check this git repo.

This is how my dashboard looks, with the Home Assistant, with all automations, sensors and switches:

But we’ll address this more on next posts, where I’ll start detailing each device I have using ESP8266.


Leave a Reply

Your email address will not be published. Required fields are marked *