Sorry it took so long for the reply. Work was kicking my @$$ this week…
Here is the code that I’m using. I’ve added some comments so that you can change the parts to fit your needs for your thingspeak account.
#! /usr/bin/python
import os
import glob
import datetime
import time
import urllib
import urllib2
import sys
THINGSPEAKKEY = 'yourthingspeakcodehere'
THINGSPEAKURL = 'https://api.thingspeak.com/update'
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
#Change to your device path(s)
lst = []
lst.append('/sys/bus/w1/devices/28-000001/w1_slave')
lst.append('/sys/bus/w1/devices/28-000002/w1_slave')
lst.append('/sys/bus/w1/devices/28-000003f/w1_slave')
#num_sensors = len(lst)
#print "Number of sensors: ", num_sensors
def get_temp(device):
#To read the sensor data, just open the w1_slave file
f = open(device, 'r')
data = f.readlines()
f.close()
deg_f = ''
if data[0].strip()[-3:] == 'YES':
temp = data[1][data[1].find('t=')+2:]
#If temp is 0 or not numeric an exception
#will occur so lets handle it gracefully
try:
if float(temp)==0:
deg_f = 32
else:
deg_f = (float(temp)/1000)*9/5+32
except:
print "Error with t=", temp
pass
return deg_f
date_log = datetime.datetime.now().strftime("%m-%d-%Y %H:%M")
#device_name = lst[0].split('/')[5]
#When there are multiple devices, a short pause
#interval between reading sensors seems to work best
intake = str(get_temp(lst[0]))
time.sleep(1)
exhaust = str(get_temp(lst[1]))
time.sleep(1)
pcexhaust = str(get_temp(lst[2]))
def sendData(url,key,field1,field2,field3,temp1,temp2,temp3):
"""
Send event to internet site
"""
values = {'key' : key, 'field1' : temp1, 'field2' : temp2, 'field3' : temp3}
postdata = urllib.urlencode(values)
req = urllib2.Request(url, postdata)
log = date_log + ","
log = log + intake + ","
log = log + exhaust + ","
log = log + pcexhaust + ","
try:
# Send data to Thingspeak
response = urllib2.urlopen(req, None, 5)
html_string = response.read()
response.close()
log = log + 'Update ' + html_string
except urllib2.HTTPError, e:
log = log + 'Server could not fulfill the request. Error code: ' + e.code
except urllib2.URLError, e:
log = log + 'Failed to reach server. Reason: ' + e.reason
except:
log = log + 'Unknown error'
print log
sendData(THINGSPEAKURL,THINGSPEAKKEY,'field1','field2','field3',intake,
exhaust,pcexhaust)
sys.stdout.flush()
You will have to change the field1, field2, etc. to match the number of fields and their titles to fit your setup. As you can see from the code above, I have three temp sensors, one near the intake for the door, one near the exhaust in the ceiling, and one near the exhaust near the PC. I hope that this helps.