//bibliotecas para utilização do Wifi no Esp32, WiFi vai controlar o Acess Point,
//e WiFicliente permite conectar a um roteador.
#include <WiFi.h>
#include <WiFiClient.h>
//biblioteca para executar um serviço de HTTP, para publicar os arquivo html
#include <WebServer.h>
#include <string.h>
//Instancia a biblioteca WebServer, indicando a porta 80 que é a padrão do HTTP
WebServer server(80);
//definido nome e senha do AP que será criado com o Esp32
const char *ssid = "Wifi-Heitor";
const char *password= "12345678";
//definição das configurações de rede
IPAddress local_IP(192,168,0,80);
IPAddress gateway(192,168,0,1);
IPAddress subnet(255,255,255,0);
//definindo as portas dos leds.
const int pinLed1 = 4;
const int pinLed2 = 22;
//variaveis global para receber os valores de intensidade e o tempo que serão
//enviados do HTML
int duty =0;
int tempo =0;
//configuração PWM
const int freq= 1000; //frequencia
const int res=8; //resolução
const int canal=1; //canal
int logado = 0;
void setup(){
Serial.begin(115200);
//Configuração do Acess Point que ira ser fornecido pelo ESP32
Serial.print("Configurando AP.....");
Serial.println(WiFi.softAPConfig(local_IP, gateway, subnet)? "OK" : "Erro");
Serial.print("Iniciando o Wifi.....");
Serial.println(WiFi.softAP(ssid, password));
Serial.print("Endereço IP");
Serial.println(WiFi.softAPIP());
pinMode(pinLed1, OUTPUT);
//Configurando o acendimento do led por PWM
ledcSetup(canal, freq, res);
ledcAttachPin(pinLed2, canal);
//Configurando o serviço de HTTP e definindo as rotas.
server.on("/", handleLogin);
server.on("/validaLogin", validaLogin); // rota default
server.on("/pagPrincipal", handleIndex);
server.on("/acendePwm", acendePwm);
server.on("/acendeTempo", acendeTempo);
server.begin();
}
void loop(){
//escutando para ver se tem alguma requisição para o serviço HTTP
server.handleClient();
acendeLuz();
}
void acendePwm(){
//atribuindo o valor do duty recebido pela função arg do WebServer
//e convertendo o valor de string para inteiro através da
//função nativa atoi
duty = atoi(server.arg("rngDuty").c_str());
server.sendHeader("Location", "/");
server.send(302, "text/html", "");
}
void acendeTempo(){
//atribuindo o valor do tempo recebido pela função arg do WebServer
//e convertendo o valor de string para inteiro através da
//função nativa atoi
tempo = atoi(server.arg("txtTempo").c_str())*1000;
server.sendHeader("Location", "/");
server.send(302, "text/html", "");
}
void acendeLuz(){
ledcWrite(canal, duty);
if(tempo > 0)
{
digitalWrite(pinLed1, HIGH);
delay(tempo);
digitalWrite(pinLed1, LOW);
tempo =0;
}
else
{
digitalWrite(pinLed1, LOW);
}
}
void validaLogin(){
String usuario = server.arg("txtUsuario");
String senha = server.arg("txtSenha");
if(usuario == "Admin" && senha == "Senai@901")
{
logado = 1;
server.sendHeader("Location", "/pagPrincipal");
server.send(302, "text/html", "");
}
else{
logado=0;
server.sendHeader("Location", "/");
server.send(302, "text/html", "");
}
}
void handleIndex(){
String html;
html = "<!DOCTYPE html>";
html += "<html lang='pt-Br'>";
html += "<head>";
html += "<meta charset='UTF-8'>";
html += "<meta name='viewport' content='width=device-width, initial-scale=1.0'>";
html += "<title>Acendendo Luz</title>";
html += "</head>";
html += "<body>";
html += "<form action='/acendePwm'>";
html += "<fieldset>";
html += "<label for='idPwm'>Intencidade da luz(0-255):</label>";
html += "<input type='range' id='idPwm' name='rngDuty' min='0' max='255'>";
html += "<br>";
html += "<input type='submit' value='Acender'>";
html += "</fieldset>";
html += "</form>";
html += "<br>";
html += "<form action='/acendeTempo'>";
html += "<fieldset>";
html += "<label for='points'>Tempo em segundos:</label>";
html += "<br>";
html += "<input type='number' id='idTempo' name='txtTempo'>";
html += "<br>";
html += "<input type='submit' value='Acender tempo'>";
html += "</fieldset>";
html += "</form>";
html += "</body>";
html += "</html>";
if(logado = 1){
server.send(302, "text/html", html);
}
else{
server.sendHeader("Location", "/");
server.send(302, "text/html", "");
}
}
void handleLogin(){
logado = 0;
String html;
html ="<!DOCTYPE html>";
html +="<html lang='pt-BR'>";
html +="<head>";
html +="<meta charset='UTF-8'>";
html +="<meta name='viewport' content='width=device-width, initial-scale=1.0'>";
html +="<title>Semáforo</title>";
html +="<style>";
html +="body{";
html +="background-color: rgb(144, 145, 145);}";
html +="fieldset{";
html +="width: 50%;";
html +="height: 25vh;";
html +="display: flex;";
html +="flex-direction: column;";
html +="justify-content: space-between;}";
html +=".conteiner{";
html +="width: 100%;";
html +="display: flex;";
html +="flex-direction: column;";
html +="align-items: center;}";
html +="</style>";
html +="</head>";
html +="<body>";
html +="<div class='conteiner'>";
html +="<h1>Console de controle de semáforo</h1>";
html +="<form action='/validaLogin' method='post'>";
html +="<fieldset>";
html +="<legend>Login:</legend>";
html +="<label for='usuarioId'>Usuário:</label>";
html +="<input type='text' name='txtUsuario' id='usuarioId' >";
html +="<label for='senhaId'>Senha:</label>";
html +="<input type='password' name='txtSenha' id='senhaId' >";
html +="<input type='submit' value='Enviar'>";
html +="</fieldset>";
html +="</form>";
html +="</div>";
html +="</body>";
html +="</html>";
server.send(302, "text/html", html);
}