Accéder au contenu principal

A simple temperature and humidity MQTT sensor for Home Assistant, based on Arduino MKR1000

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 ;-).


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"

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.

Code







Commentaires

Posts les plus consultés de ce blog

10 Myths about Google Docs

When discussing around me about Google Docs in the corporate world, I'm very often surprised to see the difference between the people who use it -and generally love it-, and the people who have never used it -and have tons of reasons for not doing so-. Here are the 10 justifications I'm earing the most: 1. Google Spreadsheets do not support complex data analysis: wrong, the function list contains hundreds of functions for Math, Date, Financial, Statistical, and Engineering Analysis. The database functions even allow Google Spreadsheets to query and process data from other databases. It can be fully customized using Google Apps Scripts, based on Javascript. You can create charts in a spreadsheet and publish them on an intranet webpage, so they get updated every time you update the data, in real time. 2. You can’t send a Google Document easily as an attachment in another format: wrong, you can export a document by selecting “Email as attachment...” and select the document...

Introduction of an IT Project Management Methodology in a fast-growing company

This article from the PMI Knowledge Shelf explains the rationale behind the creation of an in-house, customized project management methodology, iMAP, at a rapidly growing pharmaceutical company. The authors also describe the priorities that were selected to start with its implementation: a solid project initiation, a clear project life cycle, a special attention to risk management, a first step in reporting standardization, and specific attention to the activities necessary for the transition to operations. They provide an overview of their approach to IT project governance and how a defined project management methodology is the key for its success. https://docs.google.com/file/d/0B40T8ZW4GEUJNmYxMTJhY2YtODY3Mi00NWEwLWExZDMtMmNlMGNiZTUyZWEw/edit