amc:ss2025:group-d:start
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
amc:ss2025:group-d:start [2025/07/26 12:43] – 33724_students.hsrw | amc:ss2025:group-d:start [2025/07/27 13:47] (current) – 33724_students.hsrw | ||
---|---|---|---|
Line 53: | Line 53: | ||
====Software==== | ====Software==== | ||
'' | '' | ||
- | The code was developed using the Arduino IDE. | + | The code was developed using the Arduino IDE. \\ |
- | Step one of the coding process involved importing and including all necessary libraries. For better readability and maintainability, | + | Step one of the coding process involved importing and including all necessary libraries. For better readability and maintainability, |
<code C++> | <code C++> | ||
Line 78: | Line 78: | ||
</ | </ | ||
+ | Next, global variables were defined to represent the prototype' | ||
+ | <code C++> | ||
+ | bool catInside = false; | ||
+ | bool waitingForMotion = false; | ||
+ | unsigned long motionStartTime = 0; | ||
+ | bool motionDetected = false; | ||
+ | |||
+ | int pirSignal = 0; | ||
+ | int rfidWindow = 0; | ||
+ | int servoState = 0; | ||
+ | |||
+ | MFRC522 rfid(SDA_PIN, | ||
+ | Servo myServo1; | ||
+ | Servo myServo2; | ||
+ | </ | ||
+ | |||
+ | To enable communication over the internet, WiFi credentials and MQTT broker information (HiveMQ Cloud) were defined. This includes the broker address, port number, client ID, credentials, | ||
+ | These configurations allow the ESP32 to establish a secure connection to the broker and publish MQTT messages when the cat enters or exits. | ||
<code C++> | <code C++> | ||
Line 93: | Line 111: | ||
// MQTT topic | // MQTT topic | ||
const char PUBLISH_TOPIC[] = " | const char PUBLISH_TOPIC[] = " | ||
- | |||
- | bool catInside = false; | ||
- | bool waitingForMotion = false; | ||
- | unsigned long motionStartTime = 0; | ||
- | bool motionDetected = false; | ||
- | |||
- | int pirSignal = 0; | ||
- | int rfidWindow = 0; | ||
- | int servoState = 0; | ||
- | |||
- | MFRC522 rfid(SDA_PIN, | ||
- | Servo myServo1; | ||
- | Servo myServo2; | ||
WiFiClientSecure wifiClient; | WiFiClientSecure wifiClient; | ||
Line 111: | Line 116: | ||
</ | </ | ||
+ | The function connectToWiFi() is used to establish a WiFi connection using the credentials defined earlier. Serial outputs are used to provide feedback on the connection status via the Serial Monitor. This function ensures that the ESP32 is successfully connected to the network before any MQTT communication can take place | ||
<code C++> | <code C++> | ||
void connectToWiFi() { | void connectToWiFi() { | ||
Line 125: | Line 130: | ||
</ | </ | ||
- | <codeC++> | + | The function connectToMQTT() handles the MQTT client connection. It configures TLS settings using a simplified certificate check (setInsecure()), |
+ | <code C++> | ||
void connectToMQTT() { | void connectToMQTT() { | ||
Line 140: | Line 146: | ||
</ | </ | ||
+ | The setup() function runs once when the ESP32 boots up. It initializes the serial interface, connects to WiFi and MQTT, initializes the RFID reader via SPI, sets up the two servo motors with their respective pins and signal ranges, and configures the PIR motion sensor as an input. This function prepares the hardware and network environment to ensure the system is ready to process RFID scans and detect motion events from the start. | ||
<code C++> | <code C++> | ||
void setup() { | void setup() { | ||
Line 163: | Line 170: | ||
Serial.println(" | Serial.println(" | ||
} | } | ||
- | >/code> | + | </code> |
+ | |||
+ | The loop() function runs continuously during the system' | ||
+ | |||
+ | RFID Detection: If a new RFID tag is present and the system is not already waiting for motion, it unlocks the door by turning both servo motors to 90°, records the current time, and sets a flag to begin motion monitoring. | ||
+ | Motion Monitoring: While the system is waiting for motion, it checks whether motion has occurred within a 5-second window. If motion is detected, it toggles the catInside state and publishes a corresponding MQTT message ("Cat is now INSIDE" | ||
+ | This logic ensures that the door only unlocks when the registered RFID tag is detected and verifies that the cat actually entered or exited using the motion sensor. The result is then communicated via MQTT to allow remote monitoring. | ||
<code C++> | <code C++> | ||
void loop() { | void loop() { |
amc/ss2025/group-d/start.1753526618.txt.gz · Last modified: 2025/07/26 12:43 by 33724_students.hsrw