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

HID Gamepad constant disconnects. #649

Open GameDragon2k opened 6 years ago

GameDragon2k commented 6 years ago

I am using my ESP32 as a HID Gamepad and I am running into issues with it.

Connection seems fine at the beginning but after several perfectly working inputs, I get a hard lag spike followed by disconnect. The lag itself is a big issue, but after reconnecting, the ESP32 will continue to disconnect itself every 90 seconds or so, regardless if I send any inputs or not.

Here is my code: https://gist.github.com/GameDragon2k/90709fbe6b15e66c1b274dde3e1b2439

And this is debug for disconnects/reconnects:

[D][BLEDevice.cpp:96] gattServerEventHandler(): gattServerEventHandler [esp_gatt_if: 4] ... ESP_GATTS_DISCONNECT_EVT
[D][BLEUtils.cpp:1647] dumpGattServerEvent(): GATT ServerEvent: ESP_GATTS_DISCONNECT_EVT
[D][BLEUtils.cpp:1723] dumpGattServerEvent(): [conn_id: 0, remote_bda: 5c:f3:70:74:7d:d3]
[D][BLEServer.cpp:177] handleGATTServerEvent(): >> handleGATTServerEvent: ESP_GATTS_DISCONNECT_EVT
[D][BLECharacteristic.cpp:209] handleGATTServerEvent(): >> handleGATTServerEvent: ESP_GATTS_DISCONNECT_EVT
[D][BLECharacteristic.cpp:461] handleGATTServerEvent(): << handleGATTServerEvent
[D][BLECharacteristic.cpp:209] handleGATTServerEvent(): >> handleGATTServerEvent: ESP_GATTS_DISCONNECT_EVT
[D][BLECharacteristic.cpp:461] handleGATTServerEvent(): << handleGATTServerEvent
[D][BLECharacteristic.cpp:209] handleGATTServerEvent(): >> handleGATTServerEvent: ESP_GATTS_DISCONNECT_EVT
[D][BLECharacteristic.cpp:461] handleGATTServerEvent(): << handleGATTServerEvent
[D][BLECharacteristic.cpp:209] handleGATTServerEvent(): >> handleGATTServerEvent: ESP_GATTS_DISCONNECT_EVT
[D][BLECharacteristic.cpp:461] handleGATTServerEvent(): << handleGATTServerEvent
[D][BLECharacteristic.cpp:209] handleGATTServerEvent(): >> handleGATTServerEvent: ESP_GATTS_DISCONNECT_EVT
[D][BLECharacteristic.cpp:461] handleGATTServerEvent(): << handleGATTServerEvent
[D][BLECharacteristic.cpp:209] handleGATTServerEvent(): >> handleGATTServerEvent: ESP_GATTS_DISCONNECT_EVT
[D][BLECharacteristic.cpp:461] handleGATTServerEvent(): << handleGATTServerEvent
[D][BLECharacteristic.cpp:209] handleGATTServerEvent(): >> handleGATTServerEvent: ESP_GATTS_DISCONNECT_EVT
[D][BLECharacteristic.cpp:461] handleGATTServerEvent(): << handleGATTServerEvent
[D][BLECharacteristic.cpp:209] handleGATTServerEvent(): >> handleGATTServerEvent: ESP_GATTS_DISCONNECT_EVT
[D][BLECharacteristic.cpp:461] handleGATTServerEvent(): << handleGATTServerEvent
[D][BLEServer.cpp:342] startAdvertising(): >> startAdvertising
[D][BLEAdvertising.cpp:174] start(): >> start: customAdvData: 0, customScanResponseData: 0
[D][BLEAdvertising.cpp:187] start(): - advertising service: 00001812-0000-1000-8000-00805f9b34fb
[D][BLEAdvertising.cpp:231] start(): << start
[D][BLEServer.cpp:344] startAdvertising(): << startAdvertising
[D][BLEServer.cpp:295] handleGATTServerEvent(): << handleGATTServerEvent
[D][BLEUtils.cpp:1091] dumpGapEvent(): Received a GAP event: ESP_GAP_BLE_ADV_DATA_SET_COMPLETE_EVT
[D][BLEUtils.cpp:1099] dumpGapEvent(): [status: 0]
[D][BLEServer.cpp:135] handleGAPEvent(): BLEServer ... handling GAP event!
[D][BLEUtils.cpp:1091] dumpGapEvent(): Received a GAP event: ESP_GAP_BLE_SCAN_RSP_DATA_SET_COMPLETE_EVT
[D][BLEUtils.cpp:1285] dumpGapEvent(): [status: 0]
[D][BLEServer.cpp:135] handleGAPEvent(): BLEServer ... handling GAP event!
[D][BLEUtils.cpp:1091] dumpGapEvent(): Received a GAP event: ESP_GAP_BLE_ADV_START_COMPLETE_EVT
[D][BLEUtils.cpp:1123] dumpGapEvent(): [status: 0]
[D][BLEServer.cpp:135] handleGAPEvent(): BLEServer ... handling GAP event!
[D][BLEServer.cpp:342] startAdvertising(): >> startAdvertising
[D][BLEAdvertising.cpp:174] start(): >> start: customAdvData: 0, customScanResponseData: 0
[D][BLEAdvertising.cpp:187] start(): - advertising service: 00001812-0000-1000-8000-00805f9b34fb
[D][BLEAdvertising.cpp:231] start(): << start
[D][BLEServer.cpp:344] startAdvertising(): << startAdvertising
start advertising
[D][BLEUtils.cpp:1091] dumpGapEvent(): Received a GAP event: ESP_GAP_BLE_ADV_DATA_SET_COMPLETE_EVT
[D][BLEUtils.cpp:1099] dumpGapEvent(): [status: 0]
[D][BLEServer.cpp:135] handleGAPEvent(): BLEServer ... handling GAP event!
[D][BLEUtils.cpp:1091] dumpGapEvent(): Received a GAP event: ESP_GAP_BLE_SCAN_RSP_DATA_SET_COMPLETE_EVT
[D][BLEUtils.cpp:1285] dumpGapEvent(): [status: 0]
[D][BLEServer.cpp:135] handleGAPEvent(): BLEServer ... handling GAP event!
[D][BLEUtils.cpp:1091] dumpGapEvent(): Received a GAP event: ESP_GAP_BLE_ADV_START_COMPLETE_EVT
[D][BLEUtils.cpp:1123] dumpGapEvent(): [status: 0]
[D][BLEServer.cpp:135] handleGAPEvent(): BLEServer ... handling GAP event!
[D][BLEDevice.cpp:96] gattServerEventHandler(): gattServerEventHandler [esp_gatt_if: 4] ... ESP_GATTS_CONNECT_EVT
[D][BLEUtils.cpp:1647] dumpGattServerEvent(): GATT ServerEvent: ESP_GATTS_CONNECT_EVT
[D][BLEUtils.cpp:1707] dumpGattServerEvent(): [conn_id: 0, remote_bda: 5c:f3:70:74:7d:d3]
[D][BLEServer.cpp:177] handleGATTServerEvent(): >> handleGATTServerEvent: ESP_GATTS_CONNECT_EVT
[D][BLECharacteristic.cpp:209] handleGATTServerEvent(): >> handleGATTServerEvent: ESP_GATTS_CONNECT_EVT
[D][BLECharacteristic.cpp:461] handleGATTServerEvent(): << handleGATTServerEvent
[D][BLECharacteristic.cpp:209] handleGATTServerEvent(): >> handleGATTServerEvent: ESP_GATTS_CONNECT_EVT
[D][BLECharacteristic.cpp:461] handleGATTServerEvent(): << handleGATTServerEvent
[D][BLECharacteristic.cpp:209] handleGATTServerEvent(): >> handleGATTServerEvent: ESP_GATTS_CONNECT_EVT
[D][BLECharacteristic.cpp:461] handleGATTServerEvent(): << handleGATTServerEvent
[D][BLECharacteristic.cpp:209] handleGATTServerEvent(): >> handleGATTServerEvent: ESP_GATTS_CONNECT_EVT
[D][BLECharacteristic.cpp:461] handleGATTServerEvent(): << handleGATTServerEvent
[D][BLECharacteristic.cpp:209] handleGATTServerEvent(): >> handleGATTServerEvent: ESP_GATTS_CONNECT_EVT
[D][BLECharacteristic.cpp:461] handleGATTServerEvent(): << handleGATTServerEvent
[D][BLECharacteristic.cpp:209] handleGATTServerEvent(): >> handleGATTServerEvent: ESP_GATTS_CONNECT_EVT
[D][BLECharacteristic.cpp:461] handleGATTServerEvent(): << handleGATTServerEvent
[D][BLECharacteristic.cpp:209] handleGATTServerEvent(): >> handleGATTServerEvent: ESP_GATTS_CONNECT_EVT
[D][BLECharacteristic.cpp:461] handleGATTServerEvent(): << handleGATTServerEvent
[D][BLECharacteristic.cpp:209] handleGATTServerEvent(): >> handleGATTServerEvent: ESP_GATTS_CONNECT_EVT
[D][BLECharacteristic.cpp:461] handleGATTServerEvent(): << handleGATTServerEvent
Connection established!
[D][BLEServer.cpp:295] handleGATTServerEvent(): << handleGATTServerEvent
[D][BLEDevice.cpp:96] gattServerEventHandler(): gattServerEventHandler [esp_gatt_if: 4] ... ESP_GATTS_MTU_EVT
[D][BLEUtils.cpp:1647] dumpGattServerEvent(): GATT ServerEvent: ESP_GATTS_MTU_EVT
[D][BLEUtils.cpp:1766] dumpGattServerEvent(): [conn_id: 0, mtu: 517]
[I][BLEDevice.cpp:113] gattServerEventHandler(): ESP_GATTS_MTU_EVT, MTU 517
[D][BLEServer.cpp:177] handleGATTServerEvent(): >> handleGATTServerEvent: ESP_GATTS_MTU_EVT
[D][BLECharacteristic.cpp:209] handleGATTServerEvent(): >> handleGATTServerEvent: ESP_GATTS_MTU_EVT
[D][BLECharacteristic.cpp:461] handleGATTServerEvent(): << handleGATTServerEvent
[D][BLECharacteristic.cpp:209] handleGATTServerEvent(): >> handleGATTServerEvent: ESP_GATTS_MTU_EVT
[D][BLECharacteristic.cpp:461] handleGATTServerEvent(): << handleGATTServerEvent
[D][BLECharacteristic.cpp:209] handleGATTServerEvent(): >> handleGATTServerEvent: ESP_GATTS_MTU_EVT
[D][BLECharacteristic.cpp:461] handleGATTServerEvent(): << handleGATTServerEvent
[D][BLECharacteristic.cpp:209] handleGATTServerEvent(): >> handleGATTServerEvent: ESP_GATTS_MTU_EVT
[D][BLECharacteristic.cpp:461] handleGATTServerEvent(): << handleGATTServerEvent
[D][BLECharacteristic.cpp:209] handleGATTServerEvent(): >> handleGATTServerEvent: ESP_GATTS_MTU_EVT
[D][BLECharacteristic.cpp:461] handleGATTServerEvent(): << handleGATTServerEvent
[D][BLECharacteristic.cpp:209] handleGATTServerEvent(): >> handleGATTServerEvent: ESP_GATTS_MTU_EVT
[D][BLECharacteristic.cpp:461] handleGATTServerEvent(): << handleGATTServerEvent
[D][BLECharacteristic.cpp:209] handleGATTServerEvent(): >> handleGATTServerEvent: ESP_GATTS_MTU_EVT
[D][BLECharacteristic.cpp:461] handleGATTServerEvent(): << handleGATTServerEvent
[D][BLECharacteristic.cpp:209] handleGATTServerEvent(): >> handleGATTServerEvent: ESP_GATTS_MTU_EVT
[D][BLECharacteristic.cpp:461] handleGATTServerEvent(): << handleGATTServerEvent
[D][BLEServer.cpp:295] handleGATTServerEvent(): << handleGATTServerEvent
chegewara commented 6 years ago

EDIT my bad, it is required, at least it is in example code.

chegewara commented 6 years ago

@GameDragon2k i have question about your code, how those pins are mapped to esp32 GPIO pins? https://gist.github.com/GameDragon2k/90709fbe6b15e66c1b274dde3e1b2439#file-esp32_ble_arcade-ino-L39-L54

GameDragon2k commented 6 years ago

@chegewara I'm sorry, I don't understand the question. Are you asking where in the code the GPIO pins are mapped?

If so, that would be here: https://gist.github.com/GameDragon2k/90709fbe6b15e66c1b274dde3e1b2439#file-esp32_ble_arcade-ino-L153-L187

I should also mention that I have been testing this issue using only a single pushbutton connected to just one GPIO pin.

chegewara commented 6 years ago

I should also mention that I have been testing this issue using only a single pushbutton connected to one GPIO pin. Thats important information. I was asking about pin mapping because:

GameDragon2k commented 6 years ago

Arduino uses GPIO number for mapping, so pinMode(11, INPUT_PULLUP); would be mapped to FLASH CMD.

I have tested GPIOs 34-39 using external resistors and didn't run into any issues regarding inputs. GPIO9 and GPIO10 can be removed from my mapping, but using them individually in testing I noticed no difference with the issue.

chegewara commented 6 years ago

What is testing platform? android, windows or linux? if windows what version is it?

GameDragon2k commented 6 years ago

Tested on three devices, all Windows 10 Pro x64. oh you asked for version too, sorry. My current test platform is version 1803, I can't tell you the others atm.

chegewara commented 6 years ago

@GameDragon2k I have tested your code with my devKitC on windows 10 pro and i have strange behavior. At some point putty is displaying some crap and i am guessing it is the moment when your device is starting disconnecting. I have to do more tests with other esp32 board.

GameDragon2k commented 6 years ago

Thank you for testing. I am at a lost myself, so I hope you find some solid results.

chegewara commented 6 years ago

@GameDragon2k I have some issue with my windows atm and i need to reboot it, but unfortunately i cant. Could you try to change debug level to info or less and try again?

PS i meant windows version (windows 8 or 10) but you already did answer it

GameDragon2k commented 6 years ago

Dropping debug level to info didn't give me anything at all when the disconnect occurs.

Edit: Actually I didn't allow it to reconnect my first test, I do get an info message on reconnection. [I][BLEDevice.cpp:113] gattServerEventHandler(): ESP_GATTS_MTU_EVT, MTU 517

chegewara commented 6 years ago

@GameDragon2k I had to install windows latest update and reboot laptop. Now your code seems to work with no issues, no disconnection, for about 10 minutes. I didnt run putty this time, but windows game controller properties shows me that esp32 works fine.

PS you have to find one workaround because when you reconnecting with windows or other peer device it does not register for notification and esp32 will not send notifications.

GameDragon2k commented 6 years ago

PS you have to find one workaround because when you reconnecting with windows or other peer device it does not register for notification and esp32 will not send notifications.

You're referring to your workaround here? https://github.com/nkolban/esp32-snippets/issues/632#issuecomment-415982078

Thank you, I have included that bit as well. I will attempt to update and test again.

chegewara commented 6 years ago

Yes, thats the one. Nice to see that some people read my posts.

GameDragon2k commented 6 years ago

Unfortunately, I updated and still received lag and disconnect after 4 minutes of testing.

chegewara commented 6 years ago

You could try to prepare test case. Just simplify code, or use the one from #632 and send button on/off with some small delay between.

I cant explain that but there is many issues reported that i cant recreate because users code works for me, in other hand i am reporting some ble issues that cant be recreated by espressif devs.

GameDragon2k commented 6 years ago

I went ahead and tried using the code from https://github.com/nkolban/esp32-snippets/issues/632. I modified it to send inputs through GPIO33 with a delay of 20ms after each notification. Besides this, it is unchanged.

Unfortunately, within a few minutes of pressing the pushbutton, I get the same lag problem followed by the same disconnect.

chegewara commented 6 years ago

Then i really dont know. Maybe its esp32 faulty, also when did you last time update espressif/arduino-esp32 library?

GameDragon2k commented 6 years ago

I actually updated it just before I posted this issue, haha. I'll mess with it a bit more before I look into grabbing another esp32.

Thank you for your help.

GameDragon2k commented 6 years ago

I tried https://github.com/nkolban/esp32-snippets/issues/632 again but this time I left it completely intact. No code modifications, no pushbutton, no pin mapping.

I left the thing running and with 2000ms delay, notifications were being sent correctly for about an hour until the disconnection loop started.

Could there be some kind of garbage collection or heap issue?

chegewara commented 6 years ago

To be honest i never been doing such long tests. You can add some memory debug command before or after notify() and one in onDisconnect callback.

GameDragon2k commented 6 years ago

What debug command should I be using exactly?

chegewara commented 6 years ago

I am using this: ESP_LOGW(TAG, "%d: - RAM left %d", __LINE__, esp_get_free_heap_size());

GameDragon2k commented 6 years ago

Okay, well that idea is out the window. Ram seems just fine.

Edit: I'm just gonna hope at this point my esp32 is just faulty, so I will pick up another one and just try again.

GameDragon2k commented 5 years ago

Unfortunately no luck. Purchased a new Sparkfun ESP32 Thing and getting the exact same issue.

chegewara commented 5 years ago

@GameDragon2k I really dont know what to say, it works for me and few other people. You could try to uninstall and make fresh install of arduino. Last one test will be if you install binary file i have compiled and tested on my esp32.

This bin is from you code (copy/paste) and if i remember its working for me: gamedragon2k_hid.bin.txt

You have to change extension and upload it with esp32 flash download tool.

senthilkumarh commented 5 years ago

BLE_Mouse.ino.txt @ chegewara the attached arduino sketch for ESP32 BT HID mouse is working in android , but not working in windows 10. Please can you help