Home Assistant controls microcontrollers over MQTT
Home Sweet Home
Automating your four walls does not necessarily require commercial solutions. With a little skill, you can develop your own projects on a low budget.
Home automation offers many opportunities, but equally harbors many risks. If you succeed in becoming independent of commercial products, you can save money while retaining control over what data flows where. In this case, the Message Queuing Telemetry Transport (MQTT) protocol proves to be very useful.
A previous article on Z-Wave [1] showed how you can bring the Raspberry Pi up to speed with Home Assistant and components available on the market to attain the goal of achieving automation magic in your home without human interference. Plenty of components can be addressed by Home Assistant, even without the cloud. The only limits are your wallet and your imagination.
This sequel explains how you tweak both the price and the DIY factors of the components. The Raspberry Pi from model 3 onward comes with a wireless interface that is also available in many microcontroller modules and is likely to open many doors. The glue that connects the whole thing to the Home Assistant environment looked at in the previous article is an IP-capable protocol that saw the light of day long before any Internet of Things (IoT) hype did: MQTT.
The MQTT protocol was introduced in 1999 and is a text-based protocol that runs over TCP on any IP network. Transport Layer Security (TLS) can optionally be used for encryption. The sensors and actuators communicate as clients with the broker, which acts as the communications hub. Addressing relies on what are known as "topics." Transmitters are referred to as publishers, and receivers as subscribers.
Testing MQTT
To implement the protocol on the Raspberry Pi, you first need the mosquitto package. For the small implementation tests that follow, you will also need the matching client. You can set up both components on the Raspberry Pi with the commands:
$ sudo apt-get install mosquitto $ apt-get mosquitto-clients
If the broker is running after the install, you can log in to the individual topics with the client and retrieve a list of topics with a command-line call that specifies the host on which the broker is running withthe -h
option:
$ mosquitto_sub -h localhost -t "#"
In this case, it is localhost. For example, to send the value 10 to a topic named test/test1 and receive the value as a subscriber, you would use the commands:
$ mosquitto_pub -V mqttv311 -h 192.168.3.7 -t test/test1 -m 10 $ mosquitto_sub -h 192.168.3.7 -t test/test1
For a more practical example, I'll use MQTT to transfer the measured values of a one-wire temperature sensor of the DS18x20 type connected to a Raspberry Pi to the broker listening on another Raspberry Pi. To do this, you need to enable one-wire support up front with
dtoverlay=w1-gpio
in the /boot/config.txt
file.
After connecting the sensor to the standard one-wire GPIO port, the temperature values can be queried as four-digit integers in /sys/bus/w1/devices/28-0417c1b1f7ff/w1_slave
. The value 28-0417c1b1f7ff
is the unique ID of the sensor on the bus. The complete command sequence is:
$ mosquitto_pub -h 192.168.3.7 -t wohn/temp/28-0417c1b1f7ff -m $value $ mosquitto_sub -h localhost -t wohn/temp/28-0417c1b1f7ff
To transfer the values to the Home Assistant environment, you need to enter the broker you are using in the configuration.yaml
file; in this case, it is running on the same host (Listing 1, lines 1-2). The entries in the lines that follow take care of subscribing to the appropriate topic and fielding the sensor data in Home Assistant; the last line also specifies degrees Celsius as the unit.
Listing 1
configuration.yaml
01 mqtt: 02 broker: localhost 03 04 sensor: 05 - platform: mqtt 06 name: temp1 07 state_topic: "living/temp/28-0417c1b1f7ff" 08 unit_of_measurement: '°C'
ESP8266 Controller
The theoretical side is now covered, but to use a separate Raspberry Pi for each remote actuator or sensor seems a little unrealistic in practice. The ESP8266 controller will come in handy here. On the one hand, you can source low-priced and useful developer boards that use this controller; on the other hand, the chip is used in various products from the Far East, and you can take control of many of these simply by flashing them with freely available firmware.
For the tests here, I used a Sonoff Basic WiFi smart switch [2] (Figure 1). This controllable relay switch has a screw connection for a 220V mains supply with switchable output.
CAUTION: There is a danger to life if this hardware is not used correctly. In this article, I am restricting the supply voltage to the module to 3.3V.
The firmware is Tasmota. The software lets you configure a variety of supported sensors and components on the existing GPIO ports of the controller, such as the temperature sensor I referred to earlier. Tasmota uses MQTT to transmit the data from the sensors.
Flashing Tasmota
Among the several ways to flash Tasmota, one popular approach is to use a USB serial adapter. A description can be found on the Getting Started page of the project [3]. Because the Raspberry Pi comes with a serial interface, I will do the flashing directly through the GPIO. The wiring required for this is shown in Figures 1 and 2. The esptool.py
utility from the PiOS repository provides the software.
The Tasmota firmware, along with a massive collection of information, can be found in the project's GitHub repository [4]. I downloaded the sensor variant for the tests because I wanted to integrate the temperature sensor:
$ wget -c http://ota.tasmota.com/tasmota/release-9.4.0/tasmota-sensors.bin
After connecting the module as shown, you have to hold down the button on the Sonoff module while powering it on; the LED does not light up. Next, use the command
$ esptool --port /dev/ttyAMA0 chip_id
to check whether you can access the Sonoff. An ID must be shown.
Finally, flash Tasmota with the command:
$ esptool --port /dev/ttyAMA0 write_flash-fm dout 0x0 tasmota-sensors.bin
This operation should take about a minute. When the module is restarted, it reports for duty as the new WiFi access point, which you can reach at http://192.168.4.1 and on which you configure the WiFi network to be used. All further setup steps are then performed with the IP address assigned by your own WiFI network.
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Latest Cinnamon Desktop Releases with a Bold New Look
Just in time for the holidays, the developer of the Cinnamon desktop has shipped a new release to help spice up your eggnog with new features and a new look.
-
Armbian 24.11 Released with Expanded Hardware Support
If you've been waiting for Armbian to support OrangePi 5 Max and Radxa ROCK 5B+, the wait is over.
-
SUSE Renames Several Products for Better Name Recognition
SUSE has been a very powerful player in the European market, but it knows it must branch out to gain serious traction. Will a name change do the trick?
-
ESET Discovers New Linux Malware
WolfsBane is an all-in-one malware that has hit the Linux operating system and includes a dropper, a launcher, and a backdoor.
-
New Linux Kernel Patch Allows Forcing a CPU Mitigation
Even when CPU mitigations can consume precious CPU cycles, it might not be a bad idea to allow users to enable them, even if your machine isn't vulnerable.
-
Red Hat Enterprise Linux 9.5 Released
Notify your friends, loved ones, and colleagues that the latest version of RHEL is available with plenty of enhancements.
-
Linux Sees Massive Performance Increase from a Single Line of Code
With one line of code, Intel was able to increase the performance of the Linux kernel by 4,000 percent.
-
Fedora KDE Approved as an Official Spin
If you prefer the Plasma desktop environment and the Fedora distribution, you're in luck because there's now an official spin that is listed on the same level as the Fedora Workstation edition.
-
New Steam Client Ups the Ante for Linux
The latest release from Steam has some pretty cool tricks up its sleeve.
-
Gnome OS Transitioning Toward a General-Purpose Distro
If you're looking for the perfectly vanilla take on the Gnome desktop, Gnome OS might be for you.