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
$ chmod +x adafruit-cosm-temp.py
$ sudo ./adafruit-cosm-temp.py