/* Project: ESP8266 ESP-12E NodeMCU, 5V relay module Function: Start/Stop IOT device. Buttons Activate and Deactivate protected by 4 digit password 1234 (you can easily change it). 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 const int startStopPin = 5; //5V relay module signal pin attached to D1 (GPIO5)pin const int greenLEDPin = 16; //5V relay module signal pin attached to D0 (GPIO16)pin const int redLEDPin = 4; //5V relay module signal pin attached to D2 (GPIO4)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(startStopPin, OUTPUT);//set startStopPin as OUTPUT pinMode(redLEDPin, OUTPUT);//set redLEDPin as OUTPUT pinMode(greenLEDPin, OUTPUT);//set greenLEDPin as OUTPUT digitalWrite(startStopPin, HIGH);//set the device stopped 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 IOT device 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\nDevice control\r\n")); client.print(F("\n")); client.print(F("\n")); client.print(F("\n")); client.print(F("\r\n\r\n

DEVICE 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")); client.print(F("

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

\n")); client.print(F("

\n")); } if (HTTP_req.indexOf("device_activate") > -1) {// read switch state and send appropriate paragraph text DeviceActivate(); } if (HTTP_req.indexOf("device_deactivate") > -1) {// read switch state and send appropriate paragraph text DeviceDeactivate(); } 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(startStopPin)) { cl.println(F("

Device status: Stopped

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

Device status: Started

")); } } void GetCode() { codeOK = '1'; } void DeviceActivate() { digitalWrite(startStopPin, LOW); digitalWrite(redLEDPin, LOW); digitalWrite(greenLEDPin, HIGH); codeOK = '0'; } void DeviceDeactivate() { digitalWrite(startStopPin, HIGH); digitalWrite(redLEDPin, HIGH); digitalWrite(greenLEDPin, LOW); codeOK = '0'; }