Hardware components | ||||||
| × | 1 | ||||
| × | 1 | ||||
| × | 1 | ||||
| × | 1 | ||||
| × | 1 | ||||
| × | 1 | ||||
Software apps and online services | ||||||
|
Not much to say here. I gathered an ESP-12, magnetic switch, 5 VDC relay, small breadboard, and a breadboard power supply and made a garage door monitor/opener. Stuffed it all in a small plastic outlet box and uploaded the sketch. Esp is connected to the 5 VDC side of the relay and the other side connected straight to the 2 screws on the garage door opener. When the 2 connections on the garage door opener see a closed circuit for 1 sec or o the garage door operates. So all we need to do it operate the relay to open the door.
The magnetic door switch tells us when the door is closed or open and a webpage displays all of this. A 4 digit numerical password is needed to operate the door from the webpage. Ajax is used to keep the status of the door updated on the webpage.
I used regular cat5 cable to run from the esp-12 to the magnet switch and from the esp-12 to the garage door opener contacts.
/* IoT Garage Door Opener
/ This is a sketch for the ESP8266, ESP-12 in my case, using the Arduino IDE for ESP8266. This sketch will allow you to see the current
/ state of the garage door using a magnetic switch on the garage door, one side connected to pin 4 of the ESP-12 and the other side
/ connected to ground. Doing this pulls pin 4 LOW when the Garage Door is closed completely. The webpage updates every 10 seconds
/ in order to update the status of the door. It is possible to add another magnetic switch to show when the door is fully open by
/ tieing it to another pin the same way. You would have to change the code for the new pin.
/ Activating the button for the door requires a 4 digit pass code.
/
/ To open the door I am using a High level triggered single 5V relay. I am pulling pin 5 LOW in setup then high for 1 second to
/ trigger the door. ESP os on a smd adapter frpm Electrodragon. I am powering the ESP and the relay using a 5V 1A power adapter.
*/
#include <ESP8266WiFi.h>
extern "C" {
#include "user_interface.h"
}
#define F
int openClosePin = 5;
int statusPin = 4;
String HTTP_req;
String code;
WiFiServer server(8100);
//*-- IoT Information
const char* SSID = "SSID";
const char* PASS = "PASSWORD";
const char* pass_sent = "YOUR_4_DIGIT_CODE";
char codeOK='0';//start Code is blank....
void setup() {
ESP.wdtDisable();
Serial.begin(115200);
pinMode(openClosePin, OUTPUT);
pinMode(statusPin, INPUT);
digitalWrite(openClosePin, LOW);
delay(50);
Serial.print(F("Connecting to "));
Serial.println(F(SSID));
wifi_set_opmode(WIFI_STA);
WiFi.begin(SSID, PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(F("."));
}
Serial.println(F(""));
Serial.println(F("WiFi connected"));
delay(1000);
ESP.wdtEnable(WDTO_4S);
//Start the server
server.begin();
Serial.println(F("Server started"));
//Print the IP Address
Serial.print(F("Use this URL to connect: "));
Serial.print(F("http://"));
Serial.print(F(WiFi.localIP()));
Serial.println(F("/"));
delay(1000);
}
void loop() {
delay(0);
// Check if a client has connected
WiFiClient client = server.available();
// Return the response
if (client) { // got client?
boolean currentLineIsBlank = true;
codeOK='0';
while (client.connected()) {
if (client.available()) { // client data available to read
char c = client.read(); // read 1 byte (character) from client
HTTP_req += c; // save the HTTP request 1 char at a time
// last line of client request is blank and ends with \n
// respond to client only after last line received
if (c == '\n' && currentLineIsBlank) {
client.println(F("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: keep-alive"));
client.println();
if (HTTP_req.indexOf("ajax_switch") > -1) {
// read switch state and send appropriate paragraph text
GetSwitchState(client);
delay(0);
}
else { // HTTP request for web page
// send web page - contains JavaScript with AJAX calls
client.print(F("<!DOCTYPE html>\r\n<html>\r\n<head>\r\n<title>IoT Garage Door</title>\r\n<meta name='viewport' content='width=device-width', initial-scale='1'>"));
client.print(F("<script>\r\nfunction GetSwitchState() {\r\nnocache = \"&nocache=\" + Math.random() * 1000000;\r\nvar request = new XMLHttpRequest();\r\nrequest.onreadystatechange = function() {\r\nif (this.readyState == 4) {\r\nif (this.status == 200) {\r\nif (this.responseText != null) {\r\ndocument.getElementById(\"switch_txt\").innerHTML = this.responseText;\r\n}}}}\r\nrequest.open(\"GET\", \"ajax_switch\" + nocache, true);\r\nrequest.send(null);\r\nsetTimeout('GetSwitchState()', 1000);\r\n}\r\n</script>\n"));
client.print(F("<script>\r\nfunction DoorActivate() {\r\nvar request = new XMLHttpRequest();\r\nrequest.open(\"GET\", \"door_activate\" + nocache, true);\r\nrequest.send(null);\r\n}\r\n</script>\n"));
client.print(F("</head>\r\n<body onload=\"GetSwitchState()\">\r\n<center><h1>IoT Garage Door</h1><hr>\r\n<div id=\"switch_txt\">\r\n</div>\r\n<br>\n"));
client.print(F("Input password to control Garage Door.\r\n<br><br><form name=\"passcode\" onSubmit=\"GetCode()\"><input type=\"password\" name=\"password\" size='8' maxlength='4'> <input type=submit name=\"Submit\" value=\"Submit\" onClick=\"GetCode()\" style='height:22px; width:80px'></form><br><br>\n"));
if (HTTP_req.indexOf(pass_sent) > 0) {
GetCode();
}
if (codeOK == '0') {
client.print(F("<button type=\"button\" disabled style='height:50px; width:225px'>Activate the Door</button><br><br>\n"));
}
if (codeOK == '1') {
client.print(F("<button type=\"button\" onclick=\"DoorActivate()\" style='height:50px; width:225px'>Activate the Door</button><br><br>\n"));
}
client.println(F(system_get_free_heap_size()));
if (HTTP_req.indexOf("door_activate") > -1) {
// read switch state and send appropriate paragraph text
DoorActivate();
}
//}
client.println(F("</body>\r\n</html>"));
delay(0);
}
// display received HTTP request on serial port
Serial.println(F(HTTP_req));
HTTP_req = ""; // finished with request, empty string
break;
}
// every line of text received from the client ends with \r\n
if (c == '\n') {
// last character on line of received text
// starting new line with next character read
currentLineIsBlank = true;
}
else if (c != '\r') {
// a text character was received from client
currentLineIsBlank = false;
}
} // end if (client.available())
} // end while (client.connected())
delay(1); // give the web browser time to receive the data
client.stop(); // close the connection
delay(0);
} // end if (client)
}
// send the state of the switch to the web browser
void GetSwitchState(WiFiClient cl) {
if (digitalRead(4)) {
cl.println(F("<p>Garage Door is currently: <span style='background-color:#FF0000; font-size:18pt'>Open</span></p>"));
}
else {
cl.println(F("<p>Garage Door is currently: <span style='background-color:#00FF00; font-size:18pt'>Closed</span></p>"));
}
}
void GetCode() {
codeOK='1';
}
void DoorActivate() {
digitalWrite(openClosePin, HIGH);
delay(1000);
digitalWrite(openClosePin, LOW);
codeOK='0';
}
Comments