Intro: Weatherstation Deluxe With Raspberry Pi and Mysql
For the curriculum Project we received an assignment to make something to end the course.
We had 3 weeks to finish it and present it , now I will tell you what I made and how I made it.
I decided to make my own weather station, on a small scale. It works very simple, a couple of sensors connected to a raspberry pi send data to a MySQL database that is hosted on the PI. The webserver which is also hosted on the RPI shows the latest data an past data in a chart.
All the software is available on Github.
I hope everyone learns from it and enjoys it as much as I did.
Step 1: What Do You Need/Prep
- Raspberry Pi 3 Model B/B+, network cable and 8G micro SD card
- Rainsensor module
- some jumper wires
- 16x2 LCD display
- 4x 1k resistors
- 4x 470 resistors
- 9v Battery and a way to connect it
- Anemometer wind speed sensor with analog voltage output
- An Adafruit SI1145 light/UV sensor
- An Adafruit BME680 sensor(I used this one but you can get easier ones to use)
- Some sort of waterproof casing
- Soldering Iron
- Some dubbel sided tape
Step 2: Setting Up Your RPI
First you'll need some key software and edit some config files to get you RPI perfectly working.
When I made my project is used my RPI running raspian stretch, there are a lot of tutorials online to get it setup so you have a clean install ready and you have successful network connection.
- Type sudo raspi-config and go to interfacing options in the menu, enable i2c, spi.
- Type in the command sudo apt-get update && sudo apt-get upgrade and let update itself for a while
Now you will have to install some key software you your RPI, type in these commands to download the necessary packages.
- sudo apt-get install python3
- sudo apt-get install pip
Before we get actually started doing the building, we'll make sure our RPI has a steady WIFI connection
Type sudo nano /etc/wpa_supplicant/wpa_supplicant.conf
In this file we'll add the following lines of code
if you want you can also add a static IP like this
Type sudo nano /etc/dhcpcd.conf
static ip_address=YOUR STATIC IP example:192.168.0.23/24
Step 3: MySQL
First installing MySQL server
Type sudo apt-get install mysql-server This is the server
Type sudo apt-get install mysql-client This is the client
You'll be asked to enter a password for the root user, DO NOT FORGET THIS, you'll need it in the future.
To use python in combination with MySQL you'll need to install some other stuff
sudo apt-get install python-mysqldb
sudo apt-get install python-mysql.connector
Now we have MySQL installed on our RPI. Next step we'll use PHPMyadmin to manage our database.
Step 4: PHPMyAdmin
Firstly we'll install a webserver on our RPI
Type sudo apt-get install apache2 php5 libapache2-mod-php5
Follow that up with sudo apt-get install phpmyadmin
After you have done that you'll have to follow some steps, it will ask you what webserver you want to run phpmyadmin.
Now you need connect the database.
After that you'll have to enter the same password you used for MySQL ROOT
Now let it finish.
You'll have to setup Apache to use our PHPMyAdmin installation
Type sudo nano /etc/apache2/apache2.conf
At the bottom of the file add include /etc/phpmyadmin/apache.conf
Save the file and restart Apache2
Type sudo /etc/init.d/apache2 restart
You should be able to acces PHPMyAdmin from the browser now.
Test this with typing 192.168.0.23/phpmyadmin in the browser address bar.REPLACE THE IP WITH YOUR RPI's IP address
Step 5: PyCharm Setup
Its time to setup PyCharm so we can connect with our RPI
- Go to file --> settings --> Build, Execution, Deployment --> Deployment
- Create a NEW SFTP connection
- SFTP host = Ip address of the RPI
- port = 22
- root path /home/USERNAME
- username = username of the root user of your RPI
- auth type = password
- password = password of the root user of your RPI
if that worked then you're already halfway
Go to file --> settings --> project:PROJECT NAME --> project interpreter
Cogwheel in the upper right corner: Select Add remote
Select create copy of this deployment server in the IDE settings
Python interpreter path = pick the folder in which python3 is installed --- most likely is /usr/bin/python3
Now you upload your project files to your RPI
Step 6: MySQL Database
Now its time to create our database, we'll create our database in the PHPMyAdmin interface
- Select new, then create a database, I suggest you call it WeatherstationDeluxe to reduce name error later.
- Create 2 tables, 1 named Sensor and the other named History
- Add fields as you can see in the included picture make sure you get the fieldtypes right, do this for both tables
Step 7: Building
Now the assembly part, I suggest you use a bigger case then the one I used in the picture.
LCD in 4 bit mode
- GND to Ground
- Vcc to 5v
- VE pin can be connected to a trimmer or use some 1k resistors to connect it
- RS pin to GPIO pin 26
- RW pin to Ground
- E pin to GPIO pin 19
- D4 pin to GPIO pin 13
- D5 pin to GPIO pin 6
- D6 pin to GPIO pin 5
- D7 pin to GPIO pin 20
- vin to 3v3
- GND to Ground
- SCK to SCL
- SDI to SDL
- vin to 3v3
- GND to Ground
- SCL to SCL
- SDA to SDL
- VDD to 3v3
- VREF to 3v3
- AGND to Ground
- CLK to SCLK
- Dout to MISO
- Din to MOSI
- CS to CE0
- DGMD to Ground
- Blue wire to MCP3008 CH2 pin
- Brown wire to 9V
- Black wire to PI GND
- 9V ground also to PI GND
We connected the rain sensor analog
- + pin to 5v
- - pin to ground
- S pin you have to voltage regulate to 3v3 and connect it to MCP3008 CH0
Step 8: Upload Code and Run on Boot
After you have uploaded the code which you can find on my Github and tweaked it a bit, to change some details such as the MySQL db settings in the code. It should work fine, but not automatically on boot
TO RUN ON BOOT
Type sudo nano /etc/rc.local
at the bottom of the file at this
python3 /home/pi/.../weatherstation_flask.py &
... = the absolute path
You must also add the 3 datalog files in
sudo nano crontab -e
so they will collect data independently, you can choose the timing yourself.