The next program is triggered periodically by "cron". I have it set to read every 10 minutes.
This one is really messy, I will clean it up soon, but it does work. The calibration for the thermisters is done by loading data from a calibration table.
#!/usr/bin/python
import httplib, urllib
# download from <a href="
http://code.google.com/p/psutil/" title="
http://code.google.com/p/psutil/">
http://code.google.com/p/psutil/</a>
import RPi.GPIO as GPIO, spidev, time, math, os, json, datetime
# use P1 header pin numbering convention
#GPIO.setmode(GPIO.BCM)
#Calibration info
DEBUG = 0
Voltage = 0.0
Slope = -534.692
Divider0 = 10000.0
data = json.load(open('./Documents/lookup.json'))
reading = 511
# Open.sen.se info
SENSE_API_KEY = "your_key"
FEED_ID1 =
“an_id”
FEED_ID2 =
“an_id”
FEED_ID3 =
“an_id”
FEED_ID4 =
“an_id”
FEED_ID5 =
“an_id”
Air_Temperature =
“an_id”
Fish_Tank_Temperature =
“an_id”
Grow_Bed_Temperature =
“an_id”
Light_Level =
“an_id”
GB1_Siphon_State =
“an_id”
GB2_Siphon_State =
“an_id”
GB3_Siphon_State =
“an_id”
Sump_Tank_Level =
“an_id”
Yabbie_Tank_Level =
“an_id”
Water_Level =
“an_id”
delay = 0
samples = 5
def get_adc(channel): # Read by SPI
spi = spidev.SpiDev()
spi.open(0,0)
spi.max_speed_hz = 4000
# read SPI data from MCP3008 chip
# Only 8 channels 0 to 7 else return -1
if ((channel > 7) or (channel < 0)):
return -1
#time.sleep(delay)
sum = 0
maximum = 0
minimum = 1023
for value in range(samples):
r = spi.xfer2([1,(8+channel)<<4,0])
ret = ((r[1]&3) << 8) + r[2]
#print minimum, maximum
if ret < minimum:
minimum = ret
if ret > maximum:
maximum = ret
#print ret,
sum = sum + ret
#print "Average = ", ret, " Max = ", maximum, " Min = ", minimum
#Average = (sum/samples)
ret = (sum-maximum-minimum)/(samples-2)
#print "Return = ", ret, " Average = ", Average, " Max = ", maximum, " Min = ", minimum
#time.sleep(1)
# Finished reading so turn off channel
spi.close()
#GPIO.cleanup()
return ret
def calibrate(reading, parameter): # Different calibration for different sensors
#reading = 1023 - reading
#print "Resistance = ", Resistance
if parameter == "Light":
Resistance = ((1023.0*Divider0)/(1023.0-reading))-Divider0
Light = (Resistance)
return Light/100
if parameter == "Temp":
#print reading
# Lookup Temperature from imported data table
reading = int(reading)
#print reading
TempC = data.get(str(reading))
#print TempC
return TempC
if parameter == "Level":
return reading/10.0
def senseit(sensedata):
#print "\t=> Sending to OpenSense: %s" % sensedata
# prepare data
datalist = [{"feed_id" : Air_Temperature, "value" :sensedata['temp1']},
{"feed_id" : FEED_ID2, "value" :sensedata['temp2']},
{"feed_id" : FEED_ID3, "value" :sensedata['temp3']},
{"feed_id" : FEED_ID4, "value" :sensedata['light']},
]
headers = {"sense_key": SENSE_API_KEY,"content-type": "application/json"}
conn = httplib.HTTPConnection("api.sen.se")
# format a POST request with JSON content
conn.request("POST", "/events/", json.dumps(datalist), headers)
response = conn.getresponse()
# you may get interesting information here in case it fails
#print response.status, response.reason
#print response.read()
conn.close()
def doit():
field1 = 0.010
#field2 = 100.0
#field3 = 100.0
#field4 = 100.0
for channel in range(4):
Voltage = get_adc(channel)
#print Voltage
#print channel, Voltage
if Voltage <> 0:
if channel == 0:
para = "Light"
field1 = round(calibrate(Voltage, para),1)
elif channel == 4:
para = "Level"
field5 = calibrate(Voltage, para)
else:
para = "Temp"
if channel == 1:
field2 = calibrate(Voltage, para)
elif channel == 2:
field3 = calibrate(Voltage, para)
elif channel == 3:
field4 = calibrate(Voltage, para)
#time.sleep(1)
#print channel, " ", Voltage, para, round(calibrate(Voltage, para),1)
sensedata = { 'temp1' : field2, 'temp2' : field3, 'temp3' : field4, 'light' : field1}
#print sensedata
senseit(sensedata)
# Run Main
if __name__ == "__main__":
doit()