Raspberry Pi Project: Online Weather Data Log with MySQL and PHP with Sense Hat.


Dear friends welcome to another video! Today we are going to use a Raspberry Pi to
send weather data to a remote web server and store them into a MySQL database. Then we are going to display all the data
using a simple PHP script! Without any further delay, let’s get started! Hey guys, I am Nick and welcome to educ8s.tv
a channel that is all about DIY electronics projects with Arduino, Raspberry Pi, ESP8266
and other popular boards. Today’s project is very important and extremely
useful because we will learn how to store data online! I am using a Raspberry Pi 3 board along with
the Sense Hat. The Sense Hat is an add-on board for the Raspberry
Pi, which offers many sensors, including a temperature sensor, a humidity sensor, a barometric
pressure sensor and many more. I have prepared a detailed tutorial on this
add-on board a few weeks ago, you can watch it by clicking on the card here. The Raspberry Pi every 10 minutes, measures
the temperature, the humidity and the barometric pressure and sends the data via WiFi in a
remote webserver which stores them in a MySQL database. We can see all the data from anywhere in the
world simply by visiting a web page which displays all the data in a simple table. As you can see I have let the Raspberry Pi
to run for a couple of hours and I can see all the data along with the time of each measurement. Cool isn’t it? Let’s now see how to build this project. The hardware setup is really simple. All we need is a Raspberry Pi and a Sense
Hat board. The cost of the project is around $80. You can find links for the parts in the description
below. Let’s now see the software of the project. The code of the project consists of two parts,
the Raspberry Pi code, and the server code. First we are going to examine the Server Code. Of course, you must have a web server available. Since 2006 I use bluehost as my hosting
provider and I host all my websites there. If you don’t have a webserver available
with around 4$ per month you can have your own bluehost account. You can find a link for it in the description
of the video. Of course, you can use any other host you
like, I use bluehost for over 10 years that’s why I recommend it. For this project I have created a new folder
under my educ8s.tv website which is called “weather”. In this folder I have placed 3 PHP scripts. I have also created a MySQL database which
will store all the data. In the database I have created a table named
“data” which has 5 columns, the id of each entry, the date, the temperature, the
humidity and the barometric pressure. I won’t describe how I created the database
in this video because it takes time, you may not be interested and there are a lot of tutorials
online about this subject. But I would love to prepare a separate Database
tutorial. Do you want me to create a detailed tutorial
on how to create a MySQL database? Please vote by clicking on the card here. If there is enough interest I will publish
a video about that soon! The connect.php file is responsible for connecting
to the database. It contains the configuration settings for
the database. So we need to enter the Username, the Database
Password, and the hostname. In this line of code we have to enter the
database name. You have to edit this file in order to enter
your configuration settings if you build this project. The Raspberry Pi sends the data to a simple
PHP script which is called add_data.php. This script connects to the MySQL database
(using the connect.php script) and stores the data in the database. If we want to see the data from the database
all we have to do is to visit the index.php file from a browser. The index.php script will connect to database,
again using the connect.php script and it will ask the database to return all the stored
data. Then it will display all the data in an HTML
table. Now, let’s see the Raspberry Pi code. The code reads the temperature, the humidity
and the barometric pressure from the Sense Hat every 10 minutes. I tried to perform a simple calibration to
the sensors using a commercial weather station unit I own. For the temperature, since the Raspberry Pi
gets hot, I calibrate the readings of the sensor according to its CPU temperature. The hotter the CPU, the biggest the temperature
difference we get. Of course, the values we get are not very
accurate but close enough. After that, with this line of code we send
all the data to the server. As you can see we construct a web address
like this: www.educ8s.tv/weather/add_data?temp=20.1&hum=65.2&press=1013.5 We have included the temperature, the humidity
and the pressure values to the url. The add_data.php script at the server will
extract the data and it will save the data in the database. Notice that we don’t send the time of the
measurement. The add_data.php file automatically adds the
date when entering the data to database. That’s it, our setup is ready! As always you can find the code of this project
both the Raspberry Pi and the Server code in the description below. I have placed the file which is called weatherDataLogger
on the desktop so in order to run it all we have to do is to navigate to the desktop with
the command cd Desktop and run the code with the following command:
sudo python weatherDateLogger.py The program will print 4 values at the console
before sending the data to the server. The first value is the CPU temperature, then
is the temperature, the humidity and the barometric pressure. If we now visit the www.educ8s.tv/weather/index.php
website we can see that the values are now in the database! As you can see with this project we have moved
a step forward. We are now able not only to send data to a
remote webserver, but also to save them into a MySQL database. With this knowledge available we can build
more advanced and capable projects. This is just a demonstration of what we can
achieve. I used the Sense Hat in order to keep the
hardware and Raspberry Pi code simple and focus on the server code. Since the server code is now ready, we can
replace the Sense Hat with other sensors in order to reduce the cost of the project and
the accuracy. Even better we can replace the Raspberry Pi
with a Wemos D1 mini and have the cost of the project down to $10! Another option is to use the GSM shield and
send data to the server without WiFi from anywhere in the world using the GPRS internet
service! Amazing stuff! Stay tuned as many more projects like this
are coming! I would love to hear your thoughts on this
project. Do you find it useful and what kind of projects
do you plan to build that require an online database? Please post your comments and ideas below,
thanks! If this is your first time here, I would love
to have you subscribed. In this channel I post videos about DIY projects
every Saturday. I love making things and I believe that anyone
can make things, anyone can become a maker. That’s why I created this channel, in order
to share my knowledge with the community and learn from the community. I hope you will join us. Until next Saturday, Watch, Learn, Build! [ Translating these subtitles? Add your name here! ]

Danny Hutson

Leave a Reply

Your email address will not be published. Required fields are marked *