This application monitors the temperature and humidity of a DHT20 sensor connected to a Raspberry Pi, and regularly relays the data to an instance of the weather-server
This client HTTP POSTs data to the weather server for storage and display.
First set up an instance of weather-server to serve as the receiver and database for the collected temperature information.
See https://github.com/sd-hi/weather-server for more information.
For each instance of the client you want to set up, you will need:
- Raspberry Pi with Raspberry Pi OS installed (this documentation uses a Raspberry Pi 3B v1.2)
- DHT20 Temperature sensor)
- 4x female-female jumper wires
Connect the DHT20 temperature sensor to the relevant Raspberry Pi pins:
- Pi 2 - 5V
- Pi 3 - GPIO2
- Pi 9 - GND
- Pi 5 - GPIO3
For guidance on the pin numbering on your device, use command
pinoutTo see a diagram (more info can be seen in the GPIO Zero documentation)
For data consistency between sensors, set the timezone on the Raspberry Pi consistent with its location. For reference, the temperatures are sent to the server in UTC. The following command will set the device to the New York timezone.
sudo timedatectl set-timezone America/New_YorkTo see a full list of timezones, use command timedatectl list-timezones. This can be filtered with grep, for example timedatectl list-timezones | grep "America"
For the Raspberry Pi to read data via the pins, it is necessary to enable I2C interface on the device, this can be by altering the /boot/config.txt file:
sudo nano /boot/config.txtUncomment the following line by removing the preceding #
dtparam=i2c_arm=on
A reboot is required for this setting to take effect
sudo rebootFor Python to interact and read data from the DHT20 sensor, install the supporting Adafruit packages for AHTx0 sensors:
python3 -m pip install Adafruit_Blinka adafruit-circuitpython-ahtx0Download the weather-client script into the device's home directory:
cd ~
git clone https://github.com/sd-hi/weather-client.gitThis creates a ~/weather-client directory
Create the .env file by copying the .env.example file:
cd weather-client
cp .env.example .env
nano .envSet the appropriate environment variables:
-CLIENT_DEVICEID - A unique ID for this device, for instance sensor1
CLIENT_LOCATIONID- A unique ID to signify this device's location e.g.bedroom1SERVER_URL- The URL for theweather-serverinstanceSERVER_PORT- Port theweather-serveris exposed onSERVER_APIKEY- API key for theweather-serverinstance (consistent with itsAPI_KEYenvironment variable)POLL_INTERVAL- How often (in seconds) a temperature reading should be takenSENSOR_TYPE- Sensor type- Leave as
AHTx0to read from DHT20 sensor - Setting to
TESTwill avoid reading from the sensor and will feed temperature and humidity values for testing purposes
- Leave as
SUBMIT_INTERVAL- How often (in seconds) the server should submit collected readings
If the device loses network connectivity, it will continue to collect readings every POLL_INTERVAL seconds. When connection is restored all cached readings will be submitted in a single payload. Loss of power to the device will lose any cached readings not yet submitted to the weather-server.
Creating a service ensures that temperatures will continue to be collected and submitted following device reboot, without intervention from the user.
Create a weather-client.service service to be run by systemd
sudo nano /etc/systemd/system/weather-client.serviceUse the following, assuming weather client is in /home/pi/weather-client/ folder
[Unit]
Description=weather-client Service
After=network.target
[Service]
Type=simple
User=pi
WorkingDirectory=/home/pi/weather-client
ExecStartPre=/bin/sleep 10
ExecStart=/usr/bin/python3 /home/pi/weather-client/weather-client.py
Restart=on-failure
[Install]
WantedBy=multi-user.targetRefresh systemd and enable the service
sudo systemctl enable weather-client.service
sudo systemctl daemon-reload
sudo systemctl start weather-client.serviceUpon start of the service, the device should begin to submit readings to the weather-server!
If you need to stop service, use
sudo systemctl stop weather-client.serviceTo check status of service run
sudo systemctl status weather-client.serviceTo get log of service run
sudo journalctl -u weather-client.service