ESP8266 or Arduino web controlled relay with password security

//This code will work with either ESP8266 or Arduino using Arduino IDE
//Google ESP8266 or Arduino controlled relay for wiring instructions

//Make sure you have the below libraries installed in the Arduino IDE
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>

// Fill in your WiFi router SSID and password
const char* ssid = “xxxx”;
const char* password = “xxxx”;
MDNSResponder mdns;
ESP8266WebServer server(80);     //Fill in the port number you wish to use

const char INDEX_HTML[] =
“<!DOCTYPE HTML>”
“<html>”
“<head>”
“<meta name = \”viewport\” content = \”width = device-width, initial-scale = 1.0, maximum-scale = 1.0, user-scalable=0\”>”
“<title>MyGDO</title>”
“<style>”
“\”body { background-color: #808080; font-family: Arial, Helvetica, Sans-Serif; Color: #000000; }\””
“</style>”
“</head>”
“<body>”
“<blockquote><h1>MyGDO</h1></blockquote>”
“<FORM action=\”/\” method=\”post\”>”
“<P>”
“<blockquote>Password <input type=\”text\” name=\”PASS\”></blockquote><br>”
“<blockquote><INPUT type=\”submit\” value=\”Send\”></blockquote>”
“</P>”
“</FORM>”
“</body>”
“</html>”;

int LEDPIN = XX;     //Fill in the device pin number you wish to use

void handleRoot()
{
if (server.hasArg(“PASS”)) {
handleSubmit();
}
else {
server.send(200, “text/html”, INDEX_HTML);
}
}

void returnFail(String msg)
{
server.sendHeader(“Connection”, “close”);
server.sendHeader(“Access-Control-Allow-Origin”, “*”);
server.send(500, “text/plain”, msg + “\r\n”);
}

void handleSubmit()

{
String PASSvalue;
if (!server.hasArg(“PASS”)) return returnFail(“BAD ARGS”);
PASSvalue = server.arg(“PASS”);

//Again the password you used above
if (PASSvalue == “xxxx”) {
writeLED(true);
server.send(200, “text/html”, INDEX_HTML);
}

//Again the password you used above
else if (PASSvalue != “xxxx”) {
writeLED(false);
server.send(200, “text/html”, INDEX_HTML);
}
else {
returnFail(“Bad PASS value”);
}
}
void returnOK()
{
server.sendHeader(“Connection”, “close”);
server.sendHeader(“Access-Control-Allow-Origin”, “*”);
server.send(200, “text/plain”, “OK\r\n”);
}
void handleLEDon()
{
writeLED(true);
returnOK();
}
void handleLEDoff()
{
writeLED(false);
returnOK();
}

void handleNotFound()
{
String message = “File Not Found\n\n”;
message += “URI: “;
message += server.uri();
message += “\nMethod: “;
message += (server.method() == HTTP_GET)?”GET”:”POST”;
message += “\nArguments: “;
message += server.args();
message += “\n”;
for (uint8_t i=0; i<server.args(); i++){
message += ” ” + server.argName(i) + “: ” + server.arg(i) + “\n”;
}
server.send(404, “text/plain”, message);
}

void writeLED(bool LEDon)
{
if (LEDon)
digitalWrite(LEDPIN, 1);
delay(500);
digitalWrite(LEDPIN, 0);

//This part refreshes the page without resubmitting trigger data
//Fill in IP address and Port number from above or…
//put in external IP address and same port number if accessing from the web
server.sendHeader(“Location”, String(“http://xxx.xxx.xxx.xxx:xxx”), true);
server.send ( 302, “text/plain”, “”);
}
void setup(void){
pinMode(LEDPIN, OUTPUT);
//digitalWrite(LEDPIN, LOW);
writeLED(false);

Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.println(“”);

// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);     //Timer for relay on/off
Serial.print(“.”);
}
Serial.println(“”);
Serial.print(“Connected to “);
Serial.println(ssid);
Serial.print(“IP address: “);
Serial.println(WiFi.localIP());

if (mdns.begin(“esp8266WebForm”, WiFi.localIP())) {
Serial.println(“MDNS responder started”);
}

server.on(“/”, handleRoot);
server.on(“/ledon”, handleLEDon);
server.on(“/ledoff”, handleLEDoff);
server.onNotFound(handleNotFound);

server.begin();
Serial.print(“Connect to http://esp8266WebForm.local or http://”);
Serial.println(WiFi.localIP());
}

void loop(void)
{
server.handleClient();
}


Comments are closed.

eMail Me