Introduction.
In one of my previous articles, about the Home Automation System I have built for myself, I have spent some time describing the entire Architecture and the SW modules. See it here, if you haven’t read.
Regarding the HW used for Sensors’ Nodes, I haven’t said too much. I used an old Intel Edison, a Particle Photon (good one) and some Arduino MKR 1000.
Now, it is time for Rome Maker Faire 2018 event and therefore I have decided to start a new evolution for my Home Automation. Here I want to describe, in some length, the main changes I have decided to make.
New Sensors’ Nodes.
In IoT field change goes fast. And probably it goes faster in the Makers’ world.
Arduino world is amazing from this point of view and from day to day we see new exciting things coming from the global effort of a planetary community willing to innovate and share the results of such creativity.
In Arduino Day in May, M. Banzi and F. Violante announced several new boards in the MKR family. One really interesting for me was the MKR WiFi 1010.
The MKR WiFi 1010 has exactly the same form factor of MKR 1000, the same number of I/O pins and the same processor. It is much compatible. But, as described by M. Banzi, it can be seen as a big improvement over the MKR 1000, for several reasons:
- It uses for wireless communication an Espressif ESP32 (the chip is built by u-blox)
- It comes with a completely OpenSource firmware for ESP32
- It support Bluetooth in addition to WiFi (the support is coming from ESP32)
- It has onboard an ECC508 chip that can be used to encrypt and protect the communication (for example with TLS)
- It consumes less power
- and, last but not the least, it is cheaper (3 euro less on Arduino.cc at the time of writing)
The completely OpenSource firmware enables the Arduino Community to control the evolution of the library and therefore to give, in the time, big improvements in reliability, speed and quality of service.
As first result, you can see that the recommended Arduino library to work with it is a completely new one, called WiFININA, and I can tell you that people working on it are doing, day-by-day, a great work to make it reliable and performant. Have a look at: https://github.com/arduino-libraries/WiFiNINA
So, to make it short, I have decided to replace completely my set of boards for Sensors’ nodes with MKR WiFi 1010. I have built the first node and it will go “production” as part of my HomeAuto this weekend. I will buy the next set of MKR WiFi 1010 at the Arduino shop in Maker Faire.
ROOM1, first iteration.
For now, the name of the first device equipped with WiFi 1010 is ROOM1.
From the point of view of the sensors attached, it is rather simple: a DHT22 to measure temperature and humidity and a photo diode to measure light.
I’m always struggling to find nice housing for “my things”. This first prototype of the new generation is hosted inside an Ikea candle container:
The container has many holes, that is good for the DHT22, attached as you can see on the top front (with the three pending cable).
In short, probably I should think to build the housing with a 3D printer, but that’s another game.
Again, as described in Evolving my Home Automation (1), the board communicate with the Home backbone using MQTT protocol. Every XX seconds a JSON message is sent to the MQTT broker with all the sensors’ readings. The MQTT broker is hosted by a RPI 3.
Some details on the HW
Using DHT22 is not really difficult and you can find many tutorials around. In addition, if you have already used it with another Arduino board, nothing really change.
You need to use 3 pins:
- PIN1 attached to power (RED cable in the photo)
- PIN4 attached to ground
- PIN2 must be attached to one digital pin of the MKR board, for data
- PIN3 is not used at all.
You must connect a 10K resistor between PIN2 and power.
(In the above picture the board is a MKR 1000, I have not found a diagram with WiFi 1010, but the pinout is exactly the same).
Regarding DHT22 sensor and its features, well I want to give you some warnings from my personal experience. It is a cheap sensor, easy to use, but:
- I have some showing completely wrong readings (1% humidity, are you joking?)
- It is not very accurate (0.5° for temperature and 5% for humidity)
- It is a rather slow sensor, but it is OK if you want to monitor room conditions, and take readings every 30 secs.
So, if you want to be precise, you should consider more expensive and accurate sensors (I’m thinking to try Adafruit SHT15) or you should compare readings with more accurate sensors and, by software, remove any casual and systematic error.
For measuring light, I have decided to take a simple approach: a photo-diode in series with a resistance, to make a voltage divider.
The more is the light, the higher is the reading taken from an analogic port of the MKR.
For now, no conversion in LUX, the reading is simply an integer between 0 and 1023.
More details on the SW.
Biggest changes are in the Arduino code.
Three innovations here:
- WiFiNINA
- MQTT library by Joel Gahwiler
- ArduinoJSON library
First of all, to connect to WiFi I’m using WiFiNINA library. This is the new OpenSource library they’re building and pushing for the new generation of boards (not only MKR WiFI 1010 but also MKR Vidor 4000, …)
If you have a look at the github, you’ll see that the library is rapidly evolving. Before the summer it was version 1.0. Now it is 1.2
Well, there have been bugs (and sure there are). For example I opened one (Issue #5, static IP address assignment not working). Some other Makers signalled performance problems, now fixed.
The best advices here, from my experience, are:
- MKR WiFi 1010 is the future; Even if it is new, don’t revert back to MKR 1000 at the first signs of (small) troubles. It will become better and better and now it is already working fine
- Do upgrade the library at 1.2 version and keep an eye on the github for major upgrade and fixes
For MQTT protocol support I decided to adopt the Joel Gahwiler MQTT library, that you can download using the Arduino Library Manager built-in the IDE. It is a library built around lwmqtt client. In my opinion it works really well together WiFiNINA.
Last, but not the least, I decided to simplify my life in joking with JSON and I decided to adopt ArduinoJSON library. It make it easier to serialize your messages in JSON, avoiding complicated and error-prone playing with Strings (or worse C arrays).
The building of JSON payload is encapsulated in sendMessage() function (see code below), overloaded because we have two version: one for sending the data message and another for the status message (READY).
The Arduino code aspire to be reliable, as much as possible, therefore:
- it handles reconnection to the MQTT broker
- it uses MQTT QoS 1 (delivery of messages is guaranteed)
One last small thing: to avoid messing with the (possibly changing) configuration, it is completely encapsulated in a dedicated config.h file.
ROOM1, iteration2.
Ok, I have decided to spend some more time on the Design and Usability.
IT is OK to transmit data to an MQTT broker, to show in a central Dashboard, but I want also to be able to read immediately the temperature and humidity. And I want feedback if something goes wrong and the board doesn’t connect to MQTT.
In addition, if I want to put an object like this in each of my rooms, I should also think about “the Design”. Functional is good but not always “nice”.
Therefore, as next step, I have added an LCD, attached to the MKR WiFi 1010. It shows some messages in the initialization phase (IP address, MQTT Ready,…) to assist if something goes wrong. And then it continuously shows temperature, humidity and light level.
To connect the LCD to the MKR WiFi 10101 I have followed the tutorial on Arduino site, with some slight changes.
The biggest difficulty has been to arrange all the cables. It was like what our grandparents did: “putting a ship into a bottle”.
As you can see from above picture, many more cables, more complicated. There is the risk that the entire thing become less reliable and difficult to diagnose (for example, if one cable disconnect). For this reason, I’m thinking to change for an I2C LCD. It costs more but needs only four cables.
Obviously the code has changed, to handle the LCD using LiquidCrystallibrary..
Do you want to have a look at the actual code? Next section please.
The Arduino code.
HW Bill of material.
- Arduino MKR WiFi 1010
- Temperature and Humidity Sensor Adafruit DHT 22
- 1 LCD compatible with HD44780 driver (like the one you find in Arduino kit)
- 1 photodiode
- 1 10K resistor
- 1 other resistor for the voltage divider
- breadboard
- some cables
In conclusion.
In this article I have described the evolution I’m applying to my Home Automation system.
First of all, the widespread adoption of MKR WiFI 1010. After some months of tests it seems to me reliable and really an interesting step forward. I have started and hope to complete for the end of the year.
Second, the adoption of new libraries, for WiFi, MQTT and JSON serialization.
Third, it is time for Rome Maker Faire, and for sure I’ll find tons of interesting idea to apply there.
I hope to have brought to you something interesting, and if you have any comments, I’ll appreciate.