|
|
|
@ -28,6 +28,7 @@ def main():
|
|
|
|
|
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']
|
|
|
|
@ -36,6 +37,7 @@ 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)
|
|
|
|
@ -54,19 +56,35 @@ def main():
|
|
|
|
|
print("Subscribe to", topic)
|
|
|
|
|
mqtt.subscribe(topic)
|
|
|
|
|
|
|
|
|
|
last_measurement = time.time()
|
|
|
|
|
last_measurement = start_time = time.time()
|
|
|
|
|
while True:
|
|
|
|
|
# handle regular sensor data
|
|
|
|
|
if debug:
|
|
|
|
|
print("Getting values from sensors...")
|
|
|
|
|
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)
|
|
|
|
|
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)
|
|
|
|
|
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))
|
|
|
|
@ -76,6 +94,11 @@ 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()
|
|
|
|
|