Raspberry Pi with Xively

Xively is a IoT Website that graphs data for you. In This tutorial, we will learn how to Send The Raspberry Pi's CPU temperature, Free Disk Space, and Free and Used Ram to Xively, and use Cron to schedule the python script to run every 5 minutes. Here Is what we will create, setup on my RPi: https://xively.com/feeds/1579958259

1. First of all, go and create an account at Xively: https://xively.com/ , then check your email for the address you gave them and validate your account in the email they send.

2. Now, make sure you're logged in, and you should be greeted by the 'Test Drive' Page. You don't Really need to take this, so you can click 'Skip The Test Drive'

3. After skipping the test drive, you should see the Development Devices Homepage. This is where we will add and control our Raspberry Pi outputs. Don't be fooled by the invitation to 'Connect a Raspberry Pi' - Their specially designed package doesn't work! 

4. Now we Need to give our device a name, Such as 'Raspberry Pi' and make it a private device (For the moment!)

5, This will take us to our device homepage, Where we can begin to add our data channels, such as Temperature or Disk Space. Theres a few key detail on this page. Notice where it says 'Feed ID' at the top, and 'API Keys' In the middle. These are how we will link our python code to the Xively page. 

6. We now need to install some modules onto our raspberry pi before we begin writing the python code. I'm going to assume you have the Pi setup, and either using a keyboard and mouse or using SSH to access it. I'm using the latter, but it really doesn't matter which you use, only that you can copy the code from here directly into the python file over SSH, which is a little easier than typing it out! If using SSH, you can type the following commands straight in, but if you need to open a terminal prompt if you're using a screen.

In you terminal window, you need to type "sudo apt-get install python-dev "
and once that's installed "sudo apt-get install python-pip "

7. Now we need to install EEML - the markup language that our Python - Xively script uses.
To do this, type in "wget -O geekman-python-eeml.tar.gz https://github.com/geekman/python-eeml/tarball/master "

Once it has downloaded, use "tar zxvf geekman-python-eeml.tar.gz" to extract it;
and then "cd geekman-python-eeml*" to change into the extracted files directory;

and finally "sudo python setup.py install" to install it.

8. Now we've installed the required modules, we can begin writing the python script. 
Let's go back to the home directory with "cd" and make a new directory for our project using "sudo mkdir xively". Now create the python file using "sudo nano xively.py" which opens an empty .py file.
9. Now, copy and paste the following code into this window:
#!/usr/bin/env python
import time
import os
import eeml
import sys
import syslog
import json

def getCPUtemperature():
    res = os.popen('vcgencmd measure_temp').readline()

def getDiskSpace():
    p = os.popen("df -h /")
    i = 0
    while 1:
        i = i +1
        line = p.readline()
        if i==2:

def getRAMinfo():
    p = os.popen('free')
    i = 0
    while 1:
        i = i + 1
        line = p.readline()
        if i==2:

API_URL = '/v2/feeds/{feednum}.xml' .format(feednum = FEED)

CPU_temp = getCPUtemperature()
DISK_stats = getDiskSpace()
DISK_total = DISK_stats[0]
DISK_free  = DISK_stats[1]
DISK_perc  = DISK_stats[3]
RAM_stats = getRAMinfo()
RAM_total = round(int(RAM_stats[0]) / 1000,1)
RAM_used  = round(int(RAM_stats[1]) / 1000,1)
RAM_free  = round(int(RAM_stats[2]) / 1000,1)

# open up your feed
pac = eeml.Pachube(API_URL, API_KEY)

#compile data
pac.update([eeml.Data("CPU_Temperature", CPU_temp, unit=eeml.Celsius())])
pac.update([eeml.Data("Disk_free", DISK_free, unit=eeml.Celsius())])
pac.update([eeml.Data("RAM__Used", RAM_used, unit=eeml.Celsius())])
pac.update([eeml.Data("RAM_Free", RAM_free, unit=eeml.Celsius())])

# send data to cosm

This looks overwhelming at first, but it's actually quite easy to understand. At the top, we import the needed modules, then define the functions to read our various Temperature, Disk space and RAM variables. We then see the API_KEY field, which you will edit in a minute. Below this are our variables, taken from the functions above and formatted to be easily readable. After this, we see the pac.update lines, which tell xively the channel name and variable to graph, along with the units. In these lines, the words in Quotes (" ") are the names of each field as we want them to appear on our xively homepage. The next part, for example "CPU_temp", is the variable we want to graph, and finally the units the data will be displayed with. EEML only accepts SI or SI derived units, which is a pain as there are no SI units related to data size, like MB or GB, so for now i've left them as Celsius. 

10. At current, this code won't work, as you need to add you API key and Feed Number from you xively page. Go back to your Xively homepage, and look for the 'API Keys" segment and the "Feed ID". Copy these into the code where it says 'Insert API key here' and 'Insert Feed Number here' Note: The API key needs to have single quotes around it, and the feed id must not have them (for some unknown reason).

11. Now you can use Ctrl O , followed by enter, to save the file, and Ctrl X to exit! Our python Script is done, and now we can test it. type "python xively.py" and keep an eye on your xively feed. If channels appear, then Success! If not, go back and troubleshoot, checking the code and that you added your own API key and Feed ID.
Note: You won't have a graph yet, as xively needs two or more datapoints to create one.

12. We now need to make sure the file can be executed, so, while in your xively directory, type "sudo chmod +x ./xively.py"
Finally, we need to setup a Crontab task to automatically run our xively.py file every 5 minutes. Type "cd" in your command prompt, then "crontab -e", opening this window: 
Go to the bottom, then add this line "*/5 * * * * python /home/pi/xively/xively.py" This basically means 'Run this file every 5 minutes using python". The time between updates can be easily changed by changing the /5 to /10 for every ten minutes or /60 for every hour. 
If you want to look at more complex Cron formatting, have a look here:  http://www.nncron.ru/help/EN/working/cron-format.htm
Now simply do Ctrl O, enter, Ctrl X to save and exit! Reboot with "sudo reboot" and your xively page should begin to autoupdate every 5 minutes!