Introduction: Complete Raspberry Pi Weather Station
This is my very first Instructable! In this tutorial I will guide you to make a Raspberry Pi weather station with its own database and website. I made this weather station in context of a school assignment, I got my inspiration on Instructables. The weather station can measure temperature, humidity, barometric pressure, wind speed and light level in percentage. All the samples that the Raspberry Pi gathers will be stocked inside a MySQL server on the Pi itself and will be displayed on a webserver!
Step 1: Gather Your Materials
The materials of this station are very simple. You will need all the appropriate materials, sensor and a housing.
- Raspberry Pi
The type doesn’t matter much, you can even use the Raspberry Pi Zero W but make sure not to take the first revision because you will need a network connection for the webserver. In this Instructable I will use the Raspberry Pi 3.
- Some jumper cables
You will need some cables to connect all the sensors and chip with your Raspberry Pi. There are three types of jumper cables: the male to female, the male to male and the female to female. You will need around 15 of the male to female and the male to male type. Either way it wouldn’t hurt to get all three of them.
Doing electronics without a breadboard is unwieldy. If you plan to do more DIY electronics this will always come in handy.
- Temperature and humidity: Grove Temp&Hum v1.0
- Barometric pressure: Grove – Barometer Sensor BMP280 (this sensor also captures temperature)
- Wind speed: Infrared Sensor Module (FC-03) LM393
- Light: Grove light sensor (note: this is an analog sensor, an analog to digital converter such as a MCP3008 is necessarily).
The housing is a very important part of you project. Here you will place all of your electronics and sensors. The housing doesn’t need to be pretty but it sure can be. In this Instructable I will make a birdhouse with a compartment in the bottom where I can store my Raspberry Pi.
You can always chose for a less laborious housing such as a white electronics box. The only condition is that there has to be vent holes so wind and air can breeze past the sensors, otherwise you will not get accurate samples.
Step 2: Connect All the Component
Once you have all of your components, you can start by making a test setup. Here you connect all of your electronics to your Raspberry Pi. You can find the Fritzing schematic in the files. When all of the connections are made you can start by setting up your Raspberry Pi.
Step 3: Setup Raspberry Pi
If this is your first time working with a Raspberry Pi I highly recommend you to go visit the raspberrypi.org website, it has some great documentations and tutorials for beginners.
Make sure you are running the latest version of Debian. You can upgrade by typing in the terminal in Raspberry Pi or in a SSH session:
sudo apt-get update && sudo apt-get upgrade
Step 4: Install SPI and I2C
Once you are fully upgraded, we will have to edit some configurations on our Raspberry Pi. All the different sensors except the light sensor and the infrared speed sensor utilize the I2C protocol. If you want to use this protocol along with the SPI interface you will need to enable this in the settings of the Raspberry Pi. You can setup the SPI and I2C interface by following these commands.
Enable both SPI and I2C. Then reboot using:
Once rebooted, we are going to check if “dtparam=spi=on” and “dtsparam=i2C_arm=on” is inside the /boot/config file. When you have found these lines you will have to uncomment these.
sudo nano /boot/config.txt
Exit the editor using ctrl + x and save.
Now we are going to install the different libraries for controlling the sensors.
sudo apt-get install python3-spidev sudo apt-get install python-smbus sudo apt-get install i2c-tools
Step 5: Install MySQL
Once you have setup the basics of the Raspberry Pi such as connecting to the internet and updating the software. We can start setting up our database system where we will store all of our weather data. We will use MySQL. This is an easy to use database system where we can connect multiple tables with each other using relations. To install MySQL type in the terminal:
sudo apt-get install mysql-server sudo apt-get install mysql-client
During the installation you will get prompted to fill in a password for the root user. You will need this password later on. Once these packages are installed you can check the status of you MySQL server by typing:
mysql -uroot -p status
Step 6: Running the Database Script
Once the MySQL server is working, we can run the database script. This script will create a model with different tables in it. Here we will store all the date the sensor captures and all the different setting the website utilizes.
To run a MySQL script from the Pi, we first need to copy the script to the Raspberry Pi. FileZilla is a great way to copy files between your PC and your Pi. Here is a great guide how to do so.
Once the script is on your Pi you can run it by typing in the terminal:
mysql -uroot -p source /path/to/script.sql
Step 7: Installing the MySQL Connector for Python3
We want to connect our database to the webserver who runs on Python3. To install this connector you run this command.
sudo apt-get install python3-mysql.connector
Step 8: Install Flask
The webserver uses Flask. This microframework is highly customizable and easy to use. Perfect for our weather station. To install flask type this command in a terminal window.
sudo apt-get install python3-flask
Step 9: Copy the Webserver on Your Raspberry Pi
Every package is now installed and everything is setup. Now we can copy the code from GitHub. There are two ways you can get the code on your Raspberry Pi: you can either download the master zip file and copy it on your Pi with FileZilla or you can clone the repository directly on your Raspberry Pi. To clone the repository you type these commands on your Pi.
cd /path/you/like/ git clone https://github.com/LanderMoerkerke/WeatherStation
Now you should have a new directory called WeatherStation. You can check this with the following instruction:
Step 10: Edit the Database Class
All of the code is now on your Raspberry Pi. Before we can test, we need to configure the MySQL-connector. The settings of the connector are stored inside the newly made directory. To go to the file we need to change our current directory. Once we have located the file we are going to insert our root password of our MySQL server inside the file. You can do this by following these commands.
cd WeatherStation/Flask/Database/ nano pswd.py
You can now edit the file. Replace “your_password” with your root MySQL password. Now we are ready to test the code.
Step 11: Testing
Now that everything is finally installed and wired up, we can start testing. Go to the Flask directory and type in the command:
Everything should start. Now you can go to you website by typing in your address bar: http:IP_RASPBERRY:5000/.
Step 12: The Website
When you open the site for the first time you will see a login screen. You can enter the site if you use as username ‘Lander’ and password ‘Test12’. The website is written in Dutch, you can translate the website if you have any knowledge of web development.
Step 13: Put Everything in the Enclosure
Take your housing and position the electronics so there are evenly spaced out. I will use my newly made birdhouse, I included the design down below. It is a simple box with a false bottom to store the Raspberry Pi.
Note: there always has to be a stream of airflow for the humidity sensor to work properly. Once you are satisfied with the result you can close everything and the weather station is finished. You can now place it somewhere you like and collect the weather data.
Step 14: Your Weather Station Is Working
Congratulations your Raspberry Pi is now fully functional. Place it somewhere in the open and collect the data!