Make sensord micropython/esp compatible

This commit is contained in:
Sebastian Lohff 2019-07-23 21:19:24 +02:00
parent 566f5145a0
commit 31ca8475c2
5 changed files with 36 additions and 22 deletions

View File

@ -1,18 +1,27 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import argparse
import sys import sys
import time import time
from . import sensorlib if sys.implementation.name != 'micropython':
from .mqttlib import MQTTClient import argparse
from . import sensorlib
from .mqttlib import MQTTClient
else:
import sensorlib
from mqttlib import MQTTClient
def main(): def main():
if sys.implementation.name != 'micropython':
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument("-c", "--config", default="/etc/mqtt-sensord.conf") parser.add_argument("-c", "--config", default="/etc/mqtt-sensord.conf")
args = parser.parse_args() 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) sensorlib.Sensor.configure(config['host']['name'], sys.platform)
mcfg = config['mqtt'] mcfg = config['mqtt']

View File

@ -1,7 +1,7 @@
import sys import sys
if sys.implementation == 'micropython': if sys.implementation.name == 'micropython':
from esp import MQTTClient from .esp import MQTTClient
else: else:
from .paho_mqtt import MQTTClient from .paho_mqtt import MQTTClient

View File

@ -3,9 +3,8 @@ import sys
from .base import Sensor from .base import Sensor
from .config import load_config from .config import load_config
if sys.implementation == 'micropython': if sys.implementation.name == 'micropython':
from .esp_sensors import * from .esp_sensors import *
elif getattr(sys.implementation, '_multiarch') == 'arm-linux-gnueabihf': elif getattr(sys.implementation, '_multiarch') == 'arm-linux-gnueabihf':
from .pi_sensors import * from .pi_sensors import *
from .misc_sensors import * from .misc_sensors import *

View File

@ -4,9 +4,10 @@ import machine
import onewire import onewire
import time import time
from .base import Sensor from .base import Sensor, sensor
@sensor
class DS18B20(Sensor): class DS18B20(Sensor):
sensor_class = 'esp-ds18b20' sensor_class = 'esp-ds18b20'
@ -21,17 +22,17 @@ class DS18B20(Sensor):
self.ds.convert_temp() self.ds.convert_temp()
time.sleep(0.1) time.sleep(0.1)
return {"temp": self.ds.read_temp(self.sID)} return {"temp": self.ds.read_temp(self.sID)}
Sensor.register(DS18B20)
@sensor
class DHT(Sensor): class DHT(Sensor):
sensor_class = 'esp-dht' sensor_class = 'esp-dht'
def __init__(self, **sensor_conf): def __init__(self, **sensor_conf):
super(DHT, self).__init__(**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)) 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)) self.dht = dht.DHT22(machine.Pin(self.pin))
else: else:
raise ValueError("Unknown type") raise ValueError("Unknown type")
@ -40,4 +41,3 @@ class DHT(Sensor):
self.dht.measure() self.dht.measure()
time.sleep(0.1) time.sleep(0.1)
return {"temp": self.dht.temperature(), "humidity": self.dht.humidity()} return {"temp": self.dht.temperature(), "humidity": self.dht.humidity()}
Sensor.register(DHT)

View File

@ -1,10 +1,15 @@
import fcntl
import random import random
import threading
import time import time
from .base import Sensor, sensor from .base import Sensor, sensor
try:
import fcntl
import threading
CO2_IMPORTS = True
except ImportError:
CO2_IMPORTS = False
@sensor @sensor
class FakeSensor(Sensor): class FakeSensor(Sensor):
@ -28,6 +33,9 @@ class CO2Meter(Sensor):
def __init__(self, dev_path, **sensor_conf): def __init__(self, dev_path, **sensor_conf):
super(CO2Meter, self).__init__(**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.dev_path = dev_path
self.key = b'\x11\x11\x11\x11\x11\x11\x11\x11' self.key = b'\x11\x11\x11\x11\x11\x11\x11\x11'
self._dev = None self._dev = None
@ -101,7 +109,6 @@ class CO2Meter(Sensor):
except (IOError, OSError) as e: except (IOError, OSError) as e:
print("Device error: {} - trying to reopen".format(e)) print("Device error: {} - trying to reopen".format(e))
def get_co2(self): def get_co2(self):
if 0x50 in self.values: if 0x50 in self.values:
return self.values[0x50] return self.values[0x50]
@ -123,7 +130,6 @@ class CO2Meter(Sensor):
if __name__ == '__main__': if __name__ == '__main__':
import argparse import argparse
import time
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument("device", help="Device do read from") parser.add_argument("device", help="Device do read from")