#include <Arduino.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <DHT.h>
#include "env.h"
// Pin definitions (match your hardware)
#define DHTPIN 4
#define sensorPin 15
#define fan 23
#define light 22
// DHT Sensor type
#define DHTTYPE DHT22
// Global variables
bool motionDetected = false;
int pirStatus;
DHT dht(DHTPIN, DHTTYPE);
float readTemperature() {
return dht.readTemperature(); // Reads temperature in Celsius
}
bool checkMotion() {
pirStatus = digitalRead(sensorPin);
motionDetected = (pirStatus == HIGH);
return motionDetected;
}
void sendSensorReadings(float currentTemp, bool motionState) {
if (WiFi.status() != WL_CONNECTED) return;
HTTPClient client;
String jsonOut;
String sensorApiPath = String(endpoint) + "/SensorData";
Serial.println("POST to: " + sensorApiPath);
client.begin(sensorApiPath);
client.addHeader("Content-Type", "application/json");
JsonDocument payload;
payload["temperature"] = currentTemp;
payload["presence"] = motionState;
serializeJson(payload, jsonOut);
int response = client.POST(jsonOut);
Serial.print("HTTP Response Code: ");
Serial.println(response);
if (response > 0) {
Serial.print("Server Response: ");
Serial.println(client.getString());
} else {
Serial.println("Failed to send sensor data");
}
client.end();
}
void fetchFanStatus() {
if (WiFi.status() != WL_CONNECTED) return;
HTTPClient client;
String fanApiPath = String(endpoint) + "/fan";
Serial.println("GET from: " + fanApiPath);
client.begin(fanApiPath);
int response = client.GET();
if (response > 0) {
String fanJson = client.getString();
JsonDocument fanDoc;
DeserializationError error = deserializeJson(fanDoc, fanJson);
if (!error) {
bool fanState = fanDoc["fan"];
digitalWrite(fan, fanState);
Serial.println("Fan state: " + String(fanState ? "ON" : "OFF"));
} else {
Serial.print("Fan JSON Error: ");
Serial.println(error.c_str());
}
} else {
Serial.print("Fan Request Failed, Code: ");
Serial.println(response);
}
client.end();
}
void fetchLightStatus() {
if (WiFi.status() != WL_CONNECTED) return;
HTTPClient client;
String lightApiPath = String(endpoint) + "/light";
Serial.println("GET from: " + lightApiPath);
client.begin(lightApiPath);
int response = client.GET();
if (response > 0) {
String lightJson = client.getString();
JsonDocument lightDoc;
DeserializationError error = deserializeJson(lightDoc, lightJson);
if (!error) {
bool lightState = lightDoc["light"];
digitalWrite(light, lightState);
Serial.println("Light state: " + String(lightState ? "ON" : "OFF"));
} else {
Serial.print("Light JSON Error: ");
Serial.println(error.c_str());
}
} else {
Serial.print("Light Request Failed, Code: ");
Serial.println(response);
}
client.end();
}
void setup() {
Serial.begin(9600);
WiFi.disconnect(true);
// Initialize components
pinMode(light, OUTPUT);
pinMode(sensorPin, INPUT);
pinMode(fan, OUTPUT);
dht.begin();
// Connect to WiFi
WiFi.begin(ssid, password);
Serial.println("Connecting to WiFi...");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("\nWiFi Connected. IP Address: ");
Serial.println(WiFi.localIP());
}
void loop() {
bool detected = checkMotion();
float tempVal = readTemperature();
if (isnan(tempVal)) {
Serial.println("Failed to read temperature!");
} else {
Serial.println("Temperature: " + String(tempVal) + "°C, Motion: " +
String(detected ? "Detected" : "Not detected"));
}
if (WiFi.status() == WL_CONNECTED) {
sendSensorReadings(tempVal, detected);
fetchFanStatus();
fetchLightStatus();
} else {
Serial.println("WiFi Not Connected - Attempting to reconnect...");
WiFi.begin(ssid, password);
}
delay(5000); // Wait 5 seconds between updates
}
Loading
esp32-devkit-v1
esp32-devkit-v1