Holooly Plus Logo

Question 12.12: You want to log data measured with a sensor onto a USB flash......

You want to log data measured with a sensor onto a USB flash drive.

Step-by-Step
The 'Blue Check Mark' means that this solution was answered by an expert.
Learn more on how do we answer questions.

Write a Python program that writes the data to a file on a USB flash drive. By writing the file in CSV (comma-separated values), you can import directly into a spreadsheet, including Gnumeric on the Raspberry Pi (Recipe 4.2).

The example program will log temperature readings recorded from a DS18B20. So, if you wish to try this out, first build Recipe 12.9.

Open an editor (nano or IDLE) and paste in the following code. As with all the program examples in this book, you can also download the program from the Code section of the Raspberry Pi Cookbook website, where it is called temp_log.py.

import os, glob, time, datetime
log_period = 600 # seconds
logging_folder = glob.glob(‘/media/*’)[0]
dt = datetime.datetime.now()
file_name = “temp_log_{:%Y_%m_%d}.csv”.format(dt)
logging_file = logging_folder + ‘/’ + file_name
os.system(‘modprobe w1-gpio’)
os.system(‘modprobe w1-therm’)
base_dir = ‘/sys/bus/w1/devices/’

device_folder = glob.glob(base_dir + ’28*’)[0]
device_file = device_folder + ‘/w1_slave’
def read_temp_raw():
f = open(device_file, ‘r’)
lines = f.readlines()
f.close()
return lines
def read_temp():
lines = read_temp_raw()
while lines[0].strip()[-3:] != ‘YES’:
time.sleep(0.2)
lines = read_temp_raw()
equals_pos = lines[1].find(‘t=’)
if equals_pos != -1:
temp_string = lines[1][equals_pos+2:]
temp_c = float(temp_string) / 1000.0
temp_f = temp_c * 9.0 / 5.0 + 32.0
return temp_c, temp_f
def log_temp():
temp_c, temp_f = read_temp()
dt = datetime.datetime.now()
f = open(logging_file, ‘a’)
f.write(‘\n”{:%H:%M:%S}”,’.format(dt))
f.write(str(temp_c))
f.close()
print(“Logging to: ” + logging_file)
while True:
log_temp()
time.sleep(log_period)

The program is set to log the temperature every 10 minutes (600 seconds). You can alter this by changing the value of log_period.

Discussion

When you plug a USB flash drive in to a Raspberry Pi, it automatically installs it under /media. If there is more than one drive, then the program uses the first folder it finds inside /media. The name of the logging file is constructed from the current date.

If you open the file in a spreadsheet like Open Office, you will be able to edit it directly. Your spreadsheet may ask you to specify the separator for the data, which will be a comma.

Figure 12-20 shows a set of data captured using this recipe, and the resulting file has been opened with the Gnumeric spreadsheet running on the Raspberry Pi.

See Also

This program could easily be adapted for use with other types of sensor, such as light (Recipe 12.2) or acceleration (Recipe 12.8).

12.20

Related Answered Questions

Question: 12.11

Verified Answer:

Use the Tkinter library to open a window and write...
Question: 12.10

Verified Answer:

Use a low cost SR-04 rangefinder. These devices ne...
Question: 12.9

Verified Answer:

Use the DS18B20 digital temperature sensor. This d...
Question: 12.7

Verified Answer:

Use an MCP3008 ADC chip. However, unless you need ...
Question: 12.8

Verified Answer:

Use an analog accelerometer with a MCP3008 ADC chi...
Question: 12.6

Verified Answer:

Use a potential divider with one fixed resistor an...
Question: 12.2

Verified Answer:

Use the same basic recipe and code as Recipe 12.1,...
Question: 12.3

Verified Answer:

Low-cost resistive gas sensors are available that ...
Question: 12.4

Verified Answer:

The Raspberry Pi GPIO connector has only digital i...
Question: 12.5

Verified Answer:

Use a pair of resistors to act as a voltage divide...