【问题标题】:Python, Plotly, and Raspberry Pi [Errno 13]Python、Plotly 和树莓派 [Errno 13]
【发布时间】:2023-03-17 15:00:01
【问题描述】:

我正在尝试遵循 estromsnes 在以下位置显示的第二个代码示例: How to create two y-axes streaming plotly

#!/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()

我要问一个新问题,因为我无法在原始帖子中这样做。

我的 Raspberry Pi Model B+ V1.2 的终端输出状态:

Traceback(最近一次调用最后一次):

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"]);

谢谢。

【问题讨论】:

  • 当您从 shell 运行该程序时会发生什么?需要sudo权限吗?
  • 感谢您的回复。我确实在 Python 2.7.9 Shell 中尝试过。它以相同的错误消息响应。我还使用 sudo 权限从终端运行了 python 文件,并出现了相同的错误消息。 Python 3.4.2 Shell 返回“无效语法”错误。
  • 你不能从 python 解释器运行 shell 脚本 :)

标签: python plotly


【解决方案1】:

您似乎没有运行./Adafruit_DHT 命令的权限。尝试更改它的权限。在带有Adafruit_DHT 的目录中的终端中,执行以下命令:

chmod +x ./Adafruit_DHT

然后再次运行它。

有关 chmod 和文件权限的更多信息: http://catcode.com/teachmod/chmod_cmd.html

【讨论】:

  • 我试过这个。不幸的是,同样的错误出现了。您还有其他想法吗?
  • 当您在正确的文件夹中时,您是否能够执行命令./Adafruit_DHT?只需输入并让它运行?
  • 我现在可以在终端中看到温度和湿度数据。 TheAdafruit_DHT 已被移除并
  • 忽略上面...我现在可以在终端中看到温度和湿度数据。 ./Adafruit_DHT 已更改为 ./AdafruitDHT.py (注意删除了 _ 并添加了 .py。plot.ly 图表会自动加载到我的网络浏览器中。不幸的是,数据没有出现。这段代码运行良好 python - c "导入情节;plotly.tools.set_credentials_file(username='DemoAccount', api_key='lr1c37zw81')"
  • 您使用的是哪个网络浏览器?如果使用 chrome,您可以尝试再次运行代码,并通过按 ctrl+shift+i 并单击控制台来检查浏览器控制台。
猜你喜欢
  • 2015-10-14
  • 2014-09-20
  • 1970-01-01
  • 1970-01-01
  • 2023-03-30
  • 1970-01-01
  • 2014-12-09
  • 2013-11-05
  • 2015-06-02
相关资源
最近更新 更多