/** * @file main.cpp * @brief Main execution program for the CRAH Unit (TCP) Emulator. * @author Emmanuel Hernandez Cruz * @date 2025-09-02 * * @details This file contains the main execution program for an Arduino-based emulator of a CRAH unit. * The program uses a Wi-Fi connection to communicate via the Modbus IP protocol. * * The setup() function initializes the following: * - Serial communication for debugging. * - Wi-Fi connection using credentials from config.h. * - A Modbus TCP server. * - Modbus points (Coils, Holding Registers, etc.) based on a predefined map in config.h. * * The loop() function continuously: * - Services the Modbus TCP server to handle incoming requests. * - Periodically calls the main update loop for the emulated equipment, which * manages state transitions and behavior strategies. * * @see config.h for Wi-Fi and Modbus configuration. * @see Equipment.h for the main equipment logic. * @see State.h for different equipment states. * @see Strategies/Strategy_Behavior.h for value generation strategies. * @see Modbus_Point.h for the base class for all Modbus points. */ //================================================================================================================================= //Libraries and declaration of variables. #include #include "config.h" #include "ModbusPoints/Modbus_PointFactory.h" #if defined(USE_MODBUS_IP) #include #else #include #endif //================================================================================================================================= /** * @brief Initializes the application. * @details This function runs once at startup. It configures the serial communication, * Wi-Fi, and the Modbus server. It also creates and initializes all the Modbus points * based on the `mb_map` array in `config.h`. */ void setup() { Serial.begin(115200); //Serial comm start WiFi.config(local_IP, gateway, subnet); // Wifi service start WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.print("."); } Serial.println("Connected!!"); mb.server(); //Modbus server start Serial.println("Server Created"); Serial.println(map_size); for(int i = 0; i < map_size; i++){ Modbus_Point* point = createModbus_Point(&mb, mb_map[i].category, mb_map[i].address, mb_map[i].value, mb_map[i].description); if (point) { point->addToModbusServer(); EquipmentInstance.addModbus_Point(mb_map[i].description, point); } } Serial.println("All modbus Points created"); Serial.println("Setup function ended"); } //================================================================================================================================= /** * @brief The main application loop. * @details This function runs repeatedly after setup() has completed. It performs two main actions: * 1. It continuously services the Modbus server by calling `mb.task()` to handle * incoming requests from a Modbus master. * 2. At a fixed interval (defined in `config.h`), it calls `EquipmentInstance.update()` * to run the emulator's internal state machine and behavior logic. */ void loop() { mb.task(); unsigned long currentMillis = millis(); if (currentMillis - previousMillis >= interval) { previousMillis = currentMillis; unsigned long startTime = millis(); EquipmentInstance.update(); unsigned long endTime = millis(); unsigned long elapsedTime = endTime - startTime; Serial.printf("Control Execution time: %d ms\n", elapsedTime); } }