Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

thingspeak_room_temp_loop.py

By: a guest on Apr 14th, 2014  |  syntax: Python  |  size: 1.33 KB  |  views: 48  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/usr/bin/python
  2. import httplib, urllib
  3. import os
  4. import glob
  5. import time
  6.  
  7.  
  8. def read_B18_Raw():
  9.     os.system('modprobe w1-gpio')
  10.     os.system('modprobe w1-therm')
  11.     Params = urllib.urlencode({'field1': temp_f, 'key':'my key'})
  12.     Headers = {"Content-type": "application/x-www-form-urlencoded","Accept": "text/plain"}
  13.     conn = httplib.HTTPConnection("api.thingspeak.com:80")
  14.  
  15.     base_dir = '/sys/bus/w1/devices/'
  16.     device_folder = glob.glob(base_dir + '28*')[0]
  17.     device_file = device_folder + '/w1_slave'
  18.     f = open(device_file, 'r')
  19.     lines = f.readlines()
  20.     f.close()
  21.     return lines
  22.  
  23. def read_B18():
  24.     lines = read_B18_Raw()
  25.     while lines[0].strip()[-3:] != 'YES':
  26.             time.sleep(0.2)
  27.             lines = read_B18_Raw()
  28.     equals_pos = lines[1].find('t=')
  29.     if equals_pos != -1:
  30.         temp_string = lines[1][equals_pos+2:]
  31.         temp_c = float(temp_string) / 1000.0
  32.         temp_f = temp_c * 9.0 / 5.0 + 32.0
  33.         return "%.1f" % (temp_f)
  34.  
  35.         try:
  36.                 conn.request("POST", "/update", params, headers)
  37.                 response = conn.getresponse()
  38.                 print temp_f
  39.                 data = response.read()
  40.                 temp_f = read_temp()
  41.                 conn.close()
  42.         except:
  43.                 print "connection failed"
  44.  
  45. #sleep for 16 seconds (api limit of 15 secs)
  46. if __name__ == "__main__":
  47.         temp_f = 0
  48.         while True:
  49.                 read_B18()
  50.                 time.sleep(16)
clone this paste RAW Paste Data