For the last few weeks, I’ve been reading up on the ESP8266 wifi board as a potential component in my – to be developed – home automation system. Unfortunately, I’ve been unable to really do anything over the last month or two, but I have been able to read a lot of the developments on the ESP forum – http://www.esp8266.com/ – and this guys blog.

For those who don’t know, the ESP8266 is a series of boards with built in wifi access and their own MCU, and on of the main reasons many people are into them is because they are so cheap for a wifi board. For example, the ESP-01 board which I have can be bought from China for as little as £2 (and less), which is great compared to the price of the current Arduino compatible boards. They are also wildly available from a certain online auction site.

They are also really small, allowing for them to be hidden away in small devices around the home.

Since I noticed them a few months ago, there’s been a ton of development work done with them and there is now a Windows Eclipse based IDE that can be used to develop firmware to flash directly onto the board. Using this, potentially there is no need for a separate Arduino / AVR / PIC board and there are some version of the boards that have quite a few GPIO pins available (the ESP-01 only has two that I can find).

Some people have been working on porting Lua interpreters, others flashing web-servers directly to the boards, and many other things that can be found on the above link.

However, what I’m interested in is using the device as a MQTT client to send and receive data around my HA system. Over on the ESP forum, Tuanpm has been working on code to allow us to use the ESP boards as an MQTT client. The forum post is here and the wiki here. I’ll leave you to read over that if you’re interested.

LED Control

As a first test, I wanted to see if I could control a LED using MQTT.

For this, I setup Mosquitto on a Raspberry Pi, flashed adapted MQTT firmware to my ESP board, connected an LED to GPIO-2 pin, and used MyMQTT on my phone to test publishing topics.

The code is pretty much exactly the same as the in the example code from GitHub, but I changed a few small things.

  • In the function mqttConnectCb(), I changed the subscription to /LivingRoom/LED/1 and removed the publishing calls
  • I added  PIN_FUNC_SELECT(LED_GPIO_MUX, LED_GPIO_FUNC);
    to the user_init function to set GPIO-2 to output
  • Add the following to the beginning the user_main.c file (though I’m not actually sure what they do yet)#include <gpio.h>// see eagle_soc.h for these definitions
    #define LED_GPIO 2
    #define LED_GPIO_MUX PERIPHS_IO_MUX_GPIO2_U
    #define LED_GPIO_FUNC FUNC_GPIO2
  • Finally, added the following to the mqttDataCb functionif (!strcoll(topicBuf, “/LivingRoom/LED/1”)){
    if (!strcoll(dataBuf, “1”)){
    INFO(“LED Switching On\r\n”);
    GPIO_OUTPUT_SET(LED_GPIO, 1);
    }
    else{
    INFO(“LED Switching Off\r\n”);
    GPIO_OUTPUT_SET(LED_GPIO, 0);
    }
    }

The last piece of code simply turns the LED on or off depending on the content of topic /LivingRoom/LED/1

Now, by publishing either a “0” or a “1” to /LivingRoom/LED/1 I can switch the LED on and off!

MQTT Retain

One one of the things I would be looking at doing, it powering the ESP via a battery. However, the problem with that is that when the ESP board is powered down – to save battery – it won’t be subscribed to any topics, so receive the data.

The way around this issue which I’ve tried and appears to be working is to use retention in the publication. For example by tying in mosquitto_pub -r -t /LivingRoom/LED/1 -m “1” from my RasPi, I’m able to power off the ESP board, make the above call, power the ESP device on some time in the future, and once the device is booted up correctly, the LED will switch on. Great!

Coming up

My next step is to use the ESP MQTT code and turn an LED on or off using OpenHab.

Advertisements