#include <stdio.h>
#include "esp_event.h"
#include <sys/time.h>
#include "esp_netif.h"
#include "esp_netif_sntp.h"
#include "esp_sntp.h"
#include "esp_wifi.h"
#include "nvs_flash.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "driver/gpio.h"
#define ESP_WIFI_SSID "Wokwi-GUEST" //Usário do Wifi
#define ESP_WIFI_PASS "" //Senha do Wifi
#define ESP_MAXIMUM_RETRY 10 //Máximo de tentativas para se conectar
#define WIFI_CONNECTED_BIT BIT0 //Conseguiu conectar
#define WIFI_FAIL_BIT BIT1 //Não conseguiu conectar
//Cria o handler de eventos para Wifi
static EventGroupHandle_t s_wifi_event_group;
struct timeval tempoAgora;
char strftime_buf[64];
struct tm timeinfo;
time_t now;
//Conta quantas vezes tentou se conectar
static int s_retry_num = 0;
//Função de event handler para o Wifi
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) {
//Conecta ao Wifi se o evento for "START"
esp_wifi_connect();
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
//Se o evento for "DISCONNECTED"
//Tenta conectar um número de vezes igual a ESP_MAXIMUM_RETRY
if (s_retry_num < ESP_MAXIMUM_RETRY) {
esp_wifi_connect();
s_retry_num++;
printf("retry to connect to the AP\n");
} else {
//Se não se conectar define como falha
xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
}
printf("connect to the AP fail\n");
} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
//Se o evento for "GOT_IP"
//Recupera o IP para o ESP
ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
printf("got ip:" IPSTR "\n", IP2STR(&event->ip_info.ip));
s_retry_num = 0;
//Define a conexão como sucesso
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
}
}
void app_main() {
//Inicialisando NVS (Non-Volatile Storage)
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
ESP_ERROR_CHECK(ret);
//Grupo de eventos para o Wifi
s_wifi_event_group = xEventGroupCreate();
//Inicializando NETIF
ESP_ERROR_CHECK(esp_netif_init()); //Incializando
ESP_ERROR_CHECK(esp_event_loop_create_default()); //Iniciando Loop
esp_netif_create_default_wifi_sta(); //Startup Code
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); //Config padrão para Wifi
ESP_ERROR_CHECK(esp_wifi_init(&cfg)); //Inicializando Wifi
//Criando handlers de eventos
esp_event_handler_instance_t instance_any_id;
esp_event_handler_instance_t instance_got_ip;
//Atribuindo handlers de eventos para "ESP_EVENT_ANY_ID"
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
ESP_EVENT_ANY_ID,
&event_handler,
NULL,
&instance_any_id));
//Atribuindo handlers de eventos para "ESP_EVENT_STA_GOT_IP"
ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,
IP_EVENT_STA_GOT_IP,
&event_handler,
NULL,
&instance_got_ip));
//Configurações para o WIFI
wifi_config_t wifi_config = {
.sta = {
.ssid = ESP_WIFI_SSID,
.password = ESP_WIFI_PASS,
},
};
//Inicializando o Wifi
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) );
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config) );
ESP_ERROR_CHECK(esp_wifi_start() );
esp_sntp_config_t sntp_config = ESP_NETIF_SNTP_DEFAULT_CONFIG("time.google.com");
sntp_config.start = false; // start SNTP service explicitly (after connecting)
sntp_config.server_from_dhcp = true; // accept NTP offers from DHCP server, if any (need to enable *before* connecting)
sntp_config.renew_servers_after_new_IP = true; // let esp-netif update configured SNTP server(s) after receiving DHCP lease
sntp_config.index_of_first_server = 1;
sntp_config.ip_event_to_renew = IP_EVENT_STA_GOT_IP;
esp_netif_sntp_init(&sntp_config);
esp_netif_sntp_start();
int retry = 0;
const int retry_count = 15;
while (esp_netif_sntp_sync_wait(2000 / portTICK_PERIOD_MS) == ESP_ERR_TIMEOUT && ++retry < retry_count) {
printf("Waiting for system time to be set... (%d/%d)\n", retry, retry_count);
}
while(1){
time(&now);
setenv("TZ", "UTC+3", 1);
tzset();
localtime_r(&now, &timeinfo);
//gettimeofday(&tempoAgora, NULL);
//localtime_r(&tempoAgora.tv_sec, &timeinfo);
strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo);
printf("The current date/time in Natal is: %s \n", strftime_buf);
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
//return 0;
}