Introduction: RPi Weatherstation With Responsive Website

About: Student NMCT @ Howest Kortrijk, Belgium

For a school project we had to make a IoT device with a website for visualising the gathered information in a nice way.
I chose to make a weatherstation powered by a Raspberry Pi 3 running Flask for the responsive website, MySQL (MariaDB) for my database and a python script for gathering info with all my sensors.
It took me about 2 weeks from start to finish to complete.

We were encouraged to make an instructable to share our progress with the rest of the DIY community, so here it is!

Step 1: Part Selection, Tools and Materials

First I needed to figure out what kind of sensors were essential to a weather station.
I decided I wanted to measure all of the following data:

  • Temperature
  • Air pressure
  • Humidity
  • Windspeed
  • UV Index

Here are all the Tools, Materials and Parts I used.

Parts:

  • DHT22/AM2302 for temperature and moisture readings. ( 15 EUR )
  • Adafruit BMP280 for Barometric Pressure and temperature. (12 EUR )
  • Adafruit SI1145 for measuring the UV Index. (10 EUR )
  • Adafruit Analog Anemometer for measuring windspeed ( 50 EUR )
  • An MCP3008 for converting analog signals to digital.
  • 10kOhm Resistor as pull-up for my AM2302.
  • A 9V Adapter for 'powering' the Anemometer
  • A 5V Adapter for the Raspberry Pi
  • Raspberry Pi 3 (Any Pi should suffice)

Materials:

  • A plastic container for storing everthing and making it rain proof.

Tools:

  • Soldering Iron and Tin
  • Multimeter
  • Silicone
  • Some tape

So in total all sensors cost me about 85 Euro's, which is quite steep but I really wanted to include a proper windspeed meter so I think it's well worth it.

You can find a more detailed list with shops you can buy everything at, in the pdf below :)

Step 2: Connecting Our Hardware

Of course we are going to need to connect our sensors to our Raspberry Pi.
Above you can see the fritzing schematic you can follow to connect everything up properly.

On the schematic you can see a 9V battery is used as the power source for our anemometer, this is properly best used for testing only since it won't last too long, you can replace the 9V battery for any 7-12V power source you choose.

Our SI1145 and BMP280 sensors will both be controlled using the I2C protocol since this is the easiest to work with and needs less wires.

The anemometer on the schematic is shown as an LDR here since it has pretty much identical wiring as the anemometer and I couldn't find a real anemometer to put on my fritzing schematic :)

Step 3: Connecting Everthing : Setting Up the Pi

First of all, we have to make sure we are connected to the internet.

To do this in the terminal you can go to your wpa_supplicant file by running the following command:
sudo nano /etc/wpa_supplicant/wpa_supplicant.conf

In the file you can add this:

network={
ssid= "Your_Wifi_SSID"
psk="Your_Wifi_Password"
key_mgmt=WPA-PSK

}

-------------------------------------------------------------------------------

You can also choose to set your IP-address to static for easier access in the future.
to do this you need to go to the dhcpcd.conf file by running this command:
sudo nano /etc/dhcpcd.conf

Add this in the file:

interface wlan0
static ip_address=192.168.0.100/24

-------------------------------------------------------------------------------

Then we are going to make sure the packages that are already installed on our Pi are fully updated:

sudo apt-get update && sudo apt-get upgrade

This might take a while, so don't worry

-------------------------------------------------------------------------------

You will need to enable the I2C and SPI protocol inside raspi config.
you can this by running this command:

sudo raspi-config

Then go to interfacing options, and enabling both, I2C and SPI

-------------------------------------------------------------------------------

First you need to make a directory you want to put your project in (we will name it 'weatherstation'):

cd ~
mkdir weatherstation
cd weatherstation

Then we set up our python3 virtual enviroment:

python3 -m pip install --upgrade pip setuptools wheel virtualenv
python3 -m venv --system-site-packages env
source env/bin/activate
python -m pip install mysql-connector-python Flask flask-mysql mysql-connector-python passlib mysql-connector-python-rf

-------------------------------------------------------------------------------

Then we will need to install some other packages that are needed to make everthing function correctly:

sudo apt install -y python3-venv python3-pip python3-mysqldb mariadb-server uwsgi nginx uwsgi-plugin-python3

-------------------------------------------------------------------------------

Now we are going to make our database:

We still need to set up our database though.
You can do this by running the code/sql file located in the 'sql' folder like this:

sudo mariadb < sql/db_init.sql

The sql query will make the tables we need and also make a few users to make our database a little more secure.

This will also put some sample history data into our database to make sure our website displays everything properly when there is no real data gathered yet.

-------------------------------------------------------------------------------

To install Adafruit_GPIO and MyPyDHT you will need to do some more things.
First go back to your use folder and then:

git clone https://github.com/adafruit/Adafruit_Python_GPIO.git
cd Adafruit_Python_GPIO
sudo python3 setup.py install

cd ..
git clone --recursive https://github.com/freedom27/MyPyDHT
sudo python3 setup.py install

Step 4: Software / Code

We need to set up the back-end for the weatherstation, which includes:

- A mariadb database for storing my sensor readings and some other minor things
- A flask service for running the website.
- Another service running the Python file which reads out all sensors.
Above you can see my very simple database setup.
The users table is unnecessary, but since I wanted a login system because I (even though all the data is the same for all users) I decided to include in in my database.

-------------------------------------------------------------------------------

You can go ahead and clone my project's code from Github into your project folder.
Go to your user folder and run:
git clone https://github.com/BertVanhaeke/Weatherstation/ temp
mv -v temp/* weatherstation/

Then navigate to the conf folder in weatherstation and all of the files in the folder.

Change all occurences of 'USERNAME' to your username

You will also need to copy both of the .service files to systemd and test them like this:

sudo cp conf/weatherstation-*.service /etc/systemd/system/
sudo systemctl daemon-reload
sudo systemctl start weatherstation-flask.service
sudo systemctl start weatherstation-sensor.service

sudo systemctl status weatherstation-*


We then need to edit the nginx config.

sudo cp conf/nginx /etc/nginx/sites-available/weatherstation
sudo rm /etc/nginx/sites-enabled/default
sudo ln -s /etc/nginx/sites-available/weatherstation /etc/nginx/sites-enabled/weatherstation
sudo systemctl restart nginx.service
sudo systemctl status nginx.service


If everthing went well you should be able to run this and get some html printed out in the terminal:

wget -qO - localhost

-------------------------------------------------------------------------------

Everthing should be working fine now.
You can surf to your raspberry Pi's IP-address we set in the beginning and be greeted with a login screen.


Step 5: The Enclosure

Now that everything works, we need to put the whole thing into something.

I chose a simple plastic box with a transparant lid.
The anemometer is mounted on top of it, and so is a secondary tiny container which contains the DHT22 and BMP280 sensors.

These sensors are mounted inside a seperate container because they need to be in the open air (without being rained on), but the raspberry pi doesn't need to be.

As you can see I added some silicone around the edges to make it waterproof.
I also drilled some holes in the upper container to get fresh air in it.

--------------------------------------------------------------------

I hope you enjoyed my guide on how to build a weatherstation, it might be a little rough around the edges since it's my first time writing a guide like this, but I hope you liked it nonetheless :)