cliveflint
Posts: 5
Joined: Tue Dec 30, 2014 1:46 am

DHT22 Python and Cron issue

Tue Dec 30, 2014 1:53 am

Hi,
I have had to change my Xively feed to update from COSM. It's been a while and I've managed to get the new script to work when run from terminal.
I can't get it to run from CRON though. It doesn't read the sensor. The sensor is read ok when running the file manually.
I'm not sure what is wrong.

Code: Select all

#!/usr/bin/env python

import os
import xively
import subprocess
import time
import datetime
import logging
#import RPi.GPIO as GPIO
import requests
from interfaces.DHT22 import DHT22
import curses
FEED_ID = “***********”
API_KEY = “**************************”
DHTPin = 4
DEBUG = 1
logging.basicConfig(filename='/home/pi/Meteoros/logging.log' , level=logging.DEBUG)
bus=0
dht = DHT22.DHT22(DHTPin)
time.sleep(3)
# initialize api client
#api = xively.XivelyAPIClient(API_KEY)
temp = False
humidity = False
x=0
while temp == False:
  x += 1
  dht.get_data()
  temp = dht.temp()
  logging.debug(str(x) + ' Attempting to get temp')
  time.sleep(1)
  if x > 10:
     break
x=0
while humidity == False:
  x += 1
  dht.get_data()
  humidity = dht.humidity()
  logging.debug(str(x) + ' Attempting to get humidity')
  time.sleep(1)
  if x > 10:
    break
if DEBUG :
  logging.debug('temp is ' + str(temp))
  logging.debug('humidity is ' +str(humidity))
  print "temp is " + str(temp)
  print "humidity is " + str(humidity)

if(temp != False and humidity != False):
  api = xively.XivelyAPIClient(API_KEY)
  feed = api.feeds.get(FEED_ID)

  now = datetime.datetime.now()
#if (temp != False and humidity != False):
  feed.datastreams = [
	xively.Datastream(id='TemperatureSensor', current_value=temp, at=now),
	xively.Datastream(id='HumiditySensor', current_value=humidity, at=now),
  ]
  feed.update()
else:
  logging.debug('Nothing to write to Xively')
  print "Error!"
When I check the log file I see the following:

Code: Select all

DEBUG:root:1 Attempting to get temp
DEBUG:root:2 Attempting to get temp
DEBUG:root:3 Attempting to get temp
DEBUG:root:4 Attempting to get temp
DEBUG:root:5 Attempting to get temp
DEBUG:root:6 Attempting to get temp
DEBUG:root:7 Attempting to get temp
DEBUG:root:8 Attempting to get temp
DEBUG:root:9 Attempting to get temp
DEBUG:root:10 Attempting to get temp
DEBUG:root:11 Attempting to get temp
DEBUG:root:1 Attempting to get humidity
DEBUG:root:2 Attempting to get humidity
DEBUG:root:3 Attempting to get humidity
DEBUG:root:4 Attempting to get humidity
DEBUG:root:5 Attempting to get humidity
DEBUG:root:6 Attempting to get humidity
DEBUG:root:7 Attempting to get humidity
DEBUG:root:8 Attempting to get humidity
DEBUG:root:9 Attempting to get humidity
DEBUG:root:10 Attempting to get humidity
DEBUG:root:11 Attempting to get humidity
DEBUG:root:temp is False
DEBUG:root:humidity is False
DEBUG:root:Nothing to write to Xively
This is quite annoying having spent a few hours going round in circles. (On the plus side I've learnt a few things along the way)

Can anyone help me?
Cheers

cliveflint
Posts: 5
Joined: Tue Dec 30, 2014 1:46 am

Re: DHT22 Python and Cron issue

Tue Dec 30, 2014 8:37 am

My cron job in /etc/cron.d/datalogger is

Code: Select all

* * * * * root /usr/bin/python /home/pi/Meteoros/datalogger.py
and my dir listing is

Code: Select all

[email protected]:/home/pi/Meteoros# ls -l
total 36
drwxr-xr-x 21 pi   pi   4096 Jan 26  2014 Adafruit-Raspberry-Pi-Python-Code
-rwxr-xr-x  1 pi   pi   1564 Dec 30 08:31 datalogger.py
drwxr-xr-x  6 pi   pi   4096 Jan 26  2014 interfaces
-rw-r--r--  1 pi   pi    277 Jan 26  2014 README.md
-rwxr-xr-x  1 pi   pi   2927 Dec 29 20:44 Upload2.py
-rwxr-xr-x  1 root root 4755 Dec 29 19:50 Upload2.py.orig
-rwxr-xr-x  1 pi   pi   3581 Dec 29 21:16 Upload2.py.save
-rwxr-xr-x  1 pi   pi    288 Jan 26  2014 Wifi.py

[email protected]:/home/pi/Meteoros# ls interfaces/ -l
total 20
drwxr-xr-x 2 pi   pi   4096 Jan 26  2014 BMP085
drwxr-xr-x 2 pi   pi   4096 Jan 26  2014 DHT22
-rw-r--r-- 1 pi   pi      0 Jan 26  2014 __init__.py
-rw-r--r-- 1 root root  127 Jan 26  2014 __init__.pyc
drwxr-xr-x 2 pi   pi   4096 Jan 26  2014 MCP3008
drwxr-xr-x 2 pi   pi   4096 Jan 26  2014 PiPlate

kghunt
Posts: 383
Joined: Sun Mar 04, 2012 9:28 am

Re: DHT22 Python and Cron issue

Tue Dec 30, 2014 9:03 am

I can't see the "attempting to get" messages in your script are they from the dht library? The problem seems to be there. When you run it manually do you use sudo?

cliveflint
Posts: 5
Joined: Tue Dec 30, 2014 1:46 am

Re: DHT22 Python and Cron issue

Tue Dec 30, 2014 9:39 am

I had to add it myself to find out what was causing the issue. It's in the program at the top.

Code: Select all

while humidity == False:
  x += 1
  dht.get_data()
  humidity = dht.humidity()
  logging.debug(str(x) + ' Attempting to get humidity')
  time.sleep(1)
  if x > 10:
    break
I'm running as root when it runs manually and that's working fine. It just doesn't work when using cron.

cliveflint
Posts: 5
Joined: Tue Dec 30, 2014 1:46 am

Re: DHT22 Python and Cron issue

Tue Dec 30, 2014 5:50 pm

Well, it seems I've found the issue.

I added the following:

Code: Select all

os.chdir('/home/pi/Meteoros')
and it's now logging properly.

Return to “Python”