#include <WiFi.h>
#include <HTTPClient.h>
// Configurações da rede Wi-Fi
const char* ssid = "ESP32_Server";
const char* password = "12345678";
// IP do servidor ESP32 (modifique com o IP real do servidor)
const char* serverIP = "192.168.4.1"; // Endereço IP do servidor ESP32
// Configuração do sensor PIR
const int pirPin = 4; // Pino ao qual o sensor PIR está conectado
void setup() {
// Inicia o Serial Monitor
Serial.begin(115200);
// Conecta à rede Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Conectado ao Wi-Fi");
// Configura o pino do sensor PIR como entrada
pinMode(pirPin, INPUT);
}
void loop() {
// Lê o estado do sensor PIR
int pirState = digitalRead(pirPin);
// Cria a URL para a requisição HTTP
String ledState;
if (pirState == HIGH) {
ledState = "on";
Serial.println("Movimento detectado! Acendendo o LED.");
} else {
ledState = "off";
Serial.println("Nenhum movimento detectado. Desligando o LED.");
}
// Cria a URL para a requisição HTTP
String url = "http://" + String(serverIP) + "/led?state=" + ledState;
// Envia a requisição HTTP
HTTPClient http;
http.begin(url);
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
String response = http.getString();
Serial.println("Resposta do servidor: " + response);
} else {
Serial.println("Erro ao fazer a requisição: " + String(httpResponseCode));
}
http.end();
// Aguarda um segundo antes de ler novamente
delay(1000);
}