Free Shipping on orders over US$49.99

Home Automation Using Apple HomeKit And ESP8266


efy tested sani theoIt is an IoT based project through which you can control four home appliances using Apple HomeKit, Hey Siri app, and ESP8266 Wi-Fi module. The author’s prototype is shown in Fig. 1.

Fig. 1: Author’s protoype
Fig. 1: Author’s protoype

A few months ago we were thinking of building a home automation system. After searching on the internet we found that such projects were mainly based on Google Assistant and Alexa. From there we got the idea to make a home automation system that could be controlled using Apple’s assistant, the Hey Siri feature.

Fig. 2: Relay module
Fig. 2: Relay module

We searched this topic but most of the articles written required Apple’s laptop and Homebridge app. We wanted to make the project without using these tools. Then we came across an amazing library that could make it possible, though it had an example to control only one relay. Here is the home automation project through which you can control four appliances through four relays.

Fig. 3: NodeMCU board
Fig. 3: NodeMCU board

Following hardware is required to make the project:

  • 5V relay module
  • NodeMCU board
  • Jumper wires
  • Bulb holder (for 230V AC)
  • 60W/100W bulb
  • iPhone SE or any iPhone that supports Apple HomeKit application
Fig. 4: Bulb and holder
Fig. 4: Bulb and holder

Circuit

Circuit connections are very simple. The Table shows connections between relay module and NodeMCU. The complete connection diagram for controlling a bulb through a relay module is shown in Fig. 6. You can connect the circuit in a similar way to control the remaining three electrical appliances.

Fig. 5: Jumper wires
Fig. 5: Jumper wires

There are two wires in the bulb holder, as shown in Fig. 6. Cut one of the wires in the middle and connect one of the cut ends to COM pin and the other to NO terminal of the relay module. Connect pin D2 of NodeMCU to IN pin of the relay module.

Fig. 6: Circuit connections
Fig. 6: Circuit connections

circuit connectionsSoftware

Download the Arduino-HomeKit library from the link.

After downloading, open Arduino IDE and follow the steps given below.

  1. From the Arduino IDE, click on Sketch and then Include Library option, as shown in Fig. 7. Next click on Add .Zip Library and select HomeKit-ESP8266 library. The three files Example02_Switch, my_accessory.c, and wifi_info.h will be included in the Arduino IDE.

    Fig. 7: Adding .Zip Library to Arduino IDE
    Fig. 7: Adding .Zip Library to Arduino IDE

  2. Open Example02_Switch file, as shown in Fig. 8. The original ‘Example02_Switch.ino’ code includes only one relay module. For adding multiple (four) relays, you need to make some changes in the code, as explained in the next section.

    Fig. 8: Example02_Switch
    Fig. 8: Example02_Switch

  3. In Wifi_info.h file (Fig. 9 ), mention your own SSID and password.

    Fig. 9: Setting SSID and password
    Fig. 9: Setting SSID and password

  4. First make some changes in ‘my_accesory.c’ code. To add many devices, copy and paste the highlighted part in Fig.10 as many times as the number of devices to be added in the project.

    Fig. 10: The highlighted code to be copied and pasted
    Fig. 10: The highlighted code to be copied and pasted

After adding multiple times, change the ‘id’ in the line “HOMEKIT_ACCESSORY (.id=1, .category=homekit_accessory_category_switch, .services=(homekit_service_t*[])” as shown in Fig. 11.

Fig. 11: Changing the id
Fig. 11: Changing the id

Change .id=1 to .id=2. If you have added the third device, change it to .id=3. So every time you copy and paste the highlighted part, increase the .id number.

Fig. 12: Changing the channel switch number
Fig. 12: Changing the channel switch number

In the highlighted part you will find “&cha_switch_on” line, as shown in Fig. 12. This is default code for channel switch number. If you added a new highlighted part, change it to “&cha_switch_on2”. You need to increase the switch number every time you add a new highlighted part.

Fig. 13: GPIO pins on NodeMCU board
Fig. 13: GPIO pins on NodeMCU board

Next, you will find “homekit_characteristic_t cha_switch_on= HOMEKIT_CHARACTERISTIC_(ON, false);” in the same code. Here too, you need to add this line as many times as you have added the devices and also add the switch_on number. For example, if you added two devices, the line of code will be as follows:

homekit_characteristic_t cha_switch_on = HOMEKIT_CHARACTERISTIC_(ON, false);

homekit_characteristic_t cha_switch_on2 = HOMEKIT_CHARACTERISTIC_(ON, false);

5. Now make some changes in Example02_Switch code. Here, you will find the line “extern “C” homekit_characteristic_t cha_switch_on;” You need to add this line every time you add a device and also, as before, you have to change switch_on to switch_on2, switch_on3, etc.

Next, you will find a “#define PIN_SWITCH 2” line of code. Copy this line and add as many times as you have added new devices and also change each pin number. This pin actually defines the pin of NodeMCU board where the device is connected through the relay module.

Fig. 14: GPIO pins defined in the code
Fig. 14: GPIO pins defined in the code

Note that the pin 2 defined here is not the physical pin D2 of NodeMCU; it is GPIO pin 2. The GPIO pins are marked on NodeMCU board, as shown in Fig. 13. The GPIO pins defined in this project for switching the four relays are shown in Fig. 14.

To control four appliances, the author used GPIO2, GPIO16, GPIO4, and GPIO5 pins of NodeMCU. Each GPIO pin should be connected to corresponding relay module, as shown in connection diagram of Fig. 6.

Next is “cha_switch_on.setter = cha_switch_on_setter;” line of code. Copy this one too and add it as many times as the number of devices to be added in the project. For adding four new devices, change the switch_on and setter, as shown in Fig. 15.

Fig. 15: Changing the switch_on and setter in the code
Fig. 15: Changing the switch_on and setter in the code

Next is “void my_homekit_setup()” line. Inside that you will find “pinMode(PIN_SWITCH, OUTPUT);

digitalWrite(PIN_SWITCH, HIGH);” You have to add these lines as many times as you have added new devices. For four devices, you need to change the variables, as shown in Fig. 16.

Fig. 16: PinMode variables
Fig. 16: PinMode variables

After this select following lines:

“void cha_switch_on_setter (const homekit_value_t value) { bool on = value.bool_value;

cha_switch_on.value.bool_value = on; //sync the value LOG_D(“Switch: %s”, on ? “ON” : “OFF”);

digitalWrite(PIN_SWITCH, on ? LOW : HIGH); }”

Copy these lines and add as many times as you have added new devices. Then change the setter, PIN_SWITCH and cha_switch_on in the code, as shown in Fig. 17. This is basically the .setter function to get the switch-event sent from Apple’s iOS device.

Fig. 17: The .setter function to get the switch-events
Fig. 17: The .setter function to get the switch-events

Now, upload the code to NodeMCU board. Next, set up the Apple HomeKit app.

Setting up Apple HomeKit

Follow the steps given below to set up the Apple HomeKit app using Apple’s iPhone SE device.

Step 1. Download the HomeKit from App Store.

Step 2. On the top right corner, click on the plus (+) button. Select Add Accessory. You will see a pop-up window, as shown in Fig. 18. Select More options… to continue.

Fig. 18: Add Accessary window
Fig. 18: Add Accessary window

Step 3. Next another pop-up window appears, as shown in Fig. 19. Here you will find your “Switch” device. Click on the Switch.

Fig. 19: Add To My Home window
Fig. 19: Add To My Home window

Step 4. You will see a warning message popping up. Just click on Add Anyway option, as shown in Fig. 20.

Fig. 20: Setup warning message
Fig. 20: Setup warning message

Step 5. You need to add 8-digit setup code. Here, 11111111 is the default setup code, as shown in Fig. 21.

Fig. 21: Adding setup code
Fig. 21: Adding setup code

Step 6. Now it will ask for switch location. You can choose as per your setup. Here Bedroom switch is used. Click Continue to proceed with the steps.

Step 7. Next the Display As window pops up, as shown in Fig. 22. You can choose any of the devices listed, including fan or light, or switch, if they are connected. Then click Continue.

Fig. 22: Display As window
Fig. 22: Display As window

Step 8. Now it will ask for Switch name, as shown in Fig. 23. This feature gives you the ability to set any custom name for your switch. Then click Continue

Fig. 23: Switch name
Fig. 23: Switch name

Step 9. Lastly, a window pops up showing that switch has been added to your home. Click Done.

Now you can manually control all the four appliances from your iPhone device by pressing the respective buttons or through voice commands. The Hey Siri feature lets you control the appliances using your own voice commands.

For example, to turn on the bedroom light, just say “Hey Siri, turn on the bedroom light.” Immediately, the app will respond with voice “Ok, the switch is on” along with the text displayed on the app, as shown in Fig. 24. At the same time, the bedroom light will turn on.

Fig. 24: Responses from iPhone app
Fig. 24: Responses from iPhone app

Similarly, to turn it off, say “Hey Siri, turn off the bedroom light.” The app will give “Ok, the switch is off” voice response along with the text on the app. At the same time, the bedroom light will be turned off.

Similarly, you can control other appliances that are already connected to NodeMCU and programmed in the app.

Note. After uploading the code to NodeMCU, in case you want to make some changes in the code, first make sure to erase the flash memory of your NodeMCU. Then only you can upload the modified code into the NodeMCU. Otherwise, the app may give “Not available” error message.

Rohan Barnwal is an electronics hobbyist and Youtuber. Rahul Barnwal, a B.Tech from Indian Institute of Information Technology, Kottayam is a physics and electronics enthusiast

Download source code


Rohan Barnwal is an electronics hobbyist and Youtuber. Rahul Barnwal, a B.Tech from Indian Institute of Information Technology, Kottayam is a physics and electronics enthusiast





Source link

We will be happy to hear your thoughts

Leave a reply

AmElectronics
Logo
Enable registration in settings - general
Compare items
  • Total (0)
Compare
0
Shopping cart