Added SNTP and start of data logging
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
idf_component_register(SRCS "wifi.c"
|
||||
INCLUDE_DIRS "."
|
||||
REQUIRES driver esp_wifi
|
||||
REQUIRES driver esp_wifi lwip
|
||||
)
|
||||
|
||||
@@ -1,17 +1,28 @@
|
||||
#include "wifi.h"
|
||||
#include <string.h>
|
||||
|
||||
#include "esp_system.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "esp_sntp.h"
|
||||
#include "esp_wifi.h"
|
||||
#include "esp_event.h"
|
||||
#include "esp_log.h"
|
||||
|
||||
static const char* TAG = "WIFI";
|
||||
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_wifi_connect();
|
||||
ESP_LOGI(TAG, "connecting...");
|
||||
}
|
||||
else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
|
||||
esp_wifi_connect();
|
||||
@@ -20,11 +31,11 @@ static void event_handler(void* arg, esp_event_base_t event_base,
|
||||
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());
|
||||
@@ -33,6 +44,8 @@ void start_wifi() {
|
||||
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,
|
||||
@@ -58,4 +71,30 @@ void start_wifi() {
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -1,3 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdbool.h>
|
||||
// The idea with this function is to check if NTP time has been obtained
|
||||
// at least once - and therefore time is at least vaguely accurate
|
||||
// I want to call this when saving data to know whether the time is valid/trusted
|
||||
// at all
|
||||
bool has_ntp_time_obtained_once();
|
||||
|
||||
// Probably should rename this to "start networking" or something because
|
||||
// it also does NTP stuff, and maybe even establish the HTTP Server stuff?
|
||||
void start_wifi();
|
||||
|
||||
Reference in New Issue
Block a user