Introduction: Real-time Graphing With the Raspberry Pi

Picture of Real-time Graphing With the Raspberry Pi

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

Picture of 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

Picture of 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:
http://learn.adafruit.com/adafruits-raspberry-pi-lesson-6-using-ssh

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.
http://www.interlockroc.org/2012/12/06/raspberry-pi-macgyver/

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:

https://github.com/plotly/raspberrypi

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

Picture of 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
MCP3008 DGND (PIN9) : GND

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:

https://plot.ly/~demos/1415/

Comments

VìctorC167 (author)2017-05-05

Hello, i´m doing this I really apreciate the information, but I got a problem in the tutorial on streaming with the raspberry pi, the link https://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py is a dead link.

ChrisM855 (author)2017-04-21

Aloha,

Does anyone know if i can swap the recommended temperature sensor for a DS18B20
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.

Thanks!

bulletbilal123 (author)2016-12-10

help please! what i have to do?

snoop911 (author)2015-08-08

Curious about how this works under the hood.. does the pc act a client requesting data from the raspberry pi (web?) server?

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.

Or if using a wifi shield, what wifi layers/protocols would be needed for real-time data transfer?

Ranasoraus (author)snoop9112015-09-08

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!

miraspear made it! (author)2015-01-24

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.

dauletle (author)2014-08-26

I'm currently attempting to execute this project
using an I2C accelerometer from SparkFun. The sensor returns a value but I keep
getting a 411 error in addition to this

Traceback (most recent call last):
File "packAccel.py", line 277, in <module>
stream.write(dict(x=i, y=sensor_data[0]))
File "/usr/local/lib/python2.7/dist-packages/plotly/plotly/plotly.py", line 501, in write
self._stream.write(jdata, reconnect_on=reconnect_on)
File "/usr/local/lib/python2.7/dist-packages/plotly/plotly/chunked_requests/chunked_request.py", line 53, in write
msg=response.read()))
Exception: Server responded with status code: 411
and message: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html><head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>ERROR: The requested URL could not be retrieved</title> <style type="text/css"><!-- %l body :lang(fa) { direction: rtl; font-size: 100%; font-family: Tahoma, Roya, sans-serif; float: right; } :lang(he) { direction: rtl; float: right; } --></style> </head><body> <div id="titles"> <h1>ERROR</h1> <h2>The requested URL could not be retrieved</h2> </div> <hr> <div id="content"> <p><b>Invalid Request</b> error was encountered while trying to process the request:</p> <blockquote id="data"> <pre>POST / HTTP/1.1

I
assume this means that plotly is not accepting data I'm sending. My
username, apikey and token are all correct in the sensor code. There are
no syntax errors either. I am very new to plot.ly, so I'm certain that
I'm missing something simple. Any help would be appreciated. The
tutorial is very well written and appreciated.

plotlygraphs (author)dauletle2014-08-26

Hey dauletle --
Thanks so much for trying it out!
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__)

dauletle (author)plotlygraphs2014-08-27

Hi plotlygraphs. I'm using Python 2.7.3 [GCC 4.6.3] on linux2, with plotly version 1.2.7

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

stream.write({'x': i, 'y': sensor_data})

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.

relic1974 (author)2014-03-06

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 ?

plotlygraphs (author)relic19742014-03-14

pcDuino, cool! If you're comfortable with Python, you can hook into streaming with our Python-Plotly API.

There is a more bare-bones script/tutorial to do this in our Raspberry-Pi repo here: https://github.com/plotly/raspberrypi

And some examples that you can look at in our Streaming Demos here: http://nbviewer.ipython.org/github/plotly/Streamin...

Does that help?

sidwarkd made it! (author)2014-03-01

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.

http://youtu.be/IIcM1nR-omg

ruguberto (author)sidwarkd2014-03-14

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.

sidwarkd (author)ruguberto2014-03-14

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.

funky81 (author)2014-03-04

when I'm using ploty, possible to embed the image to other website?

plotlygraphs (author)funky812014-03-04

Yup! You can embed them as iFrames in HTML. They'll remain interactive and data will continue streaming to them!

Here's example iFrame code: http://plot.ly/api/python/docs/iframes

And here is an example of a plotly graph embedded in a website: http://wapo.st/14F1Zwc

Thanks for reaching out!

About This Instructable

208,029views

369favorites

License:

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