Publish temperature data to mq topics with dht22 temperature sensors and nodeMCU

Publish temperature data to mq topics with dht22 temperature sensors and nodeMCU

In my previous blog post i wrote about the Xiaomi Mijia temperature sensor. Although it is nice to look at and seeing the temperature directly displayed is cool, getting sensor data over bluetooth LE has its limitations. One of the biggest problems is the limited range of those sensors. I often only got about 2-3m before the signal was too weak. If the signal is too weak, the script i wrote is likely too hang or get no data, often resulting in high cpu usage on my pi zero.

To circumvent all those problems i needed a solution that is connected via wifi and can use asynchronous communication. I found this post https://RandomNerdTutorials.com/esp8266-nodemcu-mqtt-publish-dht11-dht22-arduino/ and bought myself a bunch of node mcu and dht22 temperature sensors. I deployed rabbitmq on my local docker host to publish to and subscribe to and pushed the Arduino software to my nodemcus. The only changes to the code i had to make, where the address of my mqtt broker and the topic names for temperature and humidity.

The dht22 sensor i ordered already came soldered onto a board and i only needed to connect it to 3v power, ground and one date pin on the node mcu.

Using my instance of home assistant, i was able to subscribe to the corresponding sensor topics and read the sensor data by adding them to home assistant configuration.yml:

#mqtt sensors
sensor:
  - platform: mqtt
    name: "Temperatur Arbeitszimmer"
    state_topic: "martin/arbeitszimmer/temperature"
    unit_of_measurement: '°C'
  - platform: mqtt
    name: "Luftfeuchtigkeit Arbeitszimmer"
    state_topic: "martin/arbeitszimmer/humidity"
    unit_of_measurement: '%'

After that i also configured home assistant to write the sensor data to influx db for later use.

influxdb:
  host: x.x.x.x
  include:
    entities:
       - sensor.temperatur_arbeitszimmer
       - sensor.luftfeuchtigkeit_arbeitszimmer
...