All posts by texnic

Humidity and temperature monitor with Raspberry PI and DHT22 (DHT11, AM2302)


  • Raspberry Pi
  • DHT22, DHT11 or AM2302
  • Power supply
  • Female-female breaker wires

Software preparation

    1. Install Raspberry as described in another post.
    2. Update Raspberry: sudo apt-get update, sudo apt-get upgrade.
    3. Install necessary tools: sudo apt-get install build-essential python3-dev python3-openssl python3-setuptools git
    4. Download Adafruit DHT library:
      cd ~
      git clone
      cd Adafruit_Python_DHT
    5. Install Adafruit library: sudo python3 install


Before connecting anything to Raspberry, I prefer to power it down: sudo shutdown now.

DHT22 pin numbering is documented here. Raspberry Pi pins are explained here.

Connect DHT22 pins 1 (VCC), 2 (data) and 4 (GND) to Raspberry pins 3.3 V, GPIO 4 and GND respectively.

Note that for DHT22 module (bare PCB), an additional pull-up resistor of a few kOhm is required between pins 1 and 2.

For AM2302, red wire is Vcc (3.3 or 5 V), yellow is data, black is ground.

Once everything is connected, power up Raspberry.


Command line interface:

cd ~/Adafruit_Python_DHT/examples
sudo ./ 22 4

Here 22 is the DHT model (for DHT11 and AM2302, it would be “11” and “2302”), 4 is the GPIO pin of Raspberry we used previously.

The above command will print the measured temperature and humidity.

In Python:

# For other sensors, replace "DHT22" with "DHT11" or "AM2302"
# (By) Kotya Karapetyan, 2019-01-25.

from Adafruit_DHT import read_retry, DHT22
from time import sleep

PIN = 4

while True:
    h, t = read_retry(DHT22, PIN)
    print("Humidity: %.1f, temperature: %.1f" % (h, t))
    sleep(5)  # sleep 5 s (allowed min 2 s)

Run with python3 to output humidity and temperature every 5 seconds. Note that these sensors cannot output data more frequently than each 2 seconds (0.5 Hz).


Raspberry Pi-based home connection

I am starting up an attempt to create a connected home. Here I will record instructions for some less obvious (to me) actions, where I had to google for more than 2 minutes to succeed.

Set up headless Raspberry Pi

I want to use headless Raspberry Pi, that is no connected keyboard or screen. Connection will be done via SSH.

Create Raspbian SD

I only have an 8 GB micro SD card at hand, so I need the smallest image I can get without much hassle. Currently it’s Raspbian Stretch Lite (download page for the current OS version is here). Using the free Etcher, I burn the image to the SD card.

Enable SSH

To enable SSH connection, with the card still mounted in the Mac, create an empty file named ssh in the SD card root location.

Start up Raspberry

Connect power and network cable (not trying WiFi at the moment). Red and green LEDs (of Raspberry 3) should light up, then only the red one remains on (power on).

After a few moments (within a couple of minutes), the router map of connected devices shows “raspberrypi”, so I can get its IP address. In my case

Connect to Raspberry

To connect via SSH: ssh pi@ It will warn that the key is not known, and ask if I still want to connect. After confirming (I am relatively sure my home network is safe, so I am connecting to this Raspberry), it logs into the Linux CLI of Raspberry. Success!


Raspberry immediately warns me that the default password should be changed using passwd. Other worthy security-related recommendations can be found here.

Time zone

I noticed that the time in Raspberry (command date) was correct but the local time zone was UTC. It can be set using sudo dpkg-reconfigure tzdata.

Grafana and InfluxDB

sudo aptget update
sudo aptget upgrade
sudo apt-get install influxdb
At this moment works!

Grafana for RPi can be taken from

sudo dpkg -i grafana_4.6.3_armhf.deb


The installer tells how to run grafana:

sudo /bin/systemctl start grafana-server

    At this moment works (shows Grafana login page; use admin/admin to log in)

These two commands:

sudo /bin/systemctl daemon-reload
sudo /bin/systemctl enable grafana-server

might enable auto-start.


Install pip

sudo apt-get install python3-pip

Use Google Authenticator with Microsoft’s

Two-step authentication is the technology to use everywhere as much as possible, to prevent identity and data theft. Many online services, including Dropbox, LastPass, Facebook, are using Google’s Authenticator app. It generates random time-dependent one-time codes that should be entered in the process of the two-factor authentication.

Microsoft’s service also supports the 2-step authentication. However, by default Microsoft wants you to use its own app to generate the one-time codes. It does not make much sense to use it, if you are using Google Authenticator (GA) for all other services. Luckily, Microsoft is smart enough to support GA as well, just in a little hidden, even though simple, manner. Here is how to set it up:

  1. “Add a new identity verification app”
  2. Choose “Android”, press “Next”
  3. (If you now choose “Install the app from the Google Play Store.”, you’ll be offered to install a Microsoft app.) DO NOT DO IT.
  4. At the bottom, there is a line: “Android device not compatible? Try this app instead.” It is a link to the Google Authenticator. Click it.
  5. On the next page, a QR-code for Google Authenticator is shown. Scan it with “Set up account” function in Google Authenticator, verify the code and you are good to go.

Getting file checksum on Synology

cksum utility can calculate the file checksum using 3 different algorithms. The third one is the well-known CRC32.

  • CRC32 of a single file: cksum -o3 filename
  • CRC32 of all files in the current directory: cksum -o3 *

To verify the checksum in Windows, I recommend using HashTab. After installing it, just check the proerpties of a file and compare the CRC32 value returned by HashTab to the value calculated by cksum. Note that HashTab shows CRC32 as a hex number while cksum shows a decimal value; you can use an online converter to compare the two values.

Example (for some file on my Synology):

ds414> cksum -o3 mrr40320.JPG
946866417 14531 mrr40320.JPG

The first number returned by cksum is the CRC32 value, the second the file size in bytes.

Value returned by HashTab for this file is 387008F1 which is the same as 946866417 returned by cksum.

Does anybody know what the 1st and the 2nd algorithms of cksum are? 

Synology command line interface

  • Control Panel: User: activate admin account, make sure you know the password.
  • Control Panel: Terminal & SNMP: activate SSH
  • Download Putty (link), a popular terminal client for Windows.
  • Run Putty and enter the Synology IP address without the port, choose connection type SSH. The port will be set automatically by Putty.
  • Press Open. Putty will ask if you want to trust the Synology key. Press yes (make sure your network is safe, otherwise your admin password may be compromised).
  • Synology should ask for login and password. Use admin and the corresponding password.

Additional info:

Torrent for legal download

A lot of people associate torrents and pear-to-pear (P2P) technology in general with illegal music, film, and software downloads. Some internet providers even block the torrent protocol claiming that it’s illegal to use it.

Next time they do it with you, tell them: P2P is fully legal, and a lot of digital material can be downloaded via torrent. The advantage of using P2P instead of the direct download links is that reduces the load on the company server and may even help get the file quicker while also helping others. Be internet ecology-friendly.

I will be collecting here examples of what you can legally download via p2p. Please feel free to contribute in the comments.

Some sites also specialize on freely distributable material:

See also:


Useful links for OpenStreetMap beginners