#include "signal_Slot.h"
// Create SignalSlot instance
SignalSlot eventSystem;
// =================================
// SENSOR SIMULATION & THRESHOLDS
// =================================
const int TEMP_THRESHOLD_HIGH = 30; // °C
const int TEMP_THRESHOLD_LOW = 15; // °C
const int HUMIDITY_THRESHOLD = 80; // %
const int LIGHT_THRESHOLD = 200; // Lux
const int MOTION_COOLDOWN = 5000; // 5 seconds
// Simulated sensor values and timing
int currentTemp = 25;
int currentHumidity = 60;
int currentLight = 150;
bool motionDetected = false;
unsigned long lastMotionTime = 0;
// =================================
// SMART HOME RESPONSES (SLOTS)
// =================================
// HVAC System Responses
void activateHeating() {
Serial.println("š„ HVAC: Heating activated - Temperature too low!");
// In real implementation: digitalWrite(HEATER_PIN, HIGH);
}
void activateCooling() {
Serial.println("āļø HVAC: Air conditioning activated - Temperature too high!");
// In real implementation: digitalWrite(AC_PIN, HIGH);
}
void normalizeTemperature() {
Serial.println("š”ļø HVAC: Temperature normalized - System idle");
// In real implementation: digitalWrite(HEATER_PIN, LOW); digitalWrite(AC_PIN, LOW);
}
// Lighting System Responses
void turnOnLights() {
Serial.println("š” LIGHTING: Smart lights turned ON (motion + low light detected)");
// In real implementation: digitalWrite(LIGHTS_PIN, HIGH);
}
void turnOffLights() {
Serial.println("š LIGHTING: Smart lights turned OFF (sufficient natural light)");
// In real implementation: digitalWrite(LIGHTS_PIN, LOW);
}
void activateNightMode() {
Serial.println("š LIGHTING: Night mode activated - Dim ambient lighting");
// In real implementation: analogWrite(LIGHTS_PIN, 50); // Dim lights
}
// Security System Responses
void triggerSecurityAlert() {
Serial.println("šØ SECURITY: Motion detected! Recording started");
// In real implementation: Start camera recording, send notification
}
void logSecurityEvent() {
Serial.println("š SECURITY: Motion event logged to SD card");
// In real implementation: Write timestamp and sensor data to SD
}
// Ventilation System Responses
void activateVentilation() {
Serial.println("š¬ļø VENTILATION: Exhaust fan ON - High humidity detected!");
// In real implementation: digitalWrite(FAN_PIN, HIGH);
}
void deactivateVentilation() {
Serial.println("š VENTILATION: Exhaust fan OFF - Humidity normalized");
// In real implementation: digitalWrite(FAN_PIN, LOW);
}
// Data Logging (Wildcard - responds to ALL events)
void logAllEvents() {
Serial.println("š LOGGER: Event logged to cloud database");
// In real implementation: Send data to ThingSpeak, AWS IoT, etc.
}
// WiFi Notifications (Wildcard)
void sendNotification() {
Serial.println("š± WIFI: Push notification sent to mobile app");
// In real implementation: HTTP POST to notification service
}
// System Status Display
void updateDisplay() {
Serial.println("šŗ DISPLAY: Dashboard updated with latest readings");
// In real implementation: Update LCD/OLED with current values
}
// =================================
// EVENT HANDLER FOR SYSTEM MESSAGES
// =================================
void systemEventHandler(const String& message) {
Serial.println("š§ SYSTEM: " + message);
// Log system errors, connection issues, etc.
}
// =================================
// SENSOR READING & EVENT EMISSION
// =================================
void checkTemperatureSensor() {
// Simulate temperature sensor reading
currentTemp = random(10, 40); // Random temperature 10-40°C
Serial.println("\nš”ļø Temperature: " + String(currentTemp) + "°C");
if (currentTemp > TEMP_THRESHOLD_HIGH) {
eventSystem.emit("temperature_high");
} else if (currentTemp < TEMP_THRESHOLD_LOW) {
eventSystem.emit("temperature_low");
} else {
eventSystem.emit("temperature_normal");
}
}
void checkHumiditySensor() {
// Simulate humidity sensor reading
currentHumidity = random(40, 90); // Random humidity 40-90%
Serial.println("š§ Humidity: " + String(currentHumidity) + "%");
if (currentHumidity > HUMIDITY_THRESHOLD) {
eventSystem.emit("humidity_high");
} else {
eventSystem.emit("humidity_normal");
}
}
void checkLightSensor() {
// Simulate light sensor reading
currentLight = random(50, 400); // Random light 50-400 Lux
Serial.println("āļø Light Level: " + String(currentLight) + " Lux");
if (currentLight < LIGHT_THRESHOLD) {
eventSystem.emit("light_low");
} else {
eventSystem.emit("light_sufficient");
}
}
void checkMotionSensor() {
// Simulate motion sensor (PIR)
// Add some randomness and cooldown period
if (millis() - lastMotionTime > MOTION_COOLDOWN) {
motionDetected = (random(0, 10) < 2); // 20% chance of motion
if (motionDetected) {
Serial.println("š¤ Motion: DETECTED");
eventSystem.emit("motion_detected");
lastMotionTime = millis();
}
}
}
// =================================
// SETUP FUNCTION
// =================================
void setup() {
Serial.begin(115200);
Serial.println("š ========== IoT HOME MONITORING SYSTEM ==========");
Serial.println("š System initializing...\n");
// Set up event callback for system messages
eventSystem.setEventCallback(systemEventHandler);
// =================================
// TEMPERATURE CONTROL CONNECTIONS
// =================================
eventSystem.connect("temperature_high", activateCooling);
eventSystem.connect("temperature_low", activateHeating);
eventSystem.connect("temperature_normal", normalizeTemperature);
// =================================
// SMART LIGHTING CONNECTIONS
// =================================
eventSystem.connect("motion_detected", turnOnLights);
eventSystem.connect("light_low", activateNightMode);
eventSystem.connect("light_sufficient", turnOffLights);
// =================================
// SECURITY SYSTEM CONNECTIONS
// =================================
eventSystem.connect("motion_detected", triggerSecurityAlert);
eventSystem.connect("motion_detected", logSecurityEvent);
// =================================
// VENTILATION SYSTEM CONNECTIONS
// =================================
eventSystem.connect("humidity_high", activateVentilation);
eventSystem.connect("humidity_normal", deactivateVentilation);
// =================================
// WILDCARD CONNECTIONS (respond to ALL events)
// =================================
eventSystem.connect("wildcard", logAllEvents); // Log everything
eventSystem.connect("wildcard", sendNotification); // Notify on all events
eventSystem.connect("wildcard", updateDisplay); // Update display always
Serial.println("ā
All sensors and systems connected!");
Serial.println("š Starting monitoring loop...\n");
delay(2000); // Give user time to read setup messages
}
// =================================
// MAIN MONITORING LOOP
// =================================
void loop() {
Serial.println("==================== SENSOR SCAN ====================");
// Read all sensors and emit appropriate events
checkTemperatureSensor();
delay(200);
checkHumiditySensor();
delay(200);
checkLightSensor();
delay(200);
checkMotionSensor();
Serial.println("==================== END SCAN ====================\n");
delay(1000); // Wait 1 seconds before next sensor reading cycle
}