-1
내 생명을 구하십시오. PYTHON + RASPI! 몇 가지 문제가 있습니다. 나는 라즈베리 파이 3 + 초음파 센서 HC-SR04 있습니다. 나는 그것으로부터 거리를 읽고 plot.ly에서 라이브 데이터 차트를 사용하고 싶지만 알아낼 수 없습니다 얼마나 :/I는 파이썬 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)