Introduction: Build a Apple HomeKit Temperature Sensor Device Using a RaspberryPI and a DHT22

Picture of  Build a Apple HomeKit Temperature Sensor Device Using a RaspberryPI and a DHT22

I was looking for a low cost temperature / humidity sensor I could use to monitor what is happening in my crawlspace, as I found that this spring it was very wet, and had a lot of damp. So I was looking for a reasonably priced sensor that I could put down there, and monitor remotely. After doing some digging on the net for what was available locally and shown to work with a RaspBerry PI and a NodeMCU ( more on this later ). I decided on a DHT22 Sensor. It was cheap, offered both temperature and humididty and available locally.

Update December 2016 - After running these for a few months, I have found that the accuracy of the humidity sensor varies greatly over time and have stopped trusting these for reasonably accurate humidty information. And am changing all my devices over to the Bosch BME280 Temperature/Humidity/Barometric Pressure sensor. So I have created a new instructable showing how to connect this sensor to the RaspberryPI ( Connect Your RaspberryPI to the BME280 Temperature and NodeMCU/ESP8266 ( Homebridge-MCUIOT ).

Step 1: Parts List

So I went to my local parts store, and purchased

1 - DHT22 / AM2303 Temperature / Humidity Sensor

1 - 4.7K Resistor

4 Pin Female header ( Sensor side )

5 Pin Female header ( RPI Side )

Heatshrink tubing narrow, and wide

Old serial mouse

To wire the sensor to PI, I used the cable from an old serial mouse I had lying around. Any used cable could be used, as long at it has 3 wires. The one I used had a couple of wires, but I used the Red, Yellow and Black to keep things simple.

Step 2: Prepare the RPI End of the Cable

Picture of Prepare the RPI End of the Cable

I then soldered the pins to my wire. The pins I had were crimp ones, but I couldn't get them to crimp correctly, so I went with solder instead.

After soldering the pins, I then inserted them into the 5 Pin Female header, with the Red in 1, Yellow in 4, and Black in 5.

RPI Connection is wired like this

RPI -> 5 Pin Header -> Description -> Wire Colour

1 -> 1 -> 3.3 VDC Power -> Red

7 -> 4 -> GPIO4 -> Yellow

9 -> 5 -> Ground -> Black

Step 3: Sensor End of the Cable

Picture of Sensor End of the Cable

At this end we use the 4 Pin female header, the resistor and the heat shrink tubing.

Solder the red and yellow wires each to a pin, and put the resistor between them as well. Also cover these with heat shrink so you don't get a short. Then solder the black wire to a pin as well. Insert the pins into the 4 Pin header as follows

1 - Red

2 - Yellow

3 - Empty

4 - Black

Then cover the wires with the larger heat shrink tubing.

Step 4: Connecting the Cables

Picture of Connecting the Cables

With your RPI powered off, carefully connect the 5 pin female to the GPIO connection, with the Red wire in pin 1 lining up with pin 1 on the GPIO connector. The header should only cover the first 5 odd numbered GPU pins.

For the sensor side, align the pins on the sensor with the header, and ensure that pin 1 of the sensor ( on the left side ), connects with pin 1 of the header ( with the red wire ).

After putting the heat shrink on, I couldn't see the wire colour anymore, so I marked it with a sharpie.

Step 5: Installing the Homebridge Software

As their are a lot of other guides for setting up a raspberry pi, I'm not going to repeat this here, but am assuming that you have your RPI setup with Raspbian Jessie, with Node.JS installed and homebridge running. Their are a number of homebridge getting started guides around covering this already.

Step 6: Install Homebridge-dht

1. Install homebridge-dht with the command

sudo npm install -g homebridge-dht

2. Update your config.json file in ~/.homebridge with the following

{    "bridge": {
     "name": "Penny",
     "username": "CC:22:3D:E3:CD:33",
     "port": 51826,
     "pin": "031-45-154"

"description": "HomeBridge",

"platforms": [],

"accessories": [
        { "accessory": "Dht",
        "name": "dht22",
	"name_temperature": "Temperature",
	"name_humidity": "Humidity",
	"service": "dht22" }


Step 7: Installing PIGPIO

1. Install the pigpiod library via these commands

sudo apt-get update

sudo apt-get install pigpio python-pigpio python3-pigpio

2. Download the DHT22 Sample program from here, and unzip the file into a empty directory.

mkdir DHTXXD




3. Copy the patch file `test_DHTXXD.patch` from /usr/local/lib/node_modules/homebridge-dht/

cp /usr/local/lib/node_modules/homebridge-dht/test_DHTXXD.patch .

4. Then issue this command to patch test_DHTXXD.c

patch < ./test_DHTXXD.patch

5. Compile with this command

gcc -Wall -pthread -o DHTXXD test_DHTXXD.c DHTXXD.c -lpigpiod_if2

6. Copy, and rename DHTXXD to /usr/local/bin/dht22, and make executable.

sudo cp DHTXXD /usr/local/bin/dht22

sudo chmod a+x /usr/local/bin/dht22

7. At this point you should be able to test your DHT22 sensor with the command


And it should respond with

0 18.4 C 51.0 %

Step 8: Raspberry PI CPU Temperature Monitoring - Optional

This is an optional step, that allows you to remotely monitor the temperature of your raspberry PI CPU as well.

1. Create a file in /usr/local/bin/cputemp containing

#!/bin/bash<br>cpuTemp0=$(cat /sys/class/thermal/thermal_zone0/temp)
cpuTempM=$(($cpuTemp2 % $cpuTemp1))

echo $cpuTemp1" C"

2. Make file executable

chmod a+x /usr/local/bin/cputemp

3. Update your config.json file in ~/.homebridge and replace the accessories section with the following:

"accessories": [
        { "accessory":          "Dht",
          "name":               "cputemp",
          "service":        "Temperature" },
        { "accessory":          "Dht",
        "name":               "Temp/Humidity Sensor",
        "service":            "dht22" }

Step 9: Start Homebridge

Start homebridge, and your log file should look like this

[6/21/2016, 9:37:31 PM] Loaded plugin: homebridge-dht
[6/21/2016, 9:37:31 PM] Registering accessory 'homebridge-dht.Dht'

[6/21/2016, 9:37:31 PM] ---

[6/21/2016, 9:37:31 PM] Loaded config.json with 2 accessories and 0 platforms.

[6/21/2016, 9:37:31 PM] ---

[6/21/2016, 9:37:32 PM] Loading 0 platforms...

[6/21/2016, 9:37:32 PM] Loading 2 accessories...

[6/21/2016, 9:37:32 PM] [cputemp] Initializing Dht accessory...

[6/21/2016, 9:37:32 PM] [cputemp] INIT: cputemp

[6/21/2016, 9:37:32 PM] [Temp/Humidity Sensor] Initializing Dht accessory...

[6/21/2016, 9:37:32 PM] [Temp/Humidity Sensor] INIT: Temp/Humidity Sensor

Scan this code with your HomeKit App on your iOS device to pair with Homebridge:


│ 031-45-154 │


[6/21/2016, 9:37:32 PM] Homebridge is running on port 51826.

Step 10: Testing With Home Kit

Picture of Testing With Home Kit

Fire up your favourite homekit client, and pair with your new accessory. You should then see the new Temperature/Humidity Sensor.

Step 11: Bonus Chapter - Dual Sensors

Picture of Bonus Chapter - Dual Sensors

After being asked by several people I thought I would include the notes needed to add a second sensor.

For the wiring, take a look at the at the attached image, this is the one that I shared with Hector305 to connect the second sensor.

And for the updated config file, this is config.json for that.

{ "accessory":   "Dht",

  "name":        "dht22 - indoor",
  "name_temperature": "Indoor Temperature",
  "name_humidity": "Indoor Humdity",
  "gpio":        "4",       
  "service":     "dht22" },
 { "accessory":   "Dht",
  "name":        "dht22 - outdoor",
  "name_temperature": "Outdoor Temperature",
  "name_humidity": "Outdoor Humdity",
  "gpio":        "2",   
  "service":     "dht22" }


FF5 (author)2017-10-31

thanks very much!!!

( ps pigpiod didn't autostart

step 7 : i've found test_DHTXXD.patch in /home/pi/node_modules/homebridge-dht/ )

Leotiedt (author)2017-08-12

Hi very good explanation but I have a question. On your npm site it says support for DHT11 is that correct? What do I have to change in the code to get it working? The pins are the normal ones not like wiring pi where GPIOxxx is WiringPi pin 0?
Thanks very much!!
Best regards

NorthernM (author)Leotiedt2017-09-01

support for the dht devices is via pigpio, and I'm just using their dht example code for the interface. According to their documentation it should just work.

hector305 made it! (author)2016-10-31

Hello , i went ahead and did everything you did , it only worked for a few minutes then it completely stopped, i thought it was the sensor but when i tried it with the arduino the sensor worked 100%

NorthernM (author)hector3052016-10-31

Hector, looking at the output I see that the dht22 command didn't return anything. What happens when you run 'dht22' from the command line? Can you post that output.

Also looking back at the instructions, I missed the step to create a cputemp command, so have added it as 8. If you don't want to see your raspberry pi cpu temperature, please use the shorter config.json in step 6. This will fix the cputemp error as well.

hector305 (author)NorthernM2016-10-31

to be honest , when i followed the instructions, it was confusing after step 2, reason is because it always gave me errors because i didnt have access, even if i runed it as sudo it still gave me errors, so i manually downloaded the file and unzipped it, but theres steps in your instructions that i dont have access on my end,example: cp /usr/local/lib/node_modules/node_modules/homebridge-dht/test_DHTXXD.patch (You have node_modules twice , in my system i only see 1)

NorthernM (author)hector3052016-10-31

Just looking back at that section, and your correct their only should be one node_modules directory and not 2. Try it again, but remove the second node_modules directory.

hector305 (author)NorthernM2016-10-31

Removing the node_modules, this is what i get

hector305 (author)hector3052016-10-31

i uninstalled and reinstalled homebridge-dht, here are the steps you said to follow, so i get stuck on 3, as you can see the patch is there in the folder to copy but it seems that it doesnt know where to paste it too

NorthernM (author)hector3052016-11-01

Hector, at the end of the cp command in step 7.3 there is a period . that you are missing.

hector305 (author)NorthernM2016-11-01

Heres with the period in the end

NorthernM (author)hector3052016-11-01

Try this, remove the double node_modules and add a .

cp /usr/local/lib/node_modules/homebridge-dht/test_DHTXXD.patch .

hector305 (author)NorthernM2016-11-01

Ok, once i did that, do i do the patch? seems like im doing the patch wrong

NorthernM (author)hector3052016-11-01

Perfect, now

gcc -Wall -pthread -o DHTXXD test_DHTXXD.c DHTXXD.c -lpigpiod_if2

cp DHTXXD /usr/local/bin/dht22

chmod a+x /usr/local/bin/dht22

hector305 (author)NorthernM2016-11-01

i get an error when i do the gcc -Wall -pthread -o DHTXXD test_DHTXXD.c DHTXXD.c -lpigpiod_if2

NorthernM (author)hector3052016-11-01

I'm thinking just start again at step 7.2 and it should work. The only thing to do first would be to clean up anything from previous tries. If you login again and do

rm -rf DHTXXD

It will clean up the previous patch / build attempt, then execute the following.

mkdir DHTXXD
cp /usr/local/lib/node_modules/homebridge-dht/test_DHTXXD.patch .
patch < ./test_DHTXXD.patch
gcc -Wall -pthread -o DHTXXD test_DHTXXD.c DHTXXD.c -lpigpiod_if2
sudo cp DHTXXD /usr/local/bin/dht22
sudo chmod a+x /usr/local/bin/dht22

You should be good to go with this step

Output should look like this from the commands

sgracey@penny:~/test$ mkdir DHTXXD


sgracey@penny:~/test$ cd DHTXXD


sgracey@penny:~/test/DHTXXD$ wget

--2016-11-01 22:26:18--

Resolving (

Connecting to (||:80... connected.

HTTP request sent, awaiting response... 200 OK

Length: 4693 (4.6K) [application/zip]

Saving to: ‘’ 100%[=====================>] 4.58K --.-KB/s in 0s

2016-11-01 22:26:18 (85.8 MB/s) - ‘’ saved [4693/4693]


sgracey@penny:~/test/DHTXXD$ unzip


inflating: DHTXXD.c

inflating: DHTXXD.h

inflating: DHTXXD_README

inflating: test_DHTXXD.c

sgracey@penny:~/test/DHTXXD$ cp /usr/local/lib/node_modules/homebridge-dht/test_DHTXXD.patch .

patching file test_DHTXXD.c


sgracey@penny:~/test/DHTXXD$ gcc -Wall -pthread -o DHTXXD test_DHTXXD.c DHTXXD.c -lpigpiod_if2


sgracey@penny:~/test/DHTXXD$ ./DHTXXD

0 19.8 C 40.0 %

sgracey@penny:~/test/DHTXXD$ sudo cp DHTXXD /usr/local/bin/dht22

[sudo] password for sgracey:

sgracey@penny:~/test/DHTXXD$ sudo chmod a+x /usr/local/bin/dht22


hector305 (author)NorthernM2016-11-02

Ok , i did all that, but it looks like it gets stuck where ./DHTXXD , it saids pigpiod not running, however i already installed pigpiod

NorthernM (author)hector3052016-11-02

That is odd, have you rebooted since the install?

hector305 (author)NorthernM2016-11-02

I just rebooted and i still get Error: pigpoid not running , i have vnc if you want to able to connect too, email me at

WimpfR (author)hector3052017-01-27

Hello :)

I got the exact same problem here, have you found a solution to this issue?

Thank you! :)

NorthernM (author)WimpfR2017-01-27

Did step 7.1 complete successfully?

sudo apt-get update

sudo apt-get install pigpio python-pigpio python3-pigpio

It should setup autostart of pigpiod.

WimpfR made it! (author)NorthernM2017-01-28

Yes, should be successfull.

Bam Bam Mantell (author)WimpfR2017-08-31

Try this first

sudo pigpiod

NorthernM (author)WimpfR2017-02-04

pigpiod should autostart on reboot. Check to see if you have /etc/init.d/pigpiod, and if it is having errors during startup.

NorthernM (author)WimpfR2017-01-28

Try rebooting

memfya made it! (author)2017-08-07

Hi! Help me please! dht22 not working, but loldht is working..

NorthernM (author)memfya2017-08-08

can you try from scratch building the dht22 command again, I'm thinking it may have been corrupted when it was patched twice. Basically step 7 again.

That the dht works with loldht, is a great indicator that the problem is in the build of the dht22 command.

radu022003 (author)2017-08-05

hello, i have followed this instructables and worked eveything very smooth, thank you.

I have one question, the DHT22 sensor is able to provide the temperature and humidity with one decimal and i can see it in the logs (eg. 22.1 or 44.9%) but in HomeKit application, on iPhone is displayed only 22 degrees and 45%, what should be done in order to have the float values also on iPhone application?

NorthernM (author)radu0220032017-08-08

That is really a question for Apple, as I'm passing to back to HomeKit the value as returned by the sensor and not dropping the decimal. I looked at the Eve app and I did see the decimal beside the temperature. I think they drop the decimal due to screen real estate.

Alek91 (author)2017-05-31


Thanks, i got it running in minutes!

One point though, do you if there's a way to get a graph of the evolution of the temperature & humidity ?

Thanks again !

NorthernM (author)Alek912017-06-26


I have updated the package with logging to google sheets, so that you can creates graphs etc. Details on the configuration are included in the README on github

NorthernM (author)Alek912017-05-31

With HomeKit unfortunately not, there is no graphing interface. Over the next few weeks I'm working on creating a google sheets data logger that you could use. Watch this space in about a month.

pallini (author)2017-04-24

Hi I followed your instructions to compile the dht-program - completed step 7. I tested the sensor and it worked running the command directly from the terminal. I also edited the config.json file an if I try running homebridge these are my errors:

[4/24/2017, 9:09:58 PM][Raspberry Pi Temperature] Initializing PiTemperature accessory...
[4/24/2017, 9:09:58 PM][Raspberry Pi Temperature] Model BCM2709/a02082 Serial 00000000cedc3a7d
[4/24/2017, 9:09:58 PM][Temp/Humidity Sensor] Initializing Dht accessory...
[4/24/2017, 9:09:58 PM][Temp/Humidity Sensor] Adding Accessory
[4/24/2017, 9:09:58 PM][Temp/Humidity Sensor] INIT: Temp/Humidity Sensor
if (!(services[0] instanceof Service)) {
TypeError: Cannot read property '0' of undefined
at Server._createAccessory (/usr/lib/node_modules/homebridge/lib/server.js:380:17)
at Server._loadAccessories (/usr/lib/node_modules/homebridge/lib/server.js:266:26)
at (/usr/lib/node_modules/homebridge/lib/server.js:81:38)
at module.exports (/usr/lib/node_modules/homebridge/lib/cli.js:40:10)
at Object.<anonymous> (/usr/lib/node_modules/homebridge/bin/homebridge:17:22)
at Module._compile (module.js:571:32)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:427:7)
at startup (bootstrap_node.js:148:9)
at bootstrap_node.js:542:3

I would appreciate any help. Thanks a lot!

NorthernM (author)pallini2017-04-24

I think the problem is in the accessory section of your config.json. Can you share your config.json ?

pallini (author)NorthernM2017-04-26

"name":"Raspberry Pi Temperature"
{ "accessory":"Dht",
"name":"Temp/Humidity Sensor",
"service":"dht11" }


I used the DHT11 Sensor and therefore my compiled program is called dht11.

NorthernM (author)pallini2017-04-26

The service parameter in customer config.json must be dht22. If you changed the executable name to be dht11, there is a different config parameter called dhtExec that you can use.

__dan__ made it! (author)2017-03-29

Great Instructable!

Made this a couple weeks ago, worked like a charm – but also changing all my devices over to the Bosch BME280 (same problem w/ humidity accuracy using the DHT22).

RichardV100 made it! (author)2017-01-11

Hello Hector,

Thank you very much for the good work! I've upgraded the homebridge-dht and it worked at once!

RichardV100 (author)2017-01-05

Hello Hector,
I have followed all the steps. I had to use sudo with patching & compiling. The file DHTXXD in the map /usr/local/bin/dht22 is created but I do not see any related file to execute with "dht22". I have to admit that I did not yet connect the DHT22 physically to pin 4. Does the DHT22 create a file itself like a D18B20 temp sensor?
What do you advice me to perform now?
Regards Richard

NorthernM (author)RichardV1002017-01-05


In step 7.6 this creates the file dht22.

sudo cp DHTXXD /usr/local/bin/dht22

sudo chmod a+x /usr/local/bin/dht22

With the CP command, I'm copying the file DHTXXD and renaming it to dht22.

RichardV100 (author)NorthernM2017-01-06

Hello Hector,
Finally it's working now. I completed the whole sequence again. Maybe a good hint to update step 7.6 that you first have to to te name DHTXXD to dht22.
If I want to have the humidity on the homebridge button itself and not in the details which routine do I need to change?
Thank you very much!

NorthernM (author)RichardV1002017-01-10

I updated the plugin to create a separate humidity sensor, so if you update the plugin you will now see a humidity as a separate sensor. With this change I added some additional options to the configuration file to allow naming of the each. Please see step 6.2.

NorthernM (author)RichardV1002017-01-06

In the plugin itself, a second accessory would need to be created. If you give me a could of days, I can add it to the plugin.

hector305 (author)2017-01-04

Hello, is there a way to add multiple dht22 sensors?

NorthernM (author)hector3052017-01-04


Multiple sensors are supported by the plugin, just not documented in the Instructable. In the file the config.json setting for multiple sensors is documented. If need more assistance, please let me know.

hector305 (author)NorthernM2017-01-04

If you can guide me through it , thank you

NorthernM (author)hector3052017-01-06

I sent an email, but did not hear back. Do you need help with the config or connecting the sensor?

hector305 (author)NorthernM2017-01-08

Never got the email

hector305 (author)hector3052017-01-08

Just replied to your email

dom_efendi (author)2017-01-06

This is what I get at Homebridge startup

thank you :)

About This Instructable




Bio: Creator of multiple Apple HomeKit devices using homebridge in an attempt to increase the WAF score of my home automation attempts.
More by NorthernM:Control Your Fireplace With HomeKit and AlexaTake Snapshots of Who Visits Your Door With Homebridge and Google DriveReceive Picture Notifications When People Approach Your House With HomeKit
Add instructable to: