Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
baf1144351 |
No files matched your search
+723
-518
File diff suppressed because it is too large.
Load diff
+31
-4
@@ -1,8 +1,10 @@
|
||||
#include "config.h"
|
||||
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "wifi.h"
|
||||
|
||||
#include "config.h"
|
||||
#include "esp_sntp.h"
|
||||
#include "esp_netif_sntp.h"
|
||||
|
||||
#define LOG_LOCAL_LEVEL ESP_LOG_VERBOSE
|
||||
#include "esp_log.h"
|
||||
@@ -11,7 +13,31 @@ static const char* TAG = "Main";
|
||||
void print_task(void *pvParameters) {
|
||||
while (1) {
|
||||
ESP_LOGI(TAG, "here");
|
||||
vTaskDelay(pdMS_TO_TICKS(500));
|
||||
vTaskDelay(pdMS_TO_TICKS(5000));
|
||||
}
|
||||
}
|
||||
|
||||
void sync_time() {
|
||||
setenv("TZ", TIMEZONE, 1);
|
||||
tzset();
|
||||
|
||||
ESP_LOGI(TAG, "Syncing time with NTP server...");
|
||||
esp_sntp_config_t config = ESP_NETIF_SNTP_DEFAULT_CONFIG("pool.ntp.org");
|
||||
esp_netif_sntp_init(&config);
|
||||
|
||||
if (esp_netif_sntp_sync_wait(pdMS_TO_TICKS(10000)) != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to update system time within 10s timeout");
|
||||
}
|
||||
|
||||
struct tm timeinfo;
|
||||
time_t now;
|
||||
|
||||
time(&now);
|
||||
|
||||
if (localtime_r(&now, &timeinfo) != NULL) {
|
||||
ESP_LOGI(TAG, "Time synchronized: %s", asctime(&timeinfo));
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Failed to obtain time");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,9 +51,10 @@ void app_main() {
|
||||
|
||||
ESP_ERROR_CHECK(ret);
|
||||
|
||||
|
||||
xTaskCreate(print_task, "print_task", 4096, NULL, 1, NULL);
|
||||
wifi_init_sta();
|
||||
vTaskDelay(pdMS_TO_TICKS(1000)); // Wait for WiFi to connect and get IP
|
||||
sync_time();
|
||||
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
+12
-5
@@ -1,5 +1,6 @@
|
||||
#include "wifi.h"
|
||||
#include "secrets.h"
|
||||
#include "config.h"
|
||||
|
||||
/* WiFi station Example
|
||||
|
||||
@@ -51,10 +52,16 @@ void wifi_init_sta(void) {
|
||||
ESP_ERROR_CHECK(esp_netif_init());
|
||||
|
||||
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
||||
esp_netif_create_default_wifi_sta();
|
||||
esp_netif_t *sta_netif = esp_netif_create_default_wifi_sta();
|
||||
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
||||
|
||||
#ifndef ESP_HOSTNAME
|
||||
#error "ESP_HOSTNAME is not defined in config.h"
|
||||
#endif
|
||||
|
||||
esp_err_t err = esp_netif_set_hostname(sta_netif, ESP_HOSTNAME);
|
||||
|
||||
esp_event_handler_instance_t instance_any_id;
|
||||
esp_event_handler_instance_t instance_got_ip;
|
||||
@@ -96,11 +103,11 @@ void wifi_init_sta(void) {
|
||||
/* xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually
|
||||
* happened. */
|
||||
if (bits & WIFI_CONNECTED_BIT) {
|
||||
ESP_LOGI(TAG, "connected to ap SSID:%s password:%s",
|
||||
ESP_WIFI_SSID, ESP_WIFI_PASS);
|
||||
ESP_LOGI(TAG, "connected to ap SSID:%s.",
|
||||
ESP_WIFI_SSID);
|
||||
} else if (bits & WIFI_FAIL_BIT) {
|
||||
ESP_LOGI(TAG, "Failed to connect to SSID:%s, password:%s",
|
||||
ESP_WIFI_SSID, ESP_WIFI_PASS);
|
||||
ESP_LOGI(TAG, "Failed to connect to SSID:%s.",
|
||||
ESP_WIFI_SSID);
|
||||
} else {
|
||||
ESP_LOGE(TAG, "UNEXPECTED EVENT");
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user