Introduction: 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

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

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

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 http://abyz.co.uk/rpi/pigpio/code/DHTXXD.zip, and unzip the file into a empty directory.

mkdir DHTXXD

cd DHTXXD

wget http://abyz.co.uk/rpi/pigpio/code/DHTXXD.zip

unzip DHTXXD.zip

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

dht22

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)
cpuTemp1=$(($cpuTemp0/1000))
cpuTemp2=$(($cpuTemp0/100))
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

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

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" }

Comments

author
Alek91 made it!(author)2017-05-31

Hey,

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 !

author
NorthernM made it!(author)2017-06-26

Alek,

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

author
NorthernM made it!(author)2017-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.

author
pallini made it!(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
/usr/lib/node_modules/homebridge/lib/server.js:380
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 Server.run (/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!

author
NorthernM made it!(author)2017-04-24

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

author
pallini made it!(author)2017-04-26

{
"bridge":{
"name":"Homebridge",
"username":"CC:22:3D:E3:CE:31",
"port":51826,
"pin":"031-45-154"
},
"accessories":[
{
"accessory":"PiTemperature",
"name":"Raspberry Pi Temperature"
},
{ "accessory":"Dht",
"name":"Temp/Humidity Sensor",
"service":"dht11" }
],
"platforms":[

]
}

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

author
NorthernM made it!(author)2017-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.

author
__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).

DHT22_3.JPGDHT22_6.JPGDHT22_1.JPG
author
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%

Screenshot 2016-10-31 02.45.22.png
author
NorthernM made it!(author)2016-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.

author
hector305 made it!(author)2016-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)

Screenshot 2016-10-31 20.11.33.pngScreenshot 2016-10-31 20.12.47.pngScreenshot 2016-10-31 20.13.13.pngScreenshot 2016-10-31 20.13.57.png
author
NorthernM made it!(author)2016-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.

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

Removing the node_modules, this is what i get

Screenshot 2016-11-01 01.00.16.png
author
hector305 made it!(author)2016-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

Screenshot 2016-11-01 01.13.29.pngScreenshot 2016-11-01 01.13.39.pngScreenshot 2016-11-01 01.15.17.png
author
NorthernM made it!(author)2016-11-01

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

author
hector305 made it!(author)2016-11-01

Heres with the period in the end

Screenshot 2016-11-01 11.42.34.png
author
NorthernM made it!(author)2016-11-01

Try this, remove the double node_modules and add a .

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

author
hector305 made it!(author)2016-11-01

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

Screenshot 2016-11-01 19.12.10.pngScreenshot 2016-11-01 19.12.31.png
author
NorthernM made it!(author)2016-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

author
hector305 made it!(author)2016-11-01

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

Screenshot 2016-11-01 20.53.46.png
author
NorthernM made it!(author)2016-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
cd DHTXXD
wget http://abyz.co.uk/rpi/pigpio/code/DHTXXD.zip
unzip DHTXXD.zip
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
./DHTXXD
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$

sgracey@penny:~/test$ cd DHTXXD

sgracey@penny:~/test/DHTXXD$

sgracey@penny:~/test/DHTXXD$ wget http://abyz.co.uk/rpi/pigpio/code/DHTXXD.zip

--2016-11-01 22:26:18-- http://abyz.co.uk/rpi/pigpio/code/DHTXXD.zip

Resolving abyz.co.uk (abyz.co.uk)... 91.238.161.4

Connecting to abyz.co.uk (abyz.co.uk)|91.238.161.4|:80... connected.

HTTP request sent, awaiting response... 200 OK

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

Saving to: ‘DHTXXD.zip’

DHTXXD.zip 100%[=====================>] 4.58K --.-KB/s in 0s

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

sgracey@penny:~/test/DHTXXD$

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

Archive: DHTXXD.zip

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$

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

sgracey@penny:~/test/DHTXXD$

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

sgracey@penny:~/test/DHTXXD$


author
hector305 made it!(author)2016-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

Screenshot 2016-11-02 12.29.19.pngScreenshot 2016-11-02 12.29.08.pngScreenshot 2016-11-02 12.28.04.png
author
NorthernM made it!(author)2016-11-02

That is odd, have you rebooted since the install?

author
hector305 made it!(author)2016-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 hector305954@gmail.com

author
WimpfR made it!(author)2017-01-27

Hello :)

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

Thank you! :)

author
NorthernM made it!(author)2017-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.

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

Yes, should be successfull.

pigpoid.png
author
NorthernM made it!(author)2017-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.

author
NorthernM made it!(author)2017-01-28

Try rebooting

author
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!

IMG_1237.PNG
author
RichardV100 made it!(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

author
NorthernM made it!(author)2017-01-05

Richard,

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.

author
RichardV100 made it!(author)2017-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!

author
NorthernM made it!(author)2017-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.

author
NorthernM made it!(author)2017-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.

author
hector305 made it!(author)2017-01-04

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

author
NorthernM made it!(author)2017-01-04

Hector,

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

author
hector305 made it!(author)2017-01-04

If you can guide me through it , thank you hector305954@gmail.com

author
NorthernM made it!(author)2017-01-06

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

author
hector305 made it!(author)2017-01-08

Never got the email

author
hector305 made it!(author)2017-01-08

Just replied to your email

author
dom_efendi made it!(author)2017-01-06

Hey
This is what I get at Homebridge startup

thank you :)

2017, 09:50.jpg2017, 09:50.jpg
author
NorthernM made it!(author)2017-01-06

Dom,

Looking at the homebridge output it is complaining that the homebridge config.json file can not be found in the directory /root/.homebridge.

Have you created the config file? And is it in that directory?

author
dom_efendi made it!(author)2017-01-06

you Sir are a legend, everything is working now :) thank you so very much !

author
dom_efendi made it!(author)2017-01-03

hey,

I need your help please :)

What am I missing/doing wrong here?

Untitled.png
author
NorthernM made it!(author)2017-01-03

Dom,

Looking at the screen shot, it appears that you are not the correct directory. When I wrote the steps I assumed they would all be executed sequentially, and Step 7.3 assumes that you are already in the DHTXXD directory. This is where you would have been at the end of Step 7.2. Try it again, but execute

cd DHTXXD

before executing the steps in 7.3 again.

good luck ;-)

author
dom_efendi made it!(author)2017-01-05

well that wasn't very clever of me at all, thank you for your patience and time :)

I got Homebridge running, but after firing up the homekit client (Apple's Home or Elgato's Eve) and trying to add a new accessory I don't get DHT22 to show up on iOS at all :/

author
NorthernM made it!(author)2017-01-05

Hummm, can you share the output from homebridge when starting? I suspect the plugin isn't starting correctly.

author
AaronB230 made it!(author)2016-10-19

Hi I'm very new to this and have limited code knowledge. Im stuck on step 6 i got to the step of sudo apt-get install pigpio python-pigpio python3-pigpio and downloaded the DHT22 sample code but I'm stuck on this bit. Apply this patch to test_DHTXXD. This adds units to the response, and adds an error when pigpiod is not running.

diff test_DHTXXD.c orig/test_DHTXXD.c

128c128

< printf("%d %.1f C %.1f %%\n", r.status, r.temperature, r.humidity);

---

> printf("%d %.1f %.1f\n", r.status, r.temperature, r.humidity);

158,160d157

< } else {

< fprintf(stderr, "ERROR: pigpiod not running\n");

< return 1;

Compile with this command

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

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

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

dht22

And it should respond with

0 18.4 C 51.0 %

I would appreciate your input

Regards Aaron

author
NorthernM made it!(author)2016-10-19

Aaron,

I went back and changed the patch instructions, and hopefully made them easier to follow this time. And I have included the patch file as part of the homebridge plugin, so you will need to run sudo npm install -g homebridge-dht before following the instructions.

About This Instructable

6,964views

19favorites

License:

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:Take Snapshots of Who Visits Your Door With Homebridge and Google DriveReceive Picture Notifications When People Approach Your House With HomeKitBuild a Apple HomeKit Temperature Sensor Device Using a RaspberryPI and a BME280
Add instructable to: