diff --git a/conf/conf.hpp b/conf/conf.hpp index 5f78a448..2b1917c9 100644 --- a/conf/conf.hpp +++ b/conf/conf.hpp @@ -19,18 +19,21 @@ namespace fablabbg static constexpr MachineID machine_id{1}; static constexpr std::string_view machine_name = "MACHINE1"; static constexpr MachineType machine_type = MachineType::LASER; - } + } // namespace conf::default_config + namespace conf::rfid_tags { static constexpr uint8_t UID_BYTE_LEN{4}; /* Number of bytes in RFID cards UID */ static constexpr uint8_t CACHE_LEN{10}; /* Number of cached UID */ - } + } // namespace conf::rfid_tags + namespace conf::lcd { static constexpr uint8_t ROWS{2}; /* Number of rows for LCD display */ static constexpr uint8_t COLS{16}; /* Number of cols for LCD display */ static constexpr auto SHORT_MESSAGE_DELAY{1s}; /* How much time shall we wait for a short message on LCD for user */ - } + } // namespace conf::lcd + namespace conf::machine { static constexpr auto DEFAULT_AUTO_LOGOFF_DELAY{8h}; /* User will be log out after this delay. If 0h, no auto-logout. This may be overriden by backend data */ @@ -41,7 +44,7 @@ namespace fablabbg static constexpr auto LONG_TAP_DURATION{10s}; /* Minimum time to confirm by long tap maintenance*/ static_assert(BEEP_PERIOD <= POWEROFF_GRACE_PERIOD); static_assert(DELAY_BETWEEN_BEEPS < BEEP_PERIOD); - } + } // namespace conf::machine namespace conf::debug { @@ -49,14 +52,16 @@ namespace fablabbg static constexpr bool ENABLE_TASK_LOGS{false}; /* True to add logs regarding tasks scheduling and statistics */ static constexpr unsigned long SERIAL_SPEED_BDS{115200}; /* Serial speed in bauds */ static constexpr bool FORCE_PORTAL_RESET{false}; /* True to force EEPROM reset */ - } + } // namespace conf::debug + namespace conf::buzzer { static constexpr unsigned short LEDC_PWM_CHANNEL{0}; /* Esp32 pwm channel for beep generation */ static constexpr auto STANDARD_BEEP_DURATION{200ms}; /* Single beep duration, typical value 200ms */ static constexpr auto NB_BEEPS{3}; /* Number of beeps every time the function is callsed */ static constexpr unsigned int BEEP_HZ{660}; /* Beep frequency in Hz */ - } + } // namespace conf::buzzer + namespace conf::tasks { static constexpr auto RFID_CHECK_PERIOD{150ms}; /* Task period to check for RFID badge (should be fast: 150ms) */ @@ -64,14 +69,15 @@ namespace fablabbg static constexpr auto MQTT_REFRESH_PERIOD{30s}; /* Query the MQTT broker for machine state at given period (default: 30s) */ static constexpr auto WATCHDOG_TIMEOUT{30s}; /* Timeout for hardware watchdog, set to 0s to disable (default: 30s) */ static constexpr auto PORTAL_CONFIG_TIMEOUT{5min}; /* Timeout for portal configuration (default: 5min) */ - } + } // namespace conf::tasks namespace conf::mqtt { static constexpr std::string_view topic{"machine"}; /* Initial part of the topic, machine ID will be added */ static constexpr std::string_view response_topic{"/reply"}; /* Server reply (sub-topic of the full machine topic) */ static constexpr auto MAX_RETRY_DURATION{2s}; /* How much time should we spend at maximum trying to contact the broker for a single request */ - } + static constexpr auto PORT_NUMBER{1883}; /* MQTT port */ + } // namespace conf::mqtt // Make sure the hardware watchdog period is not too short considering we're blocking tasks for some operations static_assert(conf::tasks::WATCHDOG_TIMEOUT == 0s || diff --git a/include/mock/MockMQTTBroker.hpp b/include/mock/MockMQTTBroker.hpp index d832df9c..1815da8b 100644 --- a/include/mock/MockMQTTBroker.hpp +++ b/include/mock/MockMQTTBroker.hpp @@ -29,7 +29,6 @@ namespace fablabbg void mainLoop(); private: - constexpr static uint16_t MQTTPORT = 1883; std::atomic is_running{false}; std::string topic = ""; std::string payload = ""; diff --git a/src/FabServer.cpp b/src/FabServer.cpp index f5dfb710..a4c3e95a 100644 --- a/src/FabServer.cpp +++ b/src/FabServer.cpp @@ -233,8 +233,20 @@ namespace fablabbg if (WiFi.status() == WL_CONNECTED && !client.connected()) { - ESP_LOGD(TAG, "Connecting to MQTT server %s", server_ip.c_str()); - client.begin(server_ip.data(), wifi_client); + IPAddress ip; + if (WiFi.hostByName(server_ip.c_str(), ip)) + { + ESP_LOGD(TAG, "Resolved MQTT server [%s] as [%s]", server_ip.c_str(), ip.toString().c_str()); + } + else + { + ESP_LOGE(TAG, "Failed to resolve MQTT server [%s]", server_ip.c_str()); + return false; + } + + ESP_LOGD(TAG, "Connecting to MQTT server [%s:%d]...", ip.toString().c_str(), conf::mqtt::PORT_NUMBER); + + client.begin(ip, conf::mqtt::PORT_NUMBER, wifi_client); callback = [&](String &a, String &b) { return messageReceived(a, b); }; diff --git a/src/mock/MockMQTTBroker.cpp b/src/mock/MockMQTTBroker.cpp index 8b414609..cf6a04c2 100644 --- a/src/mock/MockMQTTBroker.cpp +++ b/src/mock/MockMQTTBroker.cpp @@ -20,7 +20,7 @@ namespace fablabbg } if (!is_running) { - is_running = init(MockMQTTBroker::MQTTPORT, true); + is_running = init(conf::mqtt::PORT_NUMBER, true); ESP_LOGI(TAG2, "MQTT BROKER: started with result %d", is_running.load()); }