-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimescale.py
More file actions
61 lines (41 loc) · 1.44 KB
/
timescale.py
File metadata and controls
61 lines (41 loc) · 1.44 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
#!/usr/bin/env python
import csv
from scale import Scale
import datetime as dt
import matplotlib.pyplot as plt
import matplotlib.animation as animation
# Create figure for plotting
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
xs = []
ys = []
thescale = Scale('192.168.1.4','4001')
with open('output.csv', 'w', newline='') as csvfile:
datawriter = csv.writer(csvfile, delimiter=';', quotechar='"', quoting=csv.QUOTE_MINIMAL)
datawriter.writerow(["timenow","weightnow"])
# This function is called periodically from FuncAnimation
def animate(i, xs, ys):
# Add x and y to lists
timenow = dt.datetime.now().strftime('%H:%M:%S')
scaledata = thescale.lastdata()
weightnow = scaledata[2]
xs.append(timenow)
ys.append(weightnow) # Adds kg
print(timenow,weightnow)
datawriter.writerow([timenow,weightnow])
# Limit x and y lists to 120 items, 1 sec per item
xs = xs[-120:]
ys = ys[-120:]
# Draw x and y lists
ax.clear()
ax.plot(xs, ys)
# Format plot
plt.xticks(rotation=45, ha='right')
plt.subplots_adjust(bottom=0.30)
plt.title('Weight kg over Time')
plt.ylabel('Weight kg')
# Set up plot to call animate() function periodically
ani = animation.FuncAnimation(fig, animate, fargs=(xs, ys), interval=1000)
plt.show()
if __name__ == '__main__':
main()