Real-time temperature and humidity monitoring system for Raspberry Pi and Python

  • Share this:
post-title
Welcome to my tech blog. Today, I'll show you how to build a real-time temperature and humidity monitoring system using Raspberry Pi and Python. The system collects environmental data through the DHT11 sensor and displays it on the screen in real time. First, you need to prepare the following components: 1. Raspberry Pi 2. DHT11 sensor 3. Connect the USB cable of the Raspberry Pi 4. Display (such as LCD screen) Next, follow the steps below: 1. Connect the DHT11 sensor to the GPIO port of the Raspberry Pi. 2. Write code in Python, read sensor data and update the display. 3. Connect the display to the Raspberry Pi so that temperature and humidity information is displayed in real time. In this way, you can monitor the indoor temperature and humidity changes in real time. Hope this introduction helps you!

Hardware related: Raspberry Pi and Python real-time temperature and humidity monitoring system.

In today's era of rapid technological development, smart homes and Internet of Things (IoT) devices are becoming more and more popular.

These devices not only improve our quality of life, but also allow us to monitor and manage the environment more effectively.

This article will describe how to build a real-time temperature and humidity monitoring system using Raspberry Pi and DHT11 sensors.

This system can be used not only in the home environment, but also in various scenarios such as agriculture and industry.

I. The required hardware.

1. Raspberry Pi.

The Raspberry Pi is a small, low-cost single-board computer that is ideal for DIY projects and educational uses.

Its rich interfaces and powerful computing power make it ideal for IoT projects.

2. DHT11 sensor.

DHT11 is a commonly used temperature and humidity sensor with the following characteristics: - Measuring range: temperature 0-50 ℃, humidity 20-90% RH - Accuracy: temperature ± 2 ℃, humidity ± 5% RH - Output form: digital signal
3. Other accessories.

-Several DuPont lines -A piece of bread board
II. Hardware connection.

The steps to connect the DHT11 sensor to the Raspberry Pi are as follows: 1. # Power connection #: Connect the VCC pins of DHT11 to the 3.3V or 5V pins of the Raspberry Pi, and the GND pins to the GND pins of the Raspberry Pi.

2. # Data Connection #: Connect the DATA pin of DHT11 to the GPIO pin of the Raspberry Pi (such as GPIO 4).

The specific connection method is shown in the following table: | DHT11 Pin | Raspberry Pi Pin | |------------|----------------| | VCC | 3.3V/5V | | GND | GND | | DATA | GPIO 4 (or other) |

III. Software installation and configuration.

1. Update the system and install the necessary software.

First, make sure the Raspberry Pi's system is up to date and that Python and related libraries are installed.

Open the terminal and enter the following command:


sudo apt-get update
sudo apt-get upgrade
sudo apt-get install python3 python3-pip python3-dev python3-rpi.gpio

2. Install the DHT11 library.

Next, we need to install the Python library for reading DHT11 sensor data.

You can use the DHT library provided by Adafruit, which supports a variety of temperature and humidity sensors, including DHT11.


sudo pip3 install Adafruit_DHT

IV. Write Python scripts.

Now, we can write a simple Python script to read the data from the DHT11 sensor and print it to the console.

Create a read_dht11.pyFile, and copy the following code into it:


# 导入必要的库
import Adafruit_DHT
import time

# 设置传感器类型和GPIO引脚号
sensor = Adafruit_DHT.DHT11
pin = 4

# 初始化变量
temperature = None
humidity = None

while True:
    try:
        # 读取传感器数据
        temperature = Adafruit_DHT.read_retry(sensor, pin)
        if temperature is not None:
            humidity = temperature[1] * 100  # 转换为百分比
            print(f"Temperature: {temperature[0]:.2f}°C, Humidity: {humidity:.2f}%")
        else:
            print("Failed to retrieve data from sensor")
    except Exception as e:
        print(f"Error: {e}")
    
    # 每隔5秒读取一次数据
    time.sleep(5)

Code description:.

-Adafruit_DHT.DHT11The sensor type is specified as DHT11.

\n-pin = 4Specifies the GPIO pin number that connects the sensor.

\n-Adafruit_DHT.read_retry(sensor, pin)Try to read data from the sensor and try again up to 3 times.

- If the data is read successfully, print the temperature and humidity; otherwise, print the error message.

\n-time.sleep(5)Make the program read data every 5 seconds.

V. Run the script.

After saving the file, run the script with the following command:

python3 read_dht11.py

At this point, you should be able to see output like the following on the console:


Temperature: 25.00°C, Humidity: 60.00%
Temperature: 25.05°C, Humidity: 60.02%
...

VI. Extended functions.

In addition to simply printing the data, we can send the data to the cloud for storage and analysis, or display the data in real time through a Web interface.

Here are some possible directions for expansion:

1. Data storage and analysis.

- # Database #: Use databases such as SQLite or MySQL to store historical data.

- # Data Analysis #: Use libraries such as Pandas to analyze data and generate charts and reports.

2. Web interface display.

- # Flask Framework #: Use Flask to build a simple Web server that displays temperature and humidity data in real time.

- # Front-end Technology #: Use HTML, CSS and JavaScript to beautify the interface to achieve a dynamic refresh effect.

3. Alarm system.

- # Threshold Setting #: When the temperature or humidity exceeds the set value, an alarm is triggered (such as sending an email notification).

- # Integrated other sensors #: Combine smoke sensors, light sensors, etc., to build a more comprehensive security monitoring system.

VII. Summary.

Through the introduction of this article, we learned how to build a real-time temperature and humidity monitoring system using Raspberry Pi and DHT11 sensors.

This project is not only simple and easy to implement, but also has strong practicality and scalability.

Whether it is used for home environmental monitoring or industrial applications, it can play an important role.

Hope this article is helpful to you, if you have any questions or suggestions, please leave a message to discuss!