#include <WiFi.h>
#include <WebServer.h>
#include <time.h>
#define PIR 13
#define LED 2
#define LDR 34
const char* ssid = "SEU_WIFI";
const char* password = "SUA_SENHA";
WebServer server(80);
int limite = 1000;
// status sistema
String estadoLuz = "DESLIGADA";
String presenca = "NAO";
String ambiente = "CLARO";
String economia = "ATIVA";
// monitoramento
int acionamentos = 0;
unsigned long tempoLigado = 0;
unsigned long inicioLigado = 0;
bool ledLigado = false;
// data e hora
const char* ntpServer = "pool.ntp.org";
const long gmtOffset_sec = -3 * 3600;
const int daylightOffset_sec = 0;
String dataHoraAtual() {
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
return "Sem horario";
}
char buffer[30];
strftime(buffer, sizeof(buffer), "%d/%m/%Y %H:%M:%S", &timeinfo);
return String(buffer);
}
void pagina() {
String html = R"rawliteral(
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="refresh" content="2">
<title>IoT Industrial</title>
<style>
body{
font-family:Arial;
background:#f0f0f0;
text-align:center;
padding:30px;
}
.card{
background:white;
padding:20px;
margin:auto;
width:350px;
border-radius:15px;
box-shadow:0 0 10px rgba(0,0,0,0.2);
}
h1{
color:#333;
}
p{
font-size:20px;
}
</style>
</head>
<body>
<div class="card">
<h1>IoT Industrial</h1>
<p><b>Data/Hora:</b> )rawliteral";
html += dataHoraAtual();
html += R"rawliteral(</p>
<p><b>Ambiente:</b> )rawliteral";
html += ambiente;
html += R"rawliteral(</p>
<p><b>Presença:</b> )rawliteral";
html += presenca;
html += R"rawliteral(</p>
<p><b>Iluminação:</b> )rawliteral";
html += estadoLuz;
html += R"rawliteral(</p>
<p><b>Eficiência Energética:</b> )rawliteral";
html += economia;
html += R"rawliteral(</p>
<p><b>Acionamentos:</b> )rawliteral";
html += acionamentos;
html += R"rawliteral(</p>
<p><b>Tempo Ligada:</b> )rawliteral";
html += tempoLigado / 1000;
html += R"rawliteral( segundos</p>
</div>
</body>
</html>
)rawliteral";
server.send(200, "text/html", html);
}
void setup() {
pinMode(PIR, INPUT);
pinMode(LED, OUTPUT);
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.print("Conectando WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi conectado!");
Serial.print("IP: ");
Serial.println(WiFi.localIP());
// horário internet
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
server.on("/", pagina);
server.begin();
}
void loop() {
int movimento = digitalRead(PIR);
int luz = analogRead(LDR);
// ambiente
if (luz < limite) {
ambiente = "ESCURO";
} else {
ambiente = "CLARO";
}
// presença
if (movimento == HIGH) {
presenca = "DETECTADA";
} else {
presenca = "NAO";
}
// controle iluminação
if (luz < limite && movimento == HIGH) {
digitalWrite(LED, HIGH);
estadoLuz = "LIGADA";
economia = "ATIVA";
// conta acionamento
if (!ledLigado) {
acionamentos++;
inicioLigado = millis();
ledLigado = true;
}
} else {
digitalWrite(LED, LOW);
estadoLuz = "DESLIGADA";
economia = "ECONOMIZANDO ENERGIA";
// soma tempo ligado
if (ledLigado) {
tempoLigado += millis() - inicioLigado;
ledLigado = false;
}
}
server.handleClient();
delay(100);
}