amc:ss2025:group-yin:start
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
amc:ss2025:group-yin:start [2025/08/08 17:38] – 24378_students.hsrw | amc:ss2025:group-yin:start [2025/08/08 17:51] (current) – 24378_students.hsrw | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Smart Home Monitoring and Automation System ====== | ====== Smart Home Monitoring and Automation System ====== | ||
+ | '' | ||
- | + | ====Abstract==== | |
- | Abstract | + | |
This project implements a WiFi-based smart home system using an ESP32 microcontroller. The system integrates motion, temperature, | This project implements a WiFi-based smart home system using an ESP32 microcontroller. The system integrates motion, temperature, | ||
- | 1. Introduction | + | ====1. Introduction==== |
Smart home systems improve security and energy efficiency by automating device control and providing remote notifications. This work focuses on a compact implementation using ESP32 and commonly available sensors, emphasizing responsiveness and minimal false alerts. | Smart home systems improve security and energy efficiency by automating device control and providing remote notifications. This work focuses on a compact implementation using ESP32 and commonly available sensors, emphasizing responsiveness and minimal false alerts. | ||
- | 2. System Architecture and Hardware | + | ====2. System Architecture and Hardware==== |
- | + | ===2.1 System Architecture=== | |
- | 2.1 System Architecture | + | |
{{ : | {{ : | ||
Figure 1: System Architecture of the Home Automation System | Figure 1: System Architecture of the Home Automation System | ||
Line 20: | Line 17: | ||
The Application Logic Layer is where the main decision-making happens. It reads sensor data, checks if any values cross the set thresholds, and controls the devices based on that. Task scheduling is handled using FreeRTOS, where motion detection is the highest priority, temperature and humidity are in the middle, and light sensing is the lowest. At the base is the Hardware Layer, made up of the ESP32 microcontroller connected to various components: a PIR motion sensor (digital input), AHT20 temperature and humidity sensor (I2C), APDS9960 light sensor (I2C), a relay (digital output), and a servo motor (PWM). These are connected using standard protocols like GPIO and I2C. | The Application Logic Layer is where the main decision-making happens. It reads sensor data, checks if any values cross the set thresholds, and controls the devices based on that. Task scheduling is handled using FreeRTOS, where motion detection is the highest priority, temperature and humidity are in the middle, and light sensing is the lowest. At the base is the Hardware Layer, made up of the ESP32 microcontroller connected to various components: a PIR motion sensor (digital input), AHT20 temperature and humidity sensor (I2C), APDS9960 light sensor (I2C), a relay (digital output), and a servo motor (PWM). These are connected using standard protocols like GPIO and I2C. | ||
- | The diagram below shows how the main hardware components are connected. | + | The diagram below shows how the main hardware components are connected.\\ |
{{ : | {{ : | ||
Figure 2: Hardware Assembly of the System | Figure 2: Hardware Assembly of the System | ||
- | 2.1 Hardware Components | + | ===2.1 Hardware Components=== |
1. ESP32: Central microcontroller with WiFi support. | 1. ESP32: Central microcontroller with WiFi support. | ||
2. PIR Sensor: Detects human movement. | 2. PIR Sensor: Detects human movement. | ||
Line 49: | Line 44: | ||
Figure 7: Servo Motor 3.3V | Figure 7: Servo Motor 3.3V | ||
- | 2.2 Software Components | + | ===2.2 Software Components=== |
• Arduino IDE with FreeRTOS | • Arduino IDE with FreeRTOS | ||
• ESPAsyncWebServer for web interface | • ESPAsyncWebServer for web interface | ||
Line 55: | Line 50: | ||
• Servo and sensor libraries (Adafruit, SparkFun) | • Servo and sensor libraries (Adafruit, SparkFun) | ||
- | 3. Implementation | + | ====3. Implementation==== |
- | + | ===3.1 Sensor Integration=== | |
- | 3.1 Sensor Integration | + | |
Other sensors except motion sensor is activated every 5 seconds. Motion sensor is activated every 5 milliseconds when in “Away” mode. Temperature and humidity activate the relay to switch the ventilator, light intensity controls curtain adjustment via a servo. Motion is processed via interrupt and high-priority task. | Other sensors except motion sensor is activated every 5 seconds. Motion sensor is activated every 5 milliseconds when in “Away” mode. Temperature and humidity activate the relay to switch the ventilator, light intensity controls curtain adjustment via a servo. Motion is processed via interrupt and high-priority task. | ||
- | 3.2 Network and Pushover Setup | + | ===3.2 Network and Pushover Setup=== |
ESP32 connects to local WiFi and communicates with push notification with the Pushover APP. Messages are sent only when motion is detected, and the system is in Away mode. | ESP32 connects to local WiFi and communicates with push notification with the Pushover APP. Messages are sent only when motion is detected, and the system is in Away mode. | ||
The system uses the Pushover service to send motion detection alerts to the user’s mobile device. A user account was created on the Pushover platform, and a new application was registered to obtain the required User Key and API Token. These credentials are used in the ESP32 firmware to authenticate with the Pushover API. | The system uses the Pushover service to send motion detection alerts to the user’s mobile device. A user account was created on the Pushover platform, and a new application was registered to obtain the required User Key and API Token. These credentials are used in the ESP32 firmware to authenticate with the Pushover API. | ||
Line 68: | Line 60: | ||
When motion is detected and the system is in “Away” mode as described in the table 1, the ESP32 sends an HTTPS POST request to the Pushover API endpoint with the alert message. This integration allows real-time push notifications, | When motion is detected and the system is in “Away” mode as described in the table 1, the ESP32 sends an HTTPS POST request to the Pushover API endpoint with the alert message. This integration allows real-time push notifications, | ||
- | 3.3 Web Server Interface | + | ===3.3 Web Server Interface=== |
Users access a web page hosted on ESP32 to toggle between Home and Away mode. The page also displays current sensor values and relay state. | Users access a web page hosted on ESP32 to toggle between Home and Away mode. The page also displays current sensor values and relay state. | ||
- | 3.4 FreeRTOS Task Priority and Control Logic | + | ===3.4 FreeRTOS Task Priority and Control Logic=== |
Table 1: Task Priorities of The Seosor Tasks (3 = Highest, 1 = Lowest) | Table 1: Task Priorities of The Seosor Tasks (3 = Highest, 1 = Lowest) | ||
^ Tasks ^ Priority | ^ Tasks ^ Priority | ||
Line 80: | Line 70: | ||
| Light Sensor | | Light Sensor | ||
- | Table 2: Control Logic and Thresholds of Detected Environmental Values | + | Table 2: Control Logic and Thresholds of Detected Environmental Values\\ |
^ Sensor Values | ^ Sensor Values | ||
| Motion detected | | Motion detected | ||
Line 88: | Line 77: | ||
| Light Sensor | | Light Sensor | ||
- | 4. Results | + | ====4. Results==== |
- | + | ===4.1 System Behaviour=== | |
- | 4.1 System Behaviour | + | |
The system connects to WiFi successfully and displays its local IP address for user access. Motion alerts are sent via Pushover only when the system is in Away mode and actual human motion is detected. The servo motor responds to ambient light levels by adjusting the blinds—closing when light is too strong and opening when it drops. The relay is activated when either the temperature exceeds a set threshold or humidity rises above the defined limit, helping manage indoor climate conditions effectively. | The system connects to WiFi successfully and displays its local IP address for user access. Motion alerts are sent via Pushover only when the system is in Away mode and actual human motion is detected. The servo motor responds to ambient light levels by adjusting the blinds—closing when light is too strong and opening when it drops. The relay is activated when either the temperature exceeds a set threshold or humidity rises above the defined limit, helping manage indoor climate conditions effectively. | ||
- | 5. Demonstration | + | ====5. Demonstration==== |
System boots, connects to WiFi, and hosts a web page. Home or away mode can be toggled on the page for the motion sensor detection. The interface also shows the status of light, temperature and humidity. | System boots, connects to WiFi, and hosts a web page. Home or away mode can be toggled on the page for the motion sensor detection. The interface also shows the status of light, temperature and humidity. | ||
- | {{ : | + | {{ : |
Figure 8: “Home” and “Away” mode in web interface. | Figure 8: “Home” and “Away” mode in web interface. | ||
Movement in Away mode triggers Pushover notification alert as shown in figure 8. | Movement in Away mode triggers Pushover notification alert as shown in figure 8. | ||
- | {{ : | + | {{ : |
Figure 9: Triggered pushover notification in " | Figure 9: Triggered pushover notification in " | ||
Line 113: | Line 97: | ||
The video demonstrates the motion sensor detection when the system is set to away mode and how the push notification is triggered. It furthermore shows how the servo is activated when the light level is above or below the threshold and how the relay is activated based on the humidity level. However, the temperature sensor cannot be manipulated above the threshold and as it is dangerous. Nevertheless, | The video demonstrates the motion sensor detection when the system is set to away mode and how the push notification is triggered. It furthermore shows how the servo is activated when the light level is above or below the threshold and how the relay is activated based on the humidity level. However, the temperature sensor cannot be manipulated above the threshold and as it is dangerous. Nevertheless, | ||
- | 6. Limitations and Areas for Improvement | + | ====6. Limitations and Areas for Improvement==== |
• Sensitivity of Motion Detection: The motion sensor may sometimes fail to trigger alerts. | • Sensitivity of Motion Detection: The motion sensor may sometimes fail to trigger alerts. | ||
• Reliance on Stable WiFi Connection: The system requires a stable WiFi connection to function properly. Interruptions or changes in IP address may disrupt access to the web interface and push notifications. Implementing fixed IP addressing or network reconnection strategies could enhance reliability. | • Reliance on Stable WiFi Connection: The system requires a stable WiFi connection to function properly. Interruptions or changes in IP address may disrupt access to the web interface and push notifications. Implementing fixed IP addressing or network reconnection strategies could enhance reliability. | ||
Line 121: | Line 104: | ||
• Servo Movement Optimization: | • Servo Movement Optimization: | ||
- | 7. Conclusion | + | ====7. Conclusion==== |
This ESP32-based smart home system demonstrates motion-based alerting and light-driven automation. With FreeRTOS prioritization, | This ESP32-based smart home system demonstrates motion-based alerting and light-driven automation. With FreeRTOS prioritization, | ||
+ | < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | // WiFi credentials | ||
+ | const char* ssid = " | ||
+ | const char* password = " | ||
+ | |||
+ | // Pushover credentials | ||
+ | const char* pushoverUserKey = " | ||
+ | const char* pushoverAppToken = " | ||
+ | |||
+ | // Pin definitions | ||
+ | #define PIR_PIN 13 | ||
+ | #define RELAY_PIN 14 | ||
+ | #define SERVO_PIN 12 | ||
+ | |||
+ | // Hardware objects | ||
+ | SparkFun_APDS9960 apds; | ||
+ | Adafruit_AHTX0 aht; | ||
+ | Servo myservo; | ||
+ | AsyncWebServer server(80); | ||
+ | WiFiClientSecure secureClient; | ||
+ | |||
+ | // States | ||
+ | volatile bool motionDetected = false; | ||
+ | bool isHome = false; | ||
+ | bool canSendAlert = true; | ||
+ | |||
+ | float currentTemp = 0.0, currentHum = 0.0; | ||
+ | uint16_t currentLight = 0; | ||
+ | |||
+ | // Task handles | ||
+ | TaskHandle_t motionTaskHandle; | ||
+ | TaskHandle_t tempHumTaskHandle; | ||
+ | TaskHandle_t lightTaskHandle; | ||
+ | |||
+ | // Motion ISR | ||
+ | void IRAM_ATTR motionISR() { | ||
+ | if (!isHome) { | ||
+ | motionDetected = true; | ||
+ | Serial.println(" | ||
+ | } | ||
+ | } | ||
+ | |||
+ | // Send pushover message | ||
+ | void sendPushover(String message) { | ||
+ | if (WiFi.status() == WL_CONNECTED) { | ||
+ | secureClient.setInsecure(); | ||
+ | |||
+ | String postData = " | ||
+ | "& | ||
+ | "& | ||
+ | |||
+ | if (!secureClient.connect(" | ||
+ | Serial.println(" | ||
+ | return; | ||
+ | } | ||
+ | |||
+ | secureClient.println(" | ||
+ | secureClient.println(" | ||
+ | secureClient.println(" | ||
+ | secureClient.print(" | ||
+ | secureClient.println(postData.length()); | ||
+ | secureClient.println(); | ||
+ | secureClient.print(postData); | ||
+ | |||
+ | // Read HTTP response headers | ||
+ | while (secureClient.connected()) { | ||
+ | String line = secureClient.readStringUntil(' | ||
+ | Serial.println(line); | ||
+ | if (line == " | ||
+ | } | ||
+ | |||
+ | // Read HTTP body | ||
+ | String response = secureClient.readString(); | ||
+ | Serial.println(" | ||
+ | Serial.println(response); | ||
+ | Serial.println(" | ||
+ | } else { | ||
+ | Serial.println(" | ||
+ | } | ||
+ | } | ||
+ | |||
+ | void setup() { | ||
+ | Serial.begin(115200); | ||
+ | |||
+ | pinMode(PIR_PIN, | ||
+ | pinMode(RELAY_PIN, | ||
+ | digitalWrite(RELAY_PIN, | ||
+ | myservo.attach(SERVO_PIN); | ||
+ | myservo.write(0); | ||
+ | |||
+ | Wire.begin(4, | ||
+ | |||
+ | if (!apds.init()) Serial.println(" | ||
+ | else apds.enableLightSensor(false); | ||
+ | |||
+ | if (!aht.begin()) Serial.println(" | ||
+ | |||
+ | attachInterrupt(digitalPinToInterrupt(PIR_PIN), | ||
+ | |||
+ | WiFi.begin(ssid, | ||
+ | Serial.print(" | ||
+ | while (WiFi.status() != WL_CONNECTED) { | ||
+ | delay(500); | ||
+ | Serial.print(" | ||
+ | } | ||
+ | Serial.println(" | ||
+ | |||
+ | // Web UI | ||
+ | server.on("/", | ||
+ | String html = "< | ||
+ | html += "< | ||
+ | html += "< | ||
+ | html += "< | ||
+ | html += "< | ||
+ | html += "< | ||
+ | html += "< | ||
+ | html += "< | ||
+ | html += "</ | ||
+ | request-> | ||
+ | }); | ||
+ | |||
+ | server.on("/ | ||
+ | isHome = !isHome; | ||
+ | motionDetected = false; | ||
+ | canSendAlert = true; | ||
+ | request-> | ||
+ | }); | ||
+ | |||
+ | server.begin(); | ||
+ | |||
+ | // FreeRTOS tasks | ||
+ | xTaskCreatePinnedToCore(motionTask, | ||
+ | xTaskCreatePinnedToCore(tempHumTask, | ||
+ | xTaskCreatePinnedToCore(lightTask, | ||
+ | } | ||
+ | |||
+ | void loop() { | ||
+ | // RTOS handles everything | ||
+ | } | ||
+ | |||
+ | // Motion Task (priority 3) | ||
+ | void motionTask(void *pvParameters) { | ||
+ | while (1) { | ||
+ | if (!isHome && motionDetected) { | ||
+ | motionDetected = false; | ||
+ | Serial.println(" | ||
+ | |||
+ | if (canSendAlert) { | ||
+ | Serial.println(" | ||
+ | sendPushover(" | ||
+ | canSendAlert = false; | ||
+ | |||
+ | // Re-enable alert after 5s | ||
+ | xTimerHandle alertTimer = xTimerCreate(" | ||
+ | canSendAlert = true; | ||
+ | Serial.println(" | ||
+ | }); | ||
+ | xTimerStart(alertTimer, | ||
+ | } else { | ||
+ | Serial.println(" | ||
+ | } | ||
+ | } | ||
+ | |||
+ | vTaskDelay(pdMS_TO_TICKS(5)); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | // Temp/ | ||
+ | void tempHumTask(void *pvParameters) { | ||
+ | while (1) { | ||
+ | sensors_event_t humidity, temp; | ||
+ | aht.getEvent(& | ||
+ | currentTemp = temp.temperature; | ||
+ | currentHum = humidity.relative_humidity; | ||
+ | |||
+ | Serial.printf(" | ||
+ | |||
+ | if (currentTemp >= 25.0 || currentHum >= 70.0) { | ||
+ | digitalWrite(RELAY_PIN, | ||
+ | Serial.println(" | ||
+ | } else { | ||
+ | digitalWrite(RELAY_PIN, | ||
+ | Serial.println(" | ||
+ | } | ||
+ | |||
+ | vTaskDelay(pdMS_TO_TICKS(5000)); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | // Light Task (priority 1) | ||
+ | void lightTask(void *pvParameters) { | ||
+ | bool blindsClosed = false; // Track servo state | ||
+ | |||
+ | while (1) { | ||
+ | if (apds.readAmbientLight(currentLight)) { | ||
+ | Serial.print(" | ||
+ | Serial.println(currentLight); | ||
+ | |||
+ | if (currentLight >= 10000 && !blindsClosed) { | ||
+ | myservo.write(180); | ||
+ | blindsClosed = true; | ||
+ | Serial.println(" | ||
+ | } | ||
+ | else if (currentLight < 10000 && blindsClosed) { | ||
+ | myservo.write(0); | ||
+ | blindsClosed = false; | ||
+ | Serial.println(" | ||
+ | } | ||
+ | } | ||
+ | |||
+ | vTaskDelay(pdMS_TO_TICKS(5000)); | ||
+ | } | ||
+ | }</ |
amc/ss2025/group-yin/start.1754667537.txt.gz · Last modified: 2025/08/08 17:38 by 24378_students.hsrw