# Developing a Fire Monitoring System Based on MQTT, ESP-NOW, and a REM in Industrial Environments
## 3 Methodology
### YOLOv5  
You Only Look Once version 5 is an object detection model known for processing images quickly and accurately in real-time [22]. This is crucial for fire detection systems responding immediately to fire outbreaks. YOLOv5 was chosen for the following reasons:  
1. Speed: Due to its rapid image analysis, the YOLOv5 model allows the system to quickly identify and react to fires;
2. Accuracy: YOLOv5 can accurately detect fire in images, even in cluttered environments with complex backgrounds [23];
3. Pre-trained fire detection: The model has been pre-trained on the dataset specifically designed for fire detection [24], including images collected from sources like Kaggle [25]. The YOLOv5 model is trained using a fire-specific dataset from the Kaggle website that includes images of fires, smoke, and various scenarios of fire outbreaks. The images include both daytime and nighttime fire events, as well as small and large-scale fires. The model uses a convolutional neural network (CNN) architecture to analyze these images. While training the model, images that include smoke without fire, as well as other irrelevant objects (e.g., guns), were filtered out to ensure that the model accurately detects only fire. Once an anomaly such as fire is detected by the sensors, the image is processed to verify its nature. The system uses detection algorithms to further process image data. After confirmation of the presence of fire by the YOLOv5 model, the system generates an alert that is immediately sent to the central monitoring server. Using the pre-trained YOLOv5 model minimizes false positives by focusing on significant events. Raspberry Pi 4 is a lightweight, cost-effective, and portable edge computing device. Given its processing capabilities, the YOLOv5 fire detection model is configured to run on this hardware to handle real-time image analysis. This deployment takes advantage of YOLOv5's lightweight architecture, which can operate on devices with limited computational resources. The PyTorch framework (essential for running YOLOv5), along with other dependencies like OpenCV (for image processing), is installed on the Raspberry Pi 4. OpenCV helps manage image preprocessing and postprocessing tasks, such as resizing images and drawing bounding boxes around detected fire regions. The ESP32-CAM sensors in our proposed system are configured to capture images for fire detection; however, the experimental validation of the system was conducted using preprocessed datasets rather than real-time images.\" These datasets, consisting of fire and smoke images from curated sources, were used to train and evaluate the YOLOv5 model. Figure 2: Illustration of the MQTT messaging protocol. While the system is designed for real-time operation, the experimental phase focused on validating the detection model under controlled conditions. The pseudocode in Algorithm A1 in Appendix A.1 outlines the core processes in the fire detection system. Each step is designed to ensure efficient data collection, fire detection, and real-time alert generation while maintaining low power consumption and reliability. The system begins with the initialization of all key components, including the ESP32-CAM sensor modules, the ESP-NOW protocol for peer-to-peer communication, the MQTT protocol for message transmission, and the YOLOv5 model. Once initialized, the system sets up the multi-hop ESP-NOW network, which allows ESP32-CAM sensors to communicate with each other. This step also establishes the MQTT broker, which will facilitate the transmission of alerts from the sensor nodes to the central monitoring server. The system enters a continuous monitoring loop, where each ESP32-CAM sensor collects environmental data, such as temperature and smoke levels. Although Raspberry Pi 4 is not as powerful as GPUs used for deep learning tasks, it can handle YOLOv5 due to the model's optimization for resource-constrained environments.