Building A Remote Temperature and Humidity Sensor Web-Service Using A DHT22 Sensor and a Raspberry Pi 3B+

From Nearline Storage
Jump to navigation Jump to search

Materials Required

  • Raspberry Pi 3B+ (Any Pi will do, as long as it has wifi for communications.)
  • Mini SD card, 8GB or larger (For this application there's no advantage to having an SD card that's any larger than the minimum requirement.)
  • DHT22/AM2302 temperature and humidity sensor (I assume that the 3-pin version of this sensor is being used here. There is also a 4-pin version available that may, or may not, require that a resistor be soldered in between two of the pins.)
If the DHT22 sensor does not come with jumper wires to connect it to the Pi included, then these must be ordered separately.
  • Block of wood for mounting (optional)
  • Small wood screws for mounting (optional)

Instructions

  • Setup the Raspberry Pi
    • These instructions provide guidance on formatting the SD card using a program on Mac or Windows. See this tutorial for instructions on how to do the same thing on a Linux computer.
  • We will ultimately want to run the Pi as a headless system, i.e., without a keyboard or display attached, so set up ssh for remote access to a terminal.
  • Make sure that the Pi has a static IP address on the network, and even perhaps a hostname.
If the Pi is to be accessed remotely, it must have an IP address that is known. Associating that address with a hostname makes things even easier. Typically these would be things that would be configured through the DHCP and DNS settings on the local network router. The exact precedure for setting this up varies so I won't try to cover it here.
  • Enable the ssh service on the Pi. In a terminal session enter:
$ sudo systemctl start ssh
$ sudo systemctl enable ssh
  • Optional: set up key-based login so that you don't have to enter the password every time.
  • With the Pi powered off, connect the DHT22 sensor.
There are three pins on the DHT22 sensor that must be connected to the GPIO pins on the Pi. Use the pinout command on the Pi to see a text-based diagram of the GPIO pin layout on the Pi.
  • Connect the "+" (power) pin on the DHT22 to one of the 5V pins on the Pi
  • Connect the "out" pin on the DHT22 to the GPIO2 pin, pin 3, on the Pi
  • Connect the "-" (ground) pin on the DHT22 to one of the GND pins on the Pi.
  • Power the Pi back on and add an entry for the DBUS "driver" for the DHT22 sensor to /boot/config.txt:
dtoverlay=dht11,gpiopin=2
  • The gpiopin value must match the pin number that the data line of the sensor is connected to. In this case that is the GPIO2 pin.
  • Insert this line near the bottom of /boot/config.txt, immediately below the line that says "[all] and above the line that says "^M".
  • Reboot the Pi after making this change. There will now be a set of files in the /sys/bus/iio/devices/iio:device0 directory that contain information from the sensor. Check the files that contain the temperature and humidity readings:
cat /sys/bus/iio/devices/iio\:device0/in_temp_input
cat /sys/bus/iio/devices/iio\:device0/in_humidityrelative_input
These commands should return a number that is 1000 X the temperature in degrees Celcius and 1000 X the relative humidity, a percentage. They will also frequently return I/O errors. This simply means that they are busy updating the value at the moment. Trying again a few seconds later should return the expected reading.
  • Create a web service on the Raspberry Pi that any computer on your network can query to get the current temperature and humidity.
    • Install a web server and the PHO scripting language.
    • Modify the web server's home page to turn it into the web service.