0

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

2
  • If you have a programming problem, always include the literal error.
    – goldilocks
    Feb 8, 2015 at 16:22
  • File "simpletest+logging.py", line 81 f.write(str(temperature)) # Write the string temperature Throws up syntax error ^ Is the response I get in the Terminal window. The carat is under the f of f.write. Feb 8, 2015 at 19:08

2 Answers 2

0

You are missing closing bracket one line above

f = write('\n"{:%H:%M:%S}",'.format(dt)

it should be:

f = write('\n"{:%H:%M:%S}",'.format(dt)) #<-one more bracket
2
  • Correct! And goes on to solve almost the whole problem! Feb 10, 2015 at 14:55
  • Full comment should read: Correct! And goes on to solve almost the whole problem! Of course it reports a syntax problem when I leave the previous line uncompleted :.(. Now it opens the csv file and writes to it. Lots of tweaks left to do but I have something which give me an output which hopefully make it easier to debug Very Many Thanks salmon. Feb 10, 2015 at 15:02
1
f.write(str(temperature)) Throws up syntax error

Probably because no variable named temperature exists in that script.


In a comment below you've said you accidentally left some lines out including this:

if humidity is not None and temperature is not None:
    print 'Temp={0:0.1f}@C Humidity={1:0.1f}%'.format(temperature, humidity)
else: print 'Failed to get reading. Try again!' 

And that you believe "this is where the variables Temperature and Humidity are defined". You have essentially no programming experience. That's fine -- I understand you are just trying to use the script to accomplish something.

Unfortunately, working with code is not always as straightforward as one may think it is going to be (regardless of experience level). There's a simple way to determine whether the above construct could define the variables in the if -- with a short, self-contained, compatible example (SSCCE):

#!/bin/python

if foo is not None:
    print okay

Running this yields an error:

Traceback (most recent call last):
  File "./test.py", line 3, in <module>
    if foo is not None:
NameError: name 'foo' is not defined

So, evidently this doesn't define foo. You are still stuck.

I know you aren't here for a lesson in programming. If I could give you a simple quick fix, I would. Unfortunately, there is not enough information for that because we still don't have the actual code you are running (or an SSCCE of it).

General programming questions actually belong on Stack Overflow; the community is much (much much) larger there and your problems really don't depend on anything particular about the the pi (it would be better if you approached this more abstractly, as with the SSCCE). The reality is that if you want to hack two scripts together, you do need to learn some basic concepts first. Find a "Learn Python in 24 Hours" type book, or work through a few tutorials online somewhere, whatever appeals to you. But doing things this way is beyond tedious for everyone involved, I think. You clearly cannot read code, at all, currently (even if you believe you sort of can).

4
  • In my haste to get this done I removed a number of # lines. Along with this I removed: if humidity is not None and temperature is not None: print 'Temp={0:0.1f}@C Humidity={1:0.1f}%'.format(temperature, humidity) else: print 'Failed to get reading. Try again!' I have since put this back in and still get the same syntax error. I believe this is where the variables Temperature and Humidity are defined. If I change the spelling of Temperature I get a 'Name not defined' error which reinforces this belief. Feb 9, 2015 at 14:47
  • Okay, I've added a bunch of stuff above about this.
    – goldilocks
    Feb 9, 2015 at 15:10
  • Very many thanks for the advice. At 63 I don't see this ever becoming something I can make a career out of. However, it serves to keep the mind ticking over and to be able to do that in a coherent way. Appreciate you taking the time to put me on the right path. Kind Regards, Harry Feb 10, 2015 at 15:07
  • You don't need professional level skills to do this kind of thing -- I'm sure 5-10 hours of study will get you close enough. You need to understand how variables, scope, and flow control work, in a python context. If you decide to try, set aside this script for a while and just work through basic exercises (starting with "Hello world!", etc). If you refer back to some more complicated goal too early, you will only make it harder. Good luck!
    – goldilocks
    Feb 10, 2015 at 15:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.