【发布时间】:2018-05-06 14:52:12
【问题描述】:
拯救我的生命。 蟒蛇+RASPI! 我有一些问题。我有树莓派 3 + 超声波传感器 HC-SR04。我正在读取它的距离,并想在 plot.ly 中使用实时数据图表,但不知道如何:/我找到了用 python readadc.py 编写的用于距离计算的工作代码。:
import RPi.GPIO as GPIO
import time
import signal
import sys
# use Raspberry Pi board pin numbers
GPIO.setmode(GPIO.BCM)
# set GPIO Pins
pinTrigger = 23
pinEcho = 24
def close(signal, frame):
print("\nTurning off ultrasonic distance detection...\n")
GPIO.cleanup()
sys.exit(0)
signal.signal(signal.SIGINT, close)
# set GPIO input and output channels
GPIO.setup(pinTrigger, GPIO.OUT)
GPIO.setup(pinEcho, GPIO.IN)
while True:
# set Trigger to HIGH
GPIO.output(pinTrigger, True)
# set Trigger after 0.01ms to LOW
time.sleep(0.00001)
GPIO.output(pinTrigger, False)
startTime = time.time()
stopTime = time.time()
# save start time
while 0 == GPIO.input(pinEcho):
startTime = time.time()
# save time of arrival
while 1 == GPIO.input(pinEcho):
stopTime = time.time()
# time difference between start and arrival
TimeElapsed = stopTime - startTime
# multiply with the sonic speed (34300 cm/s)
# and divide by 2, because there and back
distance = (TimeElapsed * 34300) / 2
print ("Distance: %.1f cm" % distance)
time.sleep(1)
In this picture i have my distance values
现在我需要与 plotly 建立联系。我已经成功了,我正在连接,但是如何发送绘图值?我无法理解..这是我的第一部分情节连接代码:
import plotly.plotly as py
from plotly.graph_objs import Scatter, Layout, Figure
import time
import readadc
username = 'here_i_write_my_username'
api_key = 'here_i_write_my_api'
stream_token = 'here_i_write_my_token'
py.sign_in(username, api_key)
trace1 = Scatter(
x=[],
y=[],
stream=dict(
token=stream_token,
maxpoints=200
)
)
layout = Layout(
title='Raspberry Pi Streaming Sensor Data'
)
fig = Figure(data=[trace1], layout=layout)
print py.plot(fig, filename='Raspberry Pi Streaming Example Values')
我不知道下一步该做什么?如何在没有 X 和 Y 的情况下仅发送 1 行数据?我正在尝试这样的事情,但它不起作用。有人可以帮忙结束代码吗?
不工作尝试,什么都不画,空图表:
sensor_pin = 24
readadc.initialize()
i = 0
stream = py.Stream(stream_token)
stream.open()
#the main sensor reading loop
while True:
sensor_data = readadc.readadc(sensor_pin,readadc.pinEcho)
stream.write({'x': i, 'y': sensor_data})
i += 1
# delay between stream posts
time.sleep(0.5)
【问题讨论】:
标签: python plotly sensors raspberry-pi3