92 lines
3.0 KiB
C
92 lines
3.0 KiB
C
#include "wifi.h"
|
|
#include "esp_event.h"
|
|
#include "esp_log.h"
|
|
#include "esp_sntp.h"
|
|
#include "esp_wifi.h"
|
|
#include "freertos/FreeRTOS.h"
|
|
#include <string.h>
|
|
|
|
static const char *TAG = "WIFI";
|
|
|
|
static EventGroupHandle_t s_wifi_event_group;
|
|
static bool ntp_time_obtained = false;
|
|
|
|
#define WIFI_CONNECTED_BIT (1 << 0)
|
|
|
|
bool has_ntp_time_obtained_once() { return ntp_time_obtained; }
|
|
|
|
static void event_handler(void *arg, esp_event_base_t event_base,
|
|
int32_t event_id, void *event_data) {
|
|
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
|
|
esp_wifi_connect();
|
|
ESP_LOGI(TAG, "connecting...");
|
|
} else if (event_base == WIFI_EVENT &&
|
|
event_id == WIFI_EVENT_STA_DISCONNECTED) {
|
|
esp_wifi_connect();
|
|
ESP_LOGI(TAG, "connect to the AP fail - trying to reconnect");
|
|
} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
|
|
ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data;
|
|
ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip));
|
|
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
|
|
}
|
|
}
|
|
|
|
void start_wifi() {
|
|
ESP_ERROR_CHECK(esp_netif_init());
|
|
|
|
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
|
esp_netif_create_default_wifi_sta();
|
|
|
|
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
|
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
|
|
|
s_wifi_event_group = xEventGroupCreate();
|
|
|
|
esp_event_handler_instance_t instance_any_id;
|
|
esp_event_handler_instance_t instance_got_ip;
|
|
ESP_ERROR_CHECK(esp_event_handler_instance_register(
|
|
WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL, &instance_any_id));
|
|
ESP_ERROR_CHECK(esp_event_handler_instance_register(
|
|
IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL, &instance_got_ip));
|
|
|
|
wifi_config_t wifi_config = {
|
|
.sta =
|
|
{
|
|
.ssid = CONFIG_WIFI_SSID,
|
|
.password = CONFIG_WIFI_PASSWORD,
|
|
.failure_retry_cnt = 20,
|
|
// TODO Figure out what I actually removed here
|
|
},
|
|
};
|
|
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
|
|
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
|
|
ESP_ERROR_CHECK(esp_wifi_start());
|
|
ESP_LOGI(TAG, "wifi_init_sta finished.");
|
|
|
|
// Wait for WiFi to connect
|
|
xEventGroupWaitBits(s_wifi_event_group, WIFI_CONNECTED_BIT, pdFALSE, pdFALSE,
|
|
portMAX_DELAY);
|
|
esp_sntp_setservername(0, "pool.ntp.org");
|
|
esp_sntp_setoperatingmode(SNTP_OPMODE_POLL);
|
|
sntp_set_sync_status(SNTP_SYNC_STATUS_RESET);
|
|
sntp_set_sync_mode(SNTP_SYNC_MODE_SMOOTH);
|
|
esp_sntp_init();
|
|
|
|
setenv("TZ", "GMT0BST,M3.5.0/1,M10.5.0", 1);
|
|
tzset();
|
|
|
|
for (uint32_t i = 0; i < 20; ++i) {
|
|
if (sntp_get_sync_status() != SNTP_SYNC_STATUS_COMPLETED) {
|
|
ESP_LOGI(TAG, "... waiting for time update");
|
|
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
|
}
|
|
}
|
|
|
|
time_t epoch;
|
|
time(&epoch);
|
|
struct tm *timeinfo = localtime(&epoch);
|
|
|
|
ESP_LOGI(TAG, "Current time: %i:%i", timeinfo->tm_hour, timeinfo->tm_min);
|
|
ntp_time_obtained = true;
|
|
}
|