#include <LiquidCrystal_I2C.h>
#include <DHT.h>
#include <WiFi.h>
#include <WebServer.h>
#define LAMP1 4
#define LAMP2 5
int lampState1 = 1;
int lampState2 = 1;
#define DHTPIN 18
#define DTHTYPE DHT11
float t, h;
#define ADDRESS 0x27
#define COL 16
#define LIN 2
#define SSID "Nome do seu roteador"
#define SENHA "Senha do seu roteador"
#define WEBPORT 80
WebServer server(WEBPORT);
DHT dht(DHTPIN, DTHTYPE); // objeto
LiquidCrystal_I2C lcd(ADDRESS, COL, LIN);
void setup()
{
Serial.begin(115200);
lcd.init();
lcd.backlight();
lcd.clear();
dht.begin();
pinMode(LAMP1, lampState1);
pinMode(LAMP2, lampState2);
}
void loop()
{
while(WiFi.status() != WL_CONNECTED) conectaWifi();
server.handleClient();
}
// *******************************************
// DESENVOLVIMENTO DAS FUNÇÕES
// conectaWifi
bool conectaWifi()
{
WiFi.begin(SSID, SENHA);
Serial.print("Conectando a rede: ");
Serial.println(SSID);
lcd.print("Conectando red:");
lcd.setCursor(0,1);
lcd.print(SSID);
delay(1000);
while(WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
lcd.write(255);
delay(500);
}
Serial.print("\nWifi conetado!");
Serial.println(WiFi.localIP());
lcd.clear();
lcd.print(" Wifi conectado");
lcd.setCursor(0,1);
lcd.print(WiFi.localIP());
delay(2000);
serverInit();
return true;
}
// serverInit
void serverInit()
{
server.on("/", handleRoot);
server.on("/toggle1", handleLamp1);
server.on("/toggle2", handleLamp2);
server.on("/dht", handleDHT);
server.begin();
Serial.println("Servidor HTTP iniciado!");
}
// handleRoot
void handleRoot()
{
if(!leituraDHT()) return;
server.send(200, "text/html", WebPage()); // código de envio
Serial.print("Página web enviada!");
}
// handleDTH
void handleDHT()
{
if(!leituraDHT()) return;
String json = "{\"temperatura\":" + String(t) + ",\"umidade\":" + String(h) + "}";
server.send(200, "application/json", json);
Serial.println("Dados do sensor foram enviados!");
}
// handleLamp1
void handleLamp1()
{
lampState1 = !lampState1;
digitalWrite(LAMP1, lampState1);
server.sendHeader("Location", "/");
server.send(303);
}
// handleLamp2
void handleLamp2()
{
lampState2 = !lampState2;
digitalWrite(LAMP2, lampState2);
server.sendHeader("Location", "/");
server.send(303);
}
// leituraDHT
bool leituraDHT()
{
t = dht.readTemperature();
h = dht.readHumidity();
if(isnan(h) || isnan(t))
{
Serial.print("Falha no sensor");
return false;
}
return true;
}
// criação da página html
String WebPage() {
String webPage = "<!DOCTYPE html><html>"; // Inicia o HTML da página web
webPage += "<head><title>Leitura do Sensor DHT11</title>";
webPage += "<meta charset='UTF-8' http-equiv='refresh' content='3'>"; // Atualiza a página a cada 3 segundos
webPage += "<meta name='viewport' content='width=device-width, initial-scale=1.0'>"; // Meta tag para responsividade
webPage += "<link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css'>"; // Inclui a biblioteca de ícones Font Awesome
webPage += "<style>";
webPage += "body {font-family: Arial; margin: 0; padding: 0;}";
webPage += ".container {max-width: 600px; margin: 20px auto; padding: 20px; border: 2px solid #ccc; border-radius: 10px; box-shadow: 0 0 10px rgba(0,0,0,0.1); background-color: #fff;}";
webPage += "h1 {color: #333;}";
webPage += "p {font-size: 1.2em;}";
webPage += "button {padding: 10px 20px; margin: 5px; font-size: 1em; background-color: #007bff; color: #fff; border: none; border-radius: 5px; cursor: pointer;}";
webPage += "button:hover {background-color: #0056b3;}";
webPage += ".icon {margin-right: 5px;}";
webPage += "@media screen and (max-width: 600px) {"; // Media query para ajustes em dispositivos menores
webPage += ".container {margin: 10px;}";
webPage += "}";
webPage += "</style>";
webPage += "</head><body>";
webPage += "<div class='container'>";
webPage += "<h1>Leitura do Sensor DHT11</h1>";
webPage += "<p><i class='fas fa-thermometer-half icon'></i>Temperatura: " + String(t) + " °C</p>"; // Mostra a temperatura com ícone de termômetro
webPage += "<p><i class='fas fa-tint icon'></i>Umidade: " + String(h) + " %</p>"; // Mostra a umidade com ícone de gota
webPage += "<button onclick=\"location.href='/toggle1'\">"; // Botão para alternar a lâmpada 1
webPage += lampState1 ? "<i class='fas fa-toggle-on icon'></i>Ligar Lâmpada" : "<i class='fas fa-toggle-off icon'></i>Desligar Lâmpada"; // Texto do botão baseado no estado da Lâmpada 1
webPage += "</button>";
webPage += "<button onclick=\"location.href='/toggle2'\">"; // Botão para alternar a lâmpada 2
webPage += lampState2 ? "<i class='fas fa-toggle-on icon'></i>Ligar Lâmpada" : "<i class='fas fa-toggle-off icon'></i>Desligar Lâmpada"; // Texto do botão baseado no estado da Lâmpada 2
webPage += "</button>";
webPage += "</div></body></html>";
return webPage;
}