/* Project: ESP8266 ESP-12E NodeMCU, 5V relay module Function: Open/Close garrage door. Button Activate protected by 4 digit password 1234 (you can change it) Magnetic switch attached to ESP-12E D2 (GPIO4)pin and to 3.3V pin 5V relay module Nodemcu ESP-12E 5V pin 3.3V pin (used logic level converter) GND pin GND pin(used logic level converter) S (Signal) pin D1 (GPIO5)pin (used logic level converter) */ //************************************************************************************************** #include //include library code extern "C" { #include "user_interface.h" } //************************************************************************************************** #define F int openClosePin = 5; //5V relay module signal pin attached to D1 (GPIO5)pin int statusPin = 4; //magnetic switch pin attached to D2 (GPIO4)pin const int greenLEDPin = 16; //5V relay module signal pin attached to D0 (GPIO16)pin const int redLEDPin = 0; //5V relay module signal pin attached to D3 (GPIO0)pin String HTTP_req; String code; WiFiServer server(80);//server port 80 //Change SSID and password for your local WiFi network const char* SSID = "TR-2"; const char* PASS = "AQW112"; //Change password to enable the button Activate const char* pass_sent = "1234"; char codeOK = '0'; //start code is blank //************************************************************************************************** void setup() { Serial.begin(115200);//initialise serial communication at 115200 bps pinMode(redLEDPin, OUTPUT);//set redLEDPin as OUTPUT pinMode(greenLEDPin, OUTPUT);//set greenLEDPin as OUTPUT pinMode(openClosePin, OUTPUT);//set openClosePin as OUTPUT pinMode(statusPin, INPUT);//set statusPin as INPUT digitalWrite(openClosePin, HIGH);//set the door closed by default delay(50);//set delay for 50 ms Serial.print(F("Connecting to ")); Serial.println(F(SSID)); ESP.wdtDisable(); wifi_set_opmode(WIFI_STA); WiFi.begin(SSID, PASS);//connecting to the local WiFi network while (WiFi.status() != WL_CONNECTED) { delay(500);//set delay for half a second Serial.print(F("."));//print dots till connects to local WiFi network } Serial.println(F("")); Serial.println(F("WiFi connected")); delay(1000);//set delay for 1 second ESP.wdtEnable(WDTO_4S); server.begin();//start the server Serial.println(F("Server started")); Serial.print(F("Use this URL to connect: ")); Serial.print(F("http://")); Serial.print(F(WiFi.localIP()));//local IP address to control the garage door Serial.println(F("/")); delay(1000);//set delay for 1 second } //***************************************************************************************************** void loop() { delay(0); WiFiClient client = server.available();//check if a client has connected if (client) { //any 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 if (c == '\n' && currentLineIsBlank) {//last line of client request is blank and ends with \n respond to client only after last line received 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("\r\n\r\n\r\nGarage door control\r\n")); client.print(F("\n")); client.print(F("\n")); client.print(F("\r\n\r\n

GARRAGE DOOR CONTROL


\r\n
\r\n
\r\n
\n")); client.print(F("Type password: \r\n

 


\n")); if (HTTP_req.indexOf(pass_sent) > 0) { GetCode(); } if (codeOK == '0') { client.print(F("

\n")); } if (codeOK == '1') { client.print(F("

\n")); } if (HTTP_req.indexOf("door_activate") > -1) {// read switch state and send appropriate paragraph text DoorActivate(); } client.println(F("\r\n")); delay(0); } Serial.println(F(HTTP_req));//display received HTTP request on serial port 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(statusPin)) { cl.println(F("

Garage door status: Closed

")); } else { cl.println(F("

Garage door status: Opened

")); } } void GetCode() { codeOK = '1'; } void DoorActivate() { digitalWrite(openClosePin, LOW);//Trigger the garage door digitalWrite(redLEDPin, LOW); digitalWrite(greenLEDPin, HIGH); delay(1000);//set delay for 1 second digitalWrite(openClosePin, HIGH); digitalWrite(redLEDPin, HIGH); digitalWrite(greenLEDPin, LOW); codeOK = '0'; }