This project creates a weather station using two Raspberry Pis and a DHT11 sensor. The end product runs a python script that uses sensor readings from the DHT11 sensor to get the temperature and humidity. To learn more about this, check out my post Building a Weather Station with a Raspberry Pi and Firebase.
The project also calls the National Weather Service APIs (available here) to provide local forecast data. If you want to use this in your area, you'll need to first call https://api.weather.gov/points/{<latitude>,<longitude>}
and then you can get the assoicated forecast
and forecastHourly
endpoints in the JSON response. You can learn more in my post here. For the purposes of this project, you just need to update the forecastEndpoint
and forecastHourlyEndpoint
values in the file at /frontend/src/app.js
.
One Raspberry Pi runs two python scripts in the "sensors" folder (I recommend using cronjobs to set them up to repeat). The two python scripts are:
weather.py
runs every 4 minutes and collections the current temperature and humidity and calls theapi/weather
function to update those values in Firebase.noaa.py
runs every 12 hours and calls the NOAA endpoints to get the forecast and hourly temperatures and then class theapi/noaa
function to update those values in Firebase.
A second Raspberry Pi runs the node server in the server.js
file at the project root. This serves up a React App that can be reached on port 1122. So if you run localhost:1122
on the browser on the Raspberry Pi running the frontend you should see the weather station successfully. The frontend calls the api/results
endpoint every 5 minutes to update the screen to reflect the values retrieved from Firebase.
To run this project, you'll need:
- Raspberry Pi Zero W
- Raspberry Pi Model 4
- Computer Monitor with HDMI input
- DHT11 sensor similar to the one seen here
- Mouse and Keyboard (optional)
- Firebase Account
For this to run on the Raspberry Pi, you need a desktop environment (recommend the Raspbian OS with Desktop. As of 07/20/2020 Raspberry Pi offers a downloader that streamlines this process and can be seen here.
Once you've got the initial desktop OS setup, I recommend using SSH to do most of the work with the pi directly.
- To copy files from remote to local system I followed this
scp -r <pi_user>@<pi_address>:<pi_folder> <local_directory_to_upload>
- To copy files from local to remote system:
scp -r <local_directory_to_upload> <pi_user>@<pi_address>:<pi_folder>
You'll need to setup the pi with the following:
- Depending on the OS you may need to install
pip
withsudo apt install python-pip
- In order for the DHT11 to work with your python program, you'll need to follow the instructions here
Then you can run the whole project by first running ./start.sh
and then open the browser to [http://localhost:1122(http://localhost:1122) and you're good to go!
In order to interact with Firebase's Firestore Database, I created an API that you can see in the functions
folder. This API has endpoints for saving and retrieving the Firestore information. If you want to build this project, you first deploy that to Firebase and then add the HTTP endpoints to the Python scripts in the sensors
folder and the useEffect
hook in the React project.
To learn more about this I recommend reading my post Building an API with Firebase.
If you want to run this for your own setup, you'll need to do a few things to get everything in order first.
- Create a Firebase account and deploy the serverless API within the
functions
folder (see Building an API with Firebase) - Modify the
noaa_endpoint
variable in thesensors/noaa.py
Python Script to be the hosted endpoint for the Firebase Function/api/noaa
route - Modify the
weather_endpoint
variable in thesensors/weather.py
Python Script to be the hosted endpoint for the Firebase Function/api/weather
route - Modify the
results_endpoint
variable in thesrc/App.js
file to be the hosted endpoint for the Firebase Function/api/results
route - Push the Python scripts in the
sensors
folder to a Raspberry Pi that is hooked up to a DHT11 sensor and run them with cronjobs (noaa.py
every 12 hours,weather.py
every 4 hours) - Push this project to a Raspberry Pi that is hooked up to an HDMI monitor and has Node installed and then run the
start.sh
shell script that is in the project root - Go to
localhost:1122
on the Raspberry Pi hooked up to an HDMI monitor and you're all setup!
I found the following very helpful when building this project: