This article describes a way to create and manage multiple very simple temperature and humidity sensors based on Arduino MKR1000, and being able to send their information to Home Assistant.
The Arduino MKR1000 is a pretty cool device, which has an integrated Wifi chip, optimized for low power consumption. This post is not addressing the power aspect, but keep in mind that a possible enhancement of the presented configuration could be to leverage the low power and sleep mode possibilities, in order to run the sensor on battery, instead of having it constantly plugged in.
Without taking that into consideration, my requirements were the following:
- To be able to plug in a sensor in each room of my home, connected to my Wifi network;
- To read humidity and temperature every few minutes;
- To collect all data on a pre-configured Home Assistant server;
- To be able to re-connect automatically, whenever an Internet, wifi or power outage occurs (and in my place, you can expect all the 3 to happen regularly in random order ;-).
Here is my hardware setup:
It's a very simple setup. The DHT22 is connected to pin 6. Notice the 10k resistor on pin 2 of the sensor connected to the 5V. I have added a simple led on pin 2 (with a 220k ohms resistor), in order to be lighten up when a Wifi/MQTT connection is initiated. Basically, if the led is blinking, it means it is sending out a MQTT payload. If it is staying lit up, it means that it is trying to connect still, indicating either a Wifi issue, or a MQTT server (Home Assistant issue).
To properly configure your Home Assistant as a MQTT server, please follow: https://home-assistant.io/components/mqtt/
Here is the code to add to your "configuration.yaml" file in the "sensor" section:
- platform: mqtt
name: "MKR Humidity"
state_topic: "MKR1000_1/humidity"
unit_of_measurement: "%"
- platform: mqtt
name: "MKR Temperature"
state_topic: "MKR1000_1/temperature"
unit_of_measurement: "°C"
Please note the the "MKR1000_1" should be the unique device ID you give to your sensor. You can of course have several devices, with different IDs (for instance MKR1000_1, MKR1000_2, MKR1000_3, etc...), and refer them in your "configuration.yaml" file. You will see in the Code section how to set that up in your code for each device. The two parameters "/humidity" and "/temperature" are the so-called MQTT topics, which are basically data you publish and read from you MQTT sensor. For a more detailled MQTT walkthrough, you can refer to https://www.baldengineer.com/mqtt-tutorial.html.
The Arduino MKR1000 is a pretty cool device, which has an integrated Wifi chip, optimized for low power consumption. This post is not addressing the power aspect, but keep in mind that a possible enhancement of the presented configuration could be to leverage the low power and sleep mode possibilities, in order to run the sensor on battery, instead of having it constantly plugged in.
Without taking that into consideration, my requirements were the following:
- To be able to plug in a sensor in each room of my home, connected to my Wifi network;
- To read humidity and temperature every few minutes;
- To collect all data on a pre-configured Home Assistant server;
- To be able to re-connect automatically, whenever an Internet, wifi or power outage occurs (and in my place, you can expect all the 3 to happen regularly in random order ;-).
Hardware setup
I have selected the DHT22 sensor for this setup. It is not extremely precise (but more than the DHT11 for example, as it can read decimals), but it is good enough for reading room temperatures and humidity, while still being quite cheap.Here is my hardware setup:
- Arduino MKR1000
- led
- 220k ohms resistor (for led)
- DHT22 sensor
- 10k ohms resistor (for DHT222)
It's a very simple setup. The DHT22 is connected to pin 6. Notice the 10k resistor on pin 2 of the sensor connected to the 5V. I have added a simple led on pin 2 (with a 220k ohms resistor), in order to be lighten up when a Wifi/MQTT connection is initiated. Basically, if the led is blinking, it means it is sending out a MQTT payload. If it is staying lit up, it means that it is trying to connect still, indicating either a Wifi issue, or a MQTT server (Home Assistant issue).
Home Assistant setup
The goal of this setup is to communicate to a MQTT server. I have chosen to use Home Assistant as an on premise MQTT server, but basically, you could use the same setup to communicate with any MQTT server, located at your place or even in the cloud, as long as the IP address of the server accessible from your sensor.To properly configure your Home Assistant as a MQTT server, please follow: https://home-assistant.io/components/mqtt/
Here is the code to add to your "configuration.yaml" file in the "sensor" section:
- platform: mqtt
name: "MKR Humidity"
state_topic: "MKR1000_1/humidity"
unit_of_measurement: "%"
- platform: mqtt
name: "MKR Temperature"
state_topic: "MKR1000_1/temperature"
unit_of_measurement: "°C"
Commentaires
Enregistrer un commentaire