Last active
March 25, 2022 09:51
-
-
Save discarn8/77840668fee4fc3ded95a7847012ee10 to your computer and use it in GitHub Desktop.
Python script to get temperature reading from DS18B20 sensor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import os | |
import time | |
if os.path.isdir('/sys/bus/w1/devices/28-011111ffffff/'): | |
os.system('/sbin/modprobe w1-gpio') | |
os.system('/sbin/modprobe w1-therm') | |
temp_sensor = '/sys/bus/w1/devices/28-011111ffffff/w1_slave' | |
while(True): | |
t = open(temp_sensor, 'r') | |
lines = t.readlines() | |
t.close() | |
if not len(lines) == 0: | |
temp_output = lines[1].find('t=') | |
temp_string = lines[1].strip()[temp_output+2:] | |
temp_f = ((float(temp_string)/1000.0)*1.8+32) | |
temp_f = str(round(temp_f,1)) | |
f = open("/var/www/html/temp.txt", "w") | |
f.write(temp_f) | |
f.close() | |
time.sleep(2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment