User Tools

Site Tools


amc:ss2025:group-d:start

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
amc:ss2025:group-d:start [2025/07/18 17:27] 33724_students.hsrwamc:ss2025:group-d:start [2025/07/27 13:47] (current) 33724_students.hsrw
Line 53: Line 53:
 ====Software==== ====Software====
 ''by Florentina Stroissnigg and Deiona Abraham''\\  ''by Florentina Stroissnigg and Deiona Abraham''\\ 
 +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, all programmable pins connected to actuators and sensors were defined as local constants at the beginning of the code. 
 +
 <code C++> <code C++>
 #include <SPI.h> #include <SPI.h>
Line 75: Line 78:
 </code> </code>
  
 +Next, global variables were defined to represent the prototype's internal states, such as whether the cat is inside, whether motion has been detected, and the state of the servos. These variables allow the program to keep track of the system’s current logic status throughout runtime and enable state-based decision-making within the loop.
 +<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, RST_PIN);
 +Servo myServo1;
 +Servo myServo2;
 +</code>
 +
 +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, and the MQTT topic to which the system will publish updates.
 +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++>
 +// WiFi credentials
 +const char WIFI_SSID[] = "Mein Hotspot";
 +const char WIFI_PASSWORD[] = "Ra1nbowFlo";
 +
 +// MQTT broker config (HiveMQ Cloud)
 +const char MQTT_BROKER_ADDRESS[] = "a970366178a840aa8a2c2230bab330aa.s1.eu.hivemq.cloud";
 +const int MQTT_PORT = 8883; // TLS port
 +const char MQTT_CLIENT_ID[] = "";
 +const char MQTT_USERNAME[] = "hivemq.webclient.1751197217617"; // Optional
 +const char MQTT_PASSWORD[] = ";?Q<%F986R7ZliScYakj"; // Optional
 +
 +// MQTT topic
 +const char PUBLISH_TOPIC[] = "AMC/MQTT/rfid/tag";
 +
 +WiFiClientSecure wifiClient;
 +MQTTClient MQTTClient;
 +</code>
 +
 +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++>
 +void connectToWiFi() {
 +  Serial.print("Connecting to WiFi");
 +  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
 +  while (WiFi.status() != WL_CONNECTED) {
 +    delay(500);
 +    Serial.print(".");
 +  }
 +  Serial.println("\nWiFi connected.");
 +  Serial.println(WiFi.localIP());
 +}
 +</code>
 +
 +The function connectToMQTT() handles the MQTT client connection. It configures TLS settings using a simplified certificate check (setInsecure()), initializes the client with connection options, and attempts to connect using the credentials provided. A successful connection to the broker is critical for the notification system to function properly, as MQTT is the method used to report the cat’s movements.
 +<code C++>
 +void connectToMQTT() {
 +
 +  wifiClient.setInsecure();
 +
 +  MQTTClient.begin(MQTT_BROKER_ADDRESS, MQTT_PORT, wifiClient);
 +  MQTTClient.setOptions(60, true, 1500); 
 +  while (!MQTTClient.connect(MQTT_CLIENT_ID, MQTT_USERNAME, MQTT_PASSWORD)) {
 +    Serial.print(".");
 +    delay(1000);
 +  }
 +  Serial.println("\nMQTT connected.");
 +}
 +</code>
 +
 +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++>
 +void setup() {
 +  Serial.begin(115200);
 +  delay(500);
 +
 +  connectToWiFi();
 +  connectToMQTT();
 +
 +  SPI.begin(SCK_PIN, MISO_PIN, MOSI_PIN, SDA_PIN);
 +  rfid.PCD_Init();
 +
 +  myServo1.setPeriodHertz(50);
 +  myServo1.attach(SERVO_PIN_1, 500, 2400);
 +  myServo1.write(0); // locked
 +
 +  myServo2.setPeriodHertz(50);
 +  myServo2.attach(SERVO_PIN_2, 500, 2400);
 +  myServo2.write(0); // locked
 +
 +  pinMode(PIR_PIN, INPUT_PULLUP);
 +
 +  Serial.println("System ready. Waiting for tag...");
 +}
 +</code>
 +
 +The loop() function runs continuously during the system's operation. It begins by maintaining the MQTT connection and reading the PIR sensor’s state. It then performs two main tasks:
 +
 +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" or "Cat is now OUTSIDE"). Regardless of detection, it re-locks the door and resets the state.
 +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++>
 +void loop() {
 +  MQTTClient.loop();  // Maintain MQTT connection
 +  pirSignal = digitalRead(PIR_PIN);
 +
 +  // Step 1: RFID detected
 +  if (!waitingForMotion && rfid.PICC_IsNewCardPresent() && rfid.PICC_ReadCardSerial()) {
 +    Serial.println("RFID tag detected. Unlocking...");
 +    myServo1.write(90);
 + myServo2.write(90);
 +    servoState = 90;
 +
 +    motionStartTime = millis();
 +    waitingForMotion = true;
 +    motionDetected = false;
 +
 +    rfid.PICC_HaltA();
 +    rfid.PCD_StopCrypto1();
 +  }
 +
 +  // Step 2: Motion check
 +  if (waitingForMotion) {
 +    rfidWindow = 1;
 +    if (pirSignal == HIGH) {
 +      motionDetected = true;
 +    }
 +    if (millis() - motionStartTime >= 5000) {
 +      myServo1.write(0);
 +   myServo2.write(0);
 +      servoState = 0;
 +      if (motionDetected) {
 +        catInside = !catInside;
 +        String message = catInside ? "Cat is now INSIDE 🐱🏠" : "Cat is now OUTSIDE 🐱🌳";
 +        MQTTClient.publish(PUBLISH_TOPIC, message.c_str());
 +        delay(1000);
 +      } else {
 +        delay(1000);
 + }
 +      waitingForMotion = false;
 +      rfidWindow = 0;
 +    }
 +  } else {
 +    rfidWindow = 0;
 +
 +  delay(50);
 +}
 +</code>
 ===== Discussion ===== ===== Discussion =====
 ''by Mhwae Mhwae Kyaing Kyaing''\\  ''by Mhwae Mhwae Kyaing Kyaing''\\ 
-One of the main limitations encountered during the development of PAWTAL is the short detection range of the RFID sensor, which is largely due to the small antenna size (30 mm × 30 mm). [As noted in Sam’s documentation….will reference properly soon] the effective range of an RFID system is generally proportional to the size of its antenna. In our prototype, the detection range is less than 2 cm, requiring the cat to be in very close proximity to the door for the tag to be recognized.\\ +One of the main limitations encountered during the development of PAWTAL is the short detection range of the RFID sensor, which is largely due to the small antenna size, 60mm × 40 mm (Osoyoo, 2017). The effective range of an RFID system is generally proportional to the size of its antenna (Rose & Kurtz, 2016). In our prototype, the detection range is less than 2 cm, requiring the cat to be in very close proximity to the door for the tag to be recognized.\\ 
 This limitation could be addressed in future improvements through two potential approaches. Firstly by using an ultra-high frequency (UHF) RFID system operating at 868 MHz, which offers significantly greater range but comes at a high cost which may not be suitable for a student project. Secondly by increasing the size of the tag’s antenna, which could enhance the detection range but requires technical knowledge and resources beyond the current scope of the team. \\  This limitation could be addressed in future improvements through two potential approaches. Firstly by using an ultra-high frequency (UHF) RFID system operating at 868 MHz, which offers significantly greater range but comes at a high cost which may not be suitable for a student project. Secondly by increasing the size of the tag’s antenna, which could enhance the detection range but requires technical knowledge and resources beyond the current scope of the team. \\ 
 NodeRED is subscribed to all topic messages including the message that is sent when the door is unlocked but no motion is detected. This message is then sent to the email. While the other messages notify of the cat's whereabouts, this message is functionally useless and clutters the email inbox. To improve efficiency, later updates to the nodeRED flow’s function node should disallow the continued flow of unwanted messages. NodeRED is subscribed to all topic messages including the message that is sent when the door is unlocked but no motion is detected. This message is then sent to the email. While the other messages notify of the cat's whereabouts, this message is functionally useless and clutters the email inbox. To improve efficiency, later updates to the nodeRED flow’s function node should disallow the continued flow of unwanted messages.
Line 86: Line 234:
 ''by Mhwae Mhwae Kyaing Kyaing'' \\  ''by Mhwae Mhwae Kyaing Kyaing'' \\ 
 Project Pawtal has successfully integrated sensor technology and MQTT communication into a fully functional prototype with real-time data access. The system demonstrates a practical and accessible approach to automating a pet door. Despite the limited range of RFID antenna and minor issues, this project presents a strong proof of concept as a versatile product with future applications and improvements. Future developments could include the integration of location tracking sensors, AI-based behavior analysis, or facial recognition technology to eliminate the need for RFID tags. Additionally, replacing the tag with a pet microchip would further improve the user experience. Moreover, PAWTAL can be further developed into a more complete solution by adding features like a mobile/web app for real-time monitoring and integration with automated pet feeders. Project Pawtal has successfully integrated sensor technology and MQTT communication into a fully functional prototype with real-time data access. The system demonstrates a practical and accessible approach to automating a pet door. Despite the limited range of RFID antenna and minor issues, this project presents a strong proof of concept as a versatile product with future applications and improvements. Future developments could include the integration of location tracking sensors, AI-based behavior analysis, or facial recognition technology to eliminate the need for RFID tags. Additionally, replacing the tag with a pet microchip would further improve the user experience. Moreover, PAWTAL can be further developed into a more complete solution by adding features like a mobile/web app for real-time monitoring and integration with automated pet feeders.
 +
 +===== Demonstration & Presentation =====
 +{{ :amc:ss2025:group-d:youcut_20250725_093122533.mp4 |}}
 +
  
 ===== Reference ===== ===== Reference =====
 +
 +Osoyoo.(2017, September 11).Arduino lesson – RFID RC522. Osoyoo Learning.
 +https://osoyoo.com/2017/09/11/arduino-lesson-rfid-rc522/
 +
 +Rose, M., & Kurtz, J.(2016, May 16). NFC – A closer look [Presentation].Future Electronics.
 +http://www.FutureElectronics.com
 +
  
  
amc/ss2025/group-d/start.1752852459.txt.gz · Last modified: 2025/07/18 17:27 by 33724_students.hsrw