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.2KB

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