Skip to content

Datalogging with Python

Brendan Smith edited this page Sep 9, 2016 · 10 revisions

##Overview With a sensor and the time established, we have the groundwork necessary to start writing to sensor log files. For this, we need to get the time inside a python script and then write our information to a .csv file.

##Getting the time with Python Now, our Raspberry Pi knows the time without the internet, and we can use the python library datetime to get the current time of the day.

If unfamiliar with the Python interpreter, see Getting acquainted with the Python interpreter.

>>> from datetime import datetime
>>> datetime.now()
~ ~ ~
>>> datetime.now().date()
~ ~ ~
>>> datetime.now().date().year
~ ~ ~
>>> datetime.now().strftime("%Y_%m_%d")
~ ~ ~

Datetime will pull the local date from the computer, and store it as a "python object". From there, the various date fields (i.e. month, day of the week) can be accessed.

Strftime is short for "string format time" and is very commonly used amongst programming languages. It is not important to learn syntax, but Google, Stack Overflow, and the documentation will be very helpful to find a particular format.

##Python CSV Library

If unfamiliar, please see Opening files in Python.

Python CSV Library is very helpful for easily reading and writing to csv files.

Into a python interpretter:

>>> with open("sample_file.csv", "w") as sample_file:
...    csv_sample_file = csv.writer(sample_file)
...    csv_sample_file.writerow(["the", "first", "row"])
...    second_row = ["the", "second", "row"])
...    csv_sample_file.writerow(second_row)

>>>

Exit Python and enter into the terminal

cat sample_file.csv

We opened the csv file for writing, created a csv file "object", and wrote two rows.

##Writing the Datalogger Script To write the complete datalogger, we will start with the dht script which prints at regular intervals.

Navigate to your dht_script.py from section 3 (this file).

Make a copy of this script. Either on your own, or in a terminal:

cp dht_script.py datalogger.py

Open it with your text editor of choice. ###Add the current time to the printout. With the imports

from datatime import datetime

In the while loop

now = datetime.now()
date_string = now.date().strftime("%Y_%m_%d")
time_string = now.time().strftime("%H:%M:%S")

Instead of printing, create a data list

new_log = []
new_log.append(date_string)
new_log.append(time_string)
new_log.append(temperature)
new_log.append(humidity)

###Write to CSV file

  • If the file does not exist yet, make one. Else, append to existing file

import a check to see if a file exists

import os.path

generate the file name and path(i.e. file location)

#data directory path
DATA_PATH = '/home/pi/Documents/env_datalogs'
#data file name
DATA_NAME = 'dht_logs'
#todays date
now = datetime.now()
date_string = now.date().strftime("%Y_%m_%d")

#create total file path (with date)
file_name = DATA_PATH + '/' + date_string + '_' + DATA_NAME

If a file does not exist, create it and write headers for the file

if not os.path.isfile(file_name):
    csv_headers = ['Date', 'Time','Temperature', 'Humidity']
    with open(file_name, 'w') as new_data_file:
        datawriter = csv.writer(new_data_file)
        datawriter.writerow(csv_headers)

Inside the while loop, instead of printing, write to csv file

while True:
   ...

    with open(file_name, 'a') as data_log:
         logwriter = csv.writer(data_log)
         logwriter.writerow(new_log)
    print "Wrote Log: ", new_log

###Running our new script Before running, navigate to the Documents directory

cd /home/pi/Documents

And create a new env_datalogs folder

mkdir env_datalogs

Return to where your datalogger.py is located

sudo python datalogger.py

When you run the program, it will print out our result and add the logs to a file!

##From Here We now are adding logs with the correct time stamp and one sensor's measurement. Now we just need to add a few more environmental sensors to have our complete logger..

Clone this wiki locally