Skip to content

Instantly share code, notes, and snippets.

@aallan
Created June 6, 2018 10:29
Show Gist options
  • Save aallan/38346a23e70d3d7b97192ab5ee7ce084 to your computer and use it in GitHub Desktop.
Save aallan/38346a23e70d3d7b97192ab5ee7ce084 to your computer and use it in GitHub Desktop.
Code snippet to enable WIFI_PROTOCOL_LR in an ESP32
void init_wifi(wifi_mode_t mode)
{
const uint8_t protocol = WIFI_PROTOCOL_LR;
tcpip_adapter_init();
ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) );
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
ESP_ERROR_CHECK( esp_wifi_set_mode(mode) );
wifi_event_group = xEventGroupCreate();
if (mode == WIFI_MODE_STA) {
ESP_ERROR_CHECK( esp_wifi_set_protocol(WIFI_IF_STA, protocol) );
wifi_config_t config = {
.sta = {
.ssid = ap_name,
.password = pass,
.bssid_set = false
}
};
ESP_ERROR_CHECK( esp_wifi_set_config(WIFI_IF_STA, &config) );
ESP_ERROR_CHECK( esp_wifi_start() );
ESP_ERROR_CHECK( esp_wifi_connect() );
xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT,
false, true, portMAX_DELAY);
ESP_LOGI(TAG, "Connected to AP");
} else {
ESP_ERROR_CHECK( esp_wifi_set_protocol(WIFI_IF_AP, protocol) );
wifi_config_t config = {
.ap = {
.ssid = ap_name,
.password = pass,
.ssid_len = 0,
.authmode = WIFI_AUTH_WPA_WPA2_PSK,
.ssid_hidden = false,
.max_connection = 3,
.beacon_interval = 100,
}
};
ESP_ERROR_CHECK( esp_wifi_set_config(WIFI_IF_AP, &config) );
ESP_ERROR_CHECK( esp_wifi_start() );
}
}
@themightywolfie
Copy link

ERROR. PLEASE HELP FIX ISSUE

Hardware:

Board: ESP32 Dev Module
Core Installation version: ?1.0.0? ?1.0.1-rc4? ?1.0.1? ?1.0.1-git? ?1.0.2? ?1.0.3?
IDE name: Arduino IDE
Flash Frequency: 80Mhz
PSRAM enabled: no
Upload Speed: 115200
Computer OS: Windows 10

Description:

Describe your problem here
I am trying to enable low rate wifi for long range connectivity and I cannot initialize the wifi_config_t object because the GNU complier does not support non-trivial designated initializers

Sketch:

#include <esp_wifi.h>
#include <WiFi.h>

static const int CONNECTED_BIT = BIT0;
void init_wifi(wifi_mode_t mode)
{
    const uint8_t protocol = WIFI_PROTOCOL_LR;
    tcpip_adapter_init();
//    ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) );
    wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
    ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
    ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
    ESP_ERROR_CHECK( esp_wifi_set_mode(mode) );
    EventGroupHandle_t wifi_event_group = xEventGroupCreate();

    if (mode == WIFI_MODE_STA) {
        ESP_ERROR_CHECK( esp_wifi_set_protocol(WIFI_IF_STA, protocol) );
       wifi_config_t config = {
        .sta = {        //ERROR HERE
                .ssid = /*{[0]='K',[1]='h',[2]='a',[3]='t',[4]='r',[5]='i'}*/{'K','h','a','t','r','i'},
                .password = /*{[0]='s',[1]='a',[2]='m',[3]='e',[4]='e',[5]='p',[6]='1',[7]='8'}*/{'s','a','m','e','e','p','1','8'},
                .bssid_set = false
        }   
        };
        ESP_ERROR_CHECK( esp_wifi_set_config(WIFI_IF_STA, &config));
        ESP_ERROR_CHECK( esp_wifi_start() );
        ESP_ERROR_CHECK( esp_wifi_connect() );

        xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT,
                            false, true, portMAX_DELAY);
        ESP_LOGI(TAG, "Connected to AP");
    } else {
        ESP_ERROR_CHECK( esp_wifi_set_protocol(WIFI_IF_AP, protocol) );
        wifi_config_t config = {
          .ap = {                                   //ERROR HERE
                .ssid = /*{[0]='K',[1]='h',[2]='a',[3]='t',[4]='r',[5]='i'}*/{'K','h','a','t','r','i'},
                .password = /*{[0]='s',[1]='a',[2]='m',[3]='e',[4]='e',[5]='p',[6]='1',[7]='8'}*/{'s','a','m','e','e','p','1','8'},
                .ssid_len = 0,
                .authmode = WIFI_AUTH_WPA_WPA2_PSK,
                .ssid_hidden = false,
                .max_connection = 3,
                .beacon_interval = 100,
          }
        };
        ESP_ERROR_CHECK( esp_wifi_set_config(WIFI_IF_AP, &config));
        ESP_ERROR_CHECK( esp_wifi_start() );
    }
}
void setup()
{
 Serial.begin(115200);
 init_wifi(WIFI_MODE_STA);
}
void loop()
{
  if (WiFi.status()!=WL_CONNECTED)
    Serial.println("Not connected");
  else
  {
    Serial.println("Connected");
    Serial.println(WiFi.localIP());
  }
}

Debug Messages:

Arduino: 1.8.10 (Windows 10), Board: "ESP32 Dev Module, Disabled, Default 4MB with spiffs (1.2MB APP/1.5MB SPIFFS), 240MHz (WiFi/BT), QIO, 80MHz, 4MB (32Mb), 921600, Error"

C:\Users\Admin\Desktop\MCTE\long_range_wifi_test\long_range_wifi_test.ino: In function 'void init_wifi(wifi_mode_t)':

C:\Users\Admin\Desktop\MCTE\long_range_wifi_test\long_range_wifi_test.ino:24:9: sorry, unimplemented: non-trivial designated initializers not supported

         };

         ^

C:\Users\Admin\Desktop\MCTE\long_range_wifi_test\long_range_wifi_test.ino:44:9: sorry, unimplemented: non-trivial designated initializers not supported

         };

         ^

C:\Users\Admin\Desktop\MCTE\long_range_wifi_test\long_range_wifi_test.ino:44:9: sorry, unimplemented: non-trivial designated initializers not supported

C:\Users\Admin\Desktop\MCTE\long_range_wifi_test\long_range_wifi_test.ino:44:9: sorry, unimplemented: non-trivial designated initializers not supported

C:\Users\Admin\Desktop\MCTE\long_range_wifi_test\long_range_wifi_test.ino:44:9: sorry, unimplemented: non-trivial designated initializers not supported

Multiple libraries were found for "WiFi.h"
 Used: C:\Users\Admin\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.4\libraries\WiFi
 Not used: C:\Program
exit status 1
Error compiling for board ESP32 Dev Module.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment