External sensors (#153)
* proof of principle, a DS18B20 is read and its value is transferred via MQTT, also discovery message works * added names for ext sensors * bugfix for names: there was a problem with the topics & added SHT21 support * catch exceptions if a problem during readout occurs, shrink comments in config-file * include ext_sensors in README file * work in progress * bugfix, also for ID of ds18b20 sensor * Revert "bugfix, also for ID of ds18b20 sensor" This reverts commit 1353b840b0b6142670ec59438db4b33fab764b8e. file was intended for another branch * Revert "work in progress" This reverts commit b3e884f79f5356b79f64931cb4d45a150bfce2c7. these changes were intended for another branch * bugfix for ID of ds18b20 sensor
This commit is contained in:
61
ext_sensor_lib/ds18b20.py
Executable file
61
ext_sensor_lib/ds18b20.py
Executable file
@@ -0,0 +1,61 @@
|
||||
#!/usr/bin/python3
|
||||
import time
|
||||
import os
|
||||
"""
|
||||
This reads the temp value of a DS18B20 sensor
|
||||
"""
|
||||
|
||||
|
||||
def sensor_DS18B20(sensor_id, verbose=False):
|
||||
"""
|
||||
:param sensor_id: Name of the sensor id, can be found in /sys/bus/w1/devices
|
||||
:type sensor_id: string
|
||||
"""
|
||||
# read the file
|
||||
try:
|
||||
file = open('/sys/bus/w1/devices/28-%s/w1_slave' % sensor_id)
|
||||
filecontent = file.read()
|
||||
file.close()
|
||||
# read temperature value and convert it
|
||||
stringvalue = filecontent.split("\n")[1].split(" ")[9]
|
||||
temperature = float(stringvalue[2:]) / 1000
|
||||
except IOError as e:
|
||||
# if an error occurs, we return -300
|
||||
temperature = float(-300)
|
||||
|
||||
# format the temperature
|
||||
temp = '%6.1f' % temperature
|
||||
temp = round(temperature, 1)
|
||||
# if we set the verbose, we print the current temperature
|
||||
if verbose:
|
||||
print ("1-wire %s: temp=%.1f" % (sensor_id, temp))
|
||||
return temp
|
||||
|
||||
|
||||
def get_available_sensors():
|
||||
"""Returns all available sensors"""
|
||||
# https://github.com/rgbkrk/ds18b20/blob/master/ds18b20/__init__.py
|
||||
sensors = []
|
||||
for sensor in os.listdir("/sys/bus/w1/devices"):
|
||||
if sensor.startswith("28-"):
|
||||
# we put all sensor ids into the list (except the "28-" part)
|
||||
sensors.append(sensor[3:])
|
||||
return sensors
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
# call the function to get all sensor_ids as a list
|
||||
sensors_ids = get_available_sensors()
|
||||
while True:
|
||||
for sensor_id in sensors_ids:
|
||||
sensor_DS18B20(sensor_id=sensor_id, verbose=True)
|
||||
#sensor_DS18B20("0014531448ff")
|
||||
time.sleep(2)
|
||||
except IOError as e:
|
||||
errno, strerror = e.args
|
||||
print("I/O error({0}): {1}".format(errno,strerror))
|
||||
except KeyboardInterrupt:
|
||||
print ("keyboard interrupt")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user