I never programmed in python before, but here is a quick example.
The basic logic is:
Open file and read in data.
Parse the data into a useable format; in this case we want to convert from String type to Integer [number].
Divide by 1000 to get degrees in Celsius.
Use some flow control statements to determine logic.
Code: Select all
fo = open("/sys/class/thermal/thermal_zone0/temp") # opens the file
rawTemp = fo.read(); # reads contents to string
fo.close() # cleans up our file handler
# To get temperature in Celsius we must divide value by 1000
tempInC = int(rawTemp)/1000 # casts raw data to Integer, then divides by 1000
#print(tempInC); # out puts data, uncomment to debug.
if tempInC < 40:
print "Below 40 degrees, turning fan off. Current Temp: " +str(tempInC)
# code to turn fan off
elif tempInC > 50:
print "Above 50 degrees, turning fan on. Current Temp: " +str(tempInC)
# code to turn fan on
Maybe someone more familiar with python can give us some pointers, but let me know if you have any questions on that.
I have a question for you though, what if the temperature is 41 or 48 degrees? In our logic, that isn't accounted for so that is a case we probably want to define, should the fan be on or off or we don't care or other action?