@@ -30,7 +30,8 @@ Example config.json:
30
30
31
31
Create your sensor reading script, and start importing some modules in it!
32
32
``` python
33
- import plotly # plotly library
33
+ import plotly.plotly as py # plotly library
34
+ from plotly.graph_objs import Scatter, Layout # plotly graph objects
34
35
import json # used to parse config.json
35
36
import time # timer functions
36
37
import readadc # helper functions to read ADC from the Raspberry Pi
@@ -49,23 +50,15 @@ stream_token = plotly_user_config['plotly_streaming_tokens'][0]
49
50
50
51
Initialize a Plotly Object
51
52
``` python
52
- p = plotly.plotly (username, api_key)
53
+ py.sign_in (username, api_key)
53
54
```
54
55
55
56
56
57
Initialize your graph (not streaming yet)
57
58
``` python
58
- p.plot([
59
- {' x' : [],
60
- ' y' : [],
61
- ' type' : ' scatter' ,
62
- ' stream' : {
63
- ' token' : stream_token,
64
- ' maxpoints' : 1000
65
- }
66
- }],
67
- filename = ' Raspi Graph' ,
68
- fileopt = ' overwrite' )
59
+ data = [Scatter(x = [],y = [],stream = {' token' : stream_token, ' maxpoints' : 1000 })]
60
+ layout = Layout(title = ' Live graphing from a Raspberry Pi' )
61
+ py.plot(Figure(data = data, layout = layout), filename = ' Raspi Graph' , auto_open = False )
69
62
```
70
63
71
64
Specify the connected channel for your sensor
@@ -80,16 +73,14 @@ readadc.initialize()
80
73
81
74
Initialize the Plotly Streaming Object
82
75
``` python
83
- stream = plotly .Stream(stream_token)
84
- i = 0
76
+ stream = py .Stream(stream_token)
77
+ stream.open()
85
78
```
86
79
87
80
Start looping and streamin'!
88
81
``` python
89
- stream.open()
90
82
while True :
91
83
sensor_data = readadc.readadc(sensor_pin, readadc.PINS .SPICLK , readadc.PINS .SPIMOSI , readadc.PINS .SPIMISO , readadc.PINS .SPICS )
92
- stream.write({' x' : datetime.datetime.now(), ' y' : sensor_data })
93
- i+= 1 # increment 1 on the 'x' axis with each reading
84
+ stream.write({' x' : datetime.datetime.now(), ' y' : sensor_data})
94
85
time.sleep(0.1 ) # delay between stream posts
95
86
```
0 commit comments