//gerencia o sinal de Wifi
#include <WiFi.h>
#include <WiFiClient.h>
//gera servico http no esp32
#include <WebServer.h>
#include <string.h>
WebServer server(80);
const char *ssid = "seu-nomettt";
const char *password = "987654321";
IPAddress local_IP(192, 168, 4, 22);
IPAddress gateway(192, 168, 4, 9);
IPAddress subnet(255, 255, 255, 0);
const int pinLed1 =4;
const int pinLed2= 22;
int duty =0;
int tempo=0;
int freq = 1000;
int resolution = 8;
int ledChannel = 1;
void setup(){
Serial.begin(115200);
Serial.print("Setting soft-AP configuration ... ");
Serial.println(WiFi.softAPConfig(local_IP, gateway, subnet) ? "Ready" : "Failed!");
Serial.print("Setting soft-AP ... ");
Serial.println(WiFi.softAP(ssid, password) ? "Ready" : "Failed!");
Serial.print("Soft-AP IP address = ");
Serial.println(WiFi.softAPIP());
pinMode(pinLed1, OUTPUT);
ledcSetup(ledChannel,freq, resolution);
ledcAttachPin(pinLed2, ledChannel);
server.on("/", handleIndex);
server.on("/acendePWM", acendePwm);
server.on("/acendeTempo", acendeTempo);
server.begin();
}
void loop() {
// put your main code here, to run repeatedly:
server.handleClient();
acendeLuz();
}
void acendeTempo(){
tempo = atoi(server.arg("txtTempo").c_str()) * 1000;
server.sendHeader("Location", "/");
server.send(302, "text/plain", "");
}
void acendePwm(){
duty = atoi(server.arg("rngDuty").c_str());
server.sendHeader("Location", "/");
server.send(302, "text/plain", "");
}
void acendeLuz(){
if(tempo >0){
digitalWrite(pinLed1, HIGH);
delay(tempo);
digitalWrite(pinLed1, LOW);
}else
{
digitalWrite(pinLed1, LOW);
}
ledcWrite(ledChannel, duty);
}
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(200, "text/html", html);
}