#include <WiFi.h>
#include <SinricPro.h>
#include <SinricProSwitch.h>
#include <DHT.h>
// WiFi Credentials
const char* ssid = "Wokwi-GUEST";
const char* password = ""; // Replace with your actual WiFi password
// Sinric Pro Credentials
const char* appKey = "daf7cd4d-56f5-4752-b3c4-6e28e29c7d0e"; // Replace with your Sinric Pro App Key
const char* secretKey = "86e539c1-52c7-4ae1-a76f-d4822c7c86dd-dff23396-2257-4165-af33-1093e762b463"; // Replace with your Sinric Pro Secret Key
const char* serverURL = "ws.sinric.pro"; // Sinric Pro WebSocket Server
const char* lightID = "67bae70977d346f8f46d609f"; // Replace with your Light Device ID
const char* fanID = "67bae814ff1f4eb6e7604ffe"; // Replace with your Fan Device ID
// GPIO Pins
#define LIGHT_PIN 2
#define FAN_PIN 4
#define PIR_PIN 13
#define DHT_PIN 15
#define DHTTYPE DHT22 // Using DHT22 sensor
DHT dht(DHT_PIN, DHTTYPE);
bool lightState = false;
bool fanState = false;
unsigned long lastMotionTime = 0;
const unsigned long lightTimeout = 5000; // 5 seconds timeout for lights
bool motionDetectedPrev = false;
// Callback function for Alexa/Google Assistant via Sinric Pro
bool onPowerState(const String &deviceId, bool &state) {
if (deviceId == lightID) {
lightState = state;
digitalWrite(LIGHT_PIN, state ? HIGH : LOW);
Serial.println(state ? "Light ON" : "Light OFF");
} else if (deviceId == fanID) {
fanState = state;
digitalWrite(FAN_PIN, state ? HIGH : LOW);
Serial.println(state ? "Fan ON" : "Fan OFF");
}
return true;
}
void setup() {
Serial.begin(115200);
pinMode(LIGHT_PIN, OUTPUT);
pinMode(FAN_PIN, OUTPUT);
pinMode(PIR_PIN, INPUT);
WiFi.begin(ssid, password);
unsigned long startAttemptTime = millis();
while (WiFi.status() != WL_CONNECTED && millis() - startAttemptTime < 20000) { // 20 sec timeout
delay(1000);
Serial.println("Connecting to WiFi...");
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println("Connected to WiFi!");
} else {
Serial.println("WiFi Connection Failed!");
}
// Initialize Sinric Pro
SinricProSwitch &myLight = SinricPro[lightID]; // Initialize Light Device
SinricProSwitch &myFan = SinricPro[fanID]; // Initialize Fan Device
myLight.onPowerState(onPowerState);
myFan.onPowerState(onPowerState);
SinricPro.begin(appKey, secretKey, serverURL);
dht.begin();
}
void loop() {
SinricPro.handle();
// Read PIR Sensor for motion detection
bool motionDetected = digitalRead(PIR_PIN) == HIGH;
if (motionDetected && !motionDetectedPrev) {
Serial.println("Motion Detected! Turning on Light.");
digitalWrite(LIGHT_PIN, HIGH);
lightState = true;
lastMotionTime = millis();
}
motionDetectedPrev = motionDetected;
// Turn off light if motion stopped after timeout
if (lightState && !motionDetected && millis() - lastMotionTime > lightTimeout) {
digitalWrite(LIGHT_PIN, LOW);
lightState = false;
Serial.println("Light turned off due to inactivity.");
}
// Read Temperature and Humidity
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
if (!isnan(temperature)) {
Serial.print("Temperature: "); Serial.print(temperature); Serial.println(" °C");
} else {
Serial.println("Failed to read temperature!");
}
if (!isnan(humidity)) {
Serial.print("Humidity: "); Serial.print(humidity); Serial.println(" %");
} else {
Serial.println("Failed to read humidity!");
}
// Automatic Fan Control with hysteresis
if (temperature > 30 && !fanState) { // Turn on at 30°C
digitalWrite(FAN_PIN, HIGH);
fanState = true;
Serial.println("High Temp! Turning Fan ON");
} else if (temperature < 29 && fanState) { // Turn off at 29°C
digitalWrite(FAN_PIN, LOW);
fanState = false;
Serial.println("Temperature Normal. Turning Fan OFF");
}
delay(2000);
}