forked from plotly/raspberrypi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplotly-raspi-stream.py
47 lines (38 loc) · 1.06 KB
/
plotly-raspi-stream.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import plotly.plotly as py
from plotly.graph_objs import Scatter, Layout, Figure
import time
import readadc
username = 'your_plotly_username'
api_key = 'your_api_key'
stream_token = 'your_stream_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')
# 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)
try:
stream.write({'x': i, 'y': sensor_data})
except:
stream.open()
stream.write({'x': i, 'y': sensor_data})
i += 1
# delay between stream posts
time.sleep(0.25)