Skip to content

Commit

Permalink
port number config + mqtt hostname resolution test
Browse files Browse the repository at this point in the history
  • Loading branch information
PBrunot committed Jan 13, 2024
1 parent 80f2fdd commit f2b0b67
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 12 deletions.
22 changes: 14 additions & 8 deletions conf/conf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -41,37 +44,40 @@ 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
{
static constexpr bool ENABLE_LOGS{true}; /* True to add logs */
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) */
static constexpr auto RFID_SELFTEST_PERIOD{60s}; /* Performs RFID self check and reset chip if necessary (default: 60s) */
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 ||
Expand Down
1 change: 0 additions & 1 deletion include/mock/MockMQTTBroker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ namespace fablabbg
void mainLoop();

private:
constexpr static uint16_t MQTTPORT = 1883;
std::atomic<bool> is_running{false};
std::string topic = "";
std::string payload = "";
Expand Down
16 changes: 14 additions & 2 deletions src/FabServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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); };
Expand Down
2 changes: 1 addition & 1 deletion src/mock/MockMQTTBroker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down

0 comments on commit f2b0b67

Please sign in to comment.