私の人生を保存します。 PYTHON + RASPI! 私はいくつか問題があります。私はラズベリーパイ3 +超音波センサーHC-SR04を持っています。私はそれからの距離を読み取り、plot.lyでライブデータのチャートを使用したいが、どのように把握することはできません午前:私はPythonのreadadc.pyで書かれた距離計算:ラズベリー+センサーをプロットする方法は? Python
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
今私はプロットして接続する必要があります。私はそれを作った、私は接続しているが、描画のための値を送信する方法?私は理解できない..ここでは、陰謀的な接続のコードの私の最初の部分です:
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)