#include <stdio.h>
#include "lwip/err.h"
#include "lwip/apps/sntp.h"
#include "driver/gpio.h"
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/event_groups.h"
#include "esp_system.h"
#include "esp_wifi.h"
#include "esp_event.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "mqtt_client.h"
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
#include <esp_sntp.h>
#define SSID "Wokwi-GUEST"
#define PASS ""
//const char *SSID = "Wokwi-GUEST";
//const char *PASS = "";
const char *TAG = "WIFI";
static void wifi_event_handler(void *event_handler_arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
{
switch (event_id)
{
case WIFI_EVENT_STA_START:
printf("WiFi connecting ... \n");
break;
case WIFI_EVENT_STA_CONNECTED:
printf("WiFi connected ... \n");
break;
case WIFI_EVENT_STA_DISCONNECTED:
printf("WiFi lost connection ... \n");
break;
case IP_EVENT_STA_GOT_IP:
printf("WiFi got IP ... \n\n");
break;
default:
break;
}
}
void wifi_connection()
{
esp_netif_init(); // TCP/IP initiation s1.1
esp_event_loop_create_default(); // event loop s1.2
esp_netif_create_default_wifi_sta(); // WiFi station s1.3
wifi_init_config_t wifi_initiation = WIFI_INIT_CONFIG_DEFAULT();
esp_wifi_init(&wifi_initiation); // s1.4
// 2 - Wi-Fi Configuration Phase
esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, wifi_event_handler, NULL);
esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, wifi_event_handler, NULL);
wifi_config_t wifi_configuration = {
.sta = {
.ssid = SSID,
.password = PASS}
};
//strcpy((char*)wifi_configuration.sta.ssid, SSID);
//ets_printf("HERE 4\n");
//strcpy((char*)wifi_configuration.sta.password, PASS);
//ets_printf("HERE 5\n");
esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_configuration);
// 3 - Wi-Fi Start Phase
esp_wifi_start();
esp_wifi_set_mode(WIFI_MODE_STA);
// 4- Wi-Fi Connect Phase
esp_wifi_connect();
ESP_LOGI(TAG, "wifi_init_softap finished. SSID:%s password:%s",SSID,PASS);
}
void app_main(void)
{
ets_printf("HERE\n");
nvs_flash_init();
ets_printf("HERE 2\n");
esp_netif_init();
ets_printf("HERE 3\n");
wifi_connection();
}