1. #!/usr/bin/python
  2. import httplib, urllib
  3. import os
  4. import glob
  5. import time
  6.  
  7. def doit():
  8.  
  9.  def read_B18_Raw():
  10.     os.system('modprobe w1-gpio')
  11.     os.system('modprobe w1-therm')
  12.     Params = urllib.urlencode({'field1': temp_f, 'key':'my key'})
  13.     Headers = {"Content-type": "application/x-www-form-urlencoded","Accept": "text/plain"}
  14.     conn = httplib.HTTPConnection("api.thingspeak.com:80")
  15.  
  16.     base_dir = '/sys/bus/w1/devices/'
  17.     device_folder = glob.glob(base_dir + '28*')[0]
  18.     device_file = device_folder + '/w1_slave'
  19.     f = open(device_file, 'r')
  20.     lines = f.readlines()
  21.     f.close()
  22.     return lines
  23.  
  24.  def read_B18():
  25.     lines = read_B18_Raw()
  26.     while lines[0].strip()[-3:] != 'YES':
  27.             time.sleep(0.2)
  28.             lines = read_B18_Raw()
  29.     equals_pos = lines[1].find('t=')
  30.     if equals_pos != -1:
  31.         temp_string = lines[1][equals_pos+2:]
  32.         temp_c = float(temp_string) / 1000.0
  33.         temp_f = temp_c * 9.0 / 5.0 + 32.0
  34.         return "%.1f" % (temp_f)
  35.  
  36.     try:
  37.         conn.request("POST", "/update", params, headers)
  38.         response = conn.getresponse()
  39.         print temp_f
  40.         data = response.read()
  41.         temp_f = read_temp()
  42.         conn.close()
  43.     except:
  44.         print "connection failed"
  45.  
  46. #sleep for 16 seconds (api limit of 15 secs)
  47. if __name__ == "__main__":
  48.     temp_f = 0
  49.     while True:
  50.         doit()
  51.         time.sleep(16)