Skip to main content
Ingenium Logo

You are leaving IngeniumCanada.org

✖


This link leads to an external website that Ingenium does not control. Please read the third-party’s privacy policies before entering personal information or conducting a transaction on their site.

Have questions? Review our Privacy Statement

Vous quittez IngeniumCanada.org

✖


Ce lien mène à un site Web externe qu'Ingenium ne contrôle pas. Veuillez lire les politiques de confidentialité des tiers avant de partager des renseignements personnels ou d'effectuer une transaction sur leur site.

Questions? Consultez notre Énoncé de confidentialité

Ingenium The Channel

Langue

  • Français
Search Toggle

Menu des liens rapides

  • Ingenium Locations
  • Shop
  • Donate
  • Join
Menu

Main Navigation

  • Browse
    • Categories
    • Media Types
    • Boards
    • Featured Stories
  • About
    • About The Channel
    • Content Partners

Project Log #3: PHP and Databases

Share
5 m
Aug 3, 2018
Categories
Earth & Environment
Categories
Engineering & Technology
Computing
Media
Blog
Profile picture for user Mohamed Ebsim
By: Mohamed Ebsim
Ingenium
Screen capture of python code

New week, new ideas.

In my last post about my adventures in building a weather station for the Canada Aviation and Space Museum, I explained how I got the sensors to read data (like temperature and humidity) and return it back to the Raspberry Pi. Unfortunately, this functionality is not yet at the point I want it. While I can see the data on a screen, I have no way of doing anything with it. I could only locally see the data in the Arduino serial monitor. The serial monitor is a display that shows data coming in via transmission from the Arduino to the Raspberry Pi. Naturally, the next thing to do would be creating a method to save the data as it comes in.

With my experience in the Python programming language, I felt it was ideal to use for this project. It turns out reading serial communication is a rather common thing throughout programming languages. That simplifies my job significantly. Using a resources I found online, along with my own knowledge of file writing in python, I made a simple program that would read data and add it to a simple text file.

Screen capture of chunk of python code that collects data and writes it to file

Chunk of code that collects data and writes it to file. The entirety of the code is available at https://github.com/mebsim/WeatherStation.

But a text file is not useful when the ultimate goal is to have the data be available live via the internet. The best way to make the data available on the internet is to first store it in a database. There are a variety of methods to display the data from the database, so I want to start as basic as possible, in order to allow it for multiple uses.

A popular thing in the Raspberry Pi community is to use it as a mini-server. Since it is a mini-computer and the model we have is Wi-fi enabled, it can be used in a variety of ways. What I decided to do was make an internal website (only accessible within the museum’s network) that displayed the data from a database on the Pi. After some research, it seemed that a mixture of MySQL and PHP: Hypertext Preprocessor (PHP)was the way to go. PHP is a form of scripting language that runs on a server. The other half, MySQL, is a very popular database management system.

I used this tutorial to figure out how to set it up, although there are plenty of them online. To sum up what is said in the tutorial, you go through and install the appropriate resources, do some light programing (see code below), and run the program. Just as a heads up; the tutorial used an outdated version of PHP to grab data from MySQL. In order to make it work, I had to update the PHP with the library PDO. I updated it using PDO with the help of this site. 

Screen capture of PHP code

Updated PHP code I wrote that would collect and display data from the database

The final thing I had to do was update the code that saves the data, in order to change the saving location from a file to the database. The tutorial above also contained a method to access the database. I incorporated it into my own program, with some minor changes, since again the tutorial was slightly outdated.

Screen capture of updated Python code.

Updated Python code that saves data to database. This was added to the Python code above.

This took a while, but at the end I had a small server showing the data to anyone who wanted to access it. It sets the foundation for future development. 

As we get closer to the end of my time with the museum, there is going to be a larger movement from programming to physical design. Stay tuned to read about our next challenge!

All the code that has been made can be found at my GitHub page: https://github.com/mebsim/WeatherStation

Tags
DIY, weather, Weather Station, Raspberry Pi, Server, code
Author(s)
Profile picture for user Mohamed Ebsim
Mohamed Ebsim

Mohamed Ebsim is an intern at the Canada Aviation and Space Museum, working under Science Advisor Jesse Rogerson. He is also a high school student at Ashbury College.

Related Stories

A spliced photo, from left to right: Shaun the Sheep in front of a model of ESA’s European Service Module, a top view into a red bucket containing thousands of light-brown, rod-shaped pellets, and a toddler wearing a wool hat and wool sweater holds a grownup’s finger.

3 things you should know about why wool keeps us warm, and about its surprising uses in the garden and in space.

A large impact crater viewed from the rim, a woodern spoon full of small yellow grains, a close up of a forearm being tattooed.

3 things you should know about the untapped potential of millet, the permanence of tattoos, and asteroid airbursts

The thirty or so Mexican peasants who helped clear the Bacubirito meteorite, not far from Bacubirito, Mexico, 1902. N. Rosst, “La grande météorite de ‘Bacubirito’ (Mexique).” La Nature, 14 February 1903, 173.

A blaze in the northern skies and a cinder of sidereal fire: The Bacubirito meteorite

A rear view of a person wearing a yellow coat and backpack in winter, a close-up view of bright red poinsettias with small yellow central flowers.

Two things you should know about the science of wind chill, and the Orion spacecraft's selfies.

Three images side by side, Canada’s White Glacier, dried mealworms shown on a round wooden platter, and a pair of hands rubbing together, covered in soap bubbles.

3 things you should know about insects as an important source of protein, the science superpowers of soap, and monitoring glaciers in Canada’s Arctic

Three images side by side, plastic-wrapped cucumbers, a woman with an inflamed shoulder, and the James Webb Space Telescope.

3 things you should know about plastic-wrapped cucumbers, the James Webb telescope, and inflammation

Three images side by side, grocery shelves full of eggs in clear trays, coral reefs seen from space, and a map of Canada divided into four differently coloured shapes.

3 things you should know about egg refrigeration, coral reef satellite maps, and watersheds

Autumn vista of a river winding between pine trees and snow-capped mountains.

AI-Generated sound therapy for critically ill patients

A wide view of the underwater room in the RIPPLE EFFECT exhibition; the walls, floor, and ceiling are blue and information about water conservation adorns the walls.

A refreshing approach: Students create virtual exhibition to inspire water conservation

A spliced, three-part image depicts several unwashed potatoes on a white background, several springtails on ice pellets, and a black and white view of Saturn’s crater-rich moon Mimas, dominated by a very large impact crater on the right.

3 things you should know about PEI potato wart, Saturn’s moon Mimas, and animals with built in antifreeze.

Tomanowos, better known as the Willamette meteorite, American Museum of Natural History, New York City, New York. Anon., “Ça et là, par l’image.” Le Samedi, 22 February 1947, 8.

Tomanowos, a visitor from the sky or Moon: A brief look at the largest North American meteorite known today

A spliced, three-part image depicts a green-and-red plastic gadget attached to a cow’s tail, a composite image showing hundreds of meteors, and a close-up of a pair of glasses sitting on top of an open book.

3 things you should know about tech-enabled cows, meteors, and presbyopia

Footer

About The Channel

The Channel

Contact Us

Ingenium
P.O. Box 9724, Station T
Ottawa ON K1G 5A3
Canada

613-991-3044
1-866-442-4416
contact@IngeniumCanada.org
  • Facebook
  • Instagram
  • Twitter
  • Channel

    • Channel Home
    • About the Channel
    • Content Partners
  • Visit

    • Online Resources for Science at Home
    • Canada Agriculture and Food Museum
    • Canada Aviation and Space Museum
    • Canada Science and Technology Museum
    • Ingenium Centre
  • Ingenium

    • Ingenium Home
    • About Ingenium
    • The Foundation
  • For Media

    • Newsroom
    • Awards

Connect with us

Subscribe to our newsletter to receive the latest Ingenium news straight to your inbox!

Sign Up

Legal Bits

Ingenium Privacy Statement

© 2023 Ingenium

Symbol of the Government of Canada
  • Browse
    • Categories
    • Media Types
    • Boards
    • Featured Stories
  • About
    • About The Channel
    • Content Partners