nkolban / esp32-snippets

Sample ESP32 snippets and code fragments
https://leanpub.com/kolban-ESP32
Apache License 2.0
2.35k stars 710 forks source link

ble_server txPower allways -21 on arduino #611

Closed eltsov-da closed 5 years ago

eltsov-da commented 6 years ago

Hello, I am new in BLE world so may be I ask something strange but: I try BLE_server.ino and BLE_scan.ino example and got: Advertised Device: Name: MyESP32, Address: 30:ae:a4:59:21:4e, txPower: -21 and about 1m distance of successful scanning. I try:

BLEDevice::init("MyESP32");
BLEDevice::setPower(ESP_PWR_LVL_P7);

but still get the same result.

Also I try

BLEDevice::init("MyESP32");
esp_err_t errRc=::esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_DEFAULT, ESP_PWR_LVL_P7);
errRc=::esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_ADV, ESP_PWR_LVL_P7);
errRc=::esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_SCAN, ESP_PWR_LVL_P7); 

and

Serial.println("Characteristic defined! Now you can read it in your phone!");

  Serial.printf("ESP_BLE_PWR_TYPE_DEFAULT=%d \n", ::esp_ble_tx_power_get(ESP_BLE_PWR_TYPE_DEFAULT)); 
  Serial.printf("ESP_BLE_PWR_TYPE_ADV =%d \n", ::esp_ble_tx_power_get(ESP_BLE_PWR_TYPE_ADV));
  Serial.printf("ESP_BLE_PWR_TYPE_SCAN  =%d \n", ::esp_ble_tx_power_get(ESP_BLE_PWR_TYPE_SCAN));

on server side I'v got:

Starting BLE work! Characteristic defined! Now you can read it in your phone! ESP_BLE_PWR_TYPE_DEFAULT=7 ESP_BLE_PWR_TYPE_ADV =7 ESP_BLE_PWR_TYPE_SCAN =7

but on scan side Scanning... Advertised Device: Name: MyESP32, Address: 30:ae:a4:59:21:4e, txPower: -21 Devices found: 1 Scan done!

and only 1m distance.

I use master espressif/arduino-esp32 master branch (update today) ESP32 BLE Arduino 0.4.16 I use TTGO LoRa32-OLED V1 boards. Also try to compile using ESP32 DevModule (the same result)

chegewara commented 6 years ago

Hi, sorry for late reply. Could you try with some other values from this range: https://github.com/espressif/esp-idf/blob/master/components/bt/include/esp_bt.h#L157-L164

eltsov-da commented 6 years ago

So I made some more investigation.

  1. The fact is that using in a BLE_server.ino any value and both BLEDevice::setPower(ххх) or
    
    esp_err_t errRc=::esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_DEFAULT,xxx);
    errRc=::esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_ADV, xxx);
    errRc=::esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_SCAN ,xxx); 
In a BLE_scan.ino example I'v got:

`Advertised Device: Name: MyESP32, Address: 30:ae:a4:59:21:4e, txPower: -21 `

2. Also fact that maximum scan distance is less then 1m
3. It's seems to me that if I use ::esp_ble_tx_power_set I can change tx power
    3.1 If I try

esp_err_t errRc=::esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_DEFAULT,ESP_PWR_LVL_P9); errRc=::esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_ADV, ESP_PWR_LVL_P9); errRc=::esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_SCAN ,ESP_PWR_LVL_P9);

 I can got scan in about 1m range.
   3.2 if is try ESP_PWR_LVL_N12 I still can got scan but in a range !!! about 2сm 

4. it's also seems to me that if I use BLEDevice::setPower(ххх) with any values I'v got some middle range the same to esp_ble_tx_power_set and ESP_PWR_LVL_P3
5. I add 

pAdvertising->start(); Serial.println("Characteristic defined! Now you can read it in your phone!"); Serial.printf("ESP_BLE_PWR_TYPE_DEFAULT=%d \n", ::esp_ble_tx_power_get(ESP_BLE_PWR_TYPE_DEFAULT)); Serial.printf("ESP_BLE_PWR_TYPE_ADV =%d \n", ::esp_ble_tx_power_get(ESP_BLE_PWR_TYPE_ADV)); Serial.printf("ESP_BLE_PWR_TYPE_SCAN =%d \n", ::esp_ble_tx_power_get(ESP_BLE_PWR_TYPE_SCAN));

and if I use BLEDevice::setPower in BLE_server.ino Serial monitor  I'v got some thing like this:

Starting BLE work! Characteristic defined! Now you can read it in your phone! ESP_BLE_PWR_TYPE_DEFAULT=7 ESP_BLE_PWR_TYPE_ADV =5 ESP_BLE_PWR_TYPE_SCAN =5


And I include file with some debug info (debug->verbose) etc may be it help. 

The main problem for me is that the longest range is 1m only. May be that it's board hardware problem but WiFi work fine and as far as I know BLE and WiFi share radio module in esp32 chips.
[debug_esp32_ble.txt](https://github.com/nkolban/esp32-snippets/files/2246218/debug_esp32_ble.txt)
chegewara commented 6 years ago

Tomorrow i will try to perform the same tests to confirm it.

eltsov-da commented 6 years ago

tomorrow never comes :)

chegewara commented 6 years ago

Yes, sorry for that. Ive been over 10 month at work away home. Now im back and for few days i dont touch esp32. I will do this test just i need to rest.

eltsov-da commented 6 years ago

Never mind. It's just a pun comes to my head. In Russia I'll say : "We are not in a fire"

chegewara commented 6 years ago

Hi, sorry for late response. I know whats going on. What you see is not real TX power value. When you change tx power with one of this function then tx power is changing:

esp_err_t errRc=::esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_DEFAULT, ESP_PWR_LVL_N12 );
errRc=::esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_ADV, ESP_PWR_LVL_N12 );
errRc=::esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_SCAN, ESP_PWR_LVL_N12 ); 

But what you see is advertising value and seems to be hardcoded. Time to open issue in esp-idf repo.

How can i say that tx power is changing? Simply, i am using nRF connect to test, when i perform scan i can see RSSI. With different tx power in my code i have different RSSI with the same distance.

eltsov-da commented 6 years ago

And what about distance? I can got any signal only in about 1m distance

chegewara commented 6 years ago

Its possible you have issue with esp32 board. I can scan for esp32 from about 15 m with no issue.

eltsov-da commented 6 years ago

Yes, it's possible, but I have no problem with WiFi. And as far as I know WiFI and BLE share the same radio module. I have 4 TTGO LoRa OLED v1 boards and all of them work the same way. May be someone have experience with that boards? Please tell me if BLE work’s fine or not.

mws-rmain commented 6 years ago

Just found this thread. As @chegewara stated, the TxPower reported in the advertising data is not updated (appears it's stays at -21dBm, regardless of what is set - see here).

Of course that still doesn't explain the poor range you're experiencing. WiFi & BLE share the same radio on the ESP32, but the WiFi stack & Bluetooth stack can have completely different settings, so operation in WiFi doesn't really affect bluetooth operation (apart from proving the antenna interface is OK).

I'm not clear on the 'LoRa' part of these boards. Do they have another radio for operation at 868/915MHz or 433/470MHz?

eltsov-da commented 6 years ago

About LoRa radio. It's completely separate module. sx1278 or sx1276, depending of frequency (868/915MHz or 433/470MHz). It have it's own antenna and communicate with main chip throw SPI. About WiFi - BLE: of course I understand that stacks are completely different. I mention it only to avoid questions about antenna or some other hardware problems. And I believe that stack settings is software problem.

mws-rmain commented 6 years ago

I came across this video (Andreas Spiess) that does some RF Testing of some ESP32/LoRa boards (the TTGO is covered). He criticizes the on-board ESP32 antennas, and points out they are easily shorted when soldering (if you aren't very careful). Possibly something to look at?

eltsov-da commented 6 years ago

:) once more. WiFi work perfect. And I don't do any soldering with that modules.

chegewara commented 6 years ago

https://github.com/espressif/esp-idf/issues/2284