// Importando as bibliotecas
#include <WiFi.h>
#include <WebServer.h>
#include "DHT.h"
#include <Adafruit_Sensor.h>
String nome_wifi = "Wokwi-GUEST";
String senha = "";
int pinLED = 23;
int pinDHT = 4;
DHT sensorDHT(pinDHT, DHT11);
WebServer servidor(80);
void setup() {
Serial.begin(9600);
pinMode(pinLED, OUTPUT);
// Chamando a função para conectar ao WiFi
conectarWifi();
servidor.begin();
}
void loop() {
// Verificando as requisições no servidor
servidor.handleClient();
delay(1000);
}
void pagPrincipal() {
float umid = sensorDHT.readHumidity();
float temp = sensorDHT.readTemperature();
String html = "<html>";
html += "<body>";
html += "<h1> Temperatura e Umidade </h1>";
html += "<p>Umidade: " + String(umid) + "%</p>";
html += "<p>Temperatura: " + String(temp) + "°C</p>";
html += "</body>";
html += "</html>";
// Enviando a resposta ao cliente
servidor.send(200, "text/html", html);
}
void conectarWifi() {
WiFi.begin(nome_wifi, senha);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.print("WiFi conectado no IP: ");
Serial.println(WiFi.localIP());
// Definição das rotas do servidor web
servidor.on("/", pagPrincipal);
servidor.on("/led", pgLed);
servidor.on("/on", pgon);
servidor.on("/off", pgoff);
}
void pgLed() {
String html = "<html>";
html += "<body>";
html += "<h1> Controle de LED </h1>";
html += "<p><a href=\"/on\"> <button>Ligar LED</button> </a></p>";
html += "<p><a href=\"/off\"> <button>Desligar LED</button> </a></p>";
html += "</body>";
html += "</html>";
servidor.send(200, "text/html", html);
}
void pgon() {
digitalWrite(pinLED, HIGH);
servidor.sendHeader("Location", "/led");
servidor.send(303); // Redireciona para a página de controle de LED
}
void pgoff() {
digitalWrite(pinLED, LOW);
servidor.sendHeader("Location", "/led");
servidor.send(303); // Redireciona para a página de controle de LED
}