//bibliotecas para utilizar wifi
#include <WiFi.h>
#include <WiFiClient.h>
//biblioteca para criar servidor http
#include<WebServer.h>
#include<string.h>
//Criar instancia da biblioteca WebServer passando
//a porta de funcionamento nesse caso a padrão 80
WebServer server(80);
//definindo nome e senha da rede wifi
const char *ssid = "wifi-professor";
const char *password = "12345678";
//definindo a configuração de rede
IPAddress local_IP(192,168,0,100);
IPAddress gateway(192,168,0,1);
IPAddress subnet(255,255,255,0);
//definindo a porta dos led do circuito no Esp
const int pinLed1 = 4;
const int pinLed2 = 22;
//varivareis para controlar acendimento por tempo
// e intencidade irão receber os valores do html
int duty = 0;
int tempo = 0;
//Configuração do PWM
const int freq= 1000;
const int resolucao = 8;
const int channel=1;
void setup(){
Serial.begin(115200);
//Configurando e iniciando o Acess Point com o ESP
Serial.print("Configurando Wifi....");
Serial.println(WiFi.softAPConfig(local_IP, gateway, subnet)? "OK" : "Erro na conf");
Serial.print("Iniciando a Wifi......");
Serial.println(WiFi.softAP(ssid, password)? "OK": "Erro na inicialização");
Serial.print("IP do AP:");
Serial.println(WiFi.softAPIP());
pinMode(pinLed1, OUTPUT);
//Configurar o PWM e vincular o canal a porta do Esp
ledcSetup(channel, freq, resolucao);
ledcAttachPin(pinLed2, channel);
//Configurando o WebServer HTTP, definindo as rotas e iniciando o serviço
server.on("/", handleIndex ); //caminho default padrão de chamada do navegador.
server.on("/acendePwm", acendePwm);
server.on("/acendeTempo", acendeTempo);
server.begin();
}
void loop(){
//função WebServer para ficar escutando se tem algum navegador chamdo alguma rota
server.handleClient();
acendeLuz();
}
//função responsavel por receber o valor de duty vindo do html atraves da função arg da
//biblioteca WebServer, usando a função nativa atoi para converter o valor de string
//para inteiro.
void acendePwm(){
duty = atoi(server.arg("rngDuty").c_str());
server.sendHeader("Location", "/");
server.send(302, "text/html", "");
}
//função responsavel por receber o valor de tempo vindo do html atraves da função arg da
//biblioteca WebServer, usando a função nativa atoi para converter o valor de string
//para inteiro.
void acendeTempo(){
tempo = atoi(server.arg("txtTempo").c_str())*1000;
server.sendHeader("Location", "/");
server.send(302, "text/html", "");
}
void acendeLuz(){
//acende o canal pwm conforme o duty recebido do html
ledcWrite(channel, duty);
//acende o led 1 pelo tempo informado no html.
if(tempo > 0)
{
digitalWrite(pinLed1, HIGH);
delay(tempo);
digitalWrite(pinLed1, LOW);
tempo = 0;
}
else{
digitalWrite(pinLed1, LOW);
}
}
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>";
server.send(302, "text/html", html);
}