import config import logging import paho.mqtt.client as paho import socket import report import time from rpi_envsens.dht import dht_22 from rpi_envsens.bmp import bmp_180 try: from config import APP_NAME as ROOT_LOGGER_NAME except ImportError: ROOT_LOGGER_NAME = 'root' logger = logging.getLogger(ROOT_LOGGER_NAME).getChild('main') def send_data_to_mqtt(data): client= paho.Client("temp_sens") client.username_pw_set(config.MQTT_USER, config.MQTT_PASS) try: client.connect(config.MQTT_SERVER, 1883) for key in data: topic = config.MQTT_TOPIC + "/" + key logger.info("Sending Ambient Information to mqtt %s=%s", topic, str(data[key])) client.publish(topic, data[key]) except (socket.timeout, OSError) as e: logger.warning("Erro while sending ambient information") def dht_callback(**data): del(data["time"]) send_data_to_mqtt(data) def bmp_callback(**data): del(data["time"]) del(data["temperature"]) send_data_to_mqtt(data) if __name__ == '__main__': report.appLoggingConfigure(config.__BASEPATH__, config.LOGTARGET, ((config.APP_NAME, config.LOGLVL), ), fmt=config.formatter, host=config.LOGHOST, port=config.LOGPORT) # # Initialise data collectors d = dht_22(config.DHT_22_PORT, dht_callback) b = bmp_180(bmp_callback) while True: time.sleep(1)