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

By | January 20, 2020

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! ]

47 thoughts on “Raspberry Pi Project: Online Weather Data Log with MySQL and PHP with Sense Hat.

  1. E_404 Post author

    Why did u delete you weather files? Cant reach educ8ts.tv/weather

  2. Dorian Fernandez Fernandez Post author

    Can you do a security system with MySQL and PHP, Arduino and Rfid reader Rc-522?

  3. MKme Lab Post author

    Get stuff Sir! Yes on the tutorial for me. I work with SQL every day but haven't sent up my own database yet. Cheers

  4. .rpv Post author

    Good project but you should warn that your code didn't have any security measure and it's vulnerable to, lets say an sql injection or xss attacks.

  5. 2000jago Post author

    If you have not already, could you please build something using a rotary encoder to manipulate options on a lcd display. thanks.

  6. Rad Rad Post author

    Hi after some attempts it works. All Fields in the Database are of typ "int(11)" exclusively "date" The Field is a "utf8_bin"
    The Field "Id" has the function "AUTO_INCREMENT" an is the prim.key. The User and his Rights still create and that's it.I still enter the data without Pythonscript. This is the next Step.
    I am a Beginner so it was a real Challenge for me.

  7. Seddik Pilou Post author

    giving all the info and do the best as he can , I hope the best for you , very nice channel 🙂

  8. KaienSander10 Post author

    I have an idea!
    How to make a game console using a raspberry pi running RetroPie and a SNES controller

  9. George Graham Post author

    Can i do this with a DHT22 Sensor? or, can you make a tutorial using that sensor?

  10. سندس محمد Post author

    how if I need to send  data by button on the arduino uno to the web by php script please help me

  11. Ty Riggs Post author

    Can you at least give us the characteristics of the "date" field in the MySQL table? Using my limited knowledge of PHPadmin/MySQL, I managed to get the database created and running on the first try, however my Date & Time is showing all zeros. I've tried every iteration of TIMESTAMP, DATETIME, etc. Skimming the database creation kind of sucks. Without being able to create the database properly, this project is worthless.

  12. Christos Xenofontos Post author

    Hello how you create the table ? and where do you run it ? Is it connecting with the phpmyadmin server ?

  13. Keith Ng Post author


  14. Daniel Paulo Tipan Post author

    What if I only have a xampp on my computer? Can I still send the data offline? No webhost yet

  15. Vishal Prasad Post author

    I want to call a web api and retrive the data from there using a raspberry pi and then i have to display the data present in the web api on a led matrix..can help me out with this how to design this project?

  16. Elmware Post author

    You were saying that you need a Web server to connect to the senseHAT. Why not install Apache, or HTTPD, with PHP modules and MySQL, and PHPMyAdmin, on your Raspberry Pi, and connect to localhost?

    The Raspberry Pi Website has lots of info for setting these things up.

  17. M Arifin D Post author

    Amazing tutorial. Super useful. Thank god a person like you exist in this world. Keep on inspiring!

  18. Devtapra Bisnis Post author

    how about store data from Online website to Raspberry pi ? can u help me sir ?

  19. Bhargav Pattem Post author

    Please make a project that a web server to store the fingerprint using fingerprint sensor and gives access to those fingerprints from any fingerprint sensor using raspberry pi 3

  20. sonaiya hardik Post author

    Can i send the other sensors data as like you explain in video, to web page???

  21. Soon Chong Post author

    Hi there, I notice that you are using phpmyadmin to store your sql database, my question is are you able to retrieve the data from your database when your device (any device to open your weather log webpage) is not having the same internet connection with your raspberry pi?

  22. Mike Atencio Post author

    When you set it to 600 for the timer (10 minutes) can I set it to every second? What number would I use? 600 is 10 minutes, 60 is 1 minute, 6 is every second?

  23. Mike Atencio Post author

    Does the HAT sense relative humidity or just humidity itself. I'm studying ESD – electro-static discharge and need the sensor to capture RH – Relative Humidity. And can it be changed to Fahrenheit instead of Celsius?

  24. Edsel Calanoga Post author

    what are the pins that are being used by Sense Hat. Thanks for the Reply :)))

  25. Kirhim Lee Post author

    Great video please post the tutorial you mentioned in the video

  26. Nurul Nadia Abdullah Post author

    may i know when will the database video come out?

  27. JOSEPH GACER Post author

    may i know how to use a raspberry pi 3 to save counting data from machine using a sensor and save to index thank you

  28. trigoisabel Post author

    I love your tutorials, i'm doing a weather station myself, and your videos with the e-paper were inspiring. The quality of your tutorials and the fact that you use simpler language push me to keep doing more stuffs and wanted to learn more about electronics. Thank you!!!

  29. Eduardo Aguilar Post author

    I have a question I need to put all the sensors in an interface so I can see them all working how can I do it

  30. casual gamer Post author

    In which folder the php files should be stored?please help

  31. Mariam E Post author

    where should I run or save the connect.php file?

  32. Rhesa MR Post author

    The python code can't open the http url! please tell me how to open URL in python 🙁

  33. nat _tsha Post author

    Hi,can I get your email.I want to ask about my project. My project quietly same like you using webserver+raspberry pi.

  34. Ron Linder Post author

    Is there a way to get/post data from raspberry to/from a MySQL db hosted on a server? I want to create an app that will be on my pi and allowing the user to get/post data from a hosted MySQL db but unsure what language I should code it in…

  35. Ancy Jenifer Post author

    please upload how to install the database…please…

  36. ĐỨC NGUYỄN VIẾT Post author

    Hi Sir,
    At present, we would like to send data from sensor, plc via raspi to the server, after that we can build webserver for each application. My question is whether raspi can store data like dataloger before send data to the server and how to do that?

  37. barabba1977 Post author

    Good job!! Coul this project works with an old raspberry pi b+? Thanks

  38. Noormoy Foysal Post author

    how to add fingerprint authentication in laravel??

  39. Peter VanderMeulen Post author

    It looks like you are storing the database username and passport in the php file on the publicly hosted site. Is this secure? Is there a better way to restrict which users can access the database?

  40. Arlan Arota Post author

    gud day sir! can u give me some advice to our project , title of "INTELLIGENT STREET LIGHT using sensors . LDR,magnetometer and MEMS sensors and also GSM module . our problem is how to create a database that connected to the streetlights and triggered when the lamp is busted it will send sms to the PC of admin or user . I recently used vb.net and xampp . can you give some tutorial ? thanks .


Leave a Reply

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