AdafruitDHT.pyから温度用temp.py,湿度用humid.pyを作成
temp.py、tempスクリプト
URL
blog.bnikka.com/raspberrypi/am2302dht22raspberrypi.html
コード!/usr/bin/python # Copyright (c) 2014 Adafruit Industries # Author: Tony DiCola # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # The above copyright notice and this permission notice shall be included in all # copies or substantial portions of the Software. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. import sys import Adafruit_DHT # Parse command line parameters. sensor_args = { '11': Adafruit_DHT.DHT11, '22': Adafruit_DHT.DHT22, '2302': Adafruit_DHT.AM2302 } if len(sys.argv) == 3 and sys.argv[1] in sensor_args: sensor = sensor_args[sys.argv[1]] pin = sys.argv[2] else: print('usage: sudo ./Adafruit_DHT.py [11|22|2302] GPIOpin#') print('example: sudo ./Adafruit_DHT.py 2302 4 - Read from an AM2302 connected to GPIO #4') sys.exit(1) # Try to grab a sensor reading. Use the read_retry method which will retry up # to 15 times to get a sensor reading (waiting 2 seconds between each retry). humidity, temperature = Adafruit_DHT.read_retry(sensor, pin) # Un-comment the line below to convert the temperature to Fahrenheit. # temperature = temperature * 9/5.0 + 32 # Note that sometimes you won't get a reading and # the results will be null (because Linux can't # guarantee the timing of calls to read the sensor). # If this happens try again! if humidity is not None and temperature is not None: print('Temp={0:0.1f}* Humidity={1:0.1f}%'.format(temperature, humidity)) else: print('Failed to get reading. Try again!') sys.exit(1)
#!/bin/sh #%# family=auto #%# capabilities=autoconf GETNUM=`python /usr/local/bin/temp.py 22 4` if [ "$1" = "autoconf" ]; then if [ -n ${GETNUM} ] ; then echo yes exit 0 else echo no exit 0 fi fi if [ "$1" = "config" ]; then echo 'graph_title temperature' echo 'graph_args -r --lower-limit 0' echo 'graph_vlabel C' echo 'graph_category Weather' echo 'total.label temperature' echo 'total.min 0' echo 'total.draw LINE2' echo 'total.type GAUGE' exit 0 fi echo "total.value $GETNUM";ド
コード部分は``` ```で囲んで下さい。
コード部分を選択してから編集画面の上部にある<code>ボタンを押すと自動で囲んでくれます。
GETNUM=python /usr/local/bin/temp.py 22 4 を、GETNUM=`python /usr/local/bin/temp.py 22 4` に換えてみてはどうでしょうか。
#!/bin/sh -x として実行し、出力結果を追記してください。
bash -x tempを実行
++ python /usr/local/bin/temp.py 22 4
Traceback (most recent call last):
File "/usr/local/bin/temp.py",line41, in <module>
humidity,temperature = Adafruit_DHT.read_retry(sensor, pin)
File "build/bdist.linux-arm7l/egg/Adafruit_DHT/common.py", line 90, in read_retry
File "build/bdist.linux-arm7l/egg/Adafruit_DHT/common.py",line77, in read
File "build/bdist.linux-arm7l/egg/Adafruit_DHT/Raspberry_pi_2.py",line34,in read
RuntimeError:Error accessing GPIO.
+ GEtNUM=$'\\r'
temp: 行 27: 構文エラー:予期しないファイル終了(EOF)です
が出力されました
権限がなくて、python /usr/local/bin/temp.py 22 4が適切に実行されていないようですから、sudo bash -x tempと実行してください。また、GEtNUMとありますが、記述されたスクリプトにはないようですが。。。
コード部分は``` ```で囲んで下さい。 コード部分を選択してから編集画面の上部にある<code>ボタンを押すと自動で囲んでくれます。
'sudo bash -x temp'
実行後
'++ python /usr/local/bin/temp.py 22 4'
'+ GETNUM=$'30.6000003815\r''
temp: 行26: 構文エラー:予期しないファイル終了(EOF)です
表示されます。
30.6000003815は温度だとおもいます。
temp:行 26は
最後の行 'echo "total.value $GETNUM";
この質問で引用したとしているスクリプトと現在実行しているスクリプトは同一ですか?
それと最後の行の ; は必要ですか?
さらに\rが気になりますが、temp.pyもなにか変更されているような気がするのですが。。。DHT22をお使いだと思うのですが、https://github.com/adafruit/Adafruit_Python_DHT/blob/master/examples/AdafruitDHT.py#L51
とは出力のさせ方が違うような。
tempとtemp.pyを質問を編集する形で、対象のコードを``` ```で囲んで引用して下さい。 コード部分を選択してから編集画面の上部にある<code>ボタンを押すと自動で囲んでくれます。
また、参考にしたページをURLを含む形で追記してください。
回答3件
あなたの回答
tips
プレビュー