Tuesday, February 04, 2014

Plot.ly and Pi

I found Plotly since early last year, it is a very interesting online data visualization tool. I always want to use it for some logging purpose. So finally I got my Pi and I start to do a simple task, to log the CPU temperature, and here is the result.



I use Python as the programming language since it is the most famous language for Pi. If you new to Pi, here is the step to install Python.

  • sudo apt-get install python-dev
  • wget http://python-distribute.org/distribute_setup.py
  • python distribute_setup.py
  • wget https://raw.github.com/pypa/pip/master/contrib/get-pip.py
  • python get-pip.py
  • sudo pip install virtualenv

Once you have installed the Python on your Pi, the next task is to install plot.ly API.

  • pip install plotly

At the same time you can register an account with plot.ly in order to use their services.

Now everything is ready, and we need to create a Python program to get the temperature data and upload it to plot.ly. Before I continue, there is something about Python that you should know, especially C/C++ programmer.

First, you do not need to put semicolon (;) at the end of the line. It looks like a Basic code.
Second, do not mix tap and space. You will get an error if you mixed it.
Third, you do not need to declare the variable. Ok, it looks like Basic now.

I'm very new to Python with nearly zero knowledge about this language. Thanks to the Internet, you will get some examples to help you to get the code you needed.

So here is the code.

  • #!/usr/bin/env python
  • import plotly
  • import os
  • import subprocess
  • import datetime

  • def update_temp():
  •   py = plotly.plotly(username_or_email='YOUR_USERNAME', key='YOUR_API_KEY')
  •   i = datetime.datetime.now()
  •   proc = subprocess.Popen(["cat", "/sys/class/thermal/thermal_zone0/temp"], stdout=subprocess.PIPE)
  •   t = proc.stdout.read()
  •   r =  py.plot(i.strftime('%Y-%m-%d %H:%M:%S'),float(t)/1000,
  •   filename='RPiTempCont',
  •   fileopt='extend',
  •   layout={'title': 'Raspberry Pi Temperature Status'})

  • if __name__ == '__main__':
  •   import sys
  •   update_temp() 

So that is the code that get the current date, time and temperature and upload to your plot.ly account.

In order to do it automatically, you need to add the program to cron job. But before that you need to save the Python program to a location. For my case, I put it at

  • /home/pi/scripts/

To add the task to cron, you need the following commands

  • crontab -e

Then add the task

  • */10 * * * * usr/home/python /home/pi/scripts/YOUR_PYTHON_FILE.py > /dev/null

What it does is to run the Python program every 10 minutes for 24x7x365. Now you can monitor your Pi temperature in real time.

I believe you will not satisfy with this simple task only. So, my next project will be logging weather information including temperature, relative humidity and solar irradiation. Yes, you are right, solar irradiation, to monitor my mini PV farm.

Next post coming soon.

No comments:

Post a Comment