/* --------------------------------------------------------------------------
PROJETO: Smart Office Monitor - Global Solutions 2025
TEMA: O Futuro do Trabalho (Saúde e Bem-Estar)
INTEGRANTES:
- Kawan Oliveira Amorim 562197
- Alana Vieira Batista RM563796
DESCRIÇÃO TÉCNICA:
Sistema IoT baseado em ESP32 para monitoramento em tempo real de fatores
ambientais, biométricos e de ergonomia, com processamento local (EDGE)
e envio de telemetria para o ThingSpeak via HTTP.
1. COLETA: DHT22 (Temp/Umid), Ultrassom (proximidade) e Potenciômetro (simulando BPM)
2. PROCESSAMENTO: Lógica de alerta local com LED/Buzzer
3. TELEMETRIA: Registro online no ThingSpeak
-------------------------------------------------------------------------- */
#include <WiFi.h>
#include <HTTPClient.h>
#include <DHT.h>
// ------------------ HARDWARE ------------------
#define DHTPIN 15
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
#define POT_PIN 34
#define TRIGGER_PIN 5
#define ECHO_PIN 18
#define ALERT_LED_PIN 2
#define BUZZER_PIN 4
// ------------------ REDE ------------------
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* apiKey = "UN12UJN0ULVIQ5FC";
const char* server = "http://api.thingspeak.com";
// ------------------ CONTROLE DE ENVIO ------------------
unsigned long lastTime = 0;
unsigned long timerDelay = 15000; // 15s
void setup() {
Serial.begin(115200);
dht.begin();
pinMode(POT_PIN, INPUT);
pinMode(TRIGGER_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(ALERT_LED_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
digitalWrite(ALERT_LED_PIN, LOW);
digitalWrite(BUZZER_PIN, LOW);
Serial.print("Conectando ao WiFi");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println(" conectado!");
}
void loop() {
// ==========================
// LEITURA DE SENSORES
// ==========================
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
h = 0;
t = 0;
}
int potValue = analogRead(POT_PIN);
int heartRate = map(potValue, 0, 4095, 60, 150);
// Ultrassom
digitalWrite(TRIGGER_PIN, LOW);
delayMicroseconds(3);
digitalWrite(TRIGGER_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGGER_PIN, LOW);
long duracao = pulseIn(ECHO_PIN, HIGH, 25000);
long distancia = (duracao * 0.0343) / 2;
// ==========================
// EDGE: ALERTAS LOCAIS
// ==========================
bool alerta = false;
if (heartRate > 110) {
alerta = true;
tone(BUZZER_PIN, 1000, 120);
}
else if (distancia > 0 && distancia < 30) {
alerta = true;
tone(BUZZER_PIN, 500, 120);
}
else if (t > 35 || h < 30) {
alerta = true;
tone(BUZZER_PIN, 1500, 120);
}
else {
alerta = false;
noTone(BUZZER_PIN);
}
digitalWrite(ALERT_LED_PIN, alerta ? HIGH : LOW);
// ==========================
// TELEMETRIA
// ==========================
if ((millis() - lastTime) > timerDelay) {
Serial.println("\n--- ENVIANDO PARA THINGSPEAK ---");
Serial.printf("Temp: %.1f°C | Umid: %.1f%% | BPM: %d | Dist: %ldcm\n",
t, h, heartRate, distancia);
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
String url = String(server) + "/update?api_key=" + apiKey +
"&field1=" + String(t) +
"&field2=" + String(h) +
"&field3=" + String(heartRate) +
"&field4=" + String(distancia);
http.begin(url);
int httpCode = http.GET();
if (httpCode > 0) {
Serial.printf("Enviado! Código HTTP: %d\n", httpCode);
} else {
Serial.printf("Erro no envio. Código: %d\n", httpCode);
}
http.end();
}
else {
Serial.println("Sem WiFi.");
}
lastTime = millis();
}
delay(50);
}