//Importanto a biblioteca
#include <WiFi.h>
#include <WebServer.h>
#include "DHT.h"
#include <Adafruit_Sensor.h>
String nome_wifi = "Wokwi-GUEST";
String senha = "";
int pinDHT = 4;
DHT sensorDHT(pinDHT, DHT11);
WebServer servidor(80);
int pinLED = 23;
void setup() {
pinMode(pinLED, OUTPUT);
Serial.begin(9600);
//Chamando finção para conectar no wifi
conectarWifi();
sensorDHT.begin();
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>";
servidor.send(200, "text/html", html);
}
//Função criada para configurar e conectar à WiFi
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>";
servidor.send(200, "text/html", html);
}
void pgon() {
digitalWrite(pinLED, HIGH);
servidor.sendHeader("Location", "/led");
}
void pgoff() {
digitalWrite(pinLED, LOW);
servidor.sendHeader("Location", "/led");
}