Skip to content

Instantly share code, notes, and snippets.

@Henrique-Miranda
Created March 25, 2019 04:38
Show Gist options
  • Save Henrique-Miranda/b3ae5703f89e6cf7e341398d00f115e0 to your computer and use it in GitHub Desktop.
Save Henrique-Miranda/b3ae5703f89e6cf7e341398d00f115e0 to your computer and use it in GitHub Desktop.
smarthouse arduino version
#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>
#include <ESP8266mDNS.h>
#define wpump 14
#define yard 12
#define hkitchen 4
#define room 5
unsigned long TIMER = 0;
ESP8266WebServer server(80);
//Check if header is present and correct
bool is_authenticated() {
Serial.println("Enter is_authenticated");
if (server.hasHeader("Cookie")) {
Serial.print("Found cookie: ");
String cookie = server.header("Cookie");
Serial.println(cookie);
if (cookie.indexOf("ESPSESSIONID=1") != -1) {
Serial.println("Authentication Successful");
return true;
}
}
Serial.println("Authentication Failed");
return false;
}
//login page, also called for disconnect
void handleLogin() {
String msg;
if (server.hasHeader("Cookie")) {
Serial.print("Found cookie: ");
String cookie = server.header("Cookie");
Serial.println(cookie);
}
if (server.hasArg("DISCONNECT")) {
Serial.println("Disconnection");
server.sendHeader("Location", "/login");
server.sendHeader("Cache-Control", "no-cache");
server.sendHeader("Set-Cookie", "ESPSESSIONID=0");
server.send(301);
return;
}
if (server.hasArg("USERNAME") && server.hasArg("PASSWORD")) {
if (server.arg("USERNAME") == "admin" && server.arg("PASSWORD") == "admin") {
server.sendHeader("Location", "/");
server.sendHeader("Cache-Control", "no-cache");
server.sendHeader("Set-Cookie", "ESPSESSIONID=1");
server.send(301);
Serial.println("Log in Successful");
return;
}
msg = "Wrong username/password! try again.";
Serial.println("Log in Failed");
}
String content = "<!DOCTYPE html>\r\n<html lang='pt-br'>\r\n<head>\r\n<meta charset='utf-8'/>\r\n";
content += "<title>SMART HOUSE</title>\r\n";
content += "<link href='https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css' rel='stylesheet' integrity='sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO' crossorigin='anonymous'>\r\n";
content += "</head>\r\n<body>\r\n<div align='center'>\r\n<form action='/login' method='POST'>\r\n";
content += "User: <input type='text' name='USERNAME' placeholder='user name'>\r\n";
content += "Password: <input type='password' name='PASSWORD' placeholder='password'>\r\n";
content += "<input type='submit' name='SUBMIT' value='Submit'>\r\n</form>\r\n" + msg + "<br>";
content += "You also can go <a href='/inline'>here</a></div></body></html>";
server.send(200, "text/html", content);
}
//root page can be accessed only if authentication is ok
void handleRoot() {
Serial.println("Enter handleRoot");
String header;
if (!is_authenticated()) {
server.sendHeader("Location", "/login");
server.sendHeader("Cache-Control", "no-cache");
server.send(301);
return;
}
if (server.hasArg("WPUMPBTN")) {
if (server.arg("WPUMPBTN") == "OFF"){
digitalWrite(wpump, LOW);
TIMER = millis();
server.sendHeader("Location", "/");
}
else{
digitalWrite(wpump, HIGH);
TIMER = 0;
server.sendHeader("Location", "/");
}
}
if (server.hasArg("YARDBTN")) {
if (server.arg("YARDBTN") == "OFF"){
digitalWrite(yard, LOW);
server.sendHeader("Location", "/");
}
else{
digitalWrite(yard, HIGH);
server.sendHeader("Location", "/");
}
}
if (server.hasArg("HKITCHENBTN")) {
if (server.arg("HKITCHENBTN") == "OFF"){
digitalWrite(hkitchen, LOW);
server.sendHeader("Location", "/");
}
else{
digitalWrite(hkitchen, HIGH);
server.sendHeader("Location", "/");
}
}
if (server.hasArg("ROOMBTN")) {
if (server.arg("ROOMBTN") == "OFF"){
digitalWrite(room, LOW);
server.sendHeader("Location", "/");
}
else{
digitalWrite(room, HIGH);
server.sendHeader("Location", "/");
}
}
String content = "<!DOCTYPE html>\r\n<html lang='pt-br'>\r\n<head>\r\n<meta charset='utf-8'/>\r\n";
content += "<title>SMART HOUSE</title>\r\n";
content += "<link href='https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css' rel='stylesheet' integrity='sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO' crossorigin='anonymous'>\r\n";
content += "</head>\r\n<body>\r\n<div class='jumbotron' align='center'>\r\n<H2>Smart House</H2>\r\n<br><ul class='list-group'>\r\n";
if (server.hasHeader("User-Agent")) {
content += "Client user agent : " + server.header("User-Agent") + "<br><br>";
}
//Water Pump Button
content += "<form action='/' method='POST'>\r\n<li class='list-group-item'>\r\n<label for='wpumpbtn'>Water Pump: </label>\r\n<input type='submit' id='wpumpbtn' name='WPUMPBTN' value='";
content += (digitalRead(wpump) == LOW) ? "ON" : "OFF"; content += "'></li>\r\n<br><br>";
//Yard Button
content += "<li class='list-group-item'>\r\n<label for='yardbtn'>Yard: </label>\r\n<input type='submit' id='yardbtn' name='YARDBTN' value='";
content += (digitalRead(yard) == LOW) ? "ON" : "OFF"; content += "'></li>\r\n<br><br>";
//Hkitchen Button
content += "<li class='list-group-item'>\r\n<label for='yardbtn'>Hall kitchen: </label>\r\n<input type='submit' id='hkitchenbtn' name='HKITCHENBTN' value='";
content += (digitalRead(hkitchen) == LOW) ? "ON" : "OFF"; content += "'></li>\r\n<br><br>";
//Room Button
content += "<li class='list-group-item'>\r\n<label for='roombtn'>Room: </label>\r\n<input type='submit' id='roombtn' name='ROOMBTN' value='";
content += (digitalRead(room) == LOW) ? "ON" : "OFF"; content += "'></li>\r\n<br><br>";
content += "</form>\r\n";
content += "Welcome Admin. <a href=\"/login?DISCONNECT=YES\">Logout</a>\r\n</div>\r\n</ul></body>\r\n</html>";
server.send(200, "text/html", content);
}
//no need authentication
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 checkTimer() {
if (digitalRead(wpump) == LOW){
if ((millis() - TIMER) >= 300000){
digitalWrite(wpump, HIGH);
TIMER = 0;
Serial.println("Desligando bomba");
server.sendHeader("Location", "/");
}
}
}
void setup() {
pinMode(wpump, OUTPUT);
digitalWrite(wpump, HIGH);
pinMode(yard, OUTPUT);
digitalWrite(yard, HIGH);
pinMode(hkitchen, OUTPUT);
digitalWrite(hkitchen, HIGH);
pinMode(room, OUTPUT);
digitalWrite(room, HIGH);
Serial.begin(115200);
WiFiManager wifiManager;
wifiManager.autoConnect("ESP-03");
Serial.println("Conectado!");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(WiFi.SSID());
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
// Set up mDNS responder:
// - first argument is the domain name, in this example
// the fully-qualified domain name is "esp8266.local"
// - second argument is the IP address to advertise
// we send our IP address on the WiFi network
if (!MDNS.begin("smarthouse")) {
Serial.println("Error setting up MDNS responder!");
while (1) {
delay(1000);
}
}
Serial.println("mDNS responder started");
// Start TCP (HTTP) server
server.begin();
Serial.println("TCP server started");
// Add service to MDNS-SD
MDNS.addService("http", "tcp", 80);
server.on("/", handleRoot);
server.on("/login", handleLogin);
server.on("/inline", []() {
server.send(200, "text/plain", "this works without need of authentication");
});
server.onNotFound(handleNotFound);
//here the list of headers to be recorded
const char * headerkeys[] = {"User-Agent", "Cookie"} ;
size_t headerkeyssize = sizeof(headerkeys) / sizeof(char*);
//ask server to track these headers
server.collectHeaders(headerkeys, headerkeyssize);
server.begin();
Serial.println("HTTP server started");
}
void loop() {
// put your main code here, to run repeatedly:
MDNS.update();
server.handleClient();
checkTimer();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment