Saving sensor data to a file: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
| Line 41: | Line 41: | ||
[[File:Screen Shot 2021-12-23 at 9.47.13 am.png]] | [[File:Screen Shot 2021-12-23 at 9.47.13 am.png]] | ||
=== Arranging numerical data in a String === | |||
* We can create (or define) a new empty variable named data to hold all the temperature, pressure and battery data using the statement '''data = “”''' | |||
[[File:Screen Shot 2021-12-23 at 6.48.21 pm.png]] | |||
* We need to add or concatenate all the data together into one long String. | |||
* To do this we need to: | |||
** convert the numerical data to String using the '''str() function''' | |||
** then add each data String together using the plus '''(+) symbol''' | |||
* The final code is presented below. | |||
* We also added a '''print(data)''' statement so that we can see data variable. | |||
[[File:Screen Shot 2021-12-23 at 6.50.05 pm.png]] | |||
* '''Save''' and '''Run''' the program. The result should be like the following. | |||
[[File:Screen Shot 2021-12-23 at 6.50.57 pm.png]] | |||
Revision as of 07:51, 23 December 2021
Overview
- In this lesson we are going to learn how to save our sensor data to a file.
- Saving data to a file will keep it safe.
- Once data is in a file, we can read the data again and make a graph.
- Reading, saving a graphing data is very important for all environmental and Citizen Science activities.
- Sensors often deliver data a numerical data. Some simple examples are temperature, time, speed, counts, etc.
- In this example we are going to save our data as a String (or text).
- Most files store data as text. In this example we also need to separate the data using commas (,).
- If data is separated by commas it makes each data element human readable and other programs, such as Spreadsheets, can also read the file more easily.
Learning Objectives
- Learn how convert numerical data into a String (or text)
- Learn how to save data to a new empty file
- Learn how to read a saved file and check for data integrity
Converting numerical data to a String
- In this example we will build on the python program from the previous lesson.
- Our python program was used to download data from a sensor (temperature, atmospheric pressure, battery voltage).
- We will now convert this data into a String and put all the data into a single variable named data.
- Each piece of data will be separated by a comma.
Open up the Python file named atm_sensor_get.py
- From the Raspberry Pi main menu drop down select Programming > Python3 (IDLE).
- This will open the Python Shell.
- From the File drop down menu select Recent Files.
- Choose the file named atm_sensor_get.py
- Edit the file so that it appears like the example below.
- Saving and Running the file should produce an output in the Python Shell like the output below.
Arranging numerical data in a String
- We can create (or define) a new empty variable named data to hold all the temperature, pressure and battery data using the statement data = “”
- We need to add or concatenate all the data together into one long String.
- To do this we need to:
- convert the numerical data to String using the str() function
- then add each data String together using the plus (+) symbol
- The final code is presented below.
- We also added a print(data) statement so that we can see data variable.
- Save and Run the program. The result should be like the following.







