Introduction: Intro: Raspberry Pi Weatherstation With Flask and Mysql
For the curriculum Project1 we had to make something to end the course.
I decided to make a weather station.
We had about 3 weeks to put it all together, so here is what I got.
I made the weather station, with a Raspberry Pi as base, an arduino Nano V3.0 and a couple of environmental sensors to get measurements.
Here are its features:
- Data gets exported to a Mysql database
- Webserver gets hosted on the Pi
- Webpage that shows the current data
- Webpage that puts the data from the last hour into a chart
All the software is available on Github
This project has been a great learning experience for me. It got me solving some unexpected problems.
I learned quite a bit about the capabilities of the Pi and how flask handles certain things.
Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.
Step 1: Materials
- dht11 Temperature and humidity sensor
- Rainsensor module
- BMP180 Digital barometer pressure sensor
- Arduino jumper cables
- 1K resistor
- Raspberry Pi 3 model B
- Arduino Nano V3.0
- Network cable (for testing)
- Usb mini B to usb-cable to connect the Arduino Nano to the Raspberry Pi
- Usb micro B to usb-cable to power the Raspberry Pi
- Glue gun
- Soldering iron
In total the materials will cost about 86 Euros.
Step 2: Arduino Circuit
Here are 2 photos included on how to create your Arduino circuit.
Step 3: Arduino Code
The Arduino code is included.
Step 4: Setting Up the Pi
Before we get started, we must make sure all updates and upgrades are installed.
But before we can do that we have to make sure our Pi has an internet connection.
Open the wpa-supplicant file in nano:
sudo nano /etc/wpa_supplicant/wpa_supplicant.conf
We add the following in that file
Enter the following command in your command prompt on your Pi:
sudo apt-get update
This may take a while, so get comfortable.
After the update we will upgrade
sudo apt-get upgrade
Step 5: Installing MySQL
First, we need to install the MySQL server.
sudo apt-get install mysql-server
In order to connect with the database, we also need a client.
Install the Mysql client with the following instruction:
sudo apt-get install mysql-client
You will be prompted to enter a password for the root user. Make sure you write this down as we will need to use this to access the MYSQL server and connect PHPMyAdmin to it.
Now if you want to access and start making changes to the database simply enter the following command:
mysql -u root -p
It will now prompt you to enter the password we just created.
You can now enter MYSQL commands to create, alter and delete databases.
You can leave the command line by simply entering quit
If you want to be able to interact with MYSQL in Python you will need to install the Python bindings as well. You can do this by entering the following commands.
sudo apt-get install python-mysqldb
sudo apt-get install python-mysql.connector
Step 6: Installing Raspberry Pi PHPMyAdmin
You will find that installing Raspberry Pi PHPMyAdmin is very easy to do and won’t take long at all. It is even faster if you have already installed the MYSQL database and Apache.
I will go on the assumption you have not yet installed apache and PHPMyAdmin. If you already have installed them just skip this step.
sudo apt-get install apache2 php5 libapache2-mod-php5
Now let’s install the PHPMyAdmin package, you can do this by entering the following:
sudo apt-get install phpmyadmin
It will now begin to install. You will be presented with a screen asking the type of web server you want it to run off. Select apache2 as this is the server we just installed.
Next we will need to configure PHPMyAdmin to connect a database. (The one we set up previously in installing the Raspberry Pi MYSQL step). To do this select yes at the next prompt.(see image)
It will now ask for a password, enter the one we set previously when we set up MYSQL.
Next it will ask you to set a password for PHPMyAdmin. You can keep it the same as the password to the MYSQL database or something different. Make sure you remember it as this is the password you will need to access PHPMyAdmin.
We will also need to setup Apache to include our PHPMyAdmin installation. To do this enter:
sudo nano /etc/apache2/apache2.conf
Now at the bottom of this file enter the following line:
Once done save & exit by pressing CTRL +X and then y.
Now simply restart the Apache service by entering the following command:
sudo /etc/init.d/apache2 restart
Now you should be able to access the PHPMyAdmin from a browser. To test go to the following address in your browser. (Replace the IP with your IP. If you don’t have it run the command hostname –I on your Pi)
Step 7: Linking Pycharm With the Raspberry Pi
Set Deployment Environment
In Pycharm, go to the default settings and choose Build → Execution → Deployment for Deployment
Please make sure that you enter the correct data.
The SFTP host is your Raspberry network address.
If you have made an extra user (not pi - raspberry), you can fill it in with your Username.
Then press Test SFTP connection.
If the connection is successful you can continue.
Click on the Auto Detector button below the Test SFTP button.
Click OK at the bottom.
Close the following window by pressing OK.
PyCharm will now connect with the external device and some "Pycharm helpers " will be uploaded to the raspberry.
By creating a deployment environment, PyCharm will know our external device and communicate with it.
Set Project interpreter in PyCharm
Press Ctrl + Shift + A and type "Project Interpreter" and press enter.
Click on the top right and select add remote.
Select create copy of this deployment server in IDE settings.
At the Python interpreter path, choose the folder in which python3 is installed.
Usually it is /usr/bin/python3
If you do not adjust this, you will hang on to a python2 interpreter.
After that, click OK a few times until you're out of the settings.
Now you can upload your project to the Raspberry Pi.
Step 8: Housing
I used a hard electrical case.
I glued my Raindrop sensor on top of the case.
Then I punched a hole through my case through which I put my wires to connect to my raindrop sensor and to power my Raspberry Pi.
I can then reseal the hole with some good old fashioned duct tape.
After making sure everything fits, I closed my case.
Step 9: MySQL Database
I included an ERD from my database, just recreate accordingly.
if you do not know how to, I will explain it in a few steps.
Select new, then create the database, mine is called "weerstation".
Create a table 'weerstation' with 6 rows.
Then recreate from the picture.
Repeat for our username table.
Step 10: The Code
For the code you need to make sure that everything is in the same folder.
You can get the code from Github.