-
Notifications
You must be signed in to change notification settings - Fork 1
/
plotly_affich_z.py
101 lines (86 loc) · 2.96 KB
/
plotly_affich_z.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
import plotly.plotly as py # plotly library
from plotly.graph_objs import Scatter, Layout, Figure # plotly graph objects
import time # timer functions
#import readadc # helper functions to read ADC from the Raspberry Pi
# Simple demo of of the ADXL345 accelerometer library. Will print the X, Y, Z
# axis acceleration values every half second.
# Author: Tony DiCola
# License: Public Domain
# Import the ADXL345 module.
import Adafruit_ADXL345
import datetime
from plotly import tools
# Create an ADXL345 instance.
accel = Adafruit_ADXL345.ADXL345()
# Alternatively you can specify the device address and I2C bus with parameters:
#accel = Adafruit_ADXL345.ADXL345(address=0x54, busnum=2)
# You can optionally change the range to one of:
# - ADXL345_RANGE_2_G = +/-2G (default)
# - ADXL345_RANGE_4_G = +/-4G
# - ADXL345_RANGE_8_G = +/-8G
# - ADXL345_RANGE_16_G = +/-16G
# For example to set to +/- 16G:
#accel.set_range(Adafruit_ADXL345.ADXL345_RANGE_16_G)
# Or change the data rate to one of:
# - ADXL345_DATARATE_0_10_HZ = 0.1 hz
# - ADXL345_DATARATE_0_20_HZ = 0.2 hz
# - ADXL345_DATARATE_0_39_HZ = 0.39 hz
# - ADXL345_DATARATE_0_78_HZ = 0.78 hz
# - ADXL345_DATARATE_1_56_HZ = 1.56 hz
# - ADXL345_DATARATE_3_13_HZ = 3.13 hz
# - ADXL345_DATARATE_6_25HZ = 6.25 hz
# - ADXL345_DATARATE_12_5_HZ = 12.5 hz
# - ADXL345_DATARATE_25_HZ = 25 hz
# - ADXL345_DATARATE_50_HZ = 50 hz
# - ADXL345_DATARATE_100_HZ = 100 hz (default)
# - ADXL345_DATARATE_200_HZ = 200 hz
# - ADXL345_DATARATE_400_HZ = 400 hz
# - ADXL345_DATARATE_800_HZ = 800 hz
# - ADXL345_DATARATE_1600_HZ = 1600 hz
# - ADXL345_DATARATE_3200_HZ = 3200 hz
# For example to set to 6.25 hz:
#accel.set_data_rate(Adafruit_ADXL345.ADXL345_DATARATE_6_25HZ)
username = 'marouenbg'
api_key = 'Ql12pHYX2G8XdHHiCNhA'
stream_token = 'dhiunsixh2'
stream_token2 = 'lq7oyd0jnl'
print('Printing X, Y, Z axis values, press Ctrl-C to quit...')
#while True:
# # Read the X, Y, Z axis acceleration values and print them.
# x, y, z = accel.read()
# print('X={0}, Y={1}, Z={2}'.format(x, y, z))
# # Wait half a second and repeat.
# time.sleep(0.5)
py.sign_in(username, api_key)
trace1 = Scatter(
x=[],
y=[],
stream=dict(
token=stream_token2,
maxpoints=200
)
)
trace2 = Scatter(
x=[],
y=[],
stream=dict(
token=stream_token2,
maxpoints=200
)
)
layout = Layout(
title='Z_Raspberry Pi Streaming Sensor Data'
)
fig = Figure(data=[trace1], layout=layout)
#fig = tools.make_subplots(rows=1, cols=2)
#fig.append_trace(trace1, 1, 1)
#fig.append_trace(trace2, 1, 2)
print py.plot(fig, filename='Z_Raspberry Pi Streaming Example Values')
#readadc.initialize()
stream = py.Stream(stream_token2)
stream.open()
while True:
#sensor_data = readadc.readadc(sensor_pin, readadc.PINS.SPICLK, readadc.PINS.SPIMOSI, readadc.PINS.SPIMISO, readadc.PINS.SPICS)
x, y, z = accel.read()
stream.write({'x':datetime.datetime.now(), 'y': z})
time.sleep(2) # delay between stream posts