@@ -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"
|
||||
PRIV_REQUIRES
|
||||
esp_event
|
||||
esp_http_client
|
||||
esp_wifi
|
||||
json
|
||||
nvs_flash)
|
||||
|
@@ -0,0 +1 @@
|
||||
#pragma once
|
@@ -5,7 +5,7 @@
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
esp_err_t init_ntp(void);
|
||||
esp_err_t init_sntp(void);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
81
ePaper-ESP-IDF/components/connectivity/networking.c
Normal file
81
ePaper-ESP-IDF/components/connectivity/networking.c
Normal 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;
|
||||
}
|
@@ -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;
|
||||
}
|
63
ePaper-ESP-IDF/components/connectivity/sntp_utils.c
Normal file
63
ePaper-ESP-IDF/components/connectivity/sntp_utils.c
Normal 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;
|
||||
}
|
Reference in New Issue
Block a user