Attempting to make a novice weather station using RasPi 4, DHT22 sensor, and Python.

Vincenzo Scotto Di Uccio
5 min readOct 1, 2021

--

This is my first time not only writing an article about my project but also the first time I work with a Raspberry Pi.

To start, you’ll need the right equipment. The easiest way to get your hands dirty tinkering with a RasPi is by buying the CanaKit from amazon. It comes with everything you need to get yourself up and running as a beginner, and includes loads of important information like the schematic for the pins. The microSD card also comes pre-imaged so you do not need to do that yourself which saves some time. You can purchase the one I used here.

CanaKit for the RasPi 4

You’ll want to put the RasPi in the case before starting to attach all the peripheries. I built it by first attaching my keyboard, mouse, and monitor. Once those were in, I attached the heat sinks like it is described in the quick-start guide. I added the power using the USB-C that comes with the kit. Then I installed the microSD card. Finally, I turned on the switch and followed the on-screen process. The RasPi should display a red light once it is powered on, if that does not happen, make sure that everything is plugged in snug.

RasPi fully connected and turned on.
RasPi fully plugged in. I took the top off to show the board.

Once the screen is on you will see something similar to this:

OS list on RasPi start up

Here you can pick which OS you want to download. Keep in mind the amount of space you have relative to the OS size. I also connected to wifi from this menu before moving on to the download process of the OS. Once you are happy with the selection click next and you will see this page:

RasPi wizard

The entire process will take some time but once completed you will get another wizard, this article here shows exactly what it looks like and what each step means. It also has more information on packages that can be installed on RasPi, they are interesting but not my focus.

At this point you should have a RasPi that is fully functional.

Now it’s time to download python(I used 3.7), but before doing that — you need to install all the dependencies that python needs in order to function as intended.

Open a terminal and type these commands:

  • sudo apt-get upgrade
  • sudo apt-get install -y build-essential tk-dev libncurses5-dev libncursesw5-dev libreadline6-dev libdb5.3-dev libgdbm-dev libsqlite3-dev libssl-dev libbz2-dev libexpat1-dev liblzma-dev zlib1g-dev libffi-dev
  • wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tgz
  • sudo tar zxf Python-3.7.0.tgz
    cd Python-3.7.0
    sudo ./configure — enable-optimizations
    sudo make -j 4
    sudo make altinstall

These commands essentially upgrade your Pi system, download the prerequisites for python, download python, and finally builds it by extracting it from the .tgz

Once this is all complete, the next step is to attach the sensor onto the RasPi board. You will connect pings 1, 6, and 7 which equate to power, ground, and data respectively. The sensor itself will have “+”, “out”, and “-” which correspond to power, data, and ground respectively. Make sure to get the wiring right or you will potentially fry your sensor (like I did 😊)

It should look something like this (Note I bought the DHT22 that already had the resistor attached so I did not need a breadboard, here):

The pins circled are the ones you want to use.

Once this is attached, there are multiple ways of getting the python package that will help us interact/use the sensor, I did this:

  • sudo pip3 install Adafruit_DHT

Be mindful that this repo is deprecated so do not expect any updates. There is a newer version of this library but I have yet to play around with it, you can find it here.

From here we can use python to use our sensor to collect temperature and humidity data.

Code to collect temp and humidity

The code above is all you need to get the humidity and temperature readings. If you have the sensor installed as I do then you can easily apply this to your own sensor. If not, then you need to make sure your “out” pin number is correct.

You might get an error here about incompatibility or cannot import name “Beaglebone_Black_Driver”. I solved it by adding this elif statement to the platform_detect.py that exists within the Adafruit_DHT folder. On line #112:

elif match.group(1) == ‘BCM2711’: return 3

The output of the script

Here is the output of our script! I have it set to every 5 seconds we get a reading!

The last step for part 1 is to enable SSH. To do this you go to preferences>Raspberry Pi Configuration>Interfaces> SSH. Make sure the button is on “Enabled”.

Once it is, you will need to get the IP address and then SSH into the RasPi

  • hostname -l
  • ssh pi@[raspberrypi_ip_address]

Now we can access our novice weather station from another machine!

I hope this article was useful in your own application. My personal next steps are to allow access from any network, expand the type of data collected, and storing that data.

--

--

Vincenzo Scotto Di Uccio

Creating useful data science solutions one line of code at a time.