Introduction: HomeKit HomeBridge Siri Enabled Arduino ESP8266 Self Powered 110v Wifi Controlled Powerswitch

Picture of HomeKit HomeBridge Siri Enabled Arduino ESP8266 Self Powered 110v Wifi Controlled Powerswitch

HomeKit HomeBridge Siri Enabled Arduino ESP8266 Self Powered 110v Wifi Controlled Power switch

by Galen Wollenberg

Why buy a UL Listed iDevices Switch when you can potentially electrocute yourself with a self built alternative?

Here's how!

First, you must install HomeBridge on a Raspberry Pi, and configure it as a HomeKit bridge device. Instructions below.

Once you have HomeKit / HomeBridge working on your Pi and your iPhone, we can build a wireless power switch that can be controlled by Siri and the HomeKit app.

We start with a PowerTail2, and use the ESP8266 to control the on/off line.

We power the ESP8266 with a AMS1117 3.3V Power Supply Module. This brings the 5v from the charger down to the proper 3.3v that the ESP8266 needs.

We provide the power with a used cell phone charger. 110v -> 5v. This is wired directly to the 'Line' side of the PowerTail2 circuit board. It is always 'Live' or 'Hot' and will shock you.

We Load the code into the ESP8266 via your favorite USB/Serial converter (FTDI).

We plug it in. Homekit sees the device via the HomeBridge configuration file addition (accessory) on the Raspberry Pi.

You control the device on your iPhone, and turn electricity on and off at will.

More info to come.

Resources:

https://github.com/esp8266/Arduino/tree/master/lib...

http://www.electrodragon.com/w/ESP8266_AT_Commands

https://www.google.com/search?q=esp8266+arduino&es...

http://www.powerswitchtail.com/Documents/PSTK%20In...

Homekit:

https://github.com/nfarina/homebridge

https://github.com/nfarina/homebridge/wiki/Running...

https://www.npmjs.com/package/homebridge-indigo

https://www.google.com/webhp?sourceid=chrome-insta...

https://github.com/lagunacomputer/homebridge-Curre...

Step 1: Don't Electrocute Yourself.

Picture of Don't Electrocute Yourself.

Don't Electrocute Yourself.

Step 2: Install HomeBridge for HomeKit on a Raspberry Pi

Picture of Install HomeBridge for HomeKit on a Raspberry Pi

https://github.com/nfarina/homebridge

Ensure this plugin is installed. It may be installed by default in the newer versions: https://www.npmjs.com/package/homebridge-http

Once installed, make sure you can see the Bridge from within the HomeKit app on your iOS device, before continuing.

Step 3: Edit the HomeBridge /var/homebridge/config.json File on the Raspberry Pi HomeBridge

the file may alternatively be in /home/.homebridge or /root/home/./homebridge. read the docs from the github link

https://github.com/nfarina/homebridge

Ensure these plugin are installed. It may be installed by default in the newer versions:

https://github.com/lucacri/homebridge-http-temperature-humidity

https://www.npmjs.com/package/homebridge-http

Program the ESP8266. Upon powering up (3.3v do not use 5V!) it should be seen on the network.

Try something like http://192.168.1.110/gpio/1 . You should get a webpage returned.

Assuming your ESP8266 pulls a DHCP ip of :192.168.1.110

(you should probably set a DHCP reservation on your router, for each ESP8266 you add)

add this code to the config.json file. ( sudo nano /var/homebridge/config.json) etc:

mind the last comma, you may or may not need it if you have other accessories, or Homebridge is crashing on load.


{    "accessory": "Http",
    "name": "PowerTail",
    "on_url": "http://192.168.1.110/gpio/1",
    "off_url": "http://192.168.1.110/gpio/0",
    "http_method": "GET"
},

Step 4: Build It

Picture of Build It

Solder it together.

1x PowerTail II Power switch kit $19.99 + shipping

1x ESP8266 ESP-01 module $5.50

1x AMS1117-3.3 Power Module AMS1117 3.3V Power Supply Module With Heat Sink $0.99

1x 110v to 5v/1A used cell phone charger

1x small Perfboard for circuit approx 2"x1.25" inch

2x 110v extension cord to cut up $1.99 each at Home Depot

1x small SPST on/off switch

Step 5: ESP8266 Arduino Code

Picture of ESP8266 Arduino Code
<p>/*<br> *  This sketch demonstrates how to set up a simple HTTP-like server.
 *  The server will set a GPIO pin depending on the request
 *    http://server_ip/gpio/0 will set the GPIO0 low,
 *    http://server_ip/gpio/1 will set the GPIO0 high
 *  server_ip is the IP address of the ESP8266 module, will be 
 *  printed to Serial when the module is connected.
 */</p><p>#include </p><p>const char* ssid = "EDITMEWITHYOURWIFISSIDNAME";
const char* password = "EDITMEWITHYOURWIFIPASSWORD";</p><p>// Create an instance of the server
// specify the port to listen on as an argument
WiFiServer server(80);</p><p>void setup() {
  Serial.begin(115200);
  delay(10);
  pinMode(LED_BUILTIN, OUTPUT);     // Initialize the LED_BUILTIN pin as an output
  // prepare GPIO0
  pinMode(0, OUTPUT);
  digitalWrite(0, 0);
  
  // Connect to WiFi network
  //Serial.println();
  //Serial.println();
  //Serial.print("Connecting to ");
  //Serial.println(ssid);
  
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    //Serial.print(".");
  }
  //Serial.println("");
  //Serial.println("WiFi connected");
  
  // Start the server
  server.begin();
  //Serial.println("Server started");</p><p>  // Print the IP address
  //Serial.println(WiFi.localIP());
}</p><p>void loop() {
  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
  
  // Wait until the client sends some data
  //Serial.println("new client");
  while(!client.available()){
    delay(1);
  }
  
  // Read the first line of the request
  String req = client.readStringUntil('\r');
  //Serial.println(req);
  client.flush();
  
  // Match the request
  int val;
  if (req.indexOf("/gpio/0") != -1)
    val = 0;
  else if (req.indexOf("/gpio/1") != -1)
    val = 1;
  else {
    //Serial.println("invalid request");
    client.stop();
    return;
  }</p><p>  // Set GPIO0 according to the request
  digitalWrite(0, val);
   digitalWrite(LED_BUILTIN, val);   // Turn the LED on (Note that LOW is the voltage level
  client.flush();</p><p>  // Prepare the response
  String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n\r\n
\r\nGPIO is now ";
  s += (val)?"high":"low";
  s += "</p><p>\n";</p><p>  // Send the response to the client
  client.print(s);
  delay(10);
  //Serial.println("Client disonnected");</p><p>  // The client will actually be disconnected 
  // when the function returns and 'client' object is detroyed
}</p>

Step 6: Test the New Accessory in IOS HomeKit App

Picture of Test the New Accessory in IOS HomeKit App

Test the new Accessory in iOS HomeKit App and Siri

Check out my Light Sensor plugin for HomeBridge / HomeKit !

https://github.com/lagunacomputer/homebridge-Curre...

https://www.npmjs.com/package/homebridge-CurrentAm...

Comments

JozefO1 (author)2017-09-28

hello

answer from my homebridge:

http set power function failed: socket hang up

where is the problem?

please help me

thx

Ben_Murray1423 (author)2017-08-12

Does anyone know how to make a multi switch using this code? I would like to have one ESP module control multiple relays (maybe 3) for a power strip next to my bed.

GalenW1 (author)Ben_Murray14232017-08-14

Lookup 'arduino shift registers' , although it might be easier to just use 2-3 ESP's connected to the same power source, as they are so cheap

https://www.arduino.cc/en/Tutorial/ShiftOut

SeánB124 (author)2017-03-06

Anyone know how i would code in a button to override the relay and report back to homebridge the changed state?

i am useless at coding. im amazed i followed this to this point.

GalenW1 (author)SeánB1242017-08-06

check out the current version of homebridge-MotionSensor plugin. Better programmers than me have updated the code to support status updates, there is also a nice description on status possibilities within the comments, it can be modified for this purpose (basically what I did here):

https://www.npmjs.com/package/homebridge-MotionSen...

https://github.com/lagunacomputer/homebridge-MotionSensor

electricman05 (author)SeánB1242017-06-04

I don't think you can. The plugin sends request but probably doesn't receive the response.

GalenW1 (author)electricman052017-06-05

you could bypass and code or load the http page that the sensor/arduino serves:

but i don't know how to force the check upon homebridge

ie:
"on_url": "http://192.168.1.110/gpio/1",
"off_url": "http://192.168.1.110/gpio/0",

http://192.168.1.110/gpio/0 etc

GalenW1 (author)2017-05-02

ok i uploaded the correct code file, sorry about that

ДинарХ (author)2016-12-13

Good evening.

I have there is an error.

Could you send the code -.ino 5915838@gmail.com

Thank You!

GalenW1 (author)ДинарХ2016-12-19

ok

About This Instructable

23,089views

95favorites

Bio: I'm a lot of things. Galen Wollenberg https://hackaday.io/projects/hacker/246423 https://www.hackster.io/LagunaComputer
More by GalenW1:​Build a Digital Interactive Electronic Business CardHomeKit HomeBridge Siri Enabled Arduino ESP8266 NodeMCU Based PIR Motion Sensor for HomeKit AutomationHomeKit HomeBridge Siri Enabled Arduino ESP8266 NodeMCU Based Temperature and Ambient Light Room Sensors for HomeKit Automation
Add instructable to: