#!/usr/bin/env python3 import argparse import sys import time from . 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() config = sensorlib.load_config(args.config) sensorlib.Sensor.configure(config['host']['name'], sys.platform) mcfg = config['mqtt'] if 'client_id' in config['mqtt']: client_id = config['mqtt']['client_id'] else: client_id = None mqtt = MQTTClient(mcfg['host'], mcfg['user'], mcfg['password'], client_id) mqtt_topic = mcfg['sensor_topic'] print("Connected to", mqtt.host, "with topic", mqtt_topic) sensors = [] for scfg in config['sensors']: s = sensorlib.Sensor.make_sensor(**scfg) sensors.append(s) last_measurement = time.time() while True: print("Getting values from sensors...") for sensor in sensors: data = sensor.gen_datapoint() print(mqtt_topic, data) mqtt.send_data(mqtt_topic, data) time.sleep(max(0.0, time.time() + config['host']['interval'] - last_measurement)) last_measurement = time.time() if __name__ == '__main__': main()