├── .gitignore ├── README.md ├── __init__.py ├── plotly-raspi-stream.py ├── readadc.py └── readme_images └── pi.jpg /.gitignore: -------------------------------------------------------------------------------- 1 | *.pyc 2 | config.json 3 | *.out 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Raspberry Pi Realtime Streaming with Plot.ly 2 | [![Plotly-imp](readme_images/pi.jpg)](https://raspberrypi.com) 3 | #### This is an example of a streaming graph: [http://plot.ly/~streaming-demos/6/](http://plot.ly/~streaming-demos/6/) 4 | #### Here is an example of how to hook up a [TMP36 Temperature Sensor to your Pi!](https://plot.ly/raspberry-pi/tmp36-temperature-tutorial/) 5 | 6 | 7 | ### This is a tutorial on streaming with the Raspberry Pi 8 | First, install the required modules and dependencies: 9 | ```bash 10 | sudo apt-get install python-dev 11 | wget https://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py -O - | sudo python 12 | sudo easy_install -U distribute 13 | sudo apt-get install python-pip 14 | sudo pip install rpi.gpio 15 | sudo pip install plotly 16 | ``` 17 | 18 | Create your sensor reading script, and start importing some modules in it! 19 | ```python 20 | import plotly.plotly as py # plotly library 21 | from plotly.graph_objs import Scatter, Layout, Figure # plotly graph objects 22 | import time # timer functions 23 | import readadc # helper functions to read ADC from the Raspberry Pi 24 | ``` 25 | 26 | Make sure to update the credentials in the script with your own! 27 | ```python 28 | username = 'your_plotly_username' 29 | api_key = 'your_api_key' 30 | stream_token = 'your_stream_token' 31 | ``` 32 | If you don't know your credentials : 33 | 34 | Sign up to plotly here: [https://plot.ly/ssu](https://plot.ly/ssu) 35 | View your API key and streaming tokens here: [https://plot.ly/settings](https://plot.ly/settings) 36 | 37 | Initialize a Plotly Object 38 | ```python 39 | py.sign_in(username, api_key) 40 | ``` 41 | 42 | Initialize your graph (not streaming yet) 43 | ```python 44 | trace1 = Scatter( 45 | x=[], 46 | y=[], 47 | stream=dict( 48 | token=stream_token, 49 | maxpoints=200 50 | ) 51 | ) 52 | 53 | layout = Layout( 54 | title='Raspberry Pi Streaming Sensor Data' 55 | ) 56 | 57 | fig = Figure(data=[trace1], layout=layout) 58 | 59 | print py.plot(fig, filename='Raspberry Pi Streaming Example Values') 60 | ``` 61 | 62 | Specify the connected channel for your sensor 63 | ```python 64 | sensor_pin = 0 65 | ``` 66 | 67 | Initialize the GPIO 68 | ```python 69 | readadc.initialize() 70 | ``` 71 | 72 | Initialize the Plotly Streaming Object 73 | ```python 74 | stream = py.Stream(stream_token) 75 | stream.open() 76 | ``` 77 | 78 | Start looping and streamin'! 79 | ```python 80 | while True: 81 | sensor_data = readadc.readadc(sensor_pin, readadc.PINS.SPICLK, readadc.PINS.SPIMOSI, readadc.PINS.SPIMISO, readadc.PINS.SPICS) 82 | stream.write({'x': datetime.datetime.now(), 'y': sensor_data}) 83 | time.sleep(0.1) # delay between stream posts 84 | ``` 85 | 86 | Your graph will be visible in your plotly account ([https://plot.ly/plot](https://plot.ly/plot)) and at `your_graph_url`, the value assigned by the `py.plot` call above. 87 | 88 | ### Contact 89 | Questions? Suggestions? Something not look right? Get in touch! 90 | 91 | - [@plotlygraphs](https://twitter.com/plotlygraphs) 92 | - 93 | - 94 | -------------------------------------------------------------------------------- /__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/plotly/raspberrypi/a32280e52bfdbd452034cf9b857d87b21d668e55/__init__.py -------------------------------------------------------------------------------- /plotly-raspi-stream.py: -------------------------------------------------------------------------------- 1 | import plotly.plotly as py 2 | from plotly.graph_objs import Scatter, Layout, Figure 3 | import time 4 | import readadc 5 | 6 | username = 'your_plotly_username' 7 | api_key = 'your_api_key' 8 | stream_token = 'your_stream_token' 9 | 10 | py.sign_in(username, api_key) 11 | 12 | trace1 = Scatter( 13 | x=[], 14 | y=[], 15 | stream=dict( 16 | token=stream_token, 17 | maxpoints=200 18 | ) 19 | ) 20 | 21 | layout = Layout( 22 | title='Raspberry Pi Streaming Sensor Data' 23 | ) 24 | 25 | fig = Figure(data=[trace1], layout=layout) 26 | 27 | print py.plot(fig, filename='Raspberry Pi Streaming Example Values') 28 | 29 | # temperature sensor connected channel 0 of mcp3008 30 | sensor_pin = 0 31 | readadc.initialize() 32 | 33 | i = 0 34 | stream = py.Stream(stream_token) 35 | stream.open() 36 | 37 | #the main sensor reading loop 38 | while True: 39 | sensor_data = readadc.readadc(sensor_pin, readadc.PINS.SPICLK, readadc.PINS.SPIMOSI, readadc.PINS.SPIMISO, readadc.PINS.SPICS) 40 | stream.write({'x': i, 'y': sensor_data}) 41 | i += 1 42 | # delay between stream posts 43 | time.sleep(0.25) 44 | -------------------------------------------------------------------------------- /readadc.py: -------------------------------------------------------------------------------- 1 | import RPi.GPIO as GPIO 2 | # change these as desired - they're the pins connected from the 3 | # SPI port on the ADC to the GPIO Pins on the Raspi 4 | 5 | # MCP3008 to Raspi (PiCobbler) Pin connections 6 | class PINS: 7 | SPICLK = 18 8 | SPIMISO = 23 9 | SPIMOSI = 24 10 | SPICS = 25 11 | 12 | # set up the SPI interface pins 13 | def initialize(): 14 | GPIO.setmode(GPIO.BCM) 15 | GPIO.setwarnings(False) 16 | GPIO.setup(PINS.SPIMOSI, GPIO.OUT) 17 | GPIO.setup(PINS.SPIMISO, GPIO.IN) 18 | GPIO.setup(PINS.SPICLK, GPIO.OUT) 19 | GPIO.setup(PINS.SPICS, GPIO.OUT) 20 | 21 | 22 | # Function to read data from Analog Pin 0 from MCP3008 (don't need to edit) 23 | # This function will be called in our loop to get the current sensor value 24 | def readadc(adcnum, clockpin, mosipin, misopin, cspin): 25 | if ((adcnum > 7) or (adcnum < 0)): 26 | return -1 27 | GPIO.output(cspin, True) 28 | 29 | GPIO.output(clockpin, False) # start clock low 30 | GPIO.output(cspin, False) # bring CS low 31 | 32 | commandout = adcnum 33 | commandout |= 0x18 # start bit + single-ended bit 34 | commandout <<= 3 # we only need to send 5 bits here 35 | for i in range(5): 36 | if (commandout & 0x80): 37 | GPIO.output(mosipin, True) 38 | else: 39 | GPIO.output(mosipin, False) 40 | commandout <<= 1 41 | GPIO.output(clockpin, True) 42 | GPIO.output(clockpin, False) 43 | 44 | adcout = 0 45 | # read in one empty bit, one null bit and 10 ADC bits 46 | for i in range(12): 47 | GPIO.output(clockpin, True) 48 | GPIO.output(clockpin, False) 49 | adcout <<= 1 50 | if (GPIO.input(misopin)): 51 | adcout |= 0x1 52 | 53 | GPIO.output(cspin, True) 54 | 55 | adcout /= 2 # first bit is 'null' so drop it 56 | return adcout -------------------------------------------------------------------------------- /readme_images/pi.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/plotly/raspberrypi/a32280e52bfdbd452034cf9b857d87b21d668e55/readme_images/pi.jpg --------------------------------------------------------------------------------