loading date/time via SNTP

Signed-off-by: Peter Siegmund <peter@rdkr.com>
This commit is contained in:
Peter Siegmund
2024-08-27 21:57:24 +02:00
parent 79612d43a7
commit 2d9930ca9c
11 changed files with 188 additions and 34 deletions

View File

@@ -1,2 +1,3 @@
download_map:
curl -o ./data/staticmap.png https://maps.googleapis.com/maps/api/staticmap\?center\=53.541962,%209.993402\&zoom\=15\&size\=540x540\&key\=AIzaSyARgP_FKFXsrcgVd_HVWoIfH5N8-a88wlQ\&map_id\=2f371c2346218fe8
python imgconvert.py -i ./data/staticmap.png -o components/mapView/include/staticmap.h -n staticmap

View File

@@ -1,5 +1,6 @@
idf_component_register(SRCS "connectivity.c"
idf_component_register(SRCS "connectivity.c" "ntp.c"
INCLUDE_DIRS "include"
PRIV_REQUIRES
esp_event
esp_wifi)
esp_wifi
nvs_flash)

View File

@@ -0,0 +1,11 @@
#pragma once
#include "esp_err.h"
#ifdef __cplusplus
extern "C" {
#endif
esp_err_t init_ntp(void);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,76 @@
#include "ntp.h"
#include <string.h>
#include <sys/time.h>
#include <time.h>
#include "esp_attr.h"
#include "esp_event.h"
#include "esp_log.h"
#include "esp_netif_sntp.h"
#include "esp_sleep.h"
#include "esp_sntp.h"
#include "esp_system.h"
#include "lwip/ip_addr.h"
#include "nvs_flash.h"
#include "sdkconfig.h"
static const char* TAG = "NTP";
void obtain_time(void) {
ESP_LOGI(TAG, "Initializing SNTP");
esp_sntp_config_t config =
ESP_NETIF_SNTP_DEFAULT_CONFIG(CONFIG_SNTP_TIME_SERVER);
config.start = false;
config.server_from_dhcp = true;
config.renew_servers_after_new_IP = true;
config.index_of_first_server = 1;
config.ip_event_to_renew = IP_EVENT_STA_GOT_IP;
esp_netif_sntp_init(&config);
ESP_LOGI(TAG, "Starting SNTP");
esp_netif_sntp_start();
// wait for time to be set
time_t now = 0;
struct tm timeinfo = {0};
int retry = 0;
const int retry_count = 15;
while (esp_netif_sntp_sync_wait(2000 / portTICK_PERIOD_MS) ==
ESP_ERR_TIMEOUT &&
++retry < retry_count) {
ESP_LOGI(TAG, "Waiting for system time to be set... (%d/%d)", retry,
retry_count);
}
time(&now);
localtime_r(&now, &timeinfo);
esp_netif_sntp_deinit();
}
esp_err_t init_ntp(void) {
time_t now;
struct tm timeinfo;
time(&now);
localtime_r(&now, &timeinfo);
if (timeinfo.tm_year < (2016 - 1900)) {
ESP_LOGI(
TAG,
"Time is not set yet. Connecting to WiFi and getting time over NTP.");
obtain_time();
}
char strftime_buf[64];
// Set timezone to Eastern Standard Time and print local time
setenv("TZ", "CET-1CEST,M3.5.0,M10.5.0/3", 1);
tzset();
time(&now);
localtime_r(&now, &timeinfo);
strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo);
ESP_LOGI(TAG, "The current date/time in Berlin/Europe is: %s", strftime_buf);
return ESP_OK;
}

View File

@@ -1 +0,0 @@
GET https://ipapi.co/json

View File

@@ -1,9 +1,12 @@
#include "mapView.h"
#include <esp_task_wdt.h>
#include <sys/time.h>
#include <time.h>
#include <cstring>
#include <string>
#include "epd_driver.h"
#include "fonts/opensans12b.h"
#include "fonts/opensans26b.h"
#include "fonts/opensans6.h"
#include "staticmap.h"
@@ -52,29 +55,39 @@ void mapView(void* args) {
std::memset(framebuffer, 0xFF, EPD_WIDTH * EPD_HEIGHT / 2);
int32_t width = staticmap_width;
int32_t height = staticmap_height;
const uint8_t* data = staticmap_data;
Rect_t area = {
.x = EPD_WIDTH - width, .y = 0, .width = width, .height = height};
epd_copy_to_framebuffer(area, (uint8_t*)data, framebuffer);
epd_draw_rect(area.x, area.y, area.width, area.height, 0, framebuffer);
int32_t x = area.x / 2;
int32_t y = area.height / 2 - 26;
currentFont = OpenSans26B;
drawString(x, y, "Hello, World!", CENTER);
drawVessel(EPD_WIDTH - EPD_WIDTH / 4, EPD_HEIGHT - EPD_HEIGHT / 5,
"DEEPENSCHRIEWER 1");
epd_poweron();
epd_clear();
epd_draw_grayscale_image(epd_full_screen(), framebuffer);
epd_poweroff_all();
while (1) {
vTaskDelay(pdMS_TO_TICKS(100));
int32_t width = staticmap_width;
int32_t height = staticmap_height;
const uint8_t* data = staticmap_data;
Rect_t area = {
.x = EPD_WIDTH - width, .y = 0, .width = width, .height = height};
epd_copy_to_framebuffer(area, (uint8_t*)data, framebuffer);
epd_draw_rect(area.x, area.y, area.width, area.height, 0, framebuffer);
int32_t x = area.x / 2;
int32_t y = 26;
currentFont = OpenSans26B;
drawString(x, y, "Tide Display", CENTER);
time_t now;
struct tm timeinfo;
char strftime_buf[64];
currentFont = OpenSans12B;
time(&now);
localtime_r(&now, &timeinfo);
strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo);
drawString(6, area.height - 33, strftime_buf, LEFT);
drawVessel(EPD_WIDTH - EPD_WIDTH / 4, EPD_HEIGHT - EPD_HEIGHT / 5,
"DEEPENSCHRIEWER 1");
epd_poweron();
epd_clear();
epd_draw_grayscale_image(epd_full_screen(), framebuffer);
epd_poweroff_all();
vTaskDelay(pdMS_TO_TICKS(60000));
}
}

View File

@@ -1,8 +1,10 @@
#include "splash_screen.h"
#include <esp_task_wdt.h>
#include <stdio.h>
#include "connectivity.h"
#include "epd_driver.h"
#include "fonts/opensans16.h"
#include "ntp.h"
#include "nvs_flash.h"
#include "sdkconfig.h"
@@ -17,14 +19,15 @@ void nvs_init(void) {
}
void wifi_init(void) {
epd_poweron();
epd_clear();
int32_t x = 100;
int32_t y = 100;
const int16_t bufferSize = 200;
char buffer[bufferSize];
snprintf(buffer, bufferSize, "Connecting to SSDI: %s", CONFIG_WIFI_SSID);
writeln(&OpenSans16, buffer, &x, &y, NULL);
esp_err_t ret = init_wifi();
if (ret != ESP_OK) {
epd_clear();
@@ -35,14 +38,44 @@ void wifi_init(void) {
writeln(&OpenSans16, buffer, &x, &y, NULL);
epd_poweroff();
while (1) {
///
vTaskDelay(pdMS_TO_TICKS(100));
};
}
}
void time_init(void) {
epd_clear();
int32_t x = 100;
int32_t y = 100;
const int16_t bufferSize = 200;
char buffer[bufferSize];
snprintf(buffer, bufferSize, "Loading NTP Data...");
writeln(&OpenSans16, buffer, &x, &y, NULL);
esp_err_t ret = init_ntp();
if (ret != ESP_OK) {
epd_clear();
x = 100;
y = 100;
snprintf(buffer, bufferSize, "Failed to get NTP data and timezone: %s",
esp_err_to_name(ret));
writeln(&OpenSans16, buffer, &x, &y, NULL);
epd_poweroff();
while (1) {
vTaskDelay(pdMS_TO_TICKS(100));
};
}
epd_poweroff();
}
void splash_screen(void) {
epd_init();
nvs_init();
epd_poweron();
wifi_init();
time_init();
epd_poweroff();
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 40 KiB

View File

@@ -1,15 +1,21 @@
dependencies:
espressif/zlib:
component_hash: 999ec50086ac1c82b8321d8f540dc9fd10f5622948b935558aa16b4b66e95d9d
dependencies:
- name: idf
require: private
version: '>=4.4'
source:
service_url: https://api.components.espressif.com/
registry_url: https://components.espressif.com/
type: service
version: 1.3.0
idf:
component_hash: null
source:
type: idf
version: 5.2.1
manifest_hash: 9fe7e2c77ea407d47f07333832a5be5b87fc9871227faa74633b17f8cd0fd0c1
version: 5.2.2
direct_dependencies:
- espressif/zlib
- idf
manifest_hash: 7fd1b00bda391a50c91df72443d14b974ce16be6bfa2efdabe51083d3a6e3185
target: esp32s3
version: 1.0.0
version: 2.0.0

View File

@@ -21,6 +21,14 @@ menu "Tide Display Setting"
inexistent.
endmenu
menu "Time Configuration"
config SNTP_TIME_SERVER
string "SNTP server name"
default "pool.ntp.org"
help
Hostname of the main SNTP server.
endmenu
menu "Map"
choice "Map Provider"
prompt "Map Provider"

View File

@@ -20,6 +20,12 @@ CONFIG_SPIRAM_MODE_OCT=y
CONFIG_PARTITION_TABLE_CUSTOM=y
CONFIG_PARTITION_TABLE_FILENAME="partitions.csv"
#
# SNTP
#
CONFIG_LWIP_DHCP_GET_NTP_SRV=y
CONFIG_LWIP_DHCP_MAX_NTP_SERVERS=1
#
# ESP System Settings
#