Reading data from an atmospheric sensor: Difference between revisions

From Sensors in Schools
Jump to navigation Jump to search
No edit summary
No edit summary
Line 57: Line 57:


[[File:Screen Shot 2021-12-22 at 8.54.10 pm.png]]
[[File:Screen Shot 2021-12-22 at 8.54.10 pm.png]]
* The response in the Python Shell should be '''[200]'''.
* A value of [200] means that the request was successful.
* Other numbers or errors will indicate that there is a problem either with the Python code or with communication with the sensor.
[[ile:Screen Shot 2021-12-22 at 8.57.30 pm.png]]
=== 3. View sensor data in JSON format ===
* We can now add some more code so that we can look at the actual data.
* Add the statement '''print(r.json())''' and run the code.
* The method json() means that the data will be presented in '''JSON format'''.
* JSON stands for JavaScript Object Notation.
* It is an open standard to help format data for data exchange between computers and it uses human-readable text.
* The data is stored in '''attribute-value pairs''' similar to the format of a '''dictionary''.
[[File:Screen Shot 2021-12-22 at 9.03.25 pm.png]]
* You can see the data output in the python Shell Window.
[[File:Screen Shot 2021-12-22 at 9.04.14 pm.png]]

Revision as of 10:07, 22 December 2021

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

Adding Comments within files

  • We can add notes within our program called Comments.
  • Comments always start with a # (hash symbol) at the start of the line.
  • Computers ignore comments because comments are only read by humans.
  • To signal that the text has been commented out, the text colour will change to red.
  • In this example the comment is the file name.

  • To save the new file, select File > Save As

  • Navigate to your working directory and then save the file as atm_sensor_get.py
  • In Linux we never use names that include spaces.
  • Replace spaces with either the _ (underline) symbol or the – (hyphen) symbol.

Request data from sensor using python

  • Enter the following lines of request to request data from the atmospheric sensor.
  • The key items in the code are described:
    • import requests – this statement imports a python library so that the python program can get data from the internet.
    • r = requests.get(URL) – this statement gets the data from a specified URL and stores the result in the variable object named r. URL is short for Uniform Resource Location, also known as a web address.
    • print(r) – this statement prints a code that determines if the request was successful or not.
  • Save and the Run the program.

  • The response in the Python Shell should be [200].
  • A value of [200] means that the request was successful.
  • Other numbers or errors will indicate that there is a problem either with the Python code or with communication with the sensor.

ile:Screen Shot 2021-12-22 at 8.57.30 pm.png

3. View sensor data in JSON format

  • We can now add some more code so that we can look at the actual data.
  • Add the statement print(r.json()) and run the code.
  • The method json() means that the data will be presented in JSON format.
  • JSON stands for JavaScript Object Notation.
  • It is an open standard to help format data for data exchange between computers and it uses human-readable text.
  • The data is stored in attribute-value pairs' similar to the format of a dictionary.

  • You can see the data output in the python Shell Window.