|
| 1 | +""" |
| 2 | + DS18B20 Sensor Interface |
| 3 | + Connects to a DS18B20 device to get |
| 4 | + temperature readings. |
| 5 | +""" |
| 6 | +import os |
| 7 | +import glob |
| 8 | +import time |
| 9 | + |
| 10 | +from mudpi.constants import METRIC_SYSTEM |
| 11 | +from mudpi.extensions import BaseInterface |
| 12 | +from mudpi.extensions.sensor import Sensor |
| 13 | +from mudpi.logger.Logger import Logger, LOG_LEVEL |
| 14 | +from mudpi.exceptions import MudPiError, ConfigError |
| 15 | + |
| 16 | +os.system('modprobe w1-gpio') |
| 17 | +os.system('modprobe w1-therm') |
| 18 | + |
| 19 | + |
| 20 | +#device_folder = '/sys/bus/w1/devices/28-XXXXXXXXXXXXX' |
| 21 | +#device_folder = glob.glob(base_dir + '/28*')[0] |
| 22 | +#device_file = device_folder + '/w1_slave' |
| 23 | + |
| 24 | +class Interface(BaseInterface): |
| 25 | + |
| 26 | + def load(self, config): |
| 27 | + """ Load DS18B20 sensor component from configs """ |
| 28 | + sensor = ds18b20(self.mudpi, config) |
| 29 | + self.add_component(sensor) |
| 30 | + return True |
| 31 | + |
| 32 | + def validate(self, config): |
| 33 | + if not isinstance(config, list): |
| 34 | + config = [config] |
| 35 | + |
| 36 | + for conf in config: |
| 37 | + """ See if 1-wire ID was passed by the user in the config file """ |
| 38 | + if not conf.get('one_wire_ID'): |
| 39 | + Logger.log( |
| 40 | + LOG_LEVEL["debug"], |
| 41 | + 'DS18B20 one_wire_ID not set. Will search for device.' |
| 42 | + ) |
| 43 | + |
| 44 | + return config |
| 45 | + |
| 46 | +class ds18b20(Sensor): |
| 47 | + """ DS18B20 Sensor get readings temperature. """ |
| 48 | + |
| 49 | + """ Properties """ |
| 50 | + @property |
| 51 | + def id(self): |
| 52 | + """ Return a unique id for the component """ |
| 53 | + return self.config['key'] |
| 54 | + |
| 55 | + @property |
| 56 | + def name(self): |
| 57 | + """ Return the display name of the component """ |
| 58 | + return self.config.get('name') or f"{self.id.replace('_', ' ').title()}" |
| 59 | + |
| 60 | + @property |
| 61 | + def state(self): |
| 62 | + """ Return the state of the component (from memory, no IO!) """ |
| 63 | + return self._state |
| 64 | + |
| 65 | + @property |
| 66 | + def classifier(self): |
| 67 | + """ Classification further describing it, effects the data formatting """ |
| 68 | + return 'temperature' |
| 69 | + |
| 70 | + @property |
| 71 | + def one_wire_ID(self): |
| 72 | + return self.config['one_wire_ID'] |
| 73 | + |
| 74 | + |
| 75 | + """ Methods """ |
| 76 | + def init(self): |
| 77 | + """To support multiple 1-wire devices check to see if the ID is set in the config. |
| 78 | + If the ID is not set, there should only be a single 28-xxxxxxxxx directory in the base directory, so we use that. """ |
| 79 | + |
| 80 | + base_dir = '/sys/bus/w1/devices' |
| 81 | + |
| 82 | + if self.config.get('one_wire_ID') and os.path.isdir(base_dir + '/' + self.config.get('one_wire_ID')): |
| 83 | + self.device_file = base_dir + '/' + self.config.get('one_wire_ID') + '/w1_slave' |
| 84 | + Logger.log( |
| 85 | + LOG_LEVEL["debug"], |
| 86 | + 'Setting device file to ' + self.device_file |
| 87 | + ) |
| 88 | + else: |
| 89 | + Logger.log( |
| 90 | + LOG_LEVEL["debug"], |
| 91 | + 'DS18B20 one_wire_ID not set or not found.' |
| 92 | + ) |
| 93 | + """ Make sure 1-wire device directory exists """ |
| 94 | + try: |
| 95 | + device_folder = glob.glob(base_dir + '/28*')[0] |
| 96 | + except: |
| 97 | + Logger.log( |
| 98 | + LOG_LEVEL["error"], |
| 99 | + 'Failed to find 1-wire device directory. Ensure device is connected and one_wire_ID corret..' |
| 100 | + ) |
| 101 | + else: |
| 102 | + self.device_file = device_folder + '/w1_slave' |
| 103 | + return True |
| 104 | + |
| 105 | + def update(self): |
| 106 | + |
| 107 | + def read_temp_raw(): |
| 108 | + f = open(self.device_file, 'r') |
| 109 | + lines = f.readlines() |
| 110 | + f.close() |
| 111 | + return lines |
| 112 | + |
| 113 | + lines = read_temp_raw() |
| 114 | + while lines[0].strip()[-3:] != 'YES': |
| 115 | + time.sleep(0.2) |
| 116 | + lines = read_temp_raw() |
| 117 | + equals_pos = lines[1].find('t=') |
| 118 | + if equals_pos != -1: |
| 119 | + temp_string = lines[1][equals_pos+2:] |
| 120 | + temperature_c = float(temp_string) / 1000.0 |
| 121 | + temperature_f = temperature_c * 9.0 / 5.0 + 32.0 |
| 122 | + _temperature = round(temperature_c if self.mudpi.unit_system == METRIC_SYSTEM else temperature_f, 2) |
| 123 | + readings = { |
| 124 | + 'temperature': _temperature, |
| 125 | + } |
| 126 | + self._state = readings |
| 127 | + return readings |
| 128 | + else: |
| 129 | + Logger.log( |
| 130 | + LOG_LEVEL["error"], |
| 131 | + 'Failed to get reading [DS18B20]. Try again!' |
| 132 | + ) |
| 133 | + return None |
0 commit comments