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
-
New Slimbook EVO with Raw AMD Ryzen Power
If you're looking for serious power in a 14" ultrabook that is powered by Linux, Slimbook has just the thing for you.
-
The Gnome Foundation Struggling to Stay Afloat
The foundation behind the Gnome desktop environment is having to go through some serious belt-tightening due to continued financial problems.
-
Thousands of Linux Servers Infected with Stealth Malware Since 2021
Perfctl is capable of remaining undetected, which makes it dangerous and hard to mitigate.
-
Halcyon Creates Anti-Ransomware Protection for Linux
As more Linux systems are targeted by ransomware, Halcyon is stepping up its protection.
-
Valve and Arch Linux Announce Collaboration
Valve and Arch have come together for two projects that will have a serious impact on the Linux distribution.
-
Hacker Successfully Runs Linux on a CPU from the Early ‘70s
From the office of "Look what I can do," Dmitry Grinberg was able to get Linux running on a processor that was created in 1971.
-
OSI and LPI Form Strategic Alliance
With a goal of strengthening Linux and open source communities, this new alliance aims to nurture the growth of more highly skilled professionals.
-
Fedora 41 Beta Available with Some Interesting Additions
If you're a Fedora fan, you'll be excited to hear the beta version of the latest release is now available for testing and includes plenty of updates.
-
AlmaLinux Unveils New Hardware Certification Process
The AlmaLinux Hardware Certification Program run by the Certification Special Interest Group (SIG) aims to ensure seamless compatibility between AlmaLinux and a wide range of hardware configurations.
-
Wind River Introduces eLxr Pro Linux Solution
eLxr Pro offers an end-to-end Linux solution backed by expert commercial support.