Building an Apple HomeKit integration for the DHT22 Temperature and Humidity sensor and a Raspberry Pi using Go. Connects over Wifi - no bridge required.
There aren't many reliable thermometers/hygrometers (humidity sensor) available for HomeKit which do not require their own bridge. This project shows how to build one based on a Raspberry Pi and the DHT22 sensor which is commonly available. Since the Raspberry Pi is powered via DC, a wall converter or battery can power the system.
A simple transistor switch is used to power down the sensor when not in use to conserve power. The system first turns on the sensor and then waits to take a reading. Once a clean reading is taken, the sensor is powered off for an interval.
This project would not be possible without the prior work done in these fantastic Go packages:
go-dht - reading the DHT22 sensor
go-rpio - basic handling of GPIO pins
hap - development of HomeKit accessories
A summary of the program flow:
- Set up the thermometer HomeKit accessory
- Set up the hygrometer HomeKit accessory
- Start a loop to retrieve the temperature and humdity from the sensor
- Set the control pin to high, turning on the sensor
- Wait 3 seconds for the sensor to stabilize
- Read the sensor data until a valid reading is obtained
- Set the control pin to low, turning off the sensor
- Update the HomeKit accessories with the sensor data
- Wait 5 minutes for the next reading
- Start the HomeKit server
Adding to HomeKit:
- Ensure the hktemphum service is running
- Ensure the Raspberry Pi is connected to Wifi
- Open Home on an iPhone or iPad
- Click the + and Add Accessory
- Choose More options...
- Select the thermometer1 accessory
- Input the pin defined in
main.go - Add the thermometer and hygrometer
- 10k ohm resistor
- 2N2222 transistor
- DHT22 sensor package (pull down resistor included)
- Raspberry Pi Zero 2 W
- Blank PCB / Wire / Soldering Kit
Raspberry Pi GPIO pinouts: https://pinout.xyz
It is suggested to build the binary executable and then copy to the Raspberry Pi. For example, to build for Raspberry Pi on Apple silicon:
env GOOS=linux GOARCH=arm go build
After building, copy hktemphum to /opt/hktemphum
Edit /lib/systemd/system/hktemphum.service
[Unit]
Description=Temperature and Humidity HomeKit Integration
[Service]
Type=simple
Restart=always
RestartSec=60s
WorkingDirectory=/opt/hktemphum
ExecStart=/opt/hktemphum/hktemphum
[Install]
WantedBy=multi-user.target
Enable the service systemctl enable hktemphum
Start the service systemctl start hktemphum
Service status systemctl status hktemphum

