Code: Select all
import httplib, urllib, os, glob, time
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
# Add sensors here for more data
temp_0 = 0
temp_1 = 0
temp_2 = 0
temp_3 = 0
for sensors in range (4): # CHANGE THIS NUMBER to match how many sensors are attached
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[sensors]
device_file = device_folder + '/w1_slave'
print device_file
print sensors
def read_temp_raw(): # Gathers each temp
f = open(device_file, 'r')
lines = f.readlines()
f.close()
return lines
def read_temp(): # Checks for any errors
lines = read_temp_raw()
while lines[0].strip()[-3:] != 'YES':
time.sleep(0.2)
lines = read_temp_raw()
equals_pos = lines[1].find('t=')
if equals_pos != -1:
temp_string = lines[1][equals_pos+2:]
# set proper decimal place for C
temp = float(temp_string) / 1000.0
# Round temp to 2 decimal points
temp = round(temp, 1)
return temp
# Asks for temps to be read and store each in a variable
if sensors == 0:
temp_0 = read_temp()
temp_1 = read_temp()
temp_2 = read_temp()
temp_3 = read_temp()
if sensors == 1:
temp_1 = read_temp()
print temp_0
print temp_1
print temp_2
print temp_3
#Gathers data and controls to which field it goes to and where the data is coming from
temp_0 = read_temp()
temp_1 = read_temp()
temp_2 = read_temp()
temp_3 = read_temp()
#Be sure to add new field and temp info when adding more sensors
params = urllib.urlencode({'field1': temp_0, 'field2': temp_1, 'field3': temp_2, 'field4': temp_3, 'key':''})
headers = {"Content-type": "application/x-www-form-urlencoded","Accept":
"text/plain"}
conn = httplib.HTTPConnection("api.thingspeak.com:80")
conn.request("POST", "/update", params, headers)
response = conn.getresponse()
print response.status, response.reason
data = response.read()
conn.close()