Advertisement
Guest User

Untitled

a guest
Sep 20th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. # Note:
  4. # ds18b20's data pin must be connected to pin7.
  5. # Reads temperature from sensor and prints to stdout
  6. # id is the id of the sensor
  7.  
  8. import os
  9. import time
  10.  
  11. def readSensor(id):
  12. tfile = open("/sys/bus/w1/devices/"+id+"/w1_slave")
  13. text = tfile.read()
  14. tfile.close()
  15. secondline = text.split("\n")[1]
  16. temperaturedata = secondline.split(" ")[9]
  17. temperature = float(temperaturedata[2:])
  18. temperature = temperature / 1000
  19. print "Sensor: " + id + " - Current temperature : %0.3f C" % temperature
  20.  
  21.  
  22. # Reads temperature from all sensors found in /sys/bus/w1/devices/
  23. # starting with "28-...
  24. def readSensors():
  25. count = 0
  26. sensor = ""
  27. for file in os.listdir("/sys/bus/w1/devices/"):
  28. if (file.startswith("28-")):
  29. readSensor(file)
  30. count+=1
  31. if (count == 0):
  32. print "No sensor found! Check connection"
  33.  
  34. # read temperature every second for all connected sensors
  35. def loop():
  36. while True:
  37. readSensors()
  38. time.sleep(1)
  39.  
  40. # Nothing to cleanup
  41. def destroy():
  42. pass
  43.  
  44. # Main starts here
  45. if __name__ == "__main__":
  46. try:
  47. loop()
  48. except KeyboardInterrupt:
  49. destroy()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement