diff --git a/mqtt_sensord/mqtt_sensord.py b/mqtt_sensord/mqtt_sensord.py index ecfee5b..5d5c321 100755 --- a/mqtt_sensord/mqtt_sensord.py +++ b/mqtt_sensord/mqtt_sensord.py @@ -1,18 +1,27 @@ #!/usr/bin/env python3 -import argparse import sys import time -from . import sensorlib -from .mqttlib import MQTTClient +if sys.implementation.name != 'micropython': + import argparse + from . import sensorlib + from .mqttlib import MQTTClient +else: + import sensorlib + from mqttlib import MQTTClient def main(): - parser = argparse.ArgumentParser() - parser.add_argument("-c", "--config", default="/etc/mqtt-sensord.conf") - args = parser.parse_args() + if sys.implementation.name != 'micropython': + parser = argparse.ArgumentParser() + parser.add_argument("-c", "--config", default="/etc/mqtt-sensord.conf") + args = parser.parse_args() + config_path = args.config + else: + # most probably on micropython + config_path = "mqtt-sensord.conf" - config = sensorlib.load_config(args.config) + config = sensorlib.load_config(config_path) sensorlib.Sensor.configure(config['host']['name'], sys.platform) mcfg = config['mqtt'] diff --git a/mqtt_sensord/mqttlib/__init__.py b/mqtt_sensord/mqttlib/__init__.py index 7e6fc64..4244938 100644 --- a/mqtt_sensord/mqttlib/__init__.py +++ b/mqtt_sensord/mqttlib/__init__.py @@ -1,7 +1,7 @@ import sys -if sys.implementation == 'micropython': - from esp import MQTTClient +if sys.implementation.name == 'micropython': + from .esp import MQTTClient else: from .paho_mqtt import MQTTClient diff --git a/mqtt_sensord/sensorlib/__init__.py b/mqtt_sensord/sensorlib/__init__.py index 59cb366..1a0664c 100644 --- a/mqtt_sensord/sensorlib/__init__.py +++ b/mqtt_sensord/sensorlib/__init__.py @@ -3,9 +3,8 @@ import sys from .base import Sensor from .config import load_config -if sys.implementation == 'micropython': +if sys.implementation.name == 'micropython': from .esp_sensors import * elif getattr(sys.implementation, '_multiarch') == 'arm-linux-gnueabihf': from .pi_sensors import * from .misc_sensors import * - diff --git a/mqtt_sensord/sensorlib/esp_sensors.py b/mqtt_sensord/sensorlib/esp_sensors.py index 59393c7..45f13ac 100644 --- a/mqtt_sensord/sensorlib/esp_sensors.py +++ b/mqtt_sensord/sensorlib/esp_sensors.py @@ -4,9 +4,10 @@ import machine import onewire import time -from .base import Sensor +from .base import Sensor, sensor +@sensor class DS18B20(Sensor): sensor_class = 'esp-ds18b20' @@ -21,17 +22,17 @@ class DS18B20(Sensor): self.ds.convert_temp() time.sleep(0.1) return {"temp": self.ds.read_temp(self.sID)} -Sensor.register(DS18B20) +@sensor class DHT(Sensor): sensor_class = 'esp-dht' def __init__(self, **sensor_conf): super(DHT, self).__init__(**sensor_conf) - if sensor_conf["type"] == "dht11": + if sensor_conf["dht_type"] == "dht11": self.dht = dht.DHT11(machine.Pin(self.pin)) - elif sensor_conf["type"] == "dht22": + elif sensor_conf["dht_type"] == "dht22": self.dht = dht.DHT22(machine.Pin(self.pin)) else: raise ValueError("Unknown type") @@ -40,4 +41,3 @@ class DHT(Sensor): self.dht.measure() time.sleep(0.1) return {"temp": self.dht.temperature(), "humidity": self.dht.humidity()} -Sensor.register(DHT) diff --git a/mqtt_sensord/sensorlib/misc_sensors.py b/mqtt_sensord/sensorlib/misc_sensors.py index e92b296..20b38e4 100644 --- a/mqtt_sensord/sensorlib/misc_sensors.py +++ b/mqtt_sensord/sensorlib/misc_sensors.py @@ -1,10 +1,15 @@ -import fcntl import random -import threading import time from .base import Sensor, sensor +try: + import fcntl + import threading + CO2_IMPORTS = True +except ImportError: + CO2_IMPORTS = False + @sensor class FakeSensor(Sensor): @@ -28,6 +33,9 @@ class CO2Meter(Sensor): def __init__(self, dev_path, **sensor_conf): super(CO2Meter, self).__init__(**sensor_conf) + if not CO2_IMPORTS: + raise RuntimeError("Missing threading or fnctl to run this sensor") + self.dev_path = dev_path self.key = b'\x11\x11\x11\x11\x11\x11\x11\x11' self._dev = None @@ -91,9 +99,9 @@ class CO2Meter(Sensor): print("Device error: {} - trying to reopen".format(e)) self.values = {} try: - self._dev.close() + self._dev.close() except Exception: - pass + pass while self._dev.closed: time.sleep(1) try: @@ -101,7 +109,6 @@ class CO2Meter(Sensor): except (IOError, OSError) as e: print("Device error: {} - trying to reopen".format(e)) - def get_co2(self): if 0x50 in self.values: return self.values[0x50] @@ -123,7 +130,6 @@ class CO2Meter(Sensor): if __name__ == '__main__': import argparse - import time parser = argparse.ArgumentParser() parser.add_argument("device", help="Device do read from")