Turn your Raspberry Pi into a mini weather station! This guide will help you set up an API to read temperature and humidity data from a DHT22/AM2302 sensor.
- Raspberry Pi Zero with Raspbian OS installed.
- DHT22/AM2302 sensor with 3-pin configuration.
- Internet connection.
-
Connect the DHT22/AM2302 sensor to the Raspberry Pi Zero:
- VCC to 3.3V or 5V
- Data to GPIO4
- GND to GND
-
Open a terminal on your Raspberry Pi.
-
Update your Pi:
sudo apt-get update
-
Install necessary packages:
sudo apt-get install python3-pip nginx
-
Install Python libraries:
pip3 install Adafruit_DHT Flask gunicorn
-
Set up the API:
-
Copy the
temp_humidity_api.py
to your desired directory; it is the only file you need. -
mkdir -p /home/pi/temp_humidity_api mv temp_humidity_api.py /home/pi/temp_humidity_api
-
-
Test the API:
python3 temp_humidity_api.py
Your API should now be running on
http://0.0.0.0:5000/metrics
.
-
Set up Gunicorn:
-
Gunicorn will serve the Flask application. Start it with the following to test from your API directory:
gunicorn -w 4 temp_humidity_api:app -b 0.0.0.0:8000
-
-
Set up Nginx:
-
Create a new Nginx configuration:
sudo nano /etc/nginx/sites-available/temp_humidity_api
-
Add the following configuration
-
Replace
your_ip_range
with your desired IP range, e.g.,192.168.1.0/24
for access -
Replace
your_pi_ip_address
with your Raspberry Pi IPv4 Address
server { listen 80; server_name your_pi_ip_address; location / { allow your_ip_range; deny all; proxy_pass http://127.0.0.1:8000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } }
-
-
Link the Nginx configuration and restart Nginx:
sudo ln -s /etc/nginx/sites-available/temp_humidity_api /etc/nginx/sites-enabled sudo nginx -t sudo systemctl restart nginx
-
-
Create a
systemd
service file:sudo nano /etc/systemd/system/temp_humidity_api.service
-
Add the following configuration
-
Replace
/path/to/your/directory
with the path to yourtemp_humidity_api.py
file -
Replace
/path/to/gunicorn
where gunicorn is located (trywhich gunicorn
) -
your_username
is the user account the API will run under. Don't use root!
[Unit] Description=Temperature and Humidity API After=network.target [Service] User=your_username WorkingDirectory=/path/to/your/directory ExecStart=/path/to/gunicorn -w 4 temp_humidity_api:app -b 0.0.0.0:8000 Restart=always [Install] WantedBy=multi-user.target
-
-
Enable and start the service:
sudo systemctl enable temp_humidity_api.service sudo systemctl start temp_humidity_api.service
The install_api.sh
script is designed to automate the setup process described above. It's especially useful if you're setting up multiple Raspberry Pis or want a quick, hassle-free installation. The idea is that you are installing on a freshly imaged Raspbian Linux image.
- Download or copy the
install_api.sh
script to your Raspberry Pi to the same directory astemp_humidity_api.py
. - Make the script executable:
chmod +x install_api.sh
- Run the script:
./install_api.sh
- You will be asked for two items
- The IP range to allow access to the API such as
192.168.20.0/24
or10.0.0.0/22
. The default is192.168.1.0/24
- The port to run the API on. The default is
8000
.
- The IP range to allow access to the API such as
- Efficiency: The script automates multiple steps, saving time.
- Consistency: Ensures the same setup process every time, reducing potential errors.
- Less Control: While the script provides some configurability, manual setup offers more control over each step.
- Understanding: If you're new to Linux or Raspberry Pi, running scripts without understanding them can be risky. Always review script contents before executing.
Once everything is set up, you can access the API using:
http://your_pi_ip_address:port/metrics
This will return a JSON response with temperature and humidity data.
[23:31:40] ~ $ curl localhost:8000/metrics
{"humidity":52.400001525878906,"temperature_celsius":21.899999618530273,"temperature_fahrenheit":71.4199993133545}
The Nginx configuration provided restricts access to a specific IP range for added security. Ensure you configure this range according to your needs. Do not expose this API to the Internet. Do not run this API as root.
The Python 3 script get_local_data.py
is provided to store temperature and humidity data locally in a subdirectory called temperature_data
. The script is designed to be run as a cron job to store data at regular intervals. The data is stored in a CSV file with the date and time as the filename.
This will save data every 5 minutes.
*/5 * * * * /usr/bin/python3 /home/pi/temp_humidity_api/get_local_data.py