ESP32-S2 station example SSID as variable instead of constant

wifibeginner
Posts: 5
Joined: Fri May 13, 2022 9:22 am

ESP32-S2 station example SSID as variable instead of constant

Postby wifibeginner » Fri May 13, 2022 10:05 am

ESP IDF v4.4
Using Espressif IDE with C

Hi,
I've been attempting to modify the esp idf station example (examples\wifi\getting_started\station) to define 'EXAMPLE_ESP_WIFI_SSID' as a variable rather than as a preprocessor but have had no luck. I use powershell to compile and flash the board, and when it is not defined using "#define", it will not connect.

Defining the ssid though "const char* EXAMPLE_ESP_WIFI_SSID ="ssidname";", PowerShell shows "connect to the AP fail", but after retrying it correctly shows the SSID in the final line "Failed to connect to SSID: "ssidname", password: "password" ". Changing the ssid definition back to the original of "#define EXAMPLE_ESP_WIFI_SSID "ssidname" ", it connects right away. I have no clue why this happens.

The reason for having the ssid as a variable is because it will be retrieved from a HTTP POST form and extracted, then assigned to 'EXAMPLE_ESP_WIFI_SSID'. For now I am simply trying to move away from #define. Here is my code that only works with the ssid when it is assigned by #define:
  1. /* WiFi station Example
  2.    This example code is in the Public Domain (or CC0 licensed, at your option.)
  3.    Unless required by applicable law or agreed to in writing, this
  4.    software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  5.    CONDITIONS OF ANY KIND, either express or implied.*/
  6.  
  7. #include <string.h>
  8. #include "freertos/FreeRTOS.h"
  9. #include "freertos/task.h"
  10. #include "freertos/event_groups.h"
  11. #include "esp_system.h"
  12. #include "esp_wifi.h"
  13. #include "esp_event.h"
  14. #include "esp_log.h"
  15. #include "nvs_flash.h"
  16.  
  17. #include "lwip/err.h"
  18. #include "lwip/sys.h"
  19.  
  20. /* The examples use WiFi configuration that you can set via project configuration menu
  21.  
  22.    If you'd rather not, just change the below entries to strings with
  23.    the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid"*/
  24.  
  25. //uncomment the line below to test the original #define from the example. make sure to comment out line 44.
  26. //#define EXAMPLE_ESP_WIFI_SSID      "Telco5517"
  27. #define EXAMPLE_ESP_WIFI_PASS      "bzs43cr"
  28. #define EXAMPLE_ESP_MAXIMUM_RETRY  CONFIG_ESP_MAXIMUM_RETRY
  29.  
  30. /* FreeRTOS event group to signal when we are connected*/
  31. static EventGroupHandle_t s_wifi_event_group;
  32.  
  33. /* The event group allows multiple bits for each event, but we only care about two events:
  34.  * - we are connected to 9 AP with an IP
  35.  * - we failed to connect after the maximum amount of retries */
  36. #define WIFI_CONNECTED_BIT BIT0
  37. #define WIFI_FAIL_BIT      BIT1
  38.  
  39. static const char *TAG = "wifi station";
  40.  
  41. static int s_retry_num = 0;
  42.  
  43. //trying to define the ssid as a const char* instead of using #define
  44. const char* EXAMPLE_ESP_WIFI_SSID ="Telco5517";
  45.  
  46.  
  47.  
  48. static void event_handler(void* arg, esp_event_base_t event_base,
  49.                                 int32_t event_id, void* event_data)
  50. {
  51.     if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
  52.         esp_wifi_connect();
  53.     } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
  54.         if (s_retry_num < EXAMPLE_ESP_MAXIMUM_RETRY) {
  55.             esp_wifi_connect();
  56.             s_retry_num++;
  57.             ESP_LOGI(TAG, "retry to connect to the AP");
  58.         } else {
  59.             xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
  60.         }
  61.         ESP_LOGI(TAG,"connect to the AP fail");
  62.     } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
  63.         ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
  64.         ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip));
  65.         s_retry_num = 0;
  66.         xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
  67.     }
  68. }
  69.  
  70. void wifi_init_sta(void)
  71. {
  72.     s_wifi_event_group = xEventGroupCreate();
  73.  
  74.     ESP_ERROR_CHECK(esp_netif_init());
  75.  
  76.     ESP_ERROR_CHECK(esp_event_loop_create_default());
  77.     esp_netif_create_default_wifi_sta();
  78.  
  79.     wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  80.     ESP_ERROR_CHECK(esp_wifi_init(&cfg));
  81.  
  82.     esp_event_handler_instance_t instance_any_id;
  83.     esp_event_handler_instance_t instance_got_ip;
  84.     ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
  85.                                                         ESP_EVENT_ANY_ID,
  86.                                                         &event_handler,
  87.                                                         NULL,
  88.                                                         &instance_any_id));
  89.     ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,
  90.                                                         IP_EVENT_STA_GOT_IP,
  91.                                                         &event_handler,
  92.                                                         NULL,
  93.                                                         &instance_got_ip));
  94.  
  95.     wifi_config_t wifi_config = {
  96.         .sta = {
  97.             .ssid = {EXAMPLE_ESP_WIFI_SSID},
  98.             .password = {EXAMPLE_ESP_WIFI_PASS},
  99.             /* Setting a password implies station will connect to all security modes including WEP/WPA.
  100.              * However these modes are deprecated and not advisable to be used. Incase your Access point
  101.              * doesn't support WPA2, these mode can be enabled by commenting below line*/
  102.          .threshold.authmode = WIFI_AUTH_WPA2_PSK,
  103.  
  104.             .pmf_cfg = {
  105.                 .capable = true,
  106.                 .required = false
  107.             },
  108.         },
  109.     };
  110.     ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) );
  111.     ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config) );
  112.     ESP_ERROR_CHECK(esp_wifi_start() );
  113.  
  114.     ESP_LOGI(TAG, "wifi_init_sta finished.");
  115.  
  116.    /*  Waiting until either the connection is established (WIFI_CONNECTED_BIT) or connection failed for the maximum
  117.      * number of re-tries (WIFI_FAIL_BIT). The bits are set by event_handler() (see above)*/
  118.     EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group,
  119.             WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
  120.             pdFALSE,
  121.             pdFALSE,
  122.             portMAX_DELAY);
  123.  
  124.    /*  xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually
  125.      * happened.*/
  126.     if (bits & WIFI_CONNECTED_BIT) {
  127.         ESP_LOGI(TAG, "connected to ap SSID:%s password:%s",
  128.                  EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS);
  129.     } else if (bits & WIFI_FAIL_BIT) {
  130.         ESP_LOGI(TAG, "Failed to connect to SSID:%s, password:%s",
  131.                  EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS);
  132.     } else {
  133.         ESP_LOGE(TAG, "UNEXPECTED EVENT");
  134.     }
  135.  
  136.      //The event will not be processed after unregister
  137.     ESP_ERROR_CHECK(esp_event_handler_instance_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, instance_got_ip));
  138.     ESP_ERROR_CHECK(esp_event_handler_instance_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, instance_any_id));
  139.     vEventGroupDelete(s_wifi_event_group);
  140. }
  141.  
  142. void app_main(void)
  143. {
  144.  
  145.     //Initialize NVS
  146.     esp_err_t ret = nvs_flash_init();
  147.     if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  148.       ESP_ERROR_CHECK(nvs_flash_erase());
  149.       ret = nvs_flash_init();
  150.     }
  151.     ESP_ERROR_CHECK(ret);
  152.  
  153.     ESP_LOGI(TAG, "ESP_WIFI_MODE_STA");
  154.    wifi_init_sta();
  155.  
  156. }
I may be doing something simple wrong since I am a beginner, but I have tried initialising 'EXAMPLE_ESP_WIFI_SSID' as char[], char*, unsigned char, unsigned char*, using strcpy(), and moving the initialiser into main{}. All help will be appreciated.

Thanks,
wifibeginner

RalphD
Posts: 97
Joined: Thu Nov 25, 2021 9:02 pm

Re: ESP32-S2 station example SSID as variable instead of constant

Postby RalphD » Fri May 13, 2022 1:43 pm

Hi,
try this

Code: Select all

.ssid = EXAMPLE_ESP_WIFI_SSID,

RalphD
Posts: 97
Joined: Thu Nov 25, 2021 9:02 pm

Re: ESP32-S2 station example SSID as variable instead of constant

Postby RalphD » Fri May 13, 2022 3:12 pm

however, you shall use a different name for the variable

wifibeginner
Posts: 5
Joined: Fri May 13, 2022 9:22 am

Re: ESP32-S2 station example SSID as variable instead of constant

Postby wifibeginner » Sat May 14, 2022 12:10 pm

RalphD wrote:
Fri May 13, 2022 1:43 pm
Hi,
try this

Code: Select all

.ssid = EXAMPLE_ESP_WIFI_SSID,
Hi RalphD,

Thanks for the recommendation. In my original code, on line 44 I define the SSID as a const char*.
wifibeginner wrote:
  1. //trying to define the ssid as a const char* instead of using #define
  2. const char* EXAMPLE_ESP_WIFI_SSID ="Telco5517";
Changing the variable name to something other than 'EXAMPLE_ESP_WIFI_SSID' has no effect. If your recommendation instead was to remove the braces as in the original example, powershell shows several error messages "missing braces around initialiser".

Also an additional detail is that when "EXAMPLE_ESP_WIFI_SSID" is defined as a const char*, powershell shows a warning about line 97: "initialisation of 'unsigned char' from 'const char*' makes integer from pointer without a cast". I've seen this error on other forums but they were of no help.

If anyone has some ideas please send them through

Thanks,
wifibeginner

RalphD
Posts: 97
Joined: Thu Nov 25, 2021 9:02 pm

Re: ESP32-S2 station example SSID as variable instead of constant

Postby RalphD » Sun May 15, 2022 11:39 am

Dear wifibeginner,

sorry that I was trying to push you to self learning by giving a quick and dirty answer. And sorry that I did not look into this, performing the following steps before.

First you violating common naming conventions. All Upper Case letter identifiers are reserved for #define directives. When ever you run into an identifier all upper case it is declared in a #define.

Second, to find out how you can initialize a variable or constant. Constants are defined/initialized at compile time and stored in a memory area which can not be changed dynamically during run time.
Variables need to be in a changeable memory area or RAM.

As result you need to look at the definitions of types and how they are defined.

In your case you try to initialize a structure of type

Code: Select all

wifi_config_t


If you look in the relevant header file

Code: Select all

esp_wifi_types.h


you find that this typedef is a union

Code: Select all

typedef union {
    wifi_ap_config_t  ap;  /**< configuration of AP */
    wifi_sta_config_t sta; /**< configuration of STA */
} wifi_config_t;



this tells you that the .sta identifier refers to a type

Code: Select all

wifi_sta_config_t
if you look at this typedef you find this

Code: Select all

typedef struct {
    uint8_t ssid[32];      /**< SSID of target AP. */
    uint8_t password[64];  /**< Password of target AP. */
    wifi_scan_method_t scan_method;    /**< do all channel scan or fast scan */
    bool bssid_set;        /**< whether set MAC address of target AP or not. Generally, station_config.bssid_set needs to be 0; and it needs to be 1 only when users need to check the MAC address of the AP.*/
    uint8_t bssid[6];     /**< MAC address of target AP*/
    uint8_t channel;       /**< channel of target AP. Set to 1~13 to scan starting from the specified channel before connecting to AP. If the channel of AP is unknown, set it to 0.*/
    uint16_t listen_interval;   /**< Listen interval for ESP32 station to receive beacon when WIFI_PS_MAX_MODEM is set. Units: AP beacon intervals. Defaults to 3 if set to 0. */
    wifi_sort_method_t sort_method;    /**< sort the connect AP in the list by rssi or security mode */
    wifi_scan_threshold_t  threshold;     /**< When sort_method is set, only APs which have an auth mode that is more secure than the selected auth mode and a signal stronger than the minimum RSSI will be used. */
    wifi_pmf_config_t pmf_cfg;    /**< Configuration for Protected Management Frame. Will be advertized in RSN Capabilities in RSN IE. */
    uint32_t rm_enabled:1;        /**< Whether Radio Measurements are enabled for the connection */
    uint32_t btm_enabled:1;       /**< Whether BSS Transition Management is enabled for the connection */
    uint32_t mbo_enabled:1;       /**< Whether MBO is enabled for the connection */
    uint32_t reserved:29;         /**< Reserved for future feature set */
} wifi_sta_config_t;
Here you find that the ssid identifier is declared as a array of 32 bytes of type uint8_t which is a standard type of unsigned byte or 8 bit wide. It is correct that identifiers of arrays are pointers, but in this case to a fixed memory area at compile times of 32 byte in this case.

That means that you can NOT change this pointer at runtime nor you can change the content of these 32 bytes at runtime as the compiler puts this array in a non volatile memory area.

However, theoretically you can define the array as

Code: Select all

volatile
to make sure that the compiler puts the array in RAM and then use memcopy() to change the content of ssid.

However, keep in mind that I am not saying that this would be a solution for what you trying to achieve, as this is influenced by a number of other things e.g. how and what core code is using this array and it may have to be a constant defined area in a non volatile memory area.

I would recommend you to get a good book about C/C++ and use the Arduinio IDE and community for your development.

I hope this gives you an idea.

Best, Ralph

ESP_Sprite
Posts: 8921
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP32-S2 station example SSID as variable instead of constant

Postby ESP_Sprite » Mon May 16, 2022 1:33 am

Hi RalphD, thanks for helping wifibeginner out, but if you don't mind, I'd like to expand on some of your points:
RalphD wrote:
Sun May 15, 2022 11:39 am
Here you find that the ssid identifier is declared as a array of 32 bytes of type uint8_t which is a standard type of unsigned byte or 8 bit wide. It is correct that identifiers of arrays are pointers, but in this case to a fixed memory area at compile times of 32 byte in this case.

That means that you can NOT change this pointer at runtime nor you can change the content of these 32 bytes at runtime as the compiler puts this array in a non volatile memory area.
This is... not entirely true? The array of 32 bytes is part of the wifi_config_t structure; its place in memory is whatever the place of the wifi_config_t is. So if I, say, declare it as such:

Code: Select all

wifi_config_t my_struct={.ssid="bla"};
it's in RAM and as such writable (I can do a strcpy(my_struct.ssid, "lala")), but

Code: Select all

const wifi_config_t my_struct={.ssid="bla"};
is not, as the 'const' makes the struct read-only and esp-idf will take advantage of that by putting it in read-only memory.
However, theoretically you can define the array as

Code: Select all

volatile
to make sure that the compiler puts the array in RAM and then use memcopy() to change the content of ssid.
Sorry, this is just false; it is perfectly possible to have a const value (in read-only memory) that is volatile. Volatile tells the compiler that a memory location can change randomly because of something outside of the current tasks control; the compiler will read the value from memory every time rather than re-use a previous value if it knows the current task can't have changed it. It's still possible for this to be in read-only memory; for instance, if you have a status register from an peripheral. Can't write to it, but it can change by itself.

You're absolutely correct in noticing that the ssid member is an array and not a pointer, and as such you can't randomly assign a string pointer to it, but you need to copy it over. Given that you haven't declared the wifi_config_t struct as const, you can do it using memcpy or strncpy:

Code: Select all

char *my_new_ssid="my_ssid"; //the new SSID to assign
strncpy(wifi_config.ssid, my_new_ssid, 32); //copy at max 32 bytes over

RalphD
Posts: 97
Joined: Thu Nov 25, 2021 9:02 pm

Re: ESP32-S2 station example SSID as variable instead of constant

Postby RalphD » Mon May 16, 2022 6:17 am

@ESP_spite thanks for correcting me. It is like driving a car with with manual transmission, you do things without thinking about it.

wifibeginner
Posts: 5
Joined: Fri May 13, 2022 9:22 am

Re: ESP32-S2 station example SSID as variable instead of constant

Postby wifibeginner » Tue May 17, 2022 4:12 am

Hi,

Thanks for the clarification RalphD and ESP_Sprite, the code works and I understand the issue much better now.

Ultimately the piece of code that helped was:
ESP_Sprite wrote:

Code: Select all

char *my_new_ssid="my_ssid"; //the new SSID to assign
strncpy(wifi_config.ssid, my_new_ssid, 32); //copy at max 32 bytes over
I did have to modify slightly to remove some warnings treated as errors:
  1. char *ssid2 ="my_ssid";//assigning SSID
  2. strncpy((char*)wifi_config.sta.ssid,(char*)ssid2, 32);//copy
Here's the final modified station example:
  1. /* WiFi station Example
  2.    This example code is in the Public Domain (or CC0 licensed, at your option.)
  3.    Unless required by applicable law or agreed to in writing, this
  4.    software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  5.    CONDITIONS OF ANY KIND, either express or implied.*/
  6.  
  7. #include <string.h>
  8. #include "freertos/FreeRTOS.h"
  9. #include "freertos/task.h"
  10. #include "freertos/event_groups.h"
  11. #include "esp_system.h"
  12. #include "esp_wifi.h"
  13. #include "esp_event.h"
  14. #include "esp_log.h"
  15. #include "nvs_flash.h"
  16.  
  17. #include "lwip/err.h"
  18. #include "lwip/sys.h"
  19.  
  20. /* The examples use WiFi configuration that you can set via project configuration menu
  21.  
  22.    If you'd rather not, just change the below entries to strings with
  23.    the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid"*/
  24.  
  25. //uncomment the line below to test the original #define from the example. make sure to comment out line 44.
  26. //#define ssid2      "my_ssid"
  27. #define EXAMPLE_ESP_WIFI_PASS      "my_pass"
  28. #define EXAMPLE_ESP_MAXIMUM_RETRY  CONFIG_ESP_MAXIMUM_RETRY
  29.  
  30. /* FreeRTOS event group to signal when we are connected*/
  31. static EventGroupHandle_t s_wifi_event_group;
  32.  
  33. /* The event group allows multiple bits for each event, but we only care about two events:
  34.  * - we are connected to 9 AP with an IP
  35.  * - we failed to connect after the maximum amount of retries */
  36. #define WIFI_CONNECTED_BIT BIT0
  37. #define WIFI_FAIL_BIT      BIT1
  38.  
  39. static const char *TAG = "wifi station";
  40.  
  41. static int s_retry_num = 0;
  42.  
  43. //defining the ssid as a char* instead of using #define
  44. char *ssid2 ="my_ssid";
  45.  
  46.  
  47. static void event_handler(void* arg, esp_event_base_t event_base,
  48.                                 int32_t event_id, void* event_data)
  49. {
  50.     if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
  51.         esp_wifi_connect();
  52.     } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
  53.         if (s_retry_num < EXAMPLE_ESP_MAXIMUM_RETRY) {
  54.             esp_wifi_connect();
  55.             s_retry_num++;
  56.             ESP_LOGI(TAG, "retry to connect to the AP");
  57.         } else {
  58.             xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
  59.         }
  60.         ESP_LOGI(TAG,"connect to the AP fail");
  61.     } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
  62.         ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
  63.         ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip));
  64.         s_retry_num = 0;
  65.         xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
  66.     }
  67. }
  68.  
  69. void wifi_init_sta(void)
  70. {
  71.  
  72.     s_wifi_event_group = xEventGroupCreate();
  73.  
  74.     ESP_ERROR_CHECK(esp_netif_init());
  75.  
  76.     ESP_ERROR_CHECK(esp_event_loop_create_default());
  77.     esp_netif_create_default_wifi_sta();
  78.  
  79.     wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  80.     ESP_ERROR_CHECK(esp_wifi_init(&cfg));
  81.  
  82.     esp_event_handler_instance_t instance_any_id;
  83.     esp_event_handler_instance_t instance_got_ip;
  84.     ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
  85.                                                         ESP_EVENT_ANY_ID,
  86.                                                         &event_handler,
  87.                                                         NULL,
  88.                                                         &instance_any_id));
  89.     ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,
  90.                                                         IP_EVENT_STA_GOT_IP,
  91.                                                         &event_handler,
  92.                                                         NULL,
  93.                                                         &instance_got_ip));
  94.  
  95.     wifi_config_t wifi_config = {
  96.         .sta = {
  97.             .ssid = {ssid2},
  98.             .password = {EXAMPLE_ESP_WIFI_PASS},
  99.             /* Setting a password implies station will connect to all security modes including WEP/WPA.
  100.              * However these modes are deprecated and not advisable to be used. Incase your Access point
  101.              * doesn't support WPA2, these mode can be enabled by commenting below line*/
  102.          .threshold.authmode = WIFI_AUTH_WPA2_PSK,
  103.  
  104.             .pmf_cfg = {
  105.                 .capable = true,
  106.                 .required = false
  107.             },
  108.         },
  109.     };
  110.  
  111.     strncpy((char*)wifi_config.sta.ssid,(char*)ssid2, 32);//copying new ssid to wifi_config
  112.     ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) );
  113.     ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config) );
  114.     ESP_ERROR_CHECK(esp_wifi_start() );
  115.  
  116.     ESP_LOGI(TAG, "wifi_init_sta finished.");
  117.  
  118.    /*  Waiting until either the connection is established (WIFI_CONNECTED_BIT) or connection failed for the maximum
  119.      * number of re-tries (WIFI_FAIL_BIT). The bits are set by event_handler() (see above)*/
  120.     EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group,
  121.             WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
  122.             pdFALSE,
  123.             pdFALSE,
  124.             portMAX_DELAY);
  125.  
  126.    /*  xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually
  127.      * happened.*/
  128.     if (bits & WIFI_CONNECTED_BIT) {
  129.         ESP_LOGI(TAG, "connected to ap SSID:%s password:%s",
  130.                  ssid2, EXAMPLE_ESP_WIFI_PASS);
  131.     } else if (bits & WIFI_FAIL_BIT) {
  132.         ESP_LOGI(TAG, "Failed to connect to SSID:%s, password:%s",
  133.                  ssid2, EXAMPLE_ESP_WIFI_PASS);
  134.     } else {
  135.         ESP_LOGE(TAG, "UNEXPECTED EVENT");
  136.     }
  137.  
  138.      //The event will not be processed after unregister
  139.     ESP_ERROR_CHECK(esp_event_handler_instance_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, instance_got_ip));
  140.     ESP_ERROR_CHECK(esp_event_handler_instance_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, instance_any_id));
  141.     vEventGroupDelete(s_wifi_event_group);
  142. }
  143.  
  144. void app_main(void)
  145. {
  146.  
  147.     //Initialize NVS
  148.     esp_err_t ret = nvs_flash_init();
  149.     if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  150.       ESP_ERROR_CHECK(nvs_flash_erase());
  151.       ret = nvs_flash_init();
  152.     }
  153.     ESP_ERROR_CHECK(ret);
  154.  
  155.     ESP_LOGI(TAG, "ESP_WIFI_MODE_STA");
  156.    wifi_init_sta();
  157.  
  158. }
  159.  
Thanks again,
wifibeginner

Who is online

Users browsing this forum: HighVoltage, mikecarlos and 100 guests