0

よし、何か見つけた。対処方法がわからない。これは、Google で発生する一般的なエラーであることがわかりました。エラーは環境変数か何かに関係しているようです。これを処理する方法がわからない:

これはコードであり、サブプロセスが呼び出されてエラーが発生する部分です。

#!/usr/bin/python

import subprocess
import re
import sys
import time
import datetime
import gspread

# ===========================================================================
# Google Account Details
# ===========================================================================

# Account details for google docs
email       = 'my_email@gmail.com'
password    = 'my_password'
spreadsheet = 'my_spreadsheet'

# ===========================================================================
# Example Code
# ===========================================================================


# Login with your Google account
try:
  gc = gspread.login(email, password)
except:
  print "Unable to log in.  Check your email address/password"
  sys.exit()

# Open a worksheet from your spreadsheet using the filename
try:
  worksheet = gc.open(spreadsheet).sheet1
  # Alternatively, open a spreadsheet using the spreadsheet's key
  # worksheet = gc.open_by_key('0BmgG6nO_6dprdS1MN3d3MkdPa142WFRrdnRRUWl1UFE')
except:
  print "Unable to open the spreadsheet.  Check your filename: %s" % spreadsheet
  sys.exit()

# Continuously append data
while(True):
  # Run the DHT program to get the humidity and temperature readings!

  output = subprocess.check_output(["./Adafruit_DHT", "2302", "17"]);
  print output
  matches = re.search("Temp =\s+([0-9.]+)", output)
  if (not matches):
        time.sleep(3)
        continue
  temp1 = float(matches.group(1))
  temp = temp1*9/5+32 # added the extra step to converto to fahrenheit

  # search for humidity printout
  matches = re.search("Hum =\s+([0-9.]+)", output)
  if (not matches):
       time.sleep(3)
       continue
  humidity = float(matches.group(1))

  print "Temperature: %.1f F" % temp
  print "Humidity:    %.1f %%" % humidity

  # Append the data in the spreadsheet, including a timestamp
  try:
    values = [datetime.datetime.now(), temp, humidity]
    worksheet.append_row(values)
  except:
    print "Unable to append data.  Check your connection?"
    sys.exit()

  # Wait 30 seconds before continuing or just exit
  print "Wrote a row to %s" % spreadsheet
#  time.sleep(60)
  sys.exit()

それは基本的にそれです。Adafruit_DHT プログラムが同じディレクトリにある限り、「sudo python script.py」を使用して正常に動作します。

エラーは次のとおりです。

Traceback (most recent call last):
  File "/home/pi/Adafruit/dht/Ada_temp.py", line 44, in <module>
    output = subprocess.check_output(["./home/pi/Adafruit/dht/Adafruit_DHT", "2302", "17"]);
  File "/usr/lib/python2.7/subprocess.py", line 537, in check_output
    process = Popen(stdout=PIPE, *popenargs, **kwargs)
  File "/usr/lib/python2.7/subprocess.py", line 679, in __init__
    errread, errwrite)
  File "/usr/lib/python2.7/subprocess.py", line 1259, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory

Cプログラム(Adafruit_DHT)のフルパスを追加しようとしましたが、役に立ちませんでした...

4

2 に答える 2

0

問題を特定します。

  1. スクリプトはまったく実行されますか? スクリプトの最初の行で簡単なことを実行して、実際にcronから実行されることを確認します (例: /tmp のファイルに書き込みます)。

  2. なんとか実行できたら、他の問題を探します。Cron は、スクリプトの出力をメールで送信するように設定できます。私が目にする明らかなことの1つは./Adafruit_DHT、相対パスが使用されていることです。これは悪い兆候です。cronスクリプトは、おそらく実行すると思われるディレクトリで実行されません。修正してください (= 絶対パスを使用)。

于 2013-07-27T18:57:19.497 に答える