3

Can anyone help me how to find the address of DS18b20 temp sensor automatically and display the temperature using a simple python code?

Assazzin
  • 193
  • 3
  • 9

1 Answers1

6

I don't know about simple but the following works on my Pis.

#!/usr/bin/env python

# Public Domain

import glob
import time

# Typical reading
# 73 01 4b 46 7f ff 0d 10 41 : crc=41 YES
# 73 01 4b 46 7f ff 0d 10 41 t=23187

while True:

   for sensor in glob.glob("/sys/bus/w1/devices/28-00*/w1_slave"):
      id = sensor.split("/")[5]

      try:
         f = open(sensor, "r")
         data = f.read()
         f.close()
         if "YES" in data:
            (discard, sep, reading) = data.partition(' t=')
            t = float(reading) / 1000.0
            print("{} {:.1f}".format(id, t))
         else:
            print("999.9")

      except:
         pass

   time.sleep(3.0)
joan
  • 71,852
  • 5
  • 76
  • 108