Publishs ambient information to mqtt server
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ambient_info.py 1.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import config
  2. import logging
  3. import paho.mqtt.client as paho
  4. import socket
  5. import report
  6. import time
  7. from rpi_envsens.dht import dht_22
  8. from rpi_envsens.bmp import bmp_180
  9. try:
  10. from config import APP_NAME as ROOT_LOGGER_NAME
  11. except ImportError:
  12. ROOT_LOGGER_NAME = 'root'
  13. logger = logging.getLogger(ROOT_LOGGER_NAME).getChild('main')
  14. def send_data_to_mqtt(data):
  15. client= paho.Client(config.APP_NAME)
  16. client.username_pw_set(config.MQTT_USER, config.MQTT_PASS)
  17. try:
  18. client.connect(config.MQTT_SERVER, 1883)
  19. for key in data:
  20. topic = config.MQTT_TOPIC + "/" + key
  21. logger.info("Sending Ambient Information to mqtt %s=%s", topic, str(data[key]))
  22. client.publish(topic, data[key])
  23. except (socket.timeout, OSError) as e:
  24. logger.warning("Erro while sending ambient information")
  25. def dht_callback(**data):
  26. del(data["time"])
  27. send_data_to_mqtt(data)
  28. def bmp_callback(**data):
  29. del(data["time"])
  30. del(data["temperature"])
  31. send_data_to_mqtt(data)
  32. if __name__ == '__main__':
  33. report.appLoggingConfigure(config.__BASEPATH__, config.LOGTARGET, ((config.APP_NAME, config.LOGLVL), ), fmt=config.formatter, host=config.LOGHOST, port=config.LOGPORT)
  34. #
  35. # Initialise data collectors
  36. d = dht_22(config.DHT_22_PORT, dht_callback)
  37. b = bmp_180(bmp_callback)
  38. while True:
  39. time.sleep(1)