HomeBridge – MQTT controlling a Led

So now that I have installed and configured my Apple Homebridge based on MQTT. Its time to test if it works with a ESP8266-01 Module. At first I’m going to start simple, as most things do. Let’s try to turn on and off a LED over the Homebridge with Siri.

Homebridge

You need to setup your Apple Homebridge based on MQTT first to connect this setup with Apple HomeKit / Siri. For whom did not read my previous blog about setting up your Apple Homebridge based on MQTT you can find it here.

The hardware

ESP8266-01 ( you can buy one here)
FT232 FTDI USB Adaptor Module ( you can buy one here)
Tactile button ( you can buy one here)
0.1 uF Capacitor ( you can buy one here)
Led Diode (Neon Blue) ( you can buy one here)
Breadboard and Wires ( you can buy one here)

When I have collected al the necessary parts, I can setup the test environment like show in the image below.

As you can see There are two button’s on toe reset the ESP8266-01 module and one to flash the ESP8266-01 module. If you have worked with the ESP8266 for any length of time, you have undoubtedly experienced the endless resets on power-up. From what I have experienced and read from other users, there are two likely hardware causes that makes logical sense:

Inadequate power supply interface.

A flash chip failure.

In order to prevent resets, you must include the following three features in the power source to the ESP8266.

Sufficient current. A regulated 3.3V source of at least 500ma is essential. Aside from the 300ma peak current needs of the ESP8266, it is essential to also consider the current requirements for other components you have – like the sensors and controls in your circuit.

A large capacitor (suggest using 470 uF) across the Vcc to Gnd rails on your breadboard or PCB is a vital ingredient that will minimize reset inducing voltage fluctuations.

A 0.1 uF decoupling capacitor across the ESP8266 Vcc to Gnd inputs very close to the pins (within 1/2 inch). DO NOT SKIP THIS COMPONENT! This cheap yet often overlooked component, when missing, is the root cause of ESP8266 resets.

Software

To connect this hardware with our Apple Homebridge based on MQTT, we need a MQTT client. There are many open source libraries available allowing you to connect your ESP8266 to a MQTT server. The one I found most stable and complete, is the pubsubclient by Imroy. Note that there are multiple pubsubclient libraries available, so make sure you download the one by Imroy. After installing this library to your Arduino IDE, It’s time to start coding.

The Code

/* Project name: Apple Homebridge – MQTT LED Project URI: https://www.studiopieters.nl/apple-homebridge-mqtt-led Description: Apple Homebridge Turn On / OFF A LED OVER MQTT Version: 0.2.1 License: MIT*/#include<ESP8266WiFi.h>#include<PubSubClient.h>constchar*ssid="Your_SSID";// cannot be longer than 32 characters!constchar*pass="Your_PASS";//// Update these with values suitable for your network.IPAddressserver(192,168,1,10);#defineBUFFER_SIZE100voidcallback(constMQTT::Publish&pub){pinMode(2,OUTPUT);// check if the message in On or Offif(pub.payload_string()=="On"){// turn LED on:digitalWrite(2,HIGH);Serial.println(pub.payload_string());}else{// turn LED off:digitalWrite(2,LOW);Serial.println(pub.payload_string());}}WiFiClientwclient;PubSubClientclient(wclient,server);voidsetup(){// Setup consoleSerial.begin(115200);delay(10);Serial.println();Serial.println();}voidloop(){if(WiFi.status()!=WL_CONNECTED){Serial.print("Connecting to ");Serial.print(ssid);Serial.println("...");WiFi.begin(ssid,pass);if(WiFi.waitForConnectResult()!=WL_CONNECTED)return;Serial.println("WiFi connected");}if(WiFi.status()==WL_CONNECTED){if(!client.connected()){if(client.connect("arduinoClient")){client.set_callback(callback);client.subscribe("InTopic");// change InTopic to your Topic description}}if(client.connected())client.loop();}}

You can find the intopic when you have your Apple Homebridge based on MQTT up and running by going to your web browser and opening the Homekit2mqtt web server.

Homekit2mqtt webserver

To add the new accessories, homekit2mqtt has a build-in web-server. This allows you that easily add new MQTT accessories for Homekit. So to connect to your homekit2mqtt web-server, open your web browser and type:

Raspberry_ip_adress:51888

Now you can login with the username:

homekit

and password:

031-45-154

For this setup I added a accessories to the Apple Homebridge based on MQTT by removing all standard MQTT clients by selecting them and pres DEL in the lower left corner.

Then I pressed + Add, to add my new build sensor. In the service field you can select your type of service, in this case want want to add a light bulb.

In the Template field you can select a predefined template , in this case we choose Hue2mqtt color light. In the Name Field we give the new service a name.

HomeKit2MQTT now fills the rest of the fields with the predefined settings of the template.

At The bottom we change OnTrue (0)and OnFalse (254) to a String value and to OnTrue (On) and OnFalse (Off) and then select Save and close the web browser.

So your InTopic is in this case hue/set/lights/LED.

Yes it Works!

So Now we made the first step to make accessories for the Apple Homebridge based on MQTT. From here on we can make more accessories and expand our accessories, so more to come…

Do you have any questions? Leave a comment down here.

Reference

Sebastian Raff (APR 25 2018), Homekit2MQTT, Use to control a MQTT-connected “Thing” in your home automation through Siri and with HomeKit apps., https://github.com/hobbyquaker/homekit2mqtt

It hasn’t to do anything whit this subject directly, but I have to share this one with you…

Donate

Since October 2015, StudioPieters® has been writing this blog. Help accelerate its development with a small contribution! StudioPieters® is open source, and 100% free . Please consider donating to StudioPieters® before starting the fun of building and learning. StudioPieters® is a non-profit organisation devoted to making creative use of electronics accessible to everyone.