/*
------------------ FIAP --------------------
CP4 - EDGE COMPUTING & COMPUTER SYSTEMS
Participantes:
Prof. Paulo Marcotti PF2150
Arthur Berlofa Bosi RM564438
Danilo Fernandes dos Santos RM561657
Davi Melo Muniz Falcão RM561818
Mateus Saavedra de Mendonça RM563266
--------------------------------------------
Canal ThingSpeak para Processamento dos Dados
https://thingspeak.mathworks.com/channels/3056939
*/
#include <WiFi.h>
#include <HTTPClient.h>
#include <DHT.h>
#define LDR 34 // Pino LDR Sensor
#define DHTPIN 15 // Pino GPIO15 do ESP32 para o DHT22
#define DHTTYPE DHT22 // Tipo de sensor DHT (DHT22)
DHT dht(DHTPIN, DHTTYPE); // Declara o objeto DHT
// Credenciais ThingSpeak
const char* ssid = "Wokwi-GUEST"; // Rede Wi-Fi
const char* password = ""; // Senha da rede Wi-Fi
const char* apiKey = "VY00NOCNWZDZZTK0"; // Write API Key
const char* server = "http://api.thingspeak.com"; // Servidor ThingSpeak
void setup() {
Serial.begin(115200); // Inicia a comunicação Serial
dht.begin(); // Inicia o DHT
pinMode(LDR, INPUT); // Declara a porta do LDR como entrada
// Inicialização e loop de verificação da rede Wi-Fi
WiFi.begin(ssid, password);
Serial.print("Conectando ao WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println(" conectado!");
}
void loop() {
if (WiFi.status() == WL_CONNECTED) {
// Leitura dos sensores
float h = dht.readHumidity();
float t = dht.readTemperature();
int lum = map(analogRead(LDR), 0, 1023, 0, 100);
if (isnan(h) || isnan(t)) {
Serial.println("Falha ao ler o sensor DHT22!");
return;
}
// Envio de dados para o ThingSpeak
HTTPClient http;
String url = String(server) + "/update?api_key=" + apiKey + "&field1=" + String(t) + "&field2=" + String(h) + "&field3=" + String(lum);
http.begin(url);
int httpCode = http.GET();
if (httpCode > 0) {
String payload = http.getString(); // Resposta da requisição HTTP
Serial.println("Dados enviados ao ThingSpeak.");
Serial.print("Código HTTP: ");
Serial.println(httpCode);
Serial.println("Resposta: ");
Serial.println(payload);
} else {
Serial.print("Erro ao enviar dados. Código HTTP: ");
Serial.println(httpCode);
}
http.end();
} else {
Serial.println("WiFi não conectado. Tentando reconectar...");
}
// Espera 15 segundos para enviar a requisição novamente
delay(2500);
}