Magic MQTT Button for HomeKit (Homebridge)
Introduction: Magic MQTT Button for HomeKit (Homebridge)
The purpose of this project is to use the low-cost ESP8266 module to create a "Magic Switch" that can integrate with various home automation solutions such as Node-red, Home Assistant, Apple HomeKit (using Homebridge), and many more.
I decided to make use of MQTT as the messaging protocol for the Magic Switch because it is fast and reliable using custom firmware on the ESP8266 module known as Tasmota.
The purpose of the switch is to trigger an Automation or Scene in a smart home instance.
For this instructable you require a running instance of Homebridge and MQTT. Both these solutions can be easily installed on the low-cost Raspberry pi.
A good guide on getting started with Homebridge on the Raspberry pi can be found at this wiki or on the official Homebridge website.
Here is some information on getting started with MQTT on a Raspberry pi:
* link 1
* link 2
Let's get started...
Supplies
You require at minimum the following for this project:
* ESP8266 and Programmer
* 3.3V Voltage regulator module (or similar)
* Project enclosure. Here is the 3D printable enclosure.
* 11 mm x 9 mm female power adapter
* Old DC power supply (5V-12V)
* Membrane ON/OFF switch
* Running MQTT broker
* Running Home Automation hub (in this case Homebridge)
Having a soldering iron and hot glue will assist you greatly.
Step 1: Flashing ESP8266 With Tasmota
We will be flashing the ESP8266 module with Tasmota in order to connect it to our MQTT broker.
There are various resources available to assist you in flashing the ESP8266 with Tasmota.
A method for flashing the ESP-01 module using a program called Tasmotizer can be found here.
When you finished flashing Tasmota to the ESP-01 module using the provided link, we will continue to configure the module with an appropriate name as well as configuring the module to connect to our MQTT broker.
Step 2: Configure Module Friendly Name
We will give our module a friendly name using the Tasmota interface.
1. Navigate to the IP Address of your ESP-Module
2. Click "Configuration"
3. Click "Configure Other"
4. Enter a relevant name in "Device name" and in "Friendly Name 1"
5. Click "Save"
6. Wait a few seconds
7. Navigate back to the main menu
Next, we will configure the MQTT settings for our module to connect and publish messages to an MQTT topic.
Step 3: MQTT Setup
In this step we will be configuring our module to connect to an MQTT broker
1. On the main menu of the Tasmota interface, click on "Configuration"
2. Click on "Configure MQTT"
3. Now enter your MQTT broker information
1. Enter your HOST IP address
2. 382
Enter your Broker port number
3. Enter a Client (Friendly name to use for your client, I used "MagicSwitch"
4. Enter your Broker User
5. Enter your Broker Password
6. Enter your topic to publish to, I used "MagicSwitch"
* Click "Save"
* Wait a few seconds
* Navigate to the main menu
In the next section we will be testing that our MQTT connection works.
Step 4: Testing MQTT Configuration
To test the MQTT configuration, I use a program for Windows called MQTT-Explorer. Download and install it. If you are using a MAC, you can download the program from the official website.
1. Open MQTT-Explorer and connect to your MQTT broker
2. Navigate to the Tasmota interface of the module in a web browser on the same network
3. Toggle the module by clicking on "Toggle"
4. Monitor the topic you setup in the MQTT configuration step (stat/MagicSwitch/Power)
5. If all is good you should see it update as you toggle the module
In the next step we will be configuring the GPIO inputs and outputs of the ESP-01 Module.
Step 5: GPIO Configuration
The ESP-01 Module has 8 pins. Some of the pins can be set to use as inputs or outputs. For the Magic Switch project, we want one input to connect a button to, and one output for an LED indicator. 977