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

About: I'm a lot of things. Galen Wollenberg https://hackaday.io/projects/hacker/246423 https://www.hackster.io/LagunaComputer

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.

Don't Electrocute Yourself.

Step 2: 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

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

<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

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...