Saving sensor data to a file: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
|||
| Line 93: | Line 93: | ||
[[File:Screen Shot 2021-12-23 at 7.01.31 pm.png]] | [[File:Screen Shot 2021-12-23 at 7.01.31 pm.png]] | ||
== Saving data to a file == | === Saving data to a file === | ||
* Data can be saved in a text file so that we don’t lose the data, and so that other programs on our computer can access the data. | * Data can be saved in a text file so that we don’t lose the data, and so that other programs on our computer can access the data. | ||
* With bigger data sets you can also save data in a specialised database or data warehouse. | * With bigger data sets you can also save data in a specialised database or data warehouse. | ||
Revision as of 08:04, 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.
3. Adding spaces between data values
- The issue with the format of this data is that there is no gap between the data.
- To put a gap between the data we need to add a space using this additional code + “ “ +
- See the example below as a reference.
- The output of this program in the Shell is reproduced here.
Adding a human readable header to our data
- To make the data easier to read we can add an additional print() statement to act as a header for our data.
- print(“Temp, Pres, Bat”)
- With the additional print() header, it makes the data easier to read.
- We also remove the data = “” statement, because we don’t really need it in Python.
Comma separated data
- Rather than having a space separating the data it is better to use a comma (,).
- Replace all the “ “ code with “,” as in the example below.
- After saving and running the program you should get an output like the one below.
- This is an important first step in preparing data to be saved to a file.
Saving data to a file
- Data can be saved in a text file so that we don’t lose the data, and so that other programs on our computer can access the data.
- With bigger data sets you can also save data in a specialised database or data warehouse.
- Saving data to a file is much easier and it also allows us to more easily check the data for integrity.
- In this example we will be creating an empty file named data.txt and then saving new data to this file.
- Open the Terminal.
- When you open the Terminal you will be in your pi home directory. You should see the tilde (~) symbol.
- The path to the pi home directory is /home/pi yet for simplicity this is abbreviated to ~.
- Enter the command ls to list all the contents of the pi home directory.
- In my pi home directory, my project folder is named botanica-park-lake














