Compare commits
No commits in common. "mqtt-overhault" and "master" have entirely different histories.
mqtt-overh
...
master
|
@ -15,20 +15,13 @@ def main():
|
|||
if sys.implementation.name != 'micropython':
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("-c", "--config", default="/etc/mqtt-sensord.conf")
|
||||
parser.add_argument("-d", "--debug", action="store_true", default=False)
|
||||
args = parser.parse_args()
|
||||
config_path = args.config
|
||||
debug = args.debug
|
||||
else:
|
||||
# most probably on micropython
|
||||
config_path = "mqtt-sensord.conf"
|
||||
debug = False
|
||||
|
||||
config = sensorlib.load_config(config_path)
|
||||
debug = debug or config.get('debug')
|
||||
config['debug'] = debug
|
||||
|
||||
esp_reset_time = config['host'].get('esp-reset-time')
|
||||
sensorlib.Sensor.configure(config['host']['name'], sys.platform)
|
||||
|
||||
mcfg = config['mqtt']
|
||||
|
@ -37,10 +30,9 @@ def main():
|
|||
else:
|
||||
client_id = None
|
||||
|
||||
mqtt_mode = config['mqtt'].get('mode', 'split')
|
||||
mqtt = MQTTClient(mcfg['host'], mcfg['user'], mcfg['password'], client_id)
|
||||
default_mqtt_topic = mcfg.get('sensor_topic')
|
||||
print("Connected to", mqtt.host, "with default topic", default_mqtt_topic)
|
||||
mqtt_topic = mcfg['sensor_topic']
|
||||
print("Connected to", mqtt.host, "with topic", mqtt_topic)
|
||||
mqtt.set_callback(sensorlib.Sensor.run_mqtt_callback)
|
||||
|
||||
sensors = []
|
||||
|
@ -56,35 +48,17 @@ def main():
|
|||
print("Subscribe to", topic)
|
||||
mqtt.subscribe(topic)
|
||||
|
||||
last_measurement = start_time = time.time()
|
||||
last_measurement = time.time()
|
||||
while True:
|
||||
# handle regular sensor data
|
||||
if debug:
|
||||
print("Getting values from sensors...")
|
||||
if mqtt_mode == 'split':
|
||||
for sensor in sensors:
|
||||
if sensor.HAS_SENSOR_DATA:
|
||||
data = sensor.gen_datapoint()
|
||||
if debug:
|
||||
print(default_mqtt_topic, data)
|
||||
mqtt.send_data(default_mqtt_topic, data)
|
||||
print(mqtt_topic, data)
|
||||
mqtt.send_data(mqtt_topic, data)
|
||||
if not mqtt.is_async():
|
||||
mqtt.process_mqtt()
|
||||
elif mqtt_mode == 'combined':
|
||||
data = {
|
||||
'hostname': config['host']['name'],
|
||||
'platform': sys.platform,
|
||||
}
|
||||
for sensor in sensors:
|
||||
if sensor.HAS_SENSOR_DATA:
|
||||
data[sensor.id] = sensor.get_data()
|
||||
if debug:
|
||||
print(default_mqtt_topic, data)
|
||||
mqtt.send_data(default_mqtt_topic, data)
|
||||
if not mqtt.is_async():
|
||||
mqtt.process_mqtt()
|
||||
else:
|
||||
raise RuntimeError("Unknown mqtt mode {}".format(mqtt_mode))
|
||||
|
||||
if mqtt.is_async():
|
||||
time.sleep(max(0.0, time.time() + config['host']['interval'] - last_measurement))
|
||||
|
@ -94,11 +68,6 @@ def main():
|
|||
time.sleep(0.1)
|
||||
last_measurement = time.time()
|
||||
|
||||
if sys.implementation.name == 'micropython' and esp_reset_time and \
|
||||
last_measurement - start_time > esp_reset_time:
|
||||
import machine
|
||||
machine.reset()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
|
|
@ -15,6 +15,3 @@ class MQTTClientBase:
|
|||
|
||||
def is_async(self):
|
||||
return False
|
||||
|
||||
def process_mqtt(self):
|
||||
pass
|
||||
|
|
|
@ -9,13 +9,8 @@ class MQTTClient(MQTTClientBase):
|
|||
def _connect(self):
|
||||
self._mqtt = mqtt.Client(self.client_id)
|
||||
self._mqtt.username_pw_set(self.user, self.password)
|
||||
self._mqtt.on_connect = self.on_connect
|
||||
self._mqtt.connect(self.host)
|
||||
|
||||
def on_connect(self, *args, **kwargs):
|
||||
#print("Connected! client={} userdata={} msg={}".format(client, userdata, msg))
|
||||
print("Connected!", *args, **kwargs)
|
||||
|
||||
def send_data(self, topic, data):
|
||||
self._mqtt.publish(topic, json.dumps(data))
|
||||
|
||||
|
|
|
@ -41,7 +41,7 @@ class Sensor(object):
|
|||
def get_data(self):
|
||||
raise NotImplementedError("You missed a spot!")
|
||||
|
||||
def gen_datapoint(self, legacy=True):
|
||||
def gen_datapoint(self):
|
||||
return {
|
||||
'type': 'measurement',
|
||||
'tags': {
|
||||
|
|
|
@ -18,18 +18,11 @@ class DS18B20(Sensor):
|
|||
self.o = onewire.OneWire(machine.Pin(self.pin))
|
||||
self.ds = ds18x20.DS18X20(self.o)
|
||||
self.sID = self.ds.scan()[0]
|
||||
self._first_measurement = True
|
||||
|
||||
def get_data(self):
|
||||
self.ds.convert_temp()
|
||||
time.sleep(0.1)
|
||||
val = self.ds.read_temp(self.sID)
|
||||
if self._first_measurement:
|
||||
if val > 70.0:
|
||||
val = None
|
||||
else:
|
||||
self._first_measurement = False
|
||||
return {"temp": val}
|
||||
return {"temp": self.ds.read_temp(self.sID)}
|
||||
|
||||
|
||||
@sensor
|
||||
|
|
|
@ -134,12 +134,12 @@ class CO2Meter(Sensor):
|
|||
|
||||
def get_temp(self):
|
||||
if 0x42 in self.values:
|
||||
return round(self.values[0x42] / 16.0 - 273.15, 1)
|
||||
return self.values[0x42] / 16.0 - 273.15
|
||||
return None
|
||||
|
||||
def get_humidity(self):
|
||||
if 0x44 in self.values:
|
||||
return round(self.values[0x44] / 100.0, 1)
|
||||
return self.values[0x44] / 100.0
|
||||
return None
|
||||
|
||||
def get_data(self):
|
||||
|
|
Loading…
Reference in New Issue