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

http://www.instructables.com/id/HomeKit-Enabled-Arduino-ESP8266-Self-Powered-110v-/

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

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 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 2: 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 this plugin is installed. It may be installed by default in the newer versions:

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 3: ESP8266 Arduino Code

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

#include

const char* ssid = "EDITMEWITHYOURWIFISSIDNAME";
const char* password = "EDITMEWITHYOURWIFIPASSWORD";

// Create an instance of the server
// specify the port to listen on as an argument
WiFiServer server(80);

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");

// Print the IP address
//Serial.println(WiFi.localIP());
}

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;
}

// 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();

// 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 += "

\n";

// Send the response to the client
client.print(s);
delay(10);
//Serial.println("Client disonnected");

// The client will actually be disconnected
// when the function returns and 'client' object is detroyed
}

Step 4: Test the new Accessory in iOS HomeKit App

Step 5: Don’t Electrocute Yourself.

https://lagunabeachcomputer.com/wp-content/uploads/2016/10/image1-150×150.png

https://lagunabeachcomputer.com/wp-content/uploads/2016/10/homekit_siri-150×150.png

http://52.27.64.85/wp-content/uploads/2016/10/pst_ii_rev_6a_oct_2013.bmp

https://lagunabeachcomputer.com/wp-content/uploads/2016/10/esp8266-reflash-firmware-150×150.png

https://lagunabeachcomputer.com/wp-content/uploads/2016/10/IMG_4286-150×150.jpg

https://lagunabeachcomputer.com/wp-content/uploads/2016/10/IMG_4283-150×150.jpg

Arduino based Solar Panel Tracker

Arduino based Solar Tracker for Solar Panels

Works via two photoresistors.

20130331-090348.jpg

Here is the Arduino Sketch code I wrote for the tracker:

int lightPin = A0; //define a pin for Photo resistor
int ledPin=6; //define a pin for LED
int val= 1;
int lightPin1 = A1; //define a pin for Photo resistor
int ledPin5=5; //define a pin for LED
int val1= 1;
int setPoint19 = 20; // Trigger value
int setPoint25 = 25; // Trigger value
int setPoint29 = 29; // Trigger value
int setPoint35 = 35; // Trigger value

void setup()
{
Serial.begin(9600); //Begin serial communcation
pinMode( ledPin, OUTPUT );
pinMode( ledPin5, OUTPUT );
}

void loop()
{

val = (analogRead(lightPin)*1);

if (val < setPoint35) { analogWrite(ledPin, LOW); // It has got dark, turn the LED on.
}
else
{
analogWrite(ledPin, 255); // It is light again, turn the LED off.

}
Serial.print("val = ");
Serial.print(val ) ; //Write the value of the photoresistor to the serial monitor.
Serial.print(" lightPin = ");
Serial.print(analogRead(lightPin));
Serial.print(" lightPin /4-1= ");
Serial.println(analogRead(lightPin)/4-1); //Write the value of the photoresistor to the serial monitor.
//analogWrite(ledPin, val); //send the value to the ledPin. Depending on value of resistor
//you have to divide the value. for example,
//with a 10k resistor divide the value by 2, for 100k resistor divide by 4.
//Serial.println(analogRead(lightPin)/3); //Write the value of the photoresistor to the serial monitor.
delay(10); //short delay for faster response to light.

val1 = (analogRead(lightPin1)*1);

if (val1 < setPoint35) { analogWrite(ledPin5, LOW); // It has got dark, turn the LED on.
}
else
{
analogWrite(ledPin5, 255); // It is light again, turn the LED off.

}
Serial.print("val1= ");
Serial.print(val1 ) ; //Write the value of the photoresistor to the serial monitor.
Serial.print(" lightPin1= ");
Serial.print(analogRead(lightPin1));
Serial.print(" lightPin1/4-1= ");
Serial.println(analogRead(lightPin1)/4-1); //Write the value of the photoresistor to the serial monitor.
//analogWrite(ledPin, val); //send the value to the ledPin. Depending on value of resistor
//you have to divide the value. for example,
//with a 10k resistor divide the value by 2, for 100k resistor divide by 4.
//Serial.println(analogRead(lightPin)/3); //Write the value of the photoresistor to the serial monitor.
delay(10); //short delay for faster response to light.

}

Arduino Solar Tracker

Here is the Arduino sketch code I wrote that will move a 2-wire DC motor forward and reverse depending on which of 2 buttons is pressed:

#include

/*
* Switch and LED test program
*/

int ledPin = 5; // LED is connected to pin 12
int switchPin = 0; // switch is connected to pin 2
int val; // variable for reading the pin status
int ledPin6 = 6; // LED is connected to pin 12
int switchPin1 = 1; // switch is connected to pin 2
int val1; // variable for reading the pin status

void setup() {
pinMode(ledPin, OUTPUT); // Set the LED pin as output
pinMode(switchPin, INPUT); // Set the switch pin as input
pinMode(ledPin6, OUTPUT); // Set the LED pin as output
pinMode(switchPin1, INPUT); // Set the switch pin as input
//Setup Channel A
pinMode(12, OUTPUT); //Initiates Motor Channel A pin
pinMode(9, OUTPUT); //Initiates Brake Channel A pin

}

void loop(){

delay(10);

val = digitalRead(switchPin); // read input value and store it in val
if (val == LOW) { // check if the button is pressed
digitalWrite(ledPin, HIGH); // turn LED on
//Serial.begin(9600); // set up Serial library at 9600 bps
//Serial.print("sw0 on: ");Serial.println(digitalRead(switchPin));

//forward @ full speed
digitalWrite(12, HIGH); //Establishes forward direction of Channel A
digitalWrite(9, LOW); //Disengage the Brake for Channel A
analogWrite(3, 255); //Spins the motor on Channel A at full speed

delay(3000);

//delay(10);
}
if (val == HIGH) { // check if the button is not pressed
digitalWrite(ledPin, LOW); // turn LED off
delay(10);

digitalWrite(9, HIGH); //Eengage the Brake for Channel A

//delay(1000);

}

delay(10);

val = digitalRead(switchPin1); // read input value and store it in val
if (val == LOW) { // check if the button is pressed
digitalWrite(ledPin6, HIGH); // turn LED on
//Serial.print("on: ");Serial.println(digitalRead(switchPin1));

//backward @ half speed
digitalWrite(12, LOW); //Establishes backward direction of Channel A
digitalWrite(9, LOW); //Disengage the Brake for Channel A
analogWrite(3, 255); //Spins the motor on Channel A at half speed

delay(3000);

delay(10);
}
if (val == HIGH) { // check if the button is not pressed
digitalWrite(ledPin6, LOW); // turn LED off
//Serial.print("1=off: ");Serial.println(digitalRead(switchPin1));

digitalWrite(9, HIGH); //Eengage the Brake for Channel A

delay(1000);

delay(10);
}

}