Repository navigation
Expand file tree
/
Copy pathStreamer.py
More file actions
108 lines (86 loc) · 2.69 KB
/
Copy pathStreamer.py
File metadata and controls
108 lines (86 loc) · 2.69 KB
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
import plotly.plotly as py
import plotly.tools as tls
from plotly.graph_objs import *
import numpy as np # (*) numpy for math functions and arrays
import datetime
import time
import logging
logging.basicConfig(level=logging.DEBUG)
from ThermometerBus import ThermometerBus
host = 'localhost'
port = 4304
bus = ThermometerBus(host,port)
therms = bus.thermometers()
logging.debug("Number of thermometers:%d",len(therms))
stream_ids = ["d7hhv34d32","jfh75d1vr1","tz0nz3779t","j359lsu1rh","vut4dqvvlz","aml29ioq5v"]
# Make instance of stream id object
streams = []
traces = []
npoints = 60*60
for i in range(len(therms)):
streams.append( Stream(
token=stream_ids[i], # (!) link stream id to 'token' key
maxpoints=npoints
)
)
traces.append(
Scatter(
# name="Sensor "+str(i+1),
name = therms[i].id,
x=[],
y=[],
mode='lines',
stream=streams[i] # (!) embed stream id, 1 per trace
)
)
data = Data(traces)
# Add title to layout object
layout = Layout(
title='Thermolog',
yaxis={
"range":[0,40],
"type":"linear",
"title":"Temperature (C)",
"autorange":False,
"gridcolor":"rgb(204, 204, 204)",
"tickmode":"linear",
"dtick":1,
"side":"right"
},
xaxis={
"title":"Time",
"type":"date",
"autorange":True,
"tickangle":90
},
)
# 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='thermolog',fileopt="overwrite",auto_open=False,sharing="public")
for i in range(len(streams)):
streams[i] = py.Stream(stream_ids[i])
streams[i].open()
# (@) Open the stream
N = 24*60*60 # number of points to be plotted
# Delay start of stream by 5 sec (time to switch tabs)
time.sleep(5)
i = 0 # a counter
while i<N:
i += 1 # add to counter
temps = bus.simultaneousTemperatures()
gnow = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')
for j in range(len(temps)):
# Current time on x-axis, random numbers on y-axis
x = gnow
y = temps[j]
# y = 60+10*np.random.randn(1)[0]
# (-) Both x and y are numbers (i.e. not lists nor arrays)
# (@) write to Plotly stream!
streams[j].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.08) # (!) plot a point every 80 ms, for smoother plotting
for i in range(len(streams)):
# (@) Close the stream when done plotting
streams[i].close()