Skew

Reading temperatures DS18B20

May 1st, 2015
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.52 KB | None | 0 0
  1.         #Getting temperatures from thermometers
  2.         debug("Getting temperatures")
  3.         try:
  4.             terms = ["28-001414cf69ff","28-001414baa9ff","28-0014154b59ff","28-0014153bb1ff"]
  5.             data = temps[:]
  6.             for termid in range(4):
  7.                 term = terms[termid]
  8.                 for index in range(10):
  9.                     debug("Trying to get temperature from "+term+" ("+str(index-1)+" attempt)");
  10.                     #Be sure that thermometer exists
  11.                     if os.path.exists("/sys/bus/w1/devices/"+term+"/w1_slave"):
  12.                         #Read data from thermometer
  13.                         debug("Reading temperature from "+term);
  14.                         f = open("/sys/bus/w1/devices/"+term+"/w1_slave")
  15.                         lines = f.readlines()
  16.                         f.close()
  17.                         #Parse it to readable format
  18.                         debug("Parsing temperature from "+term);
  19.                         index = lines[0].index("crc=")
  20.                         if lines[0][index:].split(" ")[1].split("\n")[0] == "YES":
  21.                             #CRC is correct, read temp
  22.                             tmp = int(lines[1][lines[1].index("t=")+2:lines[1].index("\n")])/1000.0
  23.                             if tmp > 35.0 or tmp < 0:
  24.                                 error("Failed to read temp, value out of range ("+str(tmp)+"C).")
  25.                                 time.sleep(3)
  26.                             else:
  27.                                 debug("Got temperature from "+term+"! ("+str(tmp)+"C)");
  28.                                 data[termid] = tmp
  29.                                 break
  30.                         else:
  31.                             error("Failed to read temp, checksum is not valid")
  32.                             time.sleep(3)
  33.                     else:
  34.                         error("Thermometer "+term+" doesn't exist!")
  35.                         time.sleep(3)
  36.             if(len(data) < 4):
  37.                 error("Failed to read temp from all thermometers")
  38.             temps = data[:]
  39.         except Exception, e:
  40.             error("Exception while updating temps - "+str(e))
Advertisement
Add Comment
Please, Sign In to add comment