#include <WiFi.h>
#include <WebServer.h>
#define LED1pin 26
#define LED2pin 27
//const char* ssid = "Smartenergy"; //<----------Mude para o seu
//const char* password = "SmartEnergy"; //<----------Mude para o seu
//const char* ssid = "sala203"; //<----------Mude para o seu
//const char* password = "s@l@203#"; //<----------Mude para o seu
//const char* ssid = "NPITI-IoT"; //<----------Mude para o seu
//const char* password = "NPITI-IoT"; //<----------Mude para o seu
const char* ssid = "Wokwi-GUEST";
const char* password ="";
WebServer server(80);
bool LED1status = LOW;
bool LED2status = LOW;
void setup() {
Serial.begin(115200);
delay(100);
pinMode(LED1pin, OUTPUT);// Configuração dos Pinos
pinMode(LED2pin, OUTPUT);
Serial.println("Conectando em: ");
Serial.println(ssid);
//Função de conexão na Wifi
WiFi.begin(ssid, password);
//Verificação do status da conexão
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi conectada..!");
Serial.print("IP: ");
Serial.println(WiFi.localIP()); // função que mostra o IP
//Configuração das URL
server.on("/", handle_OnConnect);
server.on("/led1on", handle_led1on);
server.on("/led1off", handle_led1off);
server.on("/led2on", handle_led2on);
server.on("/led2off", handle_led2off);
server.onNotFound(handle_NotFound);// para uma url não existente
server.begin();
Serial.println("Servidor HTTP iniciado");
}
void loop() {
server.handleClient(); // Tratando os objetos do servidor
if(LED1status)// analisa o status da pagina e liga ou deliga o led 1
{digitalWrite(LED1pin, HIGH);}
else
{digitalWrite(LED1pin, LOW);}
if(LED2status)// analisa o status da pagina e liga ou deliga o led 1
{digitalWrite(LED2pin, HIGH);}
else
{digitalWrite(LED2pin, LOW);}
}
// Função inicial
void handle_OnConnect() {
LED1status = LOW;
LED2status = LOW;
Serial.println("GPIO4 Status: OFF | GPIO5 Status: OFF");
server.send(200, "text/html", SendHTML(LED1status,LED2status));
}
// Função liga o led 1
void handle_led1on() {
LED1status = HIGH;
Serial.println("GPIO4 Status: ON");
server.send(200, "text/html", SendHTML(true,LED2status));
}
// Função desliga o led 1
void handle_led1off() {
LED1status = LOW;
Serial.println("GPIO4 Status: OFF");
server.send(200, "text/html", SendHTML(false,LED2status));
}
// Função liga o led 2
void handle_led2on() {
LED2status = HIGH;
Serial.println("GPIO5 Status: ON");
server.send(200, "text/html", SendHTML(LED1status,true));
}
// Função desliga o led 2
void handle_led2off() {
LED2status = LOW;
Serial.println("GPIO5 Status: OFF");
server.send(200, "text/html", SendHTML(LED1status,false));
}
// Função para uma pagina não existente
void handle_NotFound(){
server.send(404, "text/plain", "Not found");
}
//pagina HTML
String SendHTML(uint8_t led1stat,uint8_t led2stat){
String ptr = "<!DOCTYPE html> <html>\n";
ptr +="<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n";
ptr +="<title>LED</title>\n";
ptr +="<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}\n";
ptr +="body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;} h3 {color: #444444;margin-bottom: 50px;}\n";
ptr +=".button {display: block;width: 80px;background-color: #3498db;border: none;color: white;padding: 13px 30px;text-decoration: none;font-size: 25px;margin: 0px auto 35px;cursor: pointer;border-radius: 4px;}\n";
ptr +=".button-on {background-color: #4CAF50;}\n";
ptr +=".button-on:active {background-color: #008CBA;}\n";
ptr +=".button-off {background-color: #DC143C;}\n";
ptr +=".button-off:active {background-color: #008CBA;}\n";
ptr +="p {font-size: 14px;color: #888;margin-bottom: 10px;}\n";
ptr +="</style>\n";
ptr +="</head>\n";
ptr +="<body>\n";
ptr +="<h1>Acionando LED's WIFI</h1>\n";
ptr +="<h3>Programando em HTML</h3>\n";
//ptr +="<a href="https://www.w3schools.com">Visit W3Schools.com!</a><\n";
//ptr +="<img src="https://portal.imd.ufrn.br/portal/assets/images/IMD_logo_01-01.svg" alt="Logo IMD" width="500" height="100">\n"
if(led1stat)
{ptr +="<p>LED1 Status: Ligado</p><a class=\"button button-off\" href=\"/led1off\">Desligar</a>";}
else
{ptr +="<p>LED1 Status: Desligado</p><a class=\"button button-on\" href=\"/led1on\">Ligar</a>";}
if(led2stat)
{ptr +="<p>LED2 Status: Ligado</p><a class=\"button button-off\" href=\"/led2off\">Desligar</a>";}
else
{ptr +="<p>LED2 Status: Desligado</p><a class=\"button button-on\" href=\"/led2on\">Ligar</a>";}
ptr +="</body>\n";
ptr +="</html>\n";
return ptr;
}