Python Forum
Raspberry + Sensor to Plotly HOW?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Raspberry + Sensor to Plotly HOW?
#1
Hello :)

save my life. PYTHON+RASPI! I have some problems. I have raspberry pi 3 + Ultrasound Sensor HC-SR04. I am reading distance from it and want to use live-data chart in plot.ly but can't figure out how :/

I have written all on stackowerflow with codes:

https://stackoverflow.com/questions/4744...how-python
Reply
#2
(Nov-22-2017, 08:06 PM)azairek Wrote: I have some problems

Isn't very useful. What problems are you having. If you are getting errors, you need to post the error codes (in their entirety). Be sure and read the BBCode Help in order to properly post code, errors and output.

I'll also add, it's a bit cheeky of you to ask for help on this forum and then tell people to go look on another forum to see what your problem is.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#3
This code help me to check and calculate distance between my sensor and object on his way.

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)
Now i need to change/remake this code so that it will send my distance to plotly.

import plotly.plotly as py
from plotly.graph_objs import Scatter, Layout, Figure
import time
import readadc

username = 'a_user_name'
api_key = 'an_api_key'
stream_token = 'd0olq2nktv'

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')

# temperature sensor connected channel 0 of mcp3008
sensor_pin = 0
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.PINS.SPICLK, readadc.PINS.SPIMOSI, readadc.PINS.SPIMISO, readadc.PINS.SPICS)
stream.write({'x': i, 'y': sensor_data})
i += 1
# delay between stream posts
time.sleep(0.25)
Reply
#4
I believe you lost your indentation when you pasted between the code tags. Try using the key combination "Ctrl + Shift + v" when pasting, that should maintain the indentation and any other formatting.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020