-
Notifications
You must be signed in to change notification settings - Fork 3
/
livestream_cont3.py
133 lines (95 loc) · 2.83 KB
/
livestream_cont3.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
__author__ = 'student'
# (*) To communicate with Plotly's server, sign in with credentials file
import plotly.plotly as py
# (*) Useful Python/Plotly tools
import plotly.tools as tls
# (*) Graph objects to piece together plots
from plotly.graph_objs import *
import numpy as np # (*) numpy for math functions and arrays
import datetime
import time
import serial
ser = serial.Serial('/dev/tty.usbmodem1411',baudrate = 9600, timeout = 1)
ser.close()
ser.open()
header = "A"
footer = "D"
byte = ""
temp = 0
heartrate = 0
help1 = 0
stream_ids = tls.get_credentials_file()['stream_ids']
#help(Stream) # call help()!
# Get stream id from stream id list
stream_id = stream_ids[0]
# Make instance of stream id object
stream = Stream(
token=stream_id, # (!) link stream id to 'token' key
maxpoints=200 # (!) keep a max of 80 pts on screen
)
# Initialize trace of streaming plot by embedding the unique stream_id
trace1 = Scatter(
x=[],
y=[],
mode='lines+markers',
stream=stream, # (!) embed stream id, 1 per trace
line=Line(
color='rgb(44, 160, 44)',
width=3,
shape='spline',
smoothing=1.3
)
)
data = Data([trace1])
#Add title to layout object
layout = Layout(
autosize=True,
width=1860,
height=1313,
xaxis=XAxis(
title='Time',
type='category',
autorange=True
),
yaxis=YAxis(
title='Body Temperature',
type='linear',
autorange=True
)
)
# Make a figure object
fig = Figure(data=data, layout=layout)
# (@) Send fig to Plotly, initialize streaming plot, open new tab
unique_url = py.plot(fig, filename='s7_first-stream')
#help(py.Stream) # run help() of the Stream link object
# (@) Make instance of the Stream link object,
# with same stream id as Stream id object
s = py.Stream(stream_id)
# (@) Open the stream
s.open()
# (*) Import module keep track and format current time
i = 0 # a counter
k = 5 # some shape parameter
N = 100000 # number of points to be plotted
# Delay start of stream by 5 sec (time to switch tabs)
time.sleep(5)
while 1:
i += 1 # add to counter
byte = ser.readline()
if header in byte:
help1 = ser.readline()
temp = ser.readline()
heartrate = ser.readline()
print(heartrate)
# Current time on x-axis, random numbers on y-axis
x = datetime.datetime.now().strftime('%H:%M:%S')
y = str(float(temp)+10)
# (-) Both x and y are numbers (i.e. not lists nor arrays)
# (@) write to Plotly stream!
s.write(dict(x=x, y=y))
# (!) Write numbers to stream to append current data on plot,
# write lists to overwrite existing data on plot (more in 7.2).
time.sleep(0.35) # (!) plot a point every 80 ms, for smoother plotting
# (@) Close the stream when done plotting
s.close()
ser.close()