/** * @file bluetooth.h * @brief Bluetooth interface for configuration and status reporting. * * This module initializes the Bluetooth serial interface for remote configuration. * It provides functions to set up Bluetooth, process incoming Bluetooth commands, * and send periodic status updates (including auto mode, WiFi, and MQTT connectivity). * * The status update string is built dynamically by querying the current system state. * * @author * James C. Alexander * @date * 2025-02-03 */ #ifndef BLUETOOTH_H #define BLUETOOTH_H #include #include "config.h" #include "commands.h" #include "custom_mqtt.h" #include // --------------------------------------------------------------------- // Global Bluetooth Serial Object // --------------------------------------------------------------------- /** * @brief Bluetooth Serial interface. * * This object is used to communicate over Bluetooth, allowing remote configuration * and status monitoring. */ BluetoothSerial SerialBT; /** * @brief Timestamp for the last Bluetooth status update. */ unsigned long lastBTStatusUpdate = 0; // --------------------------------------------------------------------- // Function Prototypes // --------------------------------------------------------------------- /** * @brief Initialize the Bluetooth interface. * * Sets up the Bluetooth serial connection with a predefined device name. */ void setupBluetooth(); /** * @brief Process incoming Bluetooth commands. * * Checks if any data is available on the Bluetooth serial interface. If a command is received, * it trims any extraneous whitespace and passes the command to the configuration command handler. */ void handleBluetoothCommands(); /** * @brief Send periodic status updates over Bluetooth. * * If at least 1 second has elapsed since the last update, this function sends a status update * string via Bluetooth. */ void sendBTStatusUpdate(); /** * @brief Build a formatted system status update string. * * The status update includes the current auto mode state, WiFi connectivity, and MQTT connectivity. * * @return A formatted String containing the system status. */ String getStatusUpdate() { String status = "AutoMode: "; status += autoMode ? "ON" : "OFF"; status += ", WiFi: "; status += (WiFi.status() == WL_CONNECTED) ? "Connected" : "Disconnected"; status += ", MQTT: "; status += (client.connected()) ? "Connected" : "Disconnected"; return status; } // --------------------------------------------------------------------- // Function Implementations // --------------------------------------------------------------------- void setupBluetooth() { SerialBT.begin("GrowTentController"); // Initialize Bluetooth with the device name. Serial.println("Bluetooth Started. Ready for configuration."); } void handleBluetoothCommands() { // If data is available on the Bluetooth interface, read and process the command. if (SerialBT.available()) { String command = SerialBT.readStringUntil('\n'); command.trim(); // Remove any leading/trailing whitespace. handleConfigCommand(command); } } void sendBTStatusUpdate() { // Send a status update every 1000 milliseconds (1 second). if (millis() - lastBTStatusUpdate >= 1000) { lastBTStatusUpdate = millis(); SerialBT.println(getStatusUpdate()); } } #endif // BLUETOOTH_H