2016-04-03 128 views
0

我想跟隨在由estromsnes所示的第二個代碼示例: How to create two y-axes streaming plotly的Python,Plotly和樹莓派[錯誤13]

#!/usr/bin/python 

import subprocess 
import re 
import sys 
import time 
import datetime 
import plotly.plotly as py # plotly library 
from plotly.graph_objs import Scatter, Layout, Figure, Data, Stream, YAxis 

# Plot.ly credentials and stream tokens 
username     = 'plotly_username' 
api_key     = 'plotly_api_key' 
stream_token_temperature = 'stream_token_1' 
stream_token_humidity = 'stream_token_2' 

py.sign_in(username, api_key) 

trace_temperature = Scatter(
    x=[], 
    y=[], 
    stream=Stream(
     token=stream_token_temperature 
    ), 
    yaxis='y' 
) 

trace_humidity = Scatter(
    x=[], 
    y=[], 
    stream=Stream(
     token=stream_token_humidity 
    ), 
    yaxis='y2' 
) 

layout = Layout(
    title='Raspberry Pi - Temperature and humidity', 
    yaxis=YAxis(
     title='Celcius' 
    ), 
    yaxis2=YAxis(
     title='%', 
     side='right', 
     overlaying="y" 
    ) 
) 

data = Data([trace_temperature, trace_humidity]) 
fig = Figure(data=data, layout=layout) 

print py.plot(fig, filename='Raspberry Pi - Temperature and humidity') 

stream_temperature = py.Stream(stream_token_temperature) 
stream_temperature.open() 

stream_humidity = py.Stream(stream_token_humidity) 
stream_humidity.open() 

while(True): 
    # Run the DHT program to get the humidity and temperature readings! 
    output = subprocess.check_output(["./Adafruit_DHT", "2302", "17"]); 
    print output 

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

    # 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 C" % temp 
    print "Humidity: %.1f %%" % humidity 

    # Append the data to the streams, including a timestamp 
    now = datetime.datetime.now() 
    stream_temperature.write({'x': now, 'y': temp }) 
    stream_humidity.write({'x': now, 'y': humidity }) 

    # Wait 30 seconds before continuing 
    time.sleep(30) 

stream_temperature.close() 
stream_humidity.close() 

我問新的問題,因爲我不能這樣做對原來的帖子。

我的終端的從我的樹莓派B型+ V1.2狀態輸出:

回溯(最近通話最後一個):

File "plotly5.py", line 62, in <module> 
    output = subprocess.check_output(["./Adafruit_DHT", "2302", "17"]); 
File "/usr/lib/python2.7/subprocess.py", line 566, in check output 
File "/usr/lib/python2.7/subprocess.py", line 710, in __init__ 
File "/usr/lib/python2.7/subprocess.py", line 1335, in _execute_child 
OSError: [Errno 13] Permission denied 

是否有權限設置我應該改變? 我應該改變的./Adafruit_DHT部分:

output = subprocess.check_output(["./Adafruit_DHT", "2302", "17"]); 

感謝。

+0

會發生什麼?它需要sudo權限嗎? –

+0

感謝您的回覆。我曾在Python 2.7.9 Shell中嘗試過。它使用相同的錯誤消息進行響應。我也使用sudo權限從終端運行python文件,並出現相同的錯誤消息。 Python 3.4.2 Shell返回了「無效語法」錯誤。 –

+0

你不能從python解釋器運行shell腳本:) –

回答

0

看起來您沒有權限運行./Adafruit_DHT命令。嘗試更改它的權限。在目錄中的一個終端與Adafruit_DHT,執行以下命令:

chmod +x ./Adafruit_DHT

並再次運行它。在chmod和文件權限

更多信息:當你只是從外殼程序運行該程序 http://catcode.com/teachmod/chmod_cmd.html

+0

我試過了。不幸的是出現了同樣的錯誤你還有其他建議嗎? –

+0

你是否能夠在正確的文件夾中執行命令'。/ Adafruit_DHT'?只需鍵入並讓它運行? – elsherbini

+0

我現在可以看到終端中的溫度和溼度數據。 TheAdafruit_DHT被刪除,並且 –