Reading data from an atmospheric sensor

From Sensors in Schools
Revision as of 09:44, 22 December 2021 by EdmondLascaris (talk | contribs)
Jump to navigation Jump to search

Overview

  • In this lesson we are going to write code that will allow us to download data from an atmospheric sensor.
  • The sensor will give us data on temperature, atmospheric pressure, battery voltage and signal strength.
  • We will use the python language to get this data.
  • Once we have data we can do other amazing things such as save the data and even graph the data.
  • Environmental data is very important because it allows us to monitor the environment we live in more closely.

Learning Objectives

  • Learn how to use the requests library in python to get data from a URL
  • Learn how to interpret data presented in the JSON format
  • Learn how to process data in python so that it can be saved in variables

Requesting new data from a sensor URL in Python

  • Sensors are now commonly connected to the internet.
  • This has given rise to the term – The Internet of Things.
  • If a sensor is connected to the internet then it will often have a URL (Universal Resource Locator) to allow us to interact with the sensor.
  • One common interaction is to be able to see or download data collected by the sensor.
  • To get data from a sensor we need to import the requests library in python.
  • In this lesson we will download some data from an atmospheric sensor.
  • The sensor data includes temperature, air pressure, sensor battery voltage and signal strength.
  • In this example we write a python3 program to download sensor data using the requests library.
  • The data will be presented in the python Shell.

Create a new file in python named atm_sensor_get.py

  • From the Raspberry Pi main menu drop down select Programming > Python3 (IDLE).
  • This will open up the Python Shell.
  • From the File drop down menu select New File.
  • This will open up a new file Editor Window.
  • We are going to name the file atm_sensor_get.py
  • As a placeholder we can add this file name within our new file.
  • We will however add a # (hash) in front of the name so that we comment it out.
  • When we comment out text in a file the computer will ignore it.
  • The line will only be useful for humans.
  • To signal that the text has been commented out, the text colour will change to red.