latest code

Signed-off-by: Peter Siegmund <peter@rdkr.com>
This commit is contained in:
Peter Siegmund
2024-09-06 16:29:56 +02:00
parent d1f4beed3b
commit 7774417a35
19 changed files with 8002 additions and 12422 deletions

View File

@@ -1 +1,3 @@
BasedOnStyle: Chromium BasedOnStyle: Chromium
ColumnLimit: 120
IndentWidth: 4

View File

@@ -1,6 +1,8 @@
idf_component_register(SRCS "connectivity.c" "ntp.c" idf_component_register(SRCS "networking.c" "connectivity.c" "sntp_utils.c"
INCLUDE_DIRS "include" INCLUDE_DIRS "include"
PRIV_REQUIRES PRIV_REQUIRES
esp_event esp_event
esp_http_client
esp_wifi esp_wifi
json
nvs_flash) nvs_flash)

View File

@@ -0,0 +1 @@
#pragma once

View File

@@ -5,7 +5,7 @@
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
esp_err_t init_ntp(void); esp_err_t init_sntp(void);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@@ -0,0 +1,81 @@
#include "networking.h"
#include <string.h>
#include "cJSON.h"
#include "esp_err.h"
#include "esp_http_client.h"
#include "esp_log.h"
static const char* TAG = "JSON";
esp_err_t download_and_parse_json(const char* url) {
// 1. Create HTTP Client
esp_http_client_config_t config = {
.url = url,
.event_handler = NULL, // You can add an event handler for more control
};
esp_http_client_handle_t client = esp_http_client_init(&config);
if (client == NULL) {
ESP_LOGE(TAG, "Failed to create HTTP client");
return ESP_FAIL;
}
// 2. Perform GET request
esp_err_t err = esp_http_client_perform(client);
if (err != ESP_OK) {
ESP_LOGE(TAG, "HTTP GET request failed: %s", esp_err_to_name(err));
esp_http_client_cleanup(client);
return err;
}
// 3. Check response status code
int status_code = esp_http_client_get_status_code(client);
if (status_code != 200) {
ESP_LOGE(TAG, "HTTP GET request failed with status code: %d", status_code);
esp_http_client_cleanup(client);
return ESP_FAIL;
}
// 4. Read response content
int content_length = esp_http_client_get_content_length(client);
char* response_buffer = malloc(content_length + 1);
if (response_buffer == NULL) {
ESP_LOGE(TAG, "Failed to allocate memory for response buffer");
esp_http_client_cleanup(client);
return ESP_FAIL;
}
int data_read = esp_http_client_read_response(client, response_buffer, content_length);
if (data_read != content_length) {
ESP_LOGE(TAG, "Failed to read complete response");
free(response_buffer);
esp_http_client_cleanup(client);
return ESP_FAIL;
}
response_buffer[data_read] = '\0'; // Null-terminate the string
// 5. Parse JSON
cJSON* root = cJSON_Parse(response_buffer);
if (root == NULL) {
const char* error_ptr = cJSON_GetErrorPtr();
if (error_ptr != NULL) {
ESP_LOGE(TAG, "Error before: %s", error_ptr);
}
free(response_buffer);
esp_http_client_cleanup(client);
return ESP_FAIL;
}
// 6. Access JSON data
// Example: Print the value of a key called "message"
cJSON* message_item = cJSON_GetObjectItem(root, "message");
if (cJSON_IsString(message_item) && (message_item->valuestring != NULL)) {
ESP_LOGI(TAG, "Message: %s", message_item->valuestring);
}
// 7. Clean up
cJSON_Delete(root);
free(response_buffer);
esp_http_client_cleanup(client);
return ESP_OK;
}

View File

@@ -1,76 +0,0 @@
#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

@@ -0,0 +1,63 @@
#include "sntp_utils.h"
#include <string.h>
#include <sys/time.h>
#include <time.h>
#include "esp_attr.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
int retry = 0;
const int retry_count = 15;
while (esp_netif_sntp_sync_wait(pdMS_TO_TICKS(2000)) == ESP_ERR_TIMEOUT && ++retry < retry_count) {
ESP_LOGI(TAG, "Waiting for system time to be set... (%d/%d)", retry, retry_count);
}
esp_netif_sntp_deinit();
}
esp_err_t init_sntp(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];
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,2 +1,2 @@
idf_component_register(SRCS "fota.c" idf_component_register(SRCS "fota.c"
INCLUDE_DIRS ".") INCLUDE_DIRS "include")

View File

@@ -1,7 +1,6 @@
#include <stdio.h>
#include "fota.h" #include "fota.h"
#include <stdio.h>
void func(void) void func(void) {
{ ///
} }

View File

@@ -1 +1,3 @@
#pragma once
void func(void); void func(void);

File diff suppressed because it is too large Load Diff

View File

@@ -30,8 +30,7 @@ void drawString(int32_t x, int32_t y, std::string text, alignment align) {
x = x - w / 2; x = x - w / 2;
int32_t cursor_y = y + h; int32_t cursor_y = y + h;
int32_t padding = 4; int32_t padding = 4;
epd_fill_rect(x - padding, y - padding, w + 2 * padding, h + 2 * padding, 255, epd_fill_rect(x - padding, y - padding, w + 2 * padding, h + 2 * padding, 255, framebuffer);
framebuffer);
write_string(&currentFont, data, &x, &cursor_y, framebuffer); write_string(&currentFont, data, &x, &cursor_y, framebuffer);
} }
@@ -44,8 +43,7 @@ void drawVessel(int32_t x, int32_t y, std::string vesselName) {
} }
void mapView(void* args) { void mapView(void* args) {
framebuffer = framebuffer = (uint8_t*)heap_caps_malloc(EPD_WIDTH * EPD_HEIGHT / 2, MALLOC_CAP_SPIRAM);
(uint8_t*)heap_caps_malloc(EPD_WIDTH * EPD_HEIGHT / 2, MALLOC_CAP_SPIRAM);
if (!framebuffer) { if (!framebuffer) {
printf("alloc memory failed !!!"); printf("alloc memory failed !!!");
@@ -60,8 +58,7 @@ void mapView(void* args) {
int32_t height = staticmap_height; int32_t height = staticmap_height;
const uint8_t* data = staticmap_data; const uint8_t* data = staticmap_data;
Rect_t area = { Rect_t area = {.x = EPD_WIDTH - width, .y = 0, .width = width, .height = height};
.x = EPD_WIDTH - width, .y = 0, .width = width, .height = height};
epd_copy_to_framebuffer(area, (uint8_t*)data, framebuffer); epd_copy_to_framebuffer(area, (uint8_t*)data, framebuffer);
epd_draw_rect(area.x, area.y, area.width, area.height, 0, framebuffer); epd_draw_rect(area.x, area.y, area.width, area.height, 0, framebuffer);
@@ -78,10 +75,9 @@ void mapView(void* args) {
time(&now); time(&now);
localtime_r(&now, &timeinfo); localtime_r(&now, &timeinfo);
strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo); strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo);
drawString(6, area.height - 33, strftime_buf, LEFT); drawString(area.x / 2, area.height - 33, strftime_buf, CENTER);
drawVessel(EPD_WIDTH - EPD_WIDTH / 4, EPD_HEIGHT - EPD_HEIGHT / 5, drawVessel(EPD_WIDTH - EPD_WIDTH / 4, EPD_HEIGHT - EPD_HEIGHT / 5, "DEEPENSCHRIEWER 1");
"DEEPENSCHRIEWER 1");
epd_poweron(); epd_poweron();
epd_clear(); epd_clear();

View File

@@ -1,5 +1,5 @@
idf_component_register(SRCS "smartconfig.cpp" idf_component_register(SRCS "smartconfig.cpp"
INCLUDE_DIRS "." INCLUDE_DIRS "include"
PRIV_REQUIRES PRIV_REQUIRES
nvs_flash nvs_flash
esp_wifi esp_wifi

View File

@@ -29,14 +29,10 @@ static const char* TAG = "smartconfig";
static void smartconfig_task(void* parm); static void smartconfig_task(void* parm);
static void event_handler(void* arg, static void event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) {
esp_event_base_t event_base,
int32_t event_id,
void* event_data) {
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) { if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
xTaskCreate(smartconfig_task, "smartconfig_task", 4096, NULL, 3, NULL); xTaskCreate(smartconfig_task, "smartconfig_task", 4096, NULL, 3, NULL);
} else if (event_base == WIFI_EVENT && } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
event_id == WIFI_EVENT_STA_DISCONNECTED) {
esp_wifi_connect(); esp_wifi_connect();
xEventGroupClearBits(s_wifi_event_group, CONNECTED_BIT); xEventGroupClearBits(s_wifi_event_group, CONNECTED_BIT);
} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) { } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
@@ -48,15 +44,13 @@ static void event_handler(void* arg,
} else if (event_base == SC_EVENT && event_id == SC_EVENT_GOT_SSID_PSWD) { } else if (event_base == SC_EVENT && event_id == SC_EVENT_GOT_SSID_PSWD) {
ESP_LOGI(TAG, "Got SSID and password"); ESP_LOGI(TAG, "Got SSID and password");
smartconfig_event_got_ssid_pswd_t* evt = smartconfig_event_got_ssid_pswd_t* evt = (smartconfig_event_got_ssid_pswd_t*)event_data;
(smartconfig_event_got_ssid_pswd_t*)event_data;
wifi_config_t wifi_config; wifi_config_t wifi_config;
uint8_t rvd_data[33] = {0}; uint8_t rvd_data[33] = {0};
bzero(&wifi_config, sizeof(wifi_config_t)); bzero(&wifi_config, sizeof(wifi_config_t));
memcpy(wifi_config.sta.ssid, evt->ssid, sizeof(wifi_config.sta.ssid)); memcpy(wifi_config.sta.ssid, evt->ssid, sizeof(wifi_config.sta.ssid));
memcpy(wifi_config.sta.password, evt->password, memcpy(wifi_config.sta.password, evt->password, sizeof(wifi_config.sta.password));
sizeof(wifi_config.sta.password));
if (evt->type == SC_TYPE_ESPTOUCH_V2) { if (evt->type == SC_TYPE_ESPTOUCH_V2) {
ESP_ERROR_CHECK(esp_smartconfig_get_rvd_data(rvd_data, sizeof(rvd_data))); ESP_ERROR_CHECK(esp_smartconfig_get_rvd_data(rvd_data, sizeof(rvd_data)));
@@ -85,12 +79,9 @@ static void initialise_wifi(void) {
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg)); ESP_ERROR_CHECK(esp_wifi_init(&cfg));
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL));
&event_handler, NULL)); ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL));
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, ESP_ERROR_CHECK(esp_event_handler_register(SC_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL));
&event_handler, NULL));
ESP_ERROR_CHECK(esp_event_handler_register(SC_EVENT, ESP_EVENT_ANY_ID,
&event_handler, NULL));
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
ESP_ERROR_CHECK(esp_wifi_start()); ESP_ERROR_CHECK(esp_wifi_start());
@@ -102,9 +93,7 @@ static void smartconfig_task(void* parm) {
smartconfig_start_config_t cfg = SMARTCONFIG_START_CONFIG_DEFAULT(); smartconfig_start_config_t cfg = SMARTCONFIG_START_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_smartconfig_start(&cfg)); ESP_ERROR_CHECK(esp_smartconfig_start(&cfg));
while (1) { while (1) {
uxBits = xEventGroupWaitBits(s_wifi_event_group, uxBits = xEventGroupWaitBits(s_wifi_event_group, CONNECTED_BIT | ESPTOUCH_DONE_BIT, true, false, portMAX_DELAY);
CONNECTED_BIT | ESPTOUCH_DONE_BIT, true, false,
portMAX_DELAY);
if (uxBits & CONNECTED_BIT) { if (uxBits & CONNECTED_BIT) {
ESP_LOGI(TAG, "WiFi Connected to ap"); ESP_LOGI(TAG, "WiFi Connected to ap");
} }

View File

@@ -4,14 +4,13 @@
#include "connectivity.h" #include "connectivity.h"
#include "epd_driver.h" #include "epd_driver.h"
#include "fonts/opensans16.h" #include "fonts/opensans16.h"
#include "ntp.h"
#include "nvs_flash.h" #include "nvs_flash.h"
#include "sdkconfig.h" #include "sdkconfig.h"
#include "sntp_utils.h"
void nvs_init(void) { void nvs_init(void) {
esp_err_t ret = nvs_flash_init(); esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK(nvs_flash_erase()); ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init(); ret = nvs_flash_init();
} }
@@ -33,8 +32,7 @@ void wifi_init(void) {
epd_clear(); epd_clear();
x = 100; x = 100;
y = 100; y = 100;
snprintf(buffer, bufferSize, "Failed to connect to WiFi: %s", snprintf(buffer, bufferSize, "Failed to connect to WiFi: %s", esp_err_to_name(ret));
esp_err_to_name(ret));
writeln(&OpenSans16, buffer, &x, &y, NULL); writeln(&OpenSans16, buffer, &x, &y, NULL);
epd_poweroff(); epd_poweroff();
while (1) { while (1) {
@@ -50,16 +48,15 @@ void time_init(void) {
int32_t y = 100; int32_t y = 100;
const int16_t bufferSize = 200; const int16_t bufferSize = 200;
char buffer[bufferSize]; char buffer[bufferSize];
snprintf(buffer, bufferSize, "Loading NTP Data..."); snprintf(buffer, bufferSize, "Loading SNTP Data...");
writeln(&OpenSans16, buffer, &x, &y, NULL); writeln(&OpenSans16, buffer, &x, &y, NULL);
esp_err_t ret = init_ntp(); esp_err_t ret = init_sntp();
if (ret != ESP_OK) { if (ret != ESP_OK) {
epd_clear(); epd_clear();
x = 100; x = 100;
y = 100; y = 100;
snprintf(buffer, bufferSize, "Failed to get NTP data and timezone: %s", snprintf(buffer, bufferSize, "Failed to get NTP data and timezone: %s", esp_err_to_name(ret));
esp_err_to_name(ret));
writeln(&OpenSans16, buffer, &x, &y, NULL); writeln(&OpenSans16, buffer, &x, &y, NULL);
epd_poweroff(); epd_poweroff();
while (1) { while (1) {

View File

@@ -12,7 +12,7 @@ dependencies:
idf: idf:
source: source:
type: idf type: idf
version: 5.2.2 version: 5.3.0
direct_dependencies: direct_dependencies:
- espressif/zlib - espressif/zlib
- idf - idf