I am trying to wright a code to connect my esp32 to a wifi station,
But it dont seam to work since it gives back the “failed to connect error” and i dont see it in my dhcp range on my access point
Here is my code :
#include <stdio.h>
#include <esp_wifi.h>
#include <esp_log.h>
#include <esp_system.h>
#include <nvs_flash.h> // to flash the memory (Non volatile sorage = NVS)
#include <string.h>
#include <esp_check.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#define WIFI_FAILURE 1<<1
#define WIFI_SUCCESS 1<<0
#define TAG "Wifi :"
static void wifi_event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data){
// wifi station start event
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START){
ESP_LOGI(TAG, "connectiong to AP ....");
esp_wifi_connect();
// event trigerred when the wifi sta start
// esp_wifi_connect is use to initialise the wifi connexion
}
// wifi station disconnect event
else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED){
for (int i=0; i < 10; i++){
esp_wifi_connect();
ESP_LOGI(TAG, "connecting to AP .....");
}
ESP_LOGI(TAG, "failed to connect");
// trigerred when the esp is disconnected from the esp
// here esp_wifi_connect is used to reconnect and maintain a stable connexion
}
}
static void ip_event_handler(void* asrg, esp_event_base_t event_base, int32_t event_id, void* event_data){
// IP got event
if (event_base == WIFI_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
ESP_LOGI("WIFI_EVENT", "got ip:" IPSTR, IP2STR(&event->ip_info.ip));
// is trigerred when the esp get an ip via DHCP
// the ip adresse is extracted from event data by using ESP_LOGI, it make sur that the esp has obtain an ip adress
}
}
esp_err_t conf_wifi (){
// initialise the networking interface
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default()); // create the event that will be used by wifi to handle the events
// create wifi sta in the wifi drivre
esp_netif_create_default_wifi_sta();
// define the wifi conf with default conf
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
//initialise the wifi module with the conf that we defined before
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
// event handler register, it save it in the event handler from esp-idf and link them to the spesified callback func
esp_event_handler_instance_t wifi_handler_event_instance;
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
ESP_EVENT_ANY_ID,
&wifi_event_handler,
NULL,
&wifi_handler_event_instance)); // save every WIFI_EVENT in event_handler, event_handler will be called for each wifi event
esp_event_handler_instance_t got_ip_event_instance;
ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,
IP_EVENT_STA_GOT_IP,
&ip_event_handler,
NULL,
&got_ip_event_instance));// save every IP_EVENT in event handler, call for ip event
// define and set the wifi conf
wifi_config_t wifi_config_sta = {
// use to connect to a station
.sta ={
.ssid = "",
.password = "",
.threshold.authmode = WIFI_AUTH_WPA2_PSK,
.pmf_cfg = {
.capable = true,
.required = false
},
},
};
//define the wifi mod
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
// set sta conf
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config_sta));
// set ap conf (for later)
// start wifi
ESP_ERROR_CHECK(esp_wifi_start());
ESP_LOGI(TAG, "STA conf initialized");
return WIFI_SUCCESS;
}
void app_main(void) {
// initialise nvs
esp_err_t ret = nvs_flash_init(); // is use to check if an error occure and then init the nvs flash
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();
// if the nvs is not blank erase what is written in it
}
ESP_ERROR_CHECK(ret);
// check if something is going wronge while connecting to the ap
esp_err_t wifi_status = WIFI_FAILURE;
wifi_status = conf_wifi();
if (WIFI_SUCCESS != wifi_status){
ESP_LOGI("WIFI ", "Failed to connect to the sta, dying ....");
return;
} else printf("Error 1");
}
i changed a lot the code since the first writting and separed the wifi_event_handler from the ip_event_handler
I am looking for help on understanding what is wrong with what i did and being able to fix it to understand how wifi work with the esp-idf API and going forward in my project