2016-04-03 110 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被删除,并且 –