+++ README.md
... | ... | @@ -0,0 +1,1 @@ |
1 | +# SensorLogger-통신-모듈 |
+++ main.py
... | ... | @@ -0,0 +1,107 @@ |
1 | +MIT License | |
2 | + | |
3 | +Copyright (c) 2022 Kelvin Choi | |
4 | + | |
5 | +Permission is hereby granted, free of charge, to any person obtaining a copy | |
6 | +of this software and associated documentation files (the "Software"), to deal | |
7 | +in the Software without restriction, including without limitation the rights | |
8 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
9 | +copies of the Software, and to permit persons to whom the Software is | |
10 | +furnished to do so, subject to the following conditions: | |
11 | + | |
12 | +The above copyright notice and this permission notice shall be included in all | |
13 | +copies or substantial portions of the Software. | |
14 | + | |
15 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
16 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
17 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
18 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
19 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
20 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
21 | +SOFTWARE. | |
22 | + | |
23 | +import dash | |
24 | +from dash.dependencies import Output, Input | |
25 | +from dash import dcc, html, dcc | |
26 | +from datetime import datetime | |
27 | +import json | |
28 | +import plotly.graph_objs as go | |
29 | +from collections import deque | |
30 | +from flask import Flask, request | |
31 | + | |
32 | +server = Flask(__name__) | |
33 | +app = dash.Dash(__name__, server=server) | |
34 | + | |
35 | +# CSV export and thats it. | |
36 | + | |
37 | +MAX_DATA_POINTS = 1000 | |
38 | +UPDATE_FREQ_MS = 100 | |
39 | + | |
40 | +time = deque(maxlen=MAX_DATA_POINTS) | |
41 | +accel_x = deque(maxlen=MAX_DATA_POINTS) | |
42 | +accel_y = deque(maxlen=MAX_DATA_POINTS) | |
43 | +accel_z = deque(maxlen=MAX_DATA_POINTS) | |
44 | + | |
45 | +app.layout = html.Div( | |
46 | + [ | |
47 | + dcc.Markdown( | |
48 | + children=""" | |
49 | + # Live Sensor Readings | |
50 | + Streamed from Sensor Logger: tszheichoi.com/sensorlogger | |
51 | + """ | |
52 | + ), | |
53 | + dcc.Graph(id="live_graph"), | |
54 | + dcc.Interval(id="counter", interval=UPDATE_FREQ_MS), | |
55 | + ] | |
56 | +) | |
57 | + | |
58 | + | |
59 | [email protected](Output("live_graph", "figure"), Input("counter", "n_intervals")) | |
60 | +def update_graph(_counter): | |
61 | + data = [ | |
62 | + go.Scatter(x=list(time), y=list(d), name=name) | |
63 | + for d, name in zip([accel_x, accel_y, accel_z], ["X", "Y", "Z"]) | |
64 | + ] | |
65 | + | |
66 | + graph = { | |
67 | + "data": data, | |
68 | + "layout": go.Layout( | |
69 | + { | |
70 | + "xaxis": {"type": "date"}, | |
71 | + "yaxis": {"title": "Acceleration ms<sup>-2</sup>"}, | |
72 | + } | |
73 | + ), | |
74 | + } | |
75 | + if ( | |
76 | + len(time) > 0 | |
77 | + ): # cannot adjust plot ranges until there is at least one data point | |
78 | + graph["layout"]["xaxis"]["range"] = [min(time), max(time)] | |
79 | + graph["layout"]["yaxis"]["range"] = [ | |
80 | + min(accel_x + accel_y + accel_z), | |
81 | + max(accel_x + accel_y + accel_z), | |
82 | + ] | |
83 | + | |
84 | + return graph | |
85 | + | |
86 | + | |
87 | [email protected]("/data", methods=["POST"]) | |
88 | +def data(): # listens to the data streamed from the sensor logger | |
89 | + if str(request.method) == "POST": | |
90 | + print(f'received data: {request.data}') | |
91 | + data = json.loads(request.data) | |
92 | + for d in data['payload']: | |
93 | + if ( | |
94 | + d.get("name", None) == "accelerometer" | |
95 | + ): # modify to access different sensors | |
96 | + ts = datetime.fromtimestamp(d["time"] / 1000000000) | |
97 | + if len(time) == 0 or ts > time[-1]: | |
98 | + time.append(ts) | |
99 | + # modify the following based on which sensor is accessed, log the raw json for guidance | |
100 | + accel_x.append(d["values"]["x"]) | |
101 | + accel_y.append(d["values"]["y"]) | |
102 | + accel_z.append(d["values"]["z"]) | |
103 | + return "success" | |
104 | + | |
105 | + | |
106 | +if __name__ == "__main__": | |
107 | + app.run_server(port=8000, host="0.0.0.0") |
Add a comment
Delete comment
Once you delete this comment, you won't be able to recover it. Are you sure you want to delete this comment?