-
Notifications
You must be signed in to change notification settings - Fork 7.7k
Description
Hardware:
Board: DOIT ESP32
Core Installation/update date: 22/jun/2018
IDE name: Arduino IDE
Flash Frequency: 80Mhz
Upload Speed: 921600
Description:
First of all I have to say I'm new on the ESP32, it's been literally two weeks since I started using it on Arduino. Though I'm not new to Arduino, maybe there are some things that regular users of the ESP32 know that I'm not aware of, but I'm sure this is an nonexistent feature on the Arduino core so that's why I'm writing my questions here.
So I am trying to enable the long range mode for the ESP32 and already tried different things. First I knew from this article https://blog.hackster.io/long-range-wifi-for-the-esp32-9429ab89f450 that you have to call the function "esp_wifi_set_protocol(interface, WIFI_PROTOCOL_LR)", where interface can either be WIFI_IF_STA or WIFI_IF_AP depending on your needs. That seems clear to me, the problem is where should I call the function?
In order to answer this question I had to check the ESP32 Arduino core, specifically the files where the WiFi interface is configured. So I found, and correct me if I'm wrong, that this is done on the files WiFiGeneric.cpp, WiFiAP.cpp and WiFiSTA.cpp with their corresponding header files. And all of them use the SDK, mostly they use the esp_wifi.h file for the definition of the core functions.
What I did was search for all the parts that this WiFi files call the functions mention on the article: tcpip_adapter_init(), esp_wifi_init(), esp_wifi_set_storage(), esp_wifi_set_mode(), esp_wifi_set_protocol(), esp_wifi_set_config(), esp_wifi_start() and esp_wifi_connect(). So far I found almost all of them, except for the esp_wifi_set_protocol() function, so this confirms that the long range mode is not implemented on the Arduino core.
So reading the declaration of the function on the esp_wifi.h I found that this should be call after you call the function esp_wifi_init() and this is done only inside WiFiGenericClass::mode(wifi_mode_t m), so now I know that I need to call this function after setting the WiFi mode with WiFi.mode().
Next thing I assumed was that the esp_wifi_set_protocol() function should be called before the esp_wifi_start() function. So looking for this function I found that this is called in all the three WiFi files I mention before, so this could be more tricky than I thought.
Before I do some weird stuff, I tried calling the esp_wifi_set_protocol() inside the sketch I was using. By the way for the test I used two examples, the SimpleWiFiServer running on one ESP32 board and the WiFiClientBasic running on another ESP32 board but without using WiFiMulti. So the server starts listening to anybody that connects to the port 80 and prints anything the client send, then it close the connection. The client sends some string to that port every second, opening and closing the connection every time. This works fine with the default protocol modes. But when I insert the esp_wifi_set_protocol function, just after WiFi.mode(), I can see that the server detects a client connected to the port but it doesn't show anything that the client sends.
I know that the protocol has change, first because the two boards are communicating with each other, also because I can't see the WiFi access point on my phone, and because I used the function esp_wifi_get_protocol() before and after the call of the function esp_wifi_set_protocol() to see the change of protocols and it showed the correct protocol.
So I think I made some progress but I'm still missing something. I thought it could be something with the event task handler the SDK use, I can see the article use it and also the Arduino ESP32 core, but this is something I don't know how to mess with. I also tried putting the esp_wifi_set_protocol() function inside the WiFi files just before every call of the esp_wifi_start() function but the result is the same.
I hope someone could help me with this, I couldn't find anything related to the long range mode in Arduino so I think this could help other people too.
Sketch:
#include <esp_wifi.h>
#include <WiFi.h>
const char* ssid = "yourssid";
const char* password = "yourpasswd";
WiFiServer server(80);
int buff_size = -1;
uint8_t client_buffer[1024];
uint8_t current_protocol;
esp_interface_t current_esp_interface;
wifi_interface_t current_wifi_interface;
void setup()
{
Serial.begin(115200);
pinMode(5, OUTPUT); // set the LED pin mode
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
//WiFi.begin(ssid, password);
WiFi.mode(WIFI_AP_STA);
//tcpip_adapter_init();
//wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
//esp_wifi_init(&cfg);
//esp_wifi_set_storage(WIFI_STORAGE_RAM);
//esp_wifi_set_mode(WIFI_MODE_AP);
check_protocol();
esp_wifi_set_protocol(current_wifi_interface, WIFI_PROTOCOL_LR);
check_protocol();
WiFi.softAP(ssid, password);
check_protocol();
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
server.begin();
}
int value = 0;
void loop(){
WiFiClient client = server.available(); // listen for incoming clients
//
if (client) { // if you get a client,
Serial.println("************************************************");
Serial.print("New Client in "); // print a message out the serial port
Serial.println(client.remoteIP());
String currentLine = ""; // make a String to hold incoming data from the client
if (client.connected()) { // loop while the client's connected
buff_size = client.available();
if (buff_size) { // if there's bytes to read from the client,
Serial.print("Payload size: ");
Serial.println(buff_size);
Serial.println("Data is:");
Serial.println();
client.read(client_buffer,buff_size); // read a byte, then
Serial.write(client_buffer,buff_size); // print it out the serial monitor
Serial.println();
Serial.println();
}
}
client.flush();
// close the connection:
client.stop();
Serial.println("Client Disconnected.");
Serial.println("************************************************");
Serial.println();
}
}
esp_interface_t check_protocol()
{
char error_buf1[100];
tcpip_adapter_get_esp_if(¤t_esp_interface);
if (current_esp_interface == ESP_IF_WIFI_STA)
Serial.println("Interface is ESP_IF_WIFI_STA");
else if (current_esp_interface == ESP_IF_WIFI_AP)
Serial.println("Interface is ESP_IF_WIFI_AP");
else
Serial.println("Unknown interface!!");
current_wifi_interface = current_esp_interface;
if (current_wifi_interface == WIFI_IF_STA)
Serial.println("Interface is WIFI_IF_STA");
else if (current_wifi_interface == WIFI_IF_AP)
Serial.println("Interface is WIFI_IF_AP");
else
Serial.println("Unknown interface!!");
esp_err_t error_code = esp_wifi_get_protocol(current_wifi_interface, ¤t_protocol);
esp_err_to_name_r(error_code,error_buf1,100);
Serial.print("esp_wifi_get_protocol error code: ");
Serial.println(error_buf1);
Serial.print("Current protocol code is ");
Serial.println(current_protocol);
if ((current_protocol&WIFI_PROTOCOL_11B) == WIFI_PROTOCOL_11B)
Serial.println("Protocol is WIFI_PROTOCOL_11B");
if ((current_protocol&WIFI_PROTOCOL_11G) == WIFI_PROTOCOL_11G)
Serial.println("Protocol is WIFI_PROTOCOL_11G");
if ((current_protocol&WIFI_PROTOCOL_11N) == WIFI_PROTOCOL_11N)
Serial.println("Protocol is WIFI_PROTOCOL_11N");
if ((current_protocol&WIFI_PROTOCOL_LR) == WIFI_PROTOCOL_LR)
Serial.println("Protocol is WIFI_PROTOCOL_LR");
return current_esp_interface;
}
### Debug Messages:
ets Jun 8 2016 00:22:57
rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
flash read err, 1000
ets_main.c 371
ets Jun 8 2016 00:22:57
rst:0x10 (RTCWDT_RTC_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:812
load:0x40078000,len:0
load:0x40078000,len:11572
entry 0x40078a5c
Connecting to yourssid
Interface is ESP_IF_WIFI_STA
Interface is WIFI_IF_STA
esp_wifi_get_protocol error code: ESP_OK
Current protocol code is 7
Protocol is WIFI_PROTOCOL_11B
Protocol is WIFI_PROTOCOL_11G
Protocol is WIFI_PROTOCOL_11N
Interface is ESP_IF_WIFI_STA[D][WiFiGeneric.cpp:304] _eventCallback(): Event: 3 - STA_STOP
Interface is WIFI_IF_STA
esp_wifi_get_protocol error code: ESP_OK
Current protocol code is 0
Interface is ESP_IF_WIFI_STA
Interface is WIFI_IF_STA
esp_wifi_get_protocol error code: ESP_OK[D][WiFiGeneric.cpp:304] _eventCallback(): Event: 13 - AP_START
[D][WiFiGeneric.cpp:304] _eventCallback(): Event: 13 - AP_START
Current protocol code is 8
Protocol is WIFI_PROTOCOL_LR
WiFi connected.
IP address:
0.0.0.0
[D][WiFiGeneric.cpp:304] _eventCallback(): Event: 15 - AP_STACONNECTED
New Client in 192.168.4.2
Client Disconnected.
New Client in 192.168.4.2
Client Disconnected.
New Client in 192.168.4.2
Client Disconnected.
New Client in 192.168.4.2
Client Disconnected.
New Client in 192.168.4.2
Client Disconnected.
New Client in 192.168.4.2
Client Disconnected.
New Client in 192.168.4.2
Client Disconnected.
New Client in 192.168.4.2
Client Disconnected.