Raspberry Pi and DHT22 Tutorial - Making a Weather Station - Jiuyu Zhang

Raspberry Pi and DHT22 Tutorial – Making a Weather Station

Project Publishing and Portfolio

Raspberry Pi and DHT22 Tutorial – Making a Weather Station

Preamble

In this article, you will see the process to setup your DHT22 temperature and humidity sensor

The DHT22 sensor is a humidity and temperature sensor that is enclosed in a small plastic “cage”. This cage protects the DHT22’s sensitive electronics from physical damage, especially the humidity sensor.

You will need:

  • A Raspberry Pi

  • A SD card
  • A DHT22 sensor

  • A breadboard or wires

  • A 10K resistor

Setup

Setting up your sensor is easy, only 3 out of the 4 pins on the DHT22 is needed. Refer to the diagram below or the table for connecting your sensor. You will also need a 10K resistor.

DHT22 Pins - From the RightRaspberry Pi Pins
11 (3.3V)
27 (GPIO 4)
46 (GND)
Note – Don’t forget to add the 10K resistor

Software

If you have already setup your Raspberry Pi, feel free to skip this section, but if you have not, please continue reading.

Plug your SD card into your computer’s SD card slot, note that there are 2 types of connectors, SD and microSD, check that your computer supports a microSD card. You will need to download 2 pieces of software, Etcher and NOOBS. Click on the link to get started, and download the version for your operating system.

  • Open Etcher and select from your hard drive the Raspberry Pi .img or .zip file you wish to write to the SD card.
  • Select the SD card you wish to write your image to.

Now wait for Etcher to finish, and unplug your SD card from your computer and insert it into the Raspberry Pi.

Power your Pi through the micro-USB or USB-C port, and connect the HDMI output to a monitor, TV or some other screen. Connect a mouse and keyboard to the USB port(s) of your Pi. You will be prompted to install an operating system, choose “Raspbian (FULL)”. The installer will install the necessary software and will prompt you to reboot. After rebooting, you should now be in a desktop environment. You will need to go through a basic setup, for your WiFi (if you haven’t already in the previous steps) and such. After this, you will need to reboot again. 

After the last reboot, navigate to the top “taskbar” and click on “Terminal”, refer to the icon below.

 

You will be prompted with a black window with white text. Type in “sudo raspi-config

Use your arrow keys to navigate to “Interfacing Options” and press enter. Select “SSH” and press “enable”. Now exit all menus to return to the black background.

Type in “ifconfig” to find your local IP address, it should start with “192.168.”, keep this for later use.

Now go to your computer, and download PuTTY. (Click on the link) Make sure that your computer is on the same network as your Raspberry Pi.

Open the executable application and you should be prompted with a window. In the “Host Name” box, enter in the IP address from earlier, and put “22” in the “Port” box.

Press “Connect”

You will now be prompted with a window and the text “Login as:”. Enter “pi” and press enter. 

Now you can enter in “raspberry” as your password, or if you chose a password earlier when installing the OS, enter that password and press enter.

NOTE TO BEGINNERS – The cursor will not advance forward with keystrokes, your keyboard is not broken, it’s just that way .that it is.

You are now logged into your Raspberry Pi remotely, congratulations!

ThingSpeak IOT Integration

ThingSpeak is a platform in which you can upload data through an API, and application programming interface, where your program can talk to ThingSpeak servers easily. You will need a ThingSpeak account setup for this tutorial, which is free.

Create a channel and name it with a descriptive title. Such as Raspberry Pi & DHT22 weather station or similar. Check the checkboxes next to field 1 and field2, and name them Humidity and Temperature respectively. After that, click “create”. 

You should now be prompted to an interface. Click on “API Keys” on the navigation tabs and copy your “Write API key” for the next section.

Code

Connect your Raspberry Pi via SSH, as outlined in the section before. 

Enter in “sudo apt-get install update” to update the system, and install nano via “sudo apt-get install nano

Enter “mkdir DHT22” to make a new folder, and “cd DHT22” to enter that directory. After that, execute “sudo nano test.py” to create a new document. Now copy and paste the code block underneath into the editor.

"""
test.py
Author: Jiuyu Zhang @ jy-zhang.com
"""
import datetime
import logging
import sys
import RPi.GPIO as GPIO
from time import sleep
import Adafruit_DHT
import urllib2

now = datetime.datetime.now()

logging.info(now.strftime("%Y-%m-%d %H:%M")+' Run Successfully')

myAPI = ""
def getSensorData():
   RH, T = Adafruit_DHT.read_retry(Adafruit_DHT.DHT22, 4)
   return (str(RH), str(T))
def main():
   print 'starting...'
   baseURL = 'https://api.thingspeak.com/update?api_key=%s' % myAPI
   RH, T = getSensorData()
   f = urllib2.urlopen(baseURL +
                               "&field1=%s&field2=%s" % (RH, T))
   print f.read()
   print(now.strftime("%Y-%m-%d %H:%M")+' Run Successfully')
   f.close()

# call main
if __name__ == '__main__':
   main()

Insert your API key from the previous section into the quote marks on line myAPI = ” “. Now press ctrl+x to exit, press Y and enter to save and enter again to save under the same file name.

Now execute the command “crontab -e” to manage your timings. Then enter in the block below.

* * * * * sudo python DHT22/test.py >/dev/null 2>&1

0 */2 * * * sudo reboot >/dev/null 2>&1

Cron is the timing manager for linux machines, in which your Pi is using. This script runs the test.py script every minute, to get readings and restarts the Pi every 2 hours, to keep it fresh, which is useful when running headless.

Congratulations, you now have a miniature weather station running, check ThingSpeak periodically for updates and see if your pi is functioning correctly.

One Response

  1. Wow! Great detail and thorough explanation. Looking forward to your next blog! Keep up the good work.

Leave a Reply