Time for some Raspberry Pi + Plotly Streaming fun!

Recently we completed an Instructable for visualizing temperature and humidity data using an Arduino and Plot.ly, a free online collaborative graphing tool. Now it's time to extend that fun to all Raspberry Pi users!

The Raspberry Pi is a fantastic platform for software experimentation, allowing kids, amateurs and professionals alike to hack away without fear of breaking an expensive computer system. I'm a huge fan, and have done a few projects with my Raspberry Pi, from setting up a web server in our workshop, to using it as a desktop PC when my laptop was in the shop.

Raspberry Pi fun isn't limited only to software activities though! In addition to its other great features, the Raspberry Pi has some GPIO (General Purpose IN/OUT) pins, which you can use to read data from!

The purpose of this Instructable is to take some temperature data read from a TMP36 sensor, and visualize it using real-time streaming using the Plotly Python API!

Here is an example of our final product: https://plot.ly/~demos/1415/

Step 1: What You'll Need

  • Raspberry Pi

  • MCP3008 DIP-package ADC converter chip

  • Analog Temperature Sensor TMP-36

  • Breadboard

  • Some Hookup wire

  • Some Software Packages (Details in later steps)

For the solderless option :

For the more permanent shield option:

You'll need a Plot.ly Account. Head over to:

  • Plot.ly to sign up.
  • Once you have signed up, head over to the SETTINGS page and write down your API key and Stream Token. You'll need these later!

Step 2: SSH Into Your Pi

Hopefully you've got your Raspberry Pi hooked up to your local network. If you do, SSH right into it and you're ready for the next step!

If not, check out this link, it's a great primer on getting started with SSH on your Pi:

Once you're logged in, head over to the next stop and run the command listed there.

We used this guide to get SSH working over our Laptop's WIFI. All you need is an ethernet cable. It works well for testing, and when everything is ready you can hook it up to your Router.

Step 3: Software Setup - Run All of These Commands in Your Terminal

sudo apt-get install python-dev
sudo apt-get install python-pip
sudo pip install rpi.gpio
sudo pip install plotly

Step 4: More Software

Download the Plot.ly Raspberry Pi Getting Started Script

  • OR

Clone the Repo from Github:


Change to the new directory

cd rasperrypi

Step 5: Advanced Users: Plotly Python Library

Should you want to deviate from the Quickstart method, you can of course just use the Plot.ly Python Library like you would on any other system. Take a look inside the included Quickstart Scripts to see whats going on under the hood and get started writing your own personalized scripts!

All of the normal Plot.ly Python library functions work on the Raspberry Pi as well!

Step 6: Hardware Hookup

The Raspberry Pi does have GPIO, however without some extra help from a MCP3008 Chip, it cannot read analog inputs.
No problem, just breadboard your MCP3008, and connect your Pi Cobbler, or Solder it directly onto your Prototype Shield. You'll be slingin' some analog data in no time!

Connect the following pins from the Raspberry Pi's GPIO pins the MCP chip:

MCP3008 VDD (PIN16) : 3.3V
MCP3008 VREF (PIN15) : 3.3V
MCP3008 AGND (PIN14) : GND
MCP3008 CLK (PIN13) : #18
MCP3008 DOUT (PIN12) : #23
MCP3008 DIN (PIN11): #24
MCP3008 CS (PIN10): #25

Connect the following pins from the TMP36 :
(Flat Face Forward, Left to Right)

Pin1 : 3.3V
Pin2 : MCP3008 CH0 (PIN 1)
Pin3 : GND

Step 7: Firing It Up

Now, if you have everything connected properly, and you've downloaded the getting started script, go ahead and execute it! Thanks to Plot.ly's streaming capabilities, all of your sensor data will automatically update on the graph!

Make sure you update the credentials in the script with your own! (Obtained in previous step)

username = 'your_plotly_username'
api_key = 'your_api_key'
stream_token = 'your_stream_token'
Then, run the script! (You have to sudo to access the GPIO pins)
sudo python plotly-raspi-stream.py

In this script, you will enter in your desired graph information, as well as API Key and Stream Token (Obtained earlier).

Now you can cruise right over to the URL provided from the script! Paste it right into your browser if you're using the Raspberry Pi in a Graphical Environment, or fire up your browser on your main computer to see your work!

Once you've opened your graph, feel free to click on "Save and Edit" (don't worry, your graph will be saved regardless) to edit the style and change the titles in the GUI.

Happy Streaming!!

Here is an example graph made with streamed data from a Raspberry Pi:


<p>Aloha,</p><p>Does anyone know if i can swap the recommended temperature sensor for a DS18B20<br>Temperature Sensor Cable or would this change the whole dynamics of the project? First time rashberry PI builder, could use all the help I can get.</p><p>Thanks!</p>
<p>help please! what i have to do?</p>
<p>Curious about how this works under the hood.. does the pc act a client requesting data from the raspberry pi (web?) server?</p><p>I have an outdoor worm farm that I'd like to stream real-time data from (1Mbps+), could this be done with an arduino and a bluetooth or wifi shield? BT classic (2.1 edr) ftp profile would be fast enough, but seems everything is (slow) ble these days. </p><p>Or if using a wifi shield, what wifi layers/protocols would be needed for real-time data transfer?</p>
<p>You could use Jython to pull data and write them to a database, but it all depends on your application. But, real time is difficult unless you have a pretty good understanding of network topology and could work with different interfaces. Unless someone has released something along the lines, to write you're own interpreter and broadcaster for bluetooth is a challenge on its own! </p>
<p>Hey there, I finally ironed out some issues I was having getting this project going, I was just wondering how I might be able to get the TMP36 calibrated to show a temp in celsius. As you can see my graph shows some pretty arbitrary values. I ran your other tutorial: https://plot.ly/raspberry-pi/tmp36-temperature-tutorial/ great!! Now just need to change the timezone.</p>
<p>I'm currently attempting to execute this project <br>using an I2C accelerometer from SparkFun. The sensor returns a value but I keep <br>getting a 411 error in addition to this </p><p>Traceback (most recent call last):<br> File &quot;packAccel.py&quot;, line 277, in &lt;module&gt;<br> stream.write(dict(x=i, y=sensor_data[0]))<br> File &quot;/usr/local/lib/python2.7/dist-packages/plotly/plotly/plotly.py&quot;, line 501, in write<br> self._stream.write(jdata, reconnect_on=reconnect_on)<br> File &quot;/usr/local/lib/python2.7/dist-packages/plotly/plotly/chunked_requests/chunked_request.py&quot;, line 53, in write<br> msg=response.read()))<br>Exception: Server responded with status code: 411<br>and message: &lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD HTML 4.01//EN&quot; &quot;http://www.w3.org/TR/html4/strict.dtd&quot;&gt; &lt;html&gt;&lt;head&gt; &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot;&gt; &lt;title&gt;ERROR: The requested URL could not be retrieved&lt;/title&gt; &lt;style type=&quot;text/css&quot;&gt;&lt;!-- %l body :lang(fa) { direction: rtl; font-size: 100%; font-family: Tahoma, Roya, sans-serif; float: right; } :lang(he) { direction: rtl; float: right; } --&gt;&lt;/style&gt; &lt;/head&gt;&lt;body&gt; &lt;div id=&quot;titles&quot;&gt; &lt;h1&gt;ERROR&lt;/h1&gt; &lt;h2&gt;The requested URL could not be retrieved&lt;/h2&gt; &lt;/div&gt; &lt;hr&gt; &lt;div id=&quot;content&quot;&gt; &lt;p&gt;&lt;b&gt;Invalid Request&lt;/b&gt; error was encountered while trying to process the request:&lt;/p&gt; &lt;blockquote id=&quot;data&quot;&gt; &lt;pre&gt;POST / HTTP/1.1</p><p>I <br> assume this means that plotly is not accepting data I'm sending. My <br>username, apikey and token are all correct in the sensor code. There are <br> no syntax errors either. I am very new to plot.ly, so I'm certain that <br>I'm missing something simple. Any help would be appreciated. The <br>tutorial is very well written and appreciated.</p>
Hey dauletle --<br>Thanks so much for trying it out!<br>Hm... I'm having trouble replicating this. Could you share with me your code? Are you using a Raspberry Pi? Which version of the Python library are you using? (import plotly; plotly.__version__)
<p>Hi plotlygraphs. I'm using Python 2.7.3 [GCC 4.6.3] on linux2, with plotly version 1.2.7</p><p>I'm using almost the same code as the example code posted in these instructions. The only things I really changed had to do with reading my sensor through I2C. The sensor is reading just fine, and everything looks like it's working up to the command </p><p>stream.write({'x': i, 'y': sensor_data})</p><p>where I'm getting this 411 error. I'm going to try to run this with Python 3, and get back to you you of any progress made.</p>
<p>what modifications would I need to make to run plotty on a pcDuino, running linux ubuntu? it seems like the plotty code will install fine, but then how would I sample sensor data from the pcDuino? Is it just a matter of modifying example code? Since the pcDuino is a stand-alone linux PC would it be as simple as just installing plot ly ?</p>
<p>pcDuino, cool! If you're comfortable with Python, you can hook into streaming with our Python-Plotly API. </p><p>There is a more bare-bones script/tutorial to do this in our Raspberry-Pi repo here: <a href="https://github.com/plotly/raspberrypi" rel="nofollow">https://github.com/plotly/raspberrypi</a></p><p>And some examples that you can look at in our Streaming Demos here: <a href="http://nbviewer.ipython.org/github/plotly/Streaming-Demos/tree/master/IPython%20examples/" rel="nofollow">http://nbviewer.ipython.org/github/plotly/Streamin...</a></p><p>Does that help?</p>
<p>Modified the example script to work with an I2C-based TMP102 sensor from Sparkfun. Shot a screencast of it if anyone is interested in seeing how to modify the example source.</p><p><a href="http://youtu.be/IIcM1nR-omg" rel="nofollow">http://youtu.be/IIcM1nR-omg</a></p>
<p>Hello man i see you got a lot of experience, can you share me the compiler you are using in the video, are there any examples, thanks in advice.</p>
<p>I just modified the example python script from plot.ly so there is no compiler. It's just executed using Python 2.7.3 which is installed on my Raspberry Pi. Hopefully that answers your question. If not, let me know.</p>
when I'm using ploty, possible to embed the image to other website?
<p>Yup! You can embed them as iFrames in HTML. They'll remain interactive and data will continue streaming to them!</p><p>Here's example iFrame code: <a href="http://plot.ly/api/python/docs/iframes" rel="nofollow">http://plot.ly/api/python/docs/iframes</a></p><p>And here is an example of a plotly graph embedded in a website: <a href="http://wapo.st/14F1Zwc" rel="nofollow">http://wapo.st/14F1Zwc</a></p><p>Thanks for reaching out!</p>

About This Instructable




More by plotlygraphs:Plotly + Atlas Scientific: Graph Real-Time Dissolved Oxygen with Raspberry Pi Real-time Graphing with the Raspberry Pi Embedding Interactive Graphs in Blogs and Websites 
Add instructable to: