A custom HomeKit accessory with Python

Hi πŸ‘‹,

In this short article I want to showcase how I implemented a custom HomeKit accessory with python.

My Home Assistant’s SD card died πŸͺ¦ a few days ago and the support for GPIO based sensors will be removed in newer releases. This makes it unsuitable for my needs, while giving me the perfect opportunity to try other things.

To continue monitoring temperature and humidity in my home I’ve built a custom HomeKit accessory with HAP Python.

The Sensor

A BME680 air quality sensor is used to monitor temperature and humidity. It is connected to the PI according to the following diagram:

The communication with the Pi is done using the I2C protocol. If you want to use I2C in your own setup, it has to be enabled using raspi-config, as it doesn’t come enabled by default.

# Execute
sudo raspi-config
# Then select Interfacing options->I2C and enable it.

Connection can be tested with the following command:

sudo apt-get install build-essential libi2c-dev i2c-tools python-dev libffi-dev git
/usr/sbin/i2cdetect -y 1
pi@raspberrypi:~ $ i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:                         -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- 76 -- 

It will output the address that the sensor is using, in our case the 0x76 I2C address.

The Code for the Accessory

You can browse the full code for the accessory and bme680 sensor in my git repo.

To run the program, clone the repository and ensure that you’re running it under the pi user, otherwise you will need to change some things.

cd /home/pi && git clone git@github.com:dnutiu/bme680-homekit.git && cd bme680-homekit
sudo apt-get install libavahi-compat-libdnssd-dev
pip3 install -r requirements.txt

Verify that the program works by running python3 main.py. Running it the first time will prompt you to add the accessory to the Home app. If you miss this step you can repeat it by deleting the accessory.state file located in pi’s home directory and by running the program again.

After you’ve verified that it works, you can setup a systemd service to run the accessory’s python script when the PI boots

Copy the bme680-homekit.service to /etc/systemd/system and check that the service is running.

sudo cp bme680-homekit.service /etc/systemd/system
sudo systemctl status bme680-homekit

If you want to run this under another user rather than the pi, you’ll need to tweak the bme680-homekit.service file.

Congratulations for making it this far! πŸŽ‰

You can browse more code examples in the HAP-Python repository.

Thanks for reading and have fun! πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» βš™οΈ

How to connect Ethernet devices to WiFi via WDS

Hi πŸ‘‹,

In this blog post I will show you how to connect an Ethernet only device to Wi-Fi using an extra router and the WDS functionality. I initially wanted to install the TP Link Archer T4U WiFi adapter driver on my Ubuntu 20.04 PC but unfortunately the driver is no longer supported.

Since I really needed high internet speed for my PC, I decided to connect it via an Ethernet cable and buy another TP-Link router to use in WDS mode.

WDS mode allows routers to communicate with one another without using wires.

βš™οΈ The configuration of the WDS router is as follows. I left the operation mode in Router Mode.

Then, I navigated to Network -> LAN and set the IP address to 192.168.0.2. Because my old router had the IP address of 192.168.0.1.

The next step would be to navigate to Wireless -> WDS. Click survey and select the SSID of your root router. If the WDS bridging is successfully you should see a Connected message βœ….

Finally, go to Network -> DHCP Server and disable it. All these operations should be performed on the router that you want to bridge, not the root router! πŸ•ΈοΈ

Now that your router has a WDS bridge to the root router, you can connect your PC to it via an Ethernet cable and place it next to your PC. A brand new 5GHz Wi-Fi adapter might have the same price πŸ’° as a new router πŸ€‘, and to be honest unless you’re doing some penetration testing I don’t see the need of a WiFi adapter.

Thanks for reading and happy hacking! 🐧