Publishs ambient information to mqtt server
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

ambient_info.py 1.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import config
  2. import logging
  3. import mqtt
  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. mc = mqtt.mqtt_client(config.APP_NAME, config.MQTT_SERVER, 1883, config.MQTT_USER, config.MQTT_PASS)
  15. def send_data_to_mqtt(data):
  16. for key in data:
  17. topic = config.MQTT_TOPIC + "/" + key
  18. logger.info("Sending Ambient Information to mqtt %s=%s", topic, str(data[key]))
  19. mc.send(topic, data[key])
  20. def dht_callback(**data):
  21. del(data["time"])
  22. send_data_to_mqtt(data)
  23. def bmp_callback(**data):
  24. del(data["time"])
  25. del(data["temperature"])
  26. send_data_to_mqtt(data)
  27. if __name__ == '__main__':
  28. report.appLoggingConfigure(config.__BASEPATH__, config.LOGTARGET, ((config.APP_NAME, config.LOGLVL), ), fmt=config.formatter, host=config.LOGHOST, port=config.LOGPORT)
  29. #
  30. # Initialise data collectors
  31. d = dht_22(config.DHT_22_PORT, dht_callback)
  32. b = bmp_180(bmp_callback)
  33. while True:
  34. time.sleep(1)