ESP8266 + DHT22 + Homie -> Home-Assistant

So now that I had a Home-Assistant (HA) setup working, an esp8266 booting and that I can write software for, it was time to start making the two talk to each other and make something that can become unseful based on all this.

The choice went to the simplest element I had at hand, a DHT22 temperature and humidity sensor.

There were 5 steps to do this:

  • wire the dht22 on a test setup
  • find how to use it in a Homie firmware, and flash it
  • configure the device
  • configure HA for MQTT, which led me to rewrite the whole configuration
  • test it

So let's get to this step by step

Wiring the DHT22

It can be pretty simple, without pullup resistors and capacitor, I found this on instructable

dht22
wiring

I simply put this to another GPIO on my esp12f, but that's pretty much it. The result on the breadboard looks like this:

dht22 breadboard

That's about it, for testing of course, for a final sensors, I'll have to make it cleaner than that...

Homie firmware

I started looking up for code on how to use homie with the dht22, there are libs to make use of it, running:

platformio lib search dht

Showed me the DHT sensor library that is esspressif compatible, I simply installed it via:

platformio lib install 19

After that I looked online at examples to use it, and actually found this code which does exactly what I expected. At first I started writting each lines trying to include only the needed one. But I think this is really close to the bare minimum to get something fully working, so I simply renamed it to raton-dht and switch the version to 0.0.1.

Jumping a little off-topic, the version number has an interest, because Homie support OTA, using the pretty cool homie-ota, which includes a listing of firmware and an inventory of devices, which is pretty awesome for handling yor homie devices and update them if need be. I haven't tried it yet myself, but I definitely will once I get real devices up and running in the house. This really looks like a must-have to me. Once again, thanks jpmens!

Back on tracks, not having the OTA setup yes, I simply switched my GPIO0 to low and ran:

platformio run -t upload

And the next boot the dht sensors firmware was sitting in configuration mode.

Homie configuration

In the previous post, I didn't explain how to configure the homie devices, I actually did not discover it at this step, I played with this on the previous useless firmware to understand how it works.

First it is worth noting that the homie-esp8266 documentation has been moved from github to readme.io, there on the http json api page, you can find the 3 options that you can use to configure your device in configuration mode:

  • the Web UI
  • the android app which apparently is a a webview directly to the webui (at least that's my guess)
  • a curl example, but you'll need to know what the config.json looks like like described in the dedicated page

In any case, you'll need to be connected to the AP the homie device will create when in configuration mode, for me, working on a workstation, and not having my laptop at hand (and feeling too lazy to grab it, plug it as it isn't charged right now, configure another wireless device via wpa_supplicant), that only left me with my android phone... But there is a catch: android checks that it can reach outside to actually let you use your wifi connection.

I found here that on a rooted device you can run:

settings put global captive_portal_detection_enabled 0

This did the trick for me, so I ran that, connected to the Homie-XXXXX AP, and fired the android app, which found the device, and allow me to configure it, the configuration is pretty straightforward and short, once this is done, you can finally reboot the homie device in normal mode, where it connects to your AP/router and start trying to talk to the mqtt broker. Now on to the HA setup.

Home-Assistant MQTT configuration

As said in the previous post, I was planning on using the embedded MQTT broker in HA. According to the MQTT component page, to enable MQTT support in HA, we have to add

mqtt:

To the configuration file, other parameters are for specifying the broker, to use the embedded one, without further configuration, that's it. Restarting HA with that enables the MQTT broker.

Next we need to configure the sensors, but when I started to look to add this I realized how messy my HA configuration had become, with so few things it looked already pretty cluttered with useless things, comments that are only usefull when beginning to use it. So i decided to go for a huge cleanup and rewrite.

First step, version control, I planned on splitting it in multiple files, and this won't be any easy to handle and maintain if not in git, so I created a ha-cfg github repo

I went from this to that

It took some fiddling to understand the !include which is not totally obvious to use, but I'm pretty happy with the final result.

Now in my sensors.yaml I simply added

# temperature test
- platform: mqtt
  state_topic: devices/01397be0/temperature/degrees
  name: "Test temperature"
  qos: 0
  unit_of_measurement: "°C"

# humidity test
- platform: mqtt
  state_topic: devices/01397be0/humidity/relative
  name: "Test humidity"
  qos: 0
  unit_of_measurement: "%"

And I was all set.

Testing

After that and restarting HA with the new configuration, I booted the esp again and could see:

** Booting into normal mode **
{} Stored configuration:
   Hardware device ID: 01397be0
   Device ID: 01397be0
   Boot mode: normal
   Name: dht#1
   Wi-Fi
     SSID: SFR-5458
     Password not shown
   MQTT
     Host: 192.168.0.6
     Port: 1883
     Base topic: devices/
     Auth? no
     SSL? no
   OTA
     Enabled? yes
     Host: 192.168.0.17
     Port: 80
     Path: /ota
     SSL? no
 Attempting to connect to Wi-Fi...
 Wi-Fi connected
Triggering HOMIE_WIFI_CONNECTED event...
 Attempting to connect to MQTT...
Connected
Sending initial information...  OK
Subscribing to topics...  OK
 MQTT ready
Triggering HOMIE_MQTT_CONNECTED event...
Calling setup function...
Sending Wi-Fi signal quality (100%)...  OK
Sending uptime (8s)...  OK

And on HA web-ui I know have test humidity and test temperature sensors showing up on the top row.

ha-ui

And that's it!


21 août 2016 12:39:25 -- tags [ esp8266 , dht22 , home-assistant , homie ]

0 Comments

esp8266 with Homie and PlatformIO

As I talked about in previous posts, I got some ESP8266 devices I wanted to play with, one has been setup on a breadboard for testing for a while, able to boot, but never got to try anything more on it yet.

Protocol and firmware base choice

As my goal is to do some connected devices for home automation, I wanted to make firmwares for these that could talk to Home-Assistant. Even through HA supports quite a lot of way to talk to devices, there were 2 main options I considered:

  • http: simply having a http on the esp which exposes status, and can get commands via POST for example
  • mqtt: it is meant for IoT, the main drawback was I actually didn't want to run a broker

That's when I realized HA MQTT component has an embedded broker. Which apparently is not getting much love as everyone is using mqtt with HA for owntracks, and the embedded broken is incompatible with it. But given I don't plan on using owntracks myself for now, that should be sufficient for my need.

A large part of my fiddling for home automation has been triggered by Jan-Piet Mens and he actually loves MQTT, and played around with clients and told a few times (on twitter if my memory is any good) that Homie for esp8266 was well thought and stable, as I generally find his choices, advices and tastes to match pretty well mine, I decided to go for this.

So here we are, if all goes well, my esp8266 will be running a Homie based firmware to talk to home-assistant via MQTT.

Tools

Then the question of the tools arised, most of the things I read about esps were all talking about Arduino IDE, which I found comments before even trying it, saying to sum it up, as working, but not great. For those who know me, you know I don't like UI, graphical interfaces, and mouse compliant interfaces. I was not trilled to have to use this.

There is quite some talk about esptool which apparently is a great command line tool written in python to flash you esps. That could have done it, but as a complete beginner in all this, I had no clue how to build a project to create the firmware before flashing it with esptool.

Homie github README talks about PlatformIO. I had no clue what this was, and one of the first thing you notice on their webpage is the IDE word, and screenshot of Atom sporting a toolbar to interact with PlatformIO. But after discading it in the first place, and coming back to their website and taking some time to actually read things, I noticed: PlatformIO is an open source ecosystem for IoT development. There is an IDE, but it is more an ecosystem, and everything can be done from the command line, and that was a lot more appealing to me, so I went for it.

Platform.IO

Being a python project, you can install it via pip, some python -c directly from curl as suggested in the Super-Quick Installation Guide.

I went with archlinux's AUR package, there also is a -git one, if need arises later on.

Once installed I went through this page and followed their example:

platformio init --board nodemcu

The nodemcu are based on ESP12s, as I own an esp12f, and I read somewhere it was generally working pretty fine with nocemcu as a board, I decided to go for it.

The previous command create an .ini and a lib and src folders. There also is a .pioenvs folder that contains everything PlatformIO will need to build your project, libs, the output binaries etc.

Then as explained on Homie's getting started guide I installed the lib, given I'm a bit curious I looked it up before typing what the doc suggests.

platformio lib search homie
platformio lib install 555

I then dropped the so called Bare mininum sketch

#include <Homie.h>

void setup() {
      Homie.setup();
}

void loop() {
      Homie.loop();
}

in src/test.c, and tried to compile using the command:

platformio run

But that failed because of missing PubSubClient.h in Homie includes, as PlatformIO is said to handle depedancies, that surprised me but well, whatever:

platformio lib search pubsubclient
platformio lib install 89

Then tried to compile again... this time a real compilation error arised:

In file included from .pioenvs/nodemcu/Homie_ID555/Homie/MqttClient.hpp:3:0,
from .pioenvs/nodemcu/Homie_ID555/Homie.hpp:3,
from .pioenvs/nodemcu/Homie_ID555/Homie.h:4,
from src/test.c:1:
.pioenvs/nodemcu/ESP8266WiFi/ESP8266WiFi.h:27:8: error: expected identifier
or '(' before string constant
extern "C" {
^

Given the presence of .hpp in the error, and the extern "C" section I guess I should not be assume I'm writing C, so I move src/test.c to src/test.cpp, launched the compilation, and this time it built fine.

If you have your FTDI adapter plugged in, and your ESP in flash mode, you can add --target upload to the run command line, and PlatformIO will flash your device for you.

I simply wired GPIO0 to GND and rebooted the esp, launch the run with upload flag, and my esp was flashed. I then rewired GPIO to VCC and rebooted, and the AP from Homie's configuration mode showed up in my phone wifi... I didn't yet took the time to configure it via json and test it further.

But that's already a good starting point, and that's it for now!


18 août 2016 22:59:54 -- tags [ esp8266 , wifi , homie , platformio , IoT , MQTT ]

0 Comments