dht22mqtt.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. #!/usr/bin/python3
  2. from datetime import datetime
  3. import time
  4. import os
  5. import statistics
  6. import csv
  7. import adafruit_dht
  8. # import RPi.GPIO as GPIO
  9. from gpiomapping import gpiomapping
  10. import paho.mqtt.client as mqtt
  11. # Begin
  12. dht22mqtt_start_ts = datetime.now()
  13. ###############
  14. # MQTT Params
  15. ###############
  16. mqtt_topic = os.getenv('topic', 'zigbee2mqtt/')
  17. mqtt_device_id = os.getenv('device_id', 'dht22')
  18. mqtt_brokeraddr = os.getenv('broker', '192.168.1.10')
  19. if not mqtt_topic.endswith('/'):
  20. mqtt_topic = mqtt_topic + "/"
  21. mqtt_topic = mqtt_topic + mqtt_device_id + '/'
  22. ###############
  23. # GPIO params
  24. ###############
  25. # TODO check if we can use the GPIO test https://github.com/kgbplus/gpiotest to autodetect pin
  26. # Problems with multiple sensors on the same device
  27. dht22mqtt_refresh = int(os.getenv('poll', '2'))
  28. dht22mqtt_pin = int(os.getenv('pin', '4'))
  29. dht22mqtt_device_type = str(os.getenv('device_type', 'dht22')).lower()
  30. dht22mqtt_temp_unit = os.getenv('unit', 'C')
  31. ###############
  32. # MQTT & Logging params
  33. ###############
  34. dht22mqtt_mqtt_chatter = str(os.getenv('mqtt_chatter', 'essential|ha|full')).lower()
  35. dht22mqtt_logging_mode = str(os.getenv('logging', 'None')).lower()
  36. dht22mqtt_sensor_tally = dict()
  37. ###############
  38. # Filtering & Sampling Params
  39. ###############
  40. dht22_temp_stack = []
  41. dht22_temp_stack_errors = 0
  42. dht22_hum_stack = []
  43. dht22_hum_stack_errors = 0
  44. dht22_stack_size = 10
  45. dht22_std_deviation = 3
  46. dht22_error_count_stack_flush = 3
  47. ###############
  48. # Logging functions
  49. ###############
  50. def log2file(filename, params):
  51. if('log2file' in dht22mqtt_logging_mode):
  52. ts_filename = dht22mqtt_start_ts.strftime('%Y-%m-%dT%H-%M-%SZ')+'_'+filename+".csv"
  53. with open("/log/"+ts_filename, "a+") as file:
  54. w = csv.DictWriter(file, delimiter=',', lineterminator='\n', fieldnames=params.keys())
  55. if file.tell() == 0:
  56. w.writeheader()
  57. w.writerow(params)
  58. def log2stdout(timestamp, msg):
  59. if('log2stdout' in dht22mqtt_logging_mode):
  60. print(datetime.fromtimestamp(timestamp).strftime('%Y-%m-%dT%H:%M:%SZ'), str(msg))
  61. ###############
  62. # Polling & Processing functions
  63. ###############
  64. def getTemperatureJitter(temperature):
  65. return getTemperature(temperature-0.3), getTemperature(temperature+0.3)
  66. def getTemperature(temperature):
  67. if(dht22mqtt_temp_unit == 'F'):
  68. temperature = temperature * (9 / 5) + 32
  69. return temperature
  70. def getHumidity(humidity):
  71. return humidity
  72. ###############
  73. # Polling & Processing functions
  74. ###############
  75. def processSensorValue(stack, error, value, value_type):
  76. # flush stack on accumulation of errors
  77. if(error >= dht22_error_count_stack_flush):
  78. stack = []
  79. error = 0
  80. # init stack
  81. if(len(stack) <= dht22_error_count_stack_flush):
  82. if(value not in stack):
  83. stack.append(value)
  84. # use jitter for bootstrap temperature stack
  85. if(value_type == 'temperature'):
  86. low, high = getTemperatureJitter(value)
  87. stack.append(low)
  88. stack.append(high)
  89. return stack, error, None
  90. # get statistics
  91. std = statistics.pstdev(stack)
  92. mean = statistics.mean(stack)
  93. # compute if outlier or not
  94. if(mean-std*dht22_std_deviation < value < mean+std*dht22_std_deviation):
  95. outlier = False
  96. if(value not in stack):
  97. stack.append(value)
  98. error = 0
  99. else:
  100. outlier = True
  101. error += 1
  102. # remove last element from stack
  103. if(len(stack) > 10):
  104. stack.pop(0)
  105. return stack, error, outlier
  106. ###############
  107. # MQTT update functions
  108. ###############
  109. def updateEssentialMqtt(temperature, humidity, detected):
  110. if('essential' in dht22mqtt_mqtt_chatter):
  111. if(detected == 'accurate'):
  112. payload = '{ "temperature": '+str(temperature)+', "humidity": '+str(humidity)+' }'
  113. client.publish(mqtt_topic + 'value', payload, qos=1, retain=True)
  114. client.publish(mqtt_topic + "detected", str(detected), qos=1, retain=True)
  115. else:
  116. client.publish(mqtt_topic + "detected", str(detected), qos=1, retain=True)
  117. client.publish(mqtt_topic + "updated", str(datetime.now()), qos=1, retain=True)
  118. def registerWithHomeAssitant():
  119. if('ha' in dht22mqtt_mqtt_chatter):
  120. ha_temperature_config = '{"device_class": "temperature",' + \
  121. ' "name": "'+mqtt_device_id+'_temperature",' + \
  122. ' "state_topic": "'+mqtt_topic+'value",' + \
  123. ' "unit_of_measurement": "°'+dht22mqtt_temp_unit+'",' + \
  124. ' "value_template": "{{ value_json.temperature}}" }'
  125. ha_humidity_config = '{"device_class": "humidity",' + \
  126. ' "name": "'+mqtt_device_id+'_humidity",' + \
  127. ' "state_topic": "'+mqtt_topic+'value",' + \
  128. ' "unit_of_measurement": "%",' + \
  129. ' "value_template": "{{ value_json.humidity}}" }'
  130. client.publish('homeassistant/sensor/'+mqtt_device_id+'Temperature/config', ha_temperature_config, qos=1, retain=True)
  131. client.publish('homeassistant/sensor/'+mqtt_device_id+'Humidity/config', ha_humidity_config, qos=1, retain=True)
  132. log2stdout(datetime.now().timestamp(), 'Registering sensor with home assistant success...')
  133. def updateFullSysInternalsMqtt():
  134. if('full' in dht22mqtt_mqtt_chatter):
  135. client.publish(mqtt_topic + "sys/temperature_stack_size", len(dht22_temp_stack), qos=1, retain=True)
  136. client.publish(mqtt_topic + "sys/temperature_error_count", dht22_temp_stack_errors, qos=1, retain=True)
  137. client.publish(mqtt_topic + "sys/humidity_stack_size", len(dht22_hum_stack), qos=1, retain=True)
  138. client.publish(mqtt_topic + "sys/humidity_error_count", dht22_hum_stack_errors, qos=1, retain=True)
  139. client.publish(mqtt_topic + "updated", str(datetime.now()), qos=1, retain=True)
  140. def updateFullSensorTallyMqtt(key):
  141. if('full' in dht22mqtt_mqtt_chatter):
  142. if key in dht22mqtt_sensor_tally:
  143. dht22mqtt_sensor_tally[key] += 1
  144. else:
  145. dht22mqtt_sensor_tally[key] = 1
  146. client.publish(mqtt_topic + "sys/tally/" + key, dht22mqtt_sensor_tally[key], qos=1, retain=True)
  147. client.publish(mqtt_topic + "updated", str(datetime.now()), qos=1, retain=True)
  148. ###############
  149. # Setup dht22 sensor
  150. ###############
  151. log2stdout(dht22mqtt_start_ts.timestamp(), 'Starting dht22mqtt...')
  152. if(dht22mqtt_device_type == 'dht22' or dht22mqtt_device_type == 'am2302'):
  153. dhtDevice = adafruit_dht.DHT22(gpiomapping[dht22mqtt_pin], use_pulseio=False)
  154. elif(dht22mqtt_device_type == 'dht11'):
  155. dhtDevice = adafruit_dht.DHT11(gpiomapping[dht22mqtt_pin], use_pulseio=False)
  156. else:
  157. log2stdout(datetime.now().timestamp(), 'Unsupported device '+dht22mqtt_device_type+'...')
  158. log2stdout(datetime.now().timestamp(), 'Devices supported by this container are DHT11/DHT22/AM2302')
  159. log2stdout(datetime.now().timestamp(), 'Setup dht22 sensor success...')
  160. ###############
  161. # Setup mqtt client
  162. ###############
  163. if('essential' in dht22mqtt_mqtt_chatter):
  164. client = mqtt.Client('DHT22', clean_session=True, userdata=None)
  165. # set last will for a disgraceful exit
  166. client.will_set(mqtt_topic + "state", "OFFLINE", qos=1, retain=True)
  167. # keep alive for 60 times the refresh rate
  168. client.connect(mqtt_brokeraddr, keepalive=dht22mqtt_refresh*60)
  169. client.loop_start()
  170. client.publish(mqtt_topic + "type", "sensor", qos=1, retain=True)
  171. client.publish(mqtt_topic + "device", "dht22", qos=1, retain=True)
  172. client.publish(mqtt_topic + "env/pin", dht22mqtt_pin, qos=1, retain=True)
  173. client.publish(mqtt_topic + "env/brokeraddr", mqtt_brokeraddr, qos=1, retain=True)
  174. client.publish(mqtt_topic + "env/refresh", dht22mqtt_refresh, qos=1, retain=True)
  175. client.publish(mqtt_topic + "env/logging", dht22mqtt_logging_mode, qos=1, retain=True)
  176. client.publish(mqtt_topic + "env/mqtt_chatter", dht22mqtt_mqtt_chatter, qos=1, retain=True)
  177. client.publish(mqtt_topic + "sys/dht22_stack_size", dht22_stack_size, qos=1, retain=True)
  178. client.publish(mqtt_topic + "sys/dht22_std_deviation", dht22_std_deviation, qos=1, retain=True)
  179. client.publish(mqtt_topic + "sys/dht22_error_count_stack_flush", dht22_error_count_stack_flush, qos=1, retain=True)
  180. client.publish(mqtt_topic + "updated", str(datetime.now()), qos=1, retain=True)
  181. log2stdout(datetime.now().timestamp(), 'Setup mqtt client success...')
  182. client.publish(mqtt_topic + "state", "ONLINE", qos=1, retain=True)
  183. registerWithHomeAssitant()
  184. log2stdout(datetime.now().timestamp(), 'Begin capture...')
  185. while True:
  186. try:
  187. dht22_ts = datetime.now().timestamp()
  188. temperature = getTemperature(dhtDevice.temperature)
  189. humidity = getHumidity(dhtDevice.humidity)
  190. temp_data = processSensorValue(dht22_temp_stack,
  191. dht22_temp_stack_errors,
  192. temperature,
  193. 'temperature')
  194. dht22_temp_stack = temp_data[0]
  195. dht22_temp_stack_errors = temp_data[1]
  196. temperature_outlier = temp_data[2]
  197. hum_data = processSensorValue(dht22_hum_stack,
  198. dht22_hum_stack_errors,
  199. humidity,
  200. 'humidity')
  201. dht22_hum_stack = hum_data[0]
  202. dht22_hum_stack_errors = hum_data[1]
  203. humidity_outlier = hum_data[2]
  204. # Since the intuition here is that errors in humidity and temperature readings
  205. # are heavily correlated, we can skip mqtt if we detect either.
  206. detected = ''
  207. if(temperature_outlier is False and humidity_outlier is False):
  208. detected = 'accurate'
  209. else:
  210. detected = 'outlier'
  211. updateEssentialMqtt(temperature, humidity, detected)
  212. updateFullSysInternalsMqtt()
  213. updateFullSensorTallyMqtt(detected)
  214. data = {'timestamp': dht22_ts,
  215. 'temperature': temperature,
  216. 'humidity': humidity,
  217. 'temperature_outlier': temperature_outlier,
  218. 'humidity_outlier': humidity_outlier}
  219. log2stdout(dht22_ts, data)
  220. log2file('recording', data)
  221. time.sleep(dht22mqtt_refresh)
  222. except RuntimeError as error:
  223. # DHT22 throws errors often. Keep reading.
  224. detected = 'error'
  225. updateEssentialMqtt(None, None, detected)
  226. updateFullSensorTallyMqtt(error.args[0])
  227. data = {'timestamp': dht22_ts, 'error_type': error.args[0]}
  228. log2stdout(dht22_ts, data)
  229. log2file('error', data)
  230. time.sleep(dht22mqtt_refresh)
  231. continue
  232. except Exception as error:
  233. if('essential' in dht22mqtt_mqtt_chatter):
  234. client.disconnect()
  235. dhtDevice.exit()
  236. raise error
  237. # Graceful exit
  238. if('essential' in dht22mqtt_mqtt_chatter):
  239. client.publish(mqtt_topic + "state", "OFFLINE", qos=2, retain=True)
  240. client.publish(mqtt_topic + "updated", str(datetime.now()), qos=2, retain=True)
  241. client.disconnect()
  242. dhtDevice.exit()