Python Script

Please Note: Xively no longer has free developer access to their system, so this tutorial is only for historical research. Please check out our other IoT tutorials for alternative services!

The Code

This 100+ line python script can be pasted into a editor and saved on your raspberry pi.

The script is fairly simple. Half of the code (the readadc function) is a function that will 'talk' to the MCP3008 chip using four digital pins to 'bit bang' the SPI interface (this is because not all Raspberry Pi's have the hardware SPI function).

The MCP3008 is a 10-bit ADC. That means it will read a value from 0 to 1023 (2^^10 = 1024 values) where 0 is the same as 'ground' and '1023' is the same as '3.3 volts'. We don't convert the number to voltage although its easy to do that by multiplying the number by (3.3 / 1023).

Every 30 seconds we:
  • read the adc value on channel 0 (temperature sensor)
  • convert the adc value to millivolts: millivolts = read_adc0 * ( 3300.0 / 1023.0 )
  • convert the millivolts value to a celsius temperature: temp_C = ((millivolts - 100.0) / 10.0) - 40.0
  • convert the celsius temperature to a fahrenheit temperature: temp_F = ( temp_C * 9.0 / 5.0 ) + 32 )
  • then send the data up to pachube to be saved and graphed
http://gist.github.com/3249416

Feeds and Keys

Update the API_KEY and FEED values to the ones that COSM provided you.
Copying over the API key incorrectly is a common (and easy to make) mistake. So have another person check your typing if you have problems!

Run it!

Now that you have the code modified with your keys, go ahead and make the file executable.
$ chmod +x adafruit-cosm-temp.py
Run the script. With DEBUG = 1 (default) you will see of the adc0 value, millivolts, celsius and fahrenheit on sent to your terminals STDOUT. These same values are also being sent up to COSM. 
$ sudo ./adafruit-cosm-temp.py
If you're having python crash due to an unstable internet connection, check out this handy thread over at CoSM http://community.cosm.com/node/114
Last updated on 2015-11-19 at 06.54.54 PM Published on 2012-07-29 at 11.58.37 AM