Firmware: enable syncing to NTP
This commit is contained in:
parent
b42bb7efa9
commit
57d068ffad
@ -8,10 +8,11 @@
|
|||||||
|
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
|
||||||
#include <esp_netif.h>
|
|
||||||
#include <esp_event.h>
|
#include <esp_event.h>
|
||||||
#include <esp_wifi.h>
|
|
||||||
#include <esp_log.h>
|
#include <esp_log.h>
|
||||||
|
#include <esp_netif.h>
|
||||||
|
#include <esp_netif_sntp.h>
|
||||||
|
#include <esp_wifi.h>
|
||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
@ -20,7 +21,8 @@ const char* TAG = "clock_core";
|
|||||||
|
|
||||||
constexpr EventBits_t WIFI_FAIL_BIT = BIT0;
|
constexpr EventBits_t WIFI_FAIL_BIT = BIT0;
|
||||||
constexpr EventBits_t WIFI_CONNECTED_BIT = BIT1;
|
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,
|
void wifiEventHandler(void* arg, esp_event_base_t event_base,
|
||||||
int32_t event_id, void* event_data)
|
int32_t event_id, void* event_data)
|
||||||
@ -41,7 +43,7 @@ void wifiEventHandler(void* arg, esp_event_base_t event_base,
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
xEventGroupSetBits(wifi_event_group_, WIFI_FAIL_BIT);
|
xEventGroupSetBits(clock_core_event_group_, WIFI_FAIL_BIT);
|
||||||
}
|
}
|
||||||
ESP_LOGW(TAG, "Connection to AP failed.");
|
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));
|
ESP_LOGI(TAG, "Connected to AP. Got IP: " IPSTR, IP2STR(&event->ip_info.ip));
|
||||||
retry_num = 0;
|
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)
|
std::expected<void, esp_err_t> wifiInitialize(const char* wifi_ssid, const char* wifi_password)
|
||||||
{
|
{
|
||||||
TRY_ESP(esp_netif_init());
|
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();
|
wifi_init_config_t config = WIFI_INIT_CONFIG_DEFAULT();
|
||||||
TRY_ESP(esp_wifi_init(&config));
|
TRY_ESP(esp_wifi_init(&config));
|
||||||
|
|
||||||
|
TRY_ESP(sntpInitialize());
|
||||||
|
|
||||||
esp_event_handler_instance_t instance_any_id;
|
esp_event_handler_instance_t instance_any_id;
|
||||||
esp_event_handler_instance_t instance_got_ip;
|
esp_event_handler_instance_t instance_got_ip;
|
||||||
TRY_ESP(esp_event_handler_instance_register(WIFI_EVENT,
|
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)
|
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);
|
if (const auto res = wifiInitialize(wifi_ssid, wifi_password);
|
||||||
!res.has_value())
|
!res.has_value())
|
||||||
@ -108,23 +130,36 @@ void run(const char* wifi_ssid, const char* wifi_password)
|
|||||||
abort();
|
abort();
|
||||||
}
|
}
|
||||||
|
|
||||||
const EventBits_t bits = xEventGroupWaitBits(wifi_event_group_,
|
while (true)
|
||||||
WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
|
{
|
||||||
pdFALSE,
|
const EventBits_t bits = xEventGroupWaitBits(clock_core_event_group_,
|
||||||
pdFALSE,
|
WIFI_CONNECTED_BIT | WIFI_FAIL_BIT | SNTP_SYNCED,
|
||||||
portMAX_DELAY);
|
pdTRUE,
|
||||||
|
pdFALSE,
|
||||||
|
portMAX_DELAY);
|
||||||
|
|
||||||
if (bits & WIFI_CONNECTED_BIT)
|
if (bits & WIFI_CONNECTED_BIT)
|
||||||
{
|
{
|
||||||
ESP_LOGI(TAG, "WiFi setup in clock_core::run successful.");
|
ESP_LOGI(TAG, "WiFi setup in clock_core::run successful.");
|
||||||
}
|
if (const auto res = esp_netif_sntp_start();
|
||||||
else if (bits & WIFI_FAIL_BIT)
|
res != ESP_OK)
|
||||||
{
|
{
|
||||||
ESP_LOGE(TAG, "WiFi setup in clock_core::run failed.");
|
ESP_LOGE(TAG, "SNTP setup failed. Error: %s", esp_err_to_name(res));
|
||||||
}
|
abort();
|
||||||
else
|
}
|
||||||
{
|
}
|
||||||
ESP_LOGE(TAG, "WiFi setup encountered an unexpected set of bits. %lu", bits);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -44,7 +44,7 @@ extern "C" void app_main(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// @todo: Remove later.
|
// @todo: Remove later.
|
||||||
provisioner->clearSettings();
|
// provisioner->clearSettings();
|
||||||
|
|
||||||
if (!provisioner->parametersAreConfigured())
|
if (!provisioner->parametersAreConfigured())
|
||||||
{
|
{
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user