I have a Adafruit DHT22 sensor connected to my Raspberry Pi B+. I want to put this in an outbuilding to monitor the temperature and humidity over a period of time. The building has mains power but no wifi or cable network. I propose to have the data saved on a memory stick in a csv file.
As a base for the script I am using the simpletest.py script which I downloaded from Adafruit. The script works proving there is nothing wrong with the hardware. I have a copy of the Raspberry Cookbook and have downloaded the temperature sensor data to memory stick script from that publication. I am attempting to combine them to get what I want.
#!/usr/bin/python
# imports to pick up the dependencies to make this work
import glob, datetime, os, time
# defines the period between data samples. For the purpose of test this is set low. The actual
# time will be some minutes between samples
log_period = 10 # seconds
# Add the data logging stuff below here
# logging_folder = glob.glob('/media/KINGSTON/Raspberry_Data/')[0]
logging_folder = glob.glob('/home/pi/*')[0]
dt = datetime.datetime.now()
file_name = "temp_log_{:%Y_%m_%d}.csv".format(dt)
logging_file = logging_folder + '/' + file_name
# Add the data logging stuff above here
import Adafruit_DHT
sensor = Adafruit_DHT.DHT22
pin = 4
# The two pieces of data I want are temperature and humidity. I just need to open the file
# and put them in.
#def log_temp(): # Don't believe I need a temp???
# temp_c, temp_f = read_temp() # Data names from old program
dt = datetime.datetime.now()
f = open(logging_file, 'a') # Open the logging file with the intention of amending it
#f.write('\n"{:%H:%M:%S}",'.format(dt) #Origin line removed as it caused a syntax error
f = write('\n"{:%H:%M:%S}",'.format(dt) # Create a new line and insert HMS
>>>f.write(str(temperature)) # Write the string temperature Throws up syntax error
# with carat under 'f'.
#f = write(str(temperature))
f.close()
print("Logging to: " + logging_file)
while True:
log_temp()
time.sleep(log_period)
The above works fine until it gets to the line with >>> this does not appear in the script but is only used to highlight the problem. Also, I have not directed the output to /media/ yet just to /pi/ as this is still being debugged.
I had hoped it was just a simple case of directing the two data sets (temperature and humidity) to the logging file but I seem to have lost my way.
Can anyone offer any help please?
Regards,
Harry