Firmware: enable syncing to NTP

This commit is contained in:
Erki 2024-02-10 20:45:54 +02:00
parent b42bb7efa9
commit 57d068ffad
2 changed files with 58 additions and 23 deletions

View File

@ -8,10 +8,11 @@
#include <cstring>
#include <esp_netif.h>
#include <esp_event.h>
#include <esp_wifi.h>
#include <esp_log.h>
#include <esp_netif.h>
#include <esp_netif_sntp.h>
#include <esp_wifi.h>
namespace
{
@ -20,7 +21,8 @@ const char* TAG = "clock_core";
constexpr EventBits_t WIFI_FAIL_BIT = BIT0;
constexpr EventBits_t WIFI_CONNECTED_BIT = BIT1;
EventGroupHandle_t wifi_event_group_;
constexpr EventBits_t SNTP_SYNCED = BIT2;
EventGroupHandle_t clock_core_event_group_;
void wifiEventHandler(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data)
@ -41,7 +43,7 @@ void wifiEventHandler(void* arg, esp_event_base_t event_base,
}
else
{
xEventGroupSetBits(wifi_event_group_, WIFI_FAIL_BIT);
xEventGroupSetBits(clock_core_event_group_, WIFI_FAIL_BIT);
}
ESP_LOGW(TAG, "Connection to AP failed.");
}
@ -51,10 +53,28 @@ void wifiEventHandler(void* arg, esp_event_base_t event_base,
ESP_LOGI(TAG, "Connected to AP. Got IP: " IPSTR, IP2STR(&event->ip_info.ip));
retry_num = 0;
xEventGroupSetBits(wifi_event_group_, WIFI_CONNECTED_BIT);
xEventGroupSetBits(clock_core_event_group_, WIFI_CONNECTED_BIT);
}
}
void sntpEventHandler(struct timeval*)
{
xEventGroupSetBits(clock_core_event_group_, SNTP_SYNCED);
}
esp_err_t sntpInitialize()
{
esp_sntp_config_t config = ESP_NETIF_SNTP_DEFAULT_CONFIG("pool.ntp.org");
config.smooth_sync = true;
config.server_from_dhcp = true;
config.renew_servers_after_new_IP = true;
config.sync_cb = sntpEventHandler;
config.start = false;
config.ip_event_to_renew = IP_EVENT_STA_GOT_IP;
return esp_netif_sntp_init(&config);
}
std::expected<void, esp_err_t> wifiInitialize(const char* wifi_ssid, const char* wifi_password)
{
TRY_ESP(esp_netif_init());
@ -64,6 +84,8 @@ std::expected<void, esp_err_t> wifiInitialize(const char* wifi_ssid, const char*
wifi_init_config_t config = WIFI_INIT_CONFIG_DEFAULT();
TRY_ESP(esp_wifi_init(&config));
TRY_ESP(sntpInitialize());
esp_event_handler_instance_t instance_any_id;
esp_event_handler_instance_t instance_got_ip;
TRY_ESP(esp_event_handler_instance_register(WIFI_EVENT,
@ -99,7 +121,7 @@ namespace clock_core
void run(const char* wifi_ssid, const char* wifi_password)
{
wifi_event_group_ = xEventGroupCreate();
clock_core_event_group_ = xEventGroupCreate();
if (const auto res = wifiInitialize(wifi_ssid, wifi_password);
!res.has_value())
@ -108,24 +130,37 @@ void run(const char* wifi_ssid, const char* wifi_password)
abort();
}
const EventBits_t bits = xEventGroupWaitBits(wifi_event_group_,
WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
pdFALSE,
while (true)
{
const EventBits_t bits = xEventGroupWaitBits(clock_core_event_group_,
WIFI_CONNECTED_BIT | WIFI_FAIL_BIT | SNTP_SYNCED,
pdTRUE,
pdFALSE,
portMAX_DELAY);
if (bits & WIFI_CONNECTED_BIT)
{
ESP_LOGI(TAG, "WiFi setup in clock_core::run successful.");
if (const auto res = esp_netif_sntp_start();
res != ESP_OK)
{
ESP_LOGE(TAG, "SNTP setup failed. Error: %s", esp_err_to_name(res));
abort();
}
}
else if (bits & WIFI_FAIL_BIT)
{
ESP_LOGE(TAG, "WiFi setup in clock_core::run failed.");
}
else if (bits & SNTP_SYNCED)
{
ESP_LOGI(TAG, "SNTP sync successful.");
}
else
{
ESP_LOGE(TAG, "WiFi setup encountered an unexpected set of bits. %lu", bits);
}
}
}
}

View File

@ -44,7 +44,7 @@ extern "C" void app_main(void)
}
// @todo: Remove later.
provisioner->clearSettings();
// provisioner->clearSettings();
if (!provisioner->parametersAreConfigured())
{