I have used MQTT in projects before. MQTT is a great way to connect multiple devices to a central server and create a two way permanent connection. This is useful in all kinds of application, and especially in sensor networks. The goal of this iteration is to setup the mqtt-server on the raspberry (where also Home Assistant is running) and to monitor the room temperature using an esp8266 microcontroller, connected over wifi.
Setting up the mqtt server
Fortunately, MQTT and Home Assistant play very nice together. Home Assistant can connect to a running and MQTT server and updates the values. The easiest way to get an mqtt server running is by using Mosquitto. This is the current standard for running mqtt. I found out that installing hassctl can be useful for easy process management of the hassio service. With hassctl you can start, stop and restart the service with an easy command:
# Install hassctl for easy hass management
sudo curl -o /usr/local/bin/hassctl https://raw.githubusercontent.com/dale3h/hassctl/master/hassctl
sudo chmod +x /usr/local/bin/hassctl
Installing Mosquitto on the Raspberri Pi is very easy a single install command creates a running instance of Mosquitto:
# Install mosquitto
sudo apt-get install mosquitto mosquitto-clients
# Listen to a channel
mosquitto_sub -t sensor/temperature
Mosquitto is running on port 1337.
Great development so far! Home Assistant and Mosquitto are running on the same Raspberry Pi 0W.
Configuring Home Assistant
Home Assistant is configured with the configuration.yaml file. Adding the basic Mosquitto configuration is pretty easy:
sudo nano /home/homeassistant/.homeassistant/configuration.yaml
unit_of_measurement: " C"
First, the mqtt server is added to Home Assistant. The first goal is to monitor the room temperature. In the second part this sensor is added to Home Assistant. After restarting the temperature is visible in the interface!
Temperature is added to Home Assistant (in this screenshot already with the sensor attached)
Setting up the sensor
At this point, my home is not very smart. The only smart thing running was the Google Chromecast. Right now this setup is added with the Raspberry with Home Assistant and Mosquitto. Time to update this with the first sensor!
For this setup I am using a Wemos D1 Mini connected to a DHT Temperature sensor. The code for the esp8266 microcontroller is published here at gitlab.com. This post is not about microcontroller programming, so this will not be explained here. The main thing to understand is that it uses the pubsubclient by https://pubsubclient.knolleary.net/. When the MCU starts, it tries to connect to WiFi, and after this it tries to connect to the (local) mqtt server. When these two connections are created, the MCU sends the current temperature value on a regular basis.
Great results so far! Home Assistant is running, the Mosquitto MQTT server is running, they communicate with each other and the MCU sends sensor-data!
Temperature sensor sending a measurement every 10 minutes
Great results so far, but there is still a lot to be done. Next steps include for instance: using batteries and MCU deep sleep for energy efficiency, add mysensors.io, use 2.4Ghz wireless communication modules (ordered), create a real life application, create interactivity based on events.
Stay tuned for iteration 3!