Skip to content

Using dht22 temperature and humidity sensor w/ raspberry pi zero w. Includes script to periodically send sensor data to a MQTT broker

Notifications You must be signed in to change notification settings

oscj/dht22-mqtt-connector

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

dht22-mqtt-connector

In this project, a DHT22 temperature and humidity sensor is hooked up to a Raspberry Pi Zero W, which then sends the sensor data to a MQTT broker.

Topics

  1. Hardware

  2. Software


Hardware

Setting Up The Pi

Raspberry Pi Zeros do not come installed with GPIO pins. I chose to buy some pins and solder them onto the board. If you have a 3 or 4 series Pi, no need to worry about this.

Not needed, but just wanted to flex my first solder job:

solder_job

Connecting The Sensor

This project used the AM2302 (wired DHT22) temperature-humidity sensor

Since it is a wired sensor, I decided to solder the wires straight onto the corresponding GPIO pins. This is how the GPIO pins are laid out:

gpio

I connected the red wire of the sensor to pin 1 (3V power), the black wire to pin 6 (Ground), and the yellow wire to pin 22.

And here is a picture of the sensor connected to the GPIO pins. I passed the wires through the GPIO opening of the case before soldering the wires on the pins. This makes putting on a case much easier.

sensor_finished


Software

Interfacing With The Sensor

There are a couple of dependencies needed to interface with the sensor. For python, the adafruit circuit python library is needed. To download, execute in terminal:

python3 -m pip install adafruit-circuitpython-dht

Then, you will want to install the libgpiod2 library to your pi, (assuming you have apt)

sudo apt-get install libgpiod2

Implementing MQTT Client

The MQTT client library of choice for this project is paho-mqtt. To install:

python3 -m pip install paho-mqtt

In order for the mqtt script to execute without errors, the program requires a .env file directory in the following format:

BROKER_ADDRESS=<broker ip address/host name here>
BROKER_PORT=<broker mqtt port>
CLIENT_ID=<client id>
CLIENT_PSSWD=<client-password>

About

Using dht22 temperature and humidity sensor w/ raspberry pi zero w. Includes script to periodically send sensor data to a MQTT broker

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published