arduino-libraries / ArduinoBLE

ArduinoBLE library for Arduino
GNU Lesser General Public License v2.1
313 stars 205 forks source link

Nano 33 BLE in bootmode after disconnect #172

Closed cansimkp closed 3 years ago

cansimkp commented 3 years ago

Hi, I have successfully paired an AcgamR1 clone remote control with the nano 33 ble. But after 45 seconds the remote control disconnects. Probably to save energy. Now the nano hangs up and does nothing more. No serial monitor and the Builtin LED is blinking 5 quick, 3 slow, 5 quick, 3 slow... I think the nano is in bootmode. (https://forum.arduino.cc/index.php?topic=630834.0)

Code is from here: https://github.com/bitbank2/Nano_33_Gamepad

Does anyone have a solution for this?

Software: Arduino IDE 1.8.13

1.2.0 Arduino mbed -enabled Boards V2.0.0 and same issue with 1.3.2 Hardware: Nano 33 BLE Original not a clone BLE Remote: https://www.amazon.de/magicsee-R1-Bluetooth-Wireless-Controller-Joystick/dp/B01MDM48W9
paulvha commented 3 years ago

I had this problem as well. The root cause is in HCI.cpp, void HCIClass::handleEventPkt(). When EVT_DISCONN_COMPLETE, it will try to HCI.leSetAdvertiseEnable(0x01); while you never have set anything to advertise. ArduinoBLE seems to assume you are running as a peripheral.

Solution In ArduinoBLE/src/uility/HCI.CPP fix a crash after disconnect when working as Central 3 changes are needed

1) In global space around line 73

AFTER :
     #define HCI_OE_USER_ENDED_CONNECTION 0x13  

ADD : 
 bool ScanResponseDataSet = false;

....................................................

2) In leSetScanResponseData() around line 232

AFTER : 
     memcpy(leScanResponseData.data, data, length);

ADD:
  // indicate running as peripheral {paulvha}
  ScanResponseDataSet = true;

....................................................

3) In handleEventPkt() around line 603

CHANGE : 
     HCI.leSetAdvertiseEnable(0x01);

TO :
    if (ScanResponseDataSet) HCI.leSetAdvertiseEnable(0x01);

regards, Paul

cansimkp commented 3 years ago

Thank you for your prompt reply.

But sorry, your solution doesn't work. As soon as the following condition is not met, the Nano goes into bootloader mode. `` while (peripheral.connected ()) { // while the peripheral is connected if (bChanged) { bChanged = 0;

ifdef USE_OLED

  ShowControllerState (u16Buttons); // update OLED graphic display

else

  sprintf (ucTemp, "Buttons changed: 0x% 04x", u16Buttons);
  ShowMsg (ucTemp, 0);

endif

}

} ``

I thought about breaking the while loop after 30 seconds before remote controll breaking the connection with "peripheral.disconnect ();" But here, too, the nano hangs up.

BLE.end (); does not crash the nano. This only happens when the connection is re-established.

best regards, kp

HCI.zip

facchinm commented 3 years ago

Hi @cansimkp, the "led blinking mode" signals when the core has crashed. You can get some information of what happened by connecting a serial-to-usb converter to pin TX1 configuring the port as 115200@8n1 ; pasting here the output would greatly help debugging the issue.

polldo commented 3 years ago

Hi @cansimkp This seems related to this issue https://github.com/arduino-libraries/ArduinoBLE/issues/128 This PR on the mbed core should fix it. Can you try this? thanks

facchinm commented 3 years ago

The PR should be already included in core 2.0.0, so if the core posted by @Polldo works and it still crashes on 2.0.0 we need to investigate the build process.

cansimkp commented 3 years ago

@Polldo: i need help with installation the core.I do not understand the installation instructions. I downloaded your core from github. Now i have a zip file named ArduinoCore-nRF528x-mbedos-master.zip. And now? Sorry :-)

i found this folder: C:\Users***\AppData\Local\Arduino15\packages\arduino\hardware\mbed\2.0.0

@facchinm: with normal Arduino mbed -enabled Boards V2.0.0: if connection is lost, here the serial output on TX1:

++ MbedOS Fault Handler ++

FaultType: HardFault

Context:
R0   : 00000000
R1   : 400019A8
R2   : 00000000
R3   : 400183B4
R4   : 1505EB04
R5   : 00016A0C
R6   : 20000718
R7   : 20000CAC
R8   : 20001148
R9   : 00000000
R10  : 00000000
R11  : 00000000
R12  : F8C59B03
SP   : 2000A740
LR   : 20000CB4
PC   : 000530BC
xPSR : 810F0000
PSP  : 2000A6D8
MSP  : 2003FFC0
CPUID: 410FC241
HFSR : 40000000
MMFSR: 00000000
BFSR : 00000004
UFSR : 00000000
DFSR : 00000000
AFSR : 00000000
Mode : Thread
Priv : Privileged
Stack: PSP

-- MbedOS Fault Handler --

++ MbedOS Error Info ++
Error Status: 0x80FF013D Code: 317 Module: 255
Error Message: Fault exception
Location: 0x530BC
Error Value: 0x20010764
Current Thread: main Id: 0x2000A95C Entry: 0x1922B StackSize: 0x8000 StackMem: 0x20002938 SP: 0x2000A740
For more info, visit: https://mbed.com/s/error?error=0x80FF013D&osver=60900&core=0x410FC241&comp=2&ver=100200&tgt=ARDUINO_NANO...

-- MbedOS Error Info --

**and again**

++ MbedOS Error Info ++
Error Status: 0x80FF013D Code: 317 Module: 255
Error Message: Fault exception
Location: 0x530BC
Error Value: 0x20010764
Current Thread: main Id: 0x2000A95C Entry: 0x1922B StackSize: 0x8000 StackMem: 0x20002938 SP: 0x2000A740
For more info, visit: https://mbed.com/s/error?error=0x80FF013D&osver=60900&core=0x410FC241&comp=2&ver=100200&tgt=ARDUINO_NANO...

-- MbedOS Error Info --

++ MbedOS Fault Handler ++

FaultType: HardFault

Context:
R0   : 00000000
R1   : 400019A8
R2   : 00000000
R3   : 400183B4
R4   : 1505EB04
R5   : 00016A0C
R6   : 20000718
R7   : 20000CAC
R8   : 20001148
R9   : 00000000
R10  : 00000000
R11  : 00000000
R12  : F8C59B03
SP   : 2000A740
LR   : 20000CB4
PC   : 000530BC
xPSR : 810F0000
PSP  : 2000A6D8
MSP  : 2003FFC0
CPUID: 410FC241
HFSR : 40000000
MMFSR: 00000000
BFSR : 00000004
UFSR : 00000000
DFSR : 00000000
AFSR : 00000000
Mode : Thread
Priv : Privileged
Stack: PSP

-- MbedOS Fault Handler --

++ MbedOS Error Info ++
Error Status: 0x80FF013D Code: 317 Module: 255
Error Message: Fault exception
Location: 0x530BC
Error Value: 0x20010764
Current Thread: main Id: 0x2000A95C Entry: 0x1922B StackSize: 0x8000 StackMem: 0x20002938 SP: 0x2000A740
For more info, visit: https://mbed.com/s/error?error=0x80FF013D&osver=60900&core=0x410FC241&comp=2&ver=100200&tgt=ARDUINO_NANO...

-- MbedOS Error Info --
facchinm commented 3 years ago

@cansimkp you can use this utility (https://github.com/janjongboom/mbed-os-example-fault-handler) to decode where the fault happened, given you provide the path to the compiled elf file (you can get it from the verbose output, it's in the root of the build folder). We'll try to replicate too, but it looks specific to the controller / code combination.

cansimkp commented 3 years ago

After many hours of googling, I was able to generate something. That didn't work: https://github.com/janjongboom/mbed-os-example-fault-handler But this one: https://github.com/ARMmbed/mbed-os/tree/master/tools/debug_tools/crash_log_parser Is that important?

Here the log from debugtool:

I've zipped and attached the entire arduino Build. And then the output of the compiler console.

C:\Users\nplmk\AppData\Local\Temp\arduino_build_184227>python crash_log_parser.py crash_log.txt C:\Users\nplmk\AppData\Local\Temp\arduino_build_184227\Nano_33_Gamepad.ino.elf C:\Users\nplmk\AppData\Local\Temp\arduino_build_184227\Nano_33_Gamepad.ino.map

Crash Info:
        Crash location = _free_r [0x000530BC] (based on PC value)
        Caller location = __exidx_start [0x20000CB4] (based on LR value)
        Stack Pointer at the time of crash = [2000A740]
        Target and Fault Info:
                Processor Arch: ARM-V7M or above
                Processor Variant: C24
                Forced exception, a fault with configurable priority has been escalated to HardFault
                Imprecise data access error has occurred

C:\Users\nplmk\AppData\Local\Temp\arduino_build_184227>

Console:

arduino_build_184227.zip

C:\Users\nplmk\Dropbox\Arduino_projekte\Nano_33_Gamepad\Nano_33_Gamepad.ino: In function 'void setup()':
C:\Users\nplmk\Dropbox\Arduino_projekte\Nano_33_Gamepad\Nano_33_Gamepad.ino:76:37: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
   ShowMsg("Scanning for BLE ctrl", 0);
                                     ^
C:\Users\nplmk\Dropbox\Arduino_projekte\Nano_33_Gamepad\Nano_33_Gamepad.ino:81:38: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
     ShowMsg("starting BLE failed!", 0);
                                      ^
C:\Users\nplmk\Dropbox\Arduino_projekte\Nano_33_Gamepad\Nano_33_Gamepad.ino: In function 'void HIDReportWritten(BLEDevice, BLECharacteristic)':
C:\Users\nplmk\Dropbox\Arduino_projekte\Nano_33_Gamepad\Nano_33_Gamepad.ino:131:11: warning: unused variable 'i' [-Wunused-variable]
 int iLen, i;
           ^
C:\Users\nplmk\Dropbox\Arduino_projekte\Nano_33_Gamepad\Nano_33_Gamepad.ino: In function 'void monitorActions(BLEDevice)':
C:\Users\nplmk\Dropbox\Arduino_projekte\Nano_33_Gamepad\Nano_33_Gamepad.ino:147:29: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
   ShowMsg("Connecting...", 4);
                             ^
C:\Users\nplmk\Dropbox\Arduino_projekte\Nano_33_Gamepad\Nano_33_Gamepad.ino:149:27: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
     ShowMsg("Connected", 5);
                           ^
C:\Users\nplmk\Dropbox\Arduino_projekte\Nano_33_Gamepad\Nano_33_Gamepad.ino:151:36: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
     ShowMsg("Failed to connect!", 5);
                                    ^
C:\Users\nplmk\Dropbox\Arduino_projekte\Nano_33_Gamepad\Nano_33_Gamepad.ino:158:35: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
     ShowMsg("0x1812 discovered", 6);
                                   ^
C:\Users\nplmk\Dropbox\Arduino_projekte\Nano_33_Gamepad\Nano_33_Gamepad.ino:160:36: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
     ShowMsg("0x1812 disc failed", 6);
                                    ^
C:\Users\nplmk\Dropbox\Arduino_projekte\Nano_33_Gamepad\Nano_33_Gamepad.ino:187:22: warning: converting to non-pointer type 'int' from NULL [-Wconversion-null]
   if (protmodChar != NULL)
                      ^~~~
C:\Users\nplmk\Dropbox\Arduino_projekte\Nano_33_Gamepad\Nano_33_Gamepad.ino:221:29: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
   ShowMsg("Ready to use!", 7);
                             ^
C:\Users\nplmk\Dropbox\Arduino_projekte\Nano_33_Gamepad\Nano_33_Gamepad.ino:141:8: warning: unused variable 'iLen' [-Wunused-variable]
 int i, iLen, iCount;
        ^~~~
C:\Users\nplmk\Dropbox\Arduino_projekte\libraries\arduino_955387\src\utility\ATT.cpp: In member function 'virtual bool ATTClass::handleNotify(uint16_t, const uint8_t*, int)':
C:\Users\nplmk\Dropbox\Arduino_projekte\libraries\arduino_955387\src\utility\ATT.cpp:522:39: warning: variable length array 'notification' is used [-Wvla]
     uint8_t notification[_peers[i].mtu];
                                       ^
C:\Users\nplmk\Dropbox\Arduino_projekte\libraries\arduino_955387\src\utility\ATT.cpp: In member function 'virtual bool ATTClass::handleInd(uint16_t, const uint8_t*, int)':
C:\Users\nplmk\Dropbox\Arduino_projekte\libraries\arduino_955387\src\utility\ATT.cpp:552:37: warning: variable length array 'indication' is used [-Wvla]
     uint8_t indication[_peers[i].mtu];
                                     ^
C:\Users\nplmk\Dropbox\Arduino_projekte\libraries\arduino_955387\src\utility\ATT.cpp: In member function 'virtual void ATTClass::findInfoReq(uint16_t, uint16_t, uint8_t, uint8_t*)':
C:\Users\nplmk\Dropbox\Arduino_projekte\libraries\arduino_955387\src\utility\ATT.cpp:675:23: warning: variable length array 'response' is used [-Wvla]
   uint8_t response[mtu];
                       ^
C:\Users\nplmk\Dropbox\Arduino_projekte\libraries\arduino_955387\src\utility\ATT.cpp: In member function 'virtual void ATTClass::findByTypeReq(uint16_t, uint16_t, uint8_t, uint8_t*)':
C:\Users\nplmk\Dropbox\Arduino_projekte\libraries\arduino_955387\src\utility\ATT.cpp:767:23: warning: variable length array 'response' is used [-Wvla]
   uint8_t response[mtu];
                       ^
C:\Users\nplmk\Dropbox\Arduino_projekte\libraries\arduino_955387\src\utility\ATT.cpp: In member function 'virtual void ATTClass::readByGroupReq(uint16_t, uint16_t, uint8_t, uint8_t*)':
C:\Users\nplmk\Dropbox\Arduino_projekte\libraries\arduino_955387\src\utility\ATT.cpp:817:23: warning: variable length array 'response' is used [-Wvla]
   uint8_t response[mtu];
                       ^
C:\Users\nplmk\Dropbox\Arduino_projekte\libraries\arduino_955387\src\utility\ATT.cpp: In member function 'virtual void ATTClass::readOrReadBlobReq(uint16_t, uint16_t, uint8_t, uint8_t, uint8_t*)':
C:\Users\nplmk\Dropbox\Arduino_projekte\libraries\arduino_955387\src\utility\ATT.cpp:919:23: warning: variable length array 'response' is used [-Wvla]
   uint8_t response[mtu];
                       ^
C:\Users\nplmk\Dropbox\Arduino_projekte\libraries\arduino_955387\src\utility\ATT.cpp: In member function 'virtual void ATTClass::readByTypeReq(uint16_t, uint16_t, uint8_t, uint8_t*)':
C:\Users\nplmk\Dropbox\Arduino_projekte\libraries\arduino_955387\src\utility\ATT.cpp:1024:23: warning: variable length array 'response' is used [-Wvla]
   uint8_t response[mtu];
                       ^
C:\Users\nplmk\Dropbox\Arduino_projekte\libraries\arduino_955387\src\utility\ATT.cpp: In member function 'virtual void ATTClass::writeReqOrCmd(uint16_t, uint16_t, uint8_t, uint8_t, uint8_t*)':
C:\Users\nplmk\Dropbox\Arduino_projekte\libraries\arduino_955387\src\utility\ATT.cpp:1224:25: warning: variable length array 'response' is used [-Wvla]
     uint8_t response[mtu];
                         ^
C:\Users\nplmk\Dropbox\Arduino_projekte\libraries\arduino_955387\src\utility\ATT.cpp: In member function 'virtual void ATTClass::prepWriteReq(uint16_t, uint16_t, uint8_t, uint8_t*)':
C:\Users\nplmk\Dropbox\Arduino_projekte\libraries\arduino_955387\src\utility\ATT.cpp:1310:23: warning: variable length array 'response' is used [-Wvla]
   uint8_t response[mtu];
                       ^
C:\Users\nplmk\Dropbox\Arduino_projekte\libraries\arduino_955387\src\utility\ATT.cpp: In member function 'virtual void ATTClass::execWriteReq(uint16_t, uint16_t, uint8_t, uint8_t*)':
C:\Users\nplmk\Dropbox\Arduino_projekte\libraries\arduino_955387\src\utility\ATT.cpp:1343:23: warning: variable length array 'response' is used [-Wvla]
   uint8_t response[mtu];
                       ^
C:\Users\nplmk\Dropbox\Arduino_projekte\libraries\arduino_955387\src\utility\ATT.cpp: In member function 'virtual bool ATTClass::exchangeMtu(uint16_t)':
C:\Users\nplmk\Dropbox\Arduino_projekte\libraries\arduino_955387\src\utility\ATT.cpp:1425:33: warning: variable length array 'responseBuffer' is used [-Wvla]
   uint8_t responseBuffer[_maxMtu];
                                 ^
C:\Users\nplmk\Dropbox\Arduino_projekte\libraries\arduino_955387\src\utility\ATT.cpp: In member function 'virtual bool ATTClass::discoverServices(uint16_t, BLERemoteDevice*, const char*)':
C:\Users\nplmk\Dropbox\Arduino_projekte\libraries\arduino_955387\src\utility\ATT.cpp:1439:33: warning: variable length array 'responseBuffer' is used [-Wvla]
   uint8_t responseBuffer[_maxMtu];
                                 ^
C:\Users\nplmk\Dropbox\Arduino_projekte\libraries\arduino_955387\src\utility\ATT.cpp: In member function 'virtual bool ATTClass::discoverCharacteristics(uint16_t, BLERemoteDevice*)':
C:\Users\nplmk\Dropbox\Arduino_projekte\libraries\arduino_955387\src\utility\ATT.cpp:1495:33: warning: variable length array 'responseBuffer' is used [-Wvla]
   uint8_t responseBuffer[_maxMtu];
                                 ^
C:\Users\nplmk\Dropbox\Arduino_projekte\libraries\arduino_955387\src\utility\ATT.cpp: In member function 'virtual bool ATTClass::discoverDescriptors(uint16_t, BLERemoteDevice*)':
C:\Users\nplmk\Dropbox\Arduino_projekte\libraries\arduino_955387\src\utility\ATT.cpp:1552:33: warning: variable length array 'responseBuffer' is used [-Wvla]
   uint8_t responseBuffer[_maxMtu];
                                 ^
C:\Users\nplmk\Dropbox\Arduino_projekte\libraries\arduino_955387\src\utility\HCI.cpp: In member function 'virtual int HCIClass::sendAclPkt(uint16_t, uint8_t, uint8_t, void*)':
C:\Users\nplmk\Dropbox\Arduino_projekte\libraries\arduino_955387\src\utility\HCI.cpp:435:41: warning: variable length array 'txBuffer' is used [-Wvla]
   uint8_t txBuffer[sizeof(aclHdr) + plen];
                                         ^
C:\Users\nplmk\Dropbox\Arduino_projekte\libraries\arduino_955387\src\utility\HCI.cpp: In member function 'virtual int HCIClass::sendCommand(uint16_t, uint8_t, void*)':
C:\Users\nplmk\Dropbox\Arduino_projekte\libraries\arduino_955387\src\utility\HCI.cpp:477:41: warning: variable length array 'txBuffer' is used [-Wvla]
   uint8_t txBuffer[sizeof(pktHdr) + plen];
                                         ^
C:\Users\nplmk\Dropbox\Arduino_projekte\libraries\arduino_955387\src\utility\HCICordioTransport.cpp: In function 'void bleLoop()':
C:\Users\nplmk\Dropbox\Arduino_projekte\libraries\arduino_955387\src\utility\HCICordioTransport.cpp:123:68: warning: 'us_timestamp_t mbed::TimerBase::read_high_resolution_us() const' is deprecated: Use the Chrono-based elapsed_time method.  If integer microseconds are needed, you can use `elapsed_time().count()` [since mbed-os-6.0.0] [-Wdeprecated-declarations]
         last_update_us += (uint64_t) timer.read_high_resolution_us();
                                                                    ^
In file included from C:\Users\nplmk\Documents\ArduinoData\packages\arduino\hardware\mbed\2.0.0\cores\arduino/mbed/mbed.h:82:0,
                 from C:\Users\nplmk\Documents\ArduinoData\packages\arduino\hardware\mbed\2.0.0\cores\arduino/mbed.h:14,
                 from C:\Users\nplmk\Dropbox\Arduino_projekte\libraries\arduino_955387\src\utility\HCICordioTransport.cpp:23:
C:\Users\nplmk\Documents\ArduinoData\packages\arduino\hardware\mbed\2.0.0\cores\arduino/mbed/drivers/include/drivers/Timer.h:104:20: note: declared here
     us_timestamp_t read_high_resolution_us() const;
                    ^~~~~~~~~~~~~~~~~~~~~~~
C:\Users\nplmk\Dropbox\Arduino_projekte\libraries\arduino_955387\src\utility\HCICordioTransport.cpp:145:72: warning: 'us_timestamp_t mbed::TimerBase::read_high_resolution_us() const' is deprecated: Use the Chrono-based elapsed_time method.  If integer microseconds are needed, you can use `elapsed_time().count()` [since mbed-os-6.0.0] [-Wdeprecated-declarations]
         uint64_t time_spent = (uint64_t) timer.read_high_resolution_us();
                                                                        ^
In file included from C:\Users\nplmk\Documents\ArduinoData\packages\arduino\hardware\mbed\2.0.0\cores\arduino/mbed/mbed.h:82:0,
                 from C:\Users\nplmk\Documents\ArduinoData\packages\arduino\hardware\mbed\2.0.0\cores\arduino/mbed.h:14,
                 from C:\Users\nplmk\Dropbox\Arduino_projekte\libraries\arduino_955387\src\utility\HCICordioTransport.cpp:23:
C:\Users\nplmk\Documents\ArduinoData\packages\arduino\hardware\mbed\2.0.0\cores\arduino/mbed/drivers/include/drivers/Timer.h:104:20: note: declared here
     us_timestamp_t read_high_resolution_us() const;
                    ^~~~~~~~~~~~~~~~~~~~~~~
C:\Users\nplmk\Dropbox\Arduino_projekte\libraries\arduino_955387\src\utility\HCICordioTransport.cpp:156:55: warning: 'void rtos::ThisThread::sleep_for(uint32_t)' is deprecated: Pass a chrono duration, not an integer millisecond count. For example use `5s` rather than `5000`. [since mbed-os-6.0.0] [-Wdeprecated-declarations]
               rtos::ThisThread::sleep_for(wait_time_ms);
                                                       ^
In file included from C:\Users\nplmk\Documents\ArduinoData\packages\arduino\hardware\mbed\2.0.0\cores\arduino/mbed/rtos/include/rtos/rtos.h:30:0,
                 from C:\Users\nplmk\Documents\ArduinoData\packages\arduino\hardware\mbed\2.0.0\cores\arduino/mbed/mbed.h:24,
                 from C:\Users\nplmk\Documents\ArduinoData\packages\arduino\hardware\mbed\2.0.0\cores\arduino/mbed.h:14,
                 from C:\Users\nplmk\Dropbox\Arduino_projekte\libraries\arduino_955387\src\utility\HCICordioTransport.cpp:23:
C:\Users\nplmk\Documents\ArduinoData\packages\arduino\hardware\mbed\2.0.0\cores\arduino/mbed/rtos/include/rtos/ThisThread.h:216:6: note: declared here
 void sleep_for(uint32_t millisec);
      ^~~~~~~~~
Der Sketch verwendet 316512 Bytes (32%) des Programmspeicherplatzes. Das Maximum sind 983040 Bytes.
Globale Variablen verwenden 69352 Bytes (26%) des dynamischen Speichers, 192792 Bytes für lokale Variablen verbleiben. Das Maximum sind 262144 Bytes.
Device       : nRF52840-QIAA
Version      : Arduino Bootloader (SAM-BA extended) 2.0 [Arduino:IKXYZ]
Address      : 0x0
Pages        : 256
Page Size    : 4096 bytes
Total Size   : 1024KB
Planes       : 1
Lock Regions : 0
Locked       : none
Security     : false
Erase flash

Done in 0.001 seconds
Write 316520 bytes to flash (78 pages)
[==============================] 100% (78/78 pages)
Done in 13.326 seconds
polldo commented 3 years ago

@cansimkp sorry, I initially misunderstood the problem, it's not related to the issue I linked to.

Can you enable the BLE serial debug and report here the result of the serial monitor? To enable the BLE debug you can simply add the following line in the setup function: BLE.debug(Serial)

Also, you could try to insert this three-line patch into your ArduinoBLE library: https://github.com/arduino-libraries/ArduinoBLE/pull/96/commits/980df5ae597b62422bc96e5db06c1e67bca55cab#diff-1a79f029352e98b813aaf6a97ee51cf595503c6e75ae51cb4a04a5f705db9853R261-R263 Then let me know if anything changes

Thank you

cansimkp commented 3 years ago

@Polldo: BLE.debug(Serial) shows this:

20:19:39.091 -> Found 42:6a:a9:66:5f:9b
20:19:39.091 ->  - fd6f
20:19:39.091 -> HCI EVENT RX <- 043E1002010201E72545A4AC510403036FFDBB
20:19:39.091 -> HCI EVENT RX <- 043E0C02010401E72545A4AC5100BB
20:19:39.091 -> Found ff:ff:38:5a:d2:4e
20:19:39.091 -> Magicsee R1        - 1812                            **<-- Remote connected**
20:19:39.091 -> HCI COMMAND TX -> 010C20020000
20:19:39.091 -> HCI EVENT RX <- 043E2802010201B8D3AAE8DE7E1C03036FFD17166FFD943BFBEFAC784F07388AE1CF86A21E60C5CFDDBAB6
20:19:39.124 -> HCI EVENT RX <- 040E04010C2000
20:19:39.124 -> Connecting...
20:19:39.124 -> HCI COMMAND TX -> 010D20196000300000004ED25A38FFFF0006000C000000C80004000600
20:19:39.160 -> HCI EVENT RX <- 040F0400010D20
20:19:39.160 -> HCI EVENT RX <- 043E130100000000004ED25A38FFFF08000000C80007
20:19:39.160 -> Connected
20:19:39.160 -> HCI ACLDATA TX -> 02000007000300040002F700
20:19:39.200 -> HCI EVENT RX <- 0413050104000100
20:19:39.200 -> HCI EVENT RX <- 0413050100000100
20:19:39.200 -> HCI ACLDATA RX <- 020020070003000400031700
20:19:39.200 -> HCI ACLDATA TX -> 0200000B0007000400100100FFFF0028
20:19:39.241 -> HCI EVENT RX <- 0413050104000100
20:19:39.241 -> HCI EVENT RX <- 0413050100000100
20:19:39.241 -> HCI ACLDATA RX <- 020020180014000400110601000700001808000B0001180C000E000A18
20:19:39.286 -> HCI ACLDATA TX -> 0200000B0007000400100F00FFFF0028
20:19:39.286 -> HCI EVENT RX <- 0413050104000100
20:19:39.286 -> HCI EVENT RX <- 0413050100000100
20:19:39.286 -> HCI ACLDATA RX <- 02002012000E00040011060F002700121828002A000F18
20:19:39.321 -> HCI ACLDATA TX -> 0200000B0007000400102B00FFFF0028
20:19:39.360 -> HCI EVENT RX <- 0413050104000100
20:19:39.360 -> HCI EVENT RX <- 0413050100000100
20:19:39.360 -> HCI ACLDATA RX <- 02002009000500040001102B000A
20:19:39.360 -> HCI ACLDATA TX -> 0200000B0007000400080F0027000328
20:19:39.400 -> HCI EVENT RX <- 0413050104000100
20:19:39.400 -> HCI EVENT RX <- 0413050100000100
20:19:39.400 -> HCI ACLDATA RX <- 0200201B0017000400090710000611004E2A12001213004D2A16001217004D2A
20:19:39.435 -> HCI ACLDATA TX -> 0200000B000700040008180027000328
20:19:39.476 -> HCI EVENT RX <- 0413050104000100
20:19:39.476 -> HCI EVENT RX <- 0413050100000100
20:19:39.476 -> HCI ACLDATA RX <- 0200201B001700040009071A00121B004D2A1E00121F004D2A22000223004B2A
20:19:39.476 -> HCI ACLDATA TX -> 0200000B000700040008240027000328
20:19:39.516 -> HCI EVENT RX <- 0413050104000100
20:19:39.516 -> HCI EVENT RX <- 0413050100000100
20:19:39.516 -> HCI ACLDATA RX <- 020020140010000400090724000225004A2A26000427004C2A
20:19:39.550 -> HCI ACLDATA TX -> 0200000B000700040008280027000328
20:19:39.550 -> HCI EVENT RX <- 0413050104000100
20:19:39.550 -> HCI EVENT RX <- 0413050100000100
20:19:39.590 -> HCI ACLDATA RX <- 020020090005000400010828000A
20:19:39.590 -> HCI ACLDATA TX -> 0200000900050004000412001300
20:19:39.590 -> HCI EVENT RX <- 0413050104000100
20:19:39.590 -> HCI EVENT RX <- 0413050100000100
20:19:39.624 -> HCI ACLDATA RX <- 0200200E000A00040005011200032813004D2A
20:19:39.624 -> HCI ACLDATA TX -> 0200000900050004000414001300
20:19:39.624 -> HCI EVENT RX <- 0413050104000100
20:19:39.624 -> HCI EVENT RX <- 0413050100000100
20:19:39.664 -> HCI ACLDATA RX <- 020020090005000400010414000A
20:19:39.664 -> HCI ACLDATA TX -> 0200000900050004000414001700
20:19:39.664 -> HCI EVENT RX <- 0413050104000100
20:19:39.700 -> HCI EVENT RX <- 0413050100000100
20:19:39.700 -> HCI ACLDATA RX <- 020020160012000400050114000229150008291600032817004D2A
20:19:39.700 -> HCI ACLDATA TX -> 0200000900050004000418001700
20:19:39.746 -> HCI EVENT RX <- 0413050104000100
20:19:39.746 -> HCI EVENT RX <- 0413050100000100
20:19:39.746 -> HCI ACLDATA RX <- 020020090005000400010418000A
20:19:39.746 -> HCI ACLDATA TX -> 0200000900050004000418001B00
20:19:39.787 -> HCI EVENT RX <- 0413050104000100
20:19:39.787 -> HCI EVENT RX <- 0413050100000100
20:19:39.787 -> HCI ACLDATA RX <- 020020160012000400050118000229190008291A0003281B004D2A
20:19:39.787 -> HCI ACLDATA TX -> 020000090005000400041C001B00
20:19:39.834 -> HCI EVENT RX <- 0413050104000100
20:19:39.834 -> HCI EVENT RX <- 0413050100000100
20:19:39.834 -> HCI ACLDATA RX <- 02002009000500040001041C000A
20:19:39.834 -> HCI ACLDATA TX -> 020000090005000400041C001F00
20:19:39.834 -> HCI EVENT RX <- 0413050104000100
20:19:39.868 -> HCI EVENT RX <- 0413050100000100
20:19:39.868 -> HCI ACLDATA RX <- 02002016001200040005011C0002291D0008291E0003281F004D2A
20:19:39.868 -> HCI ACLDATA TX -> 0200000900050004000420001F00
20:19:39.908 -> HCI EVENT RX <- 0413050104000100
20:19:39.908 -> HCI EVENT RX <- 0413050100000100
20:19:39.908 -> HCI ACLDATA RX <- 020020090005000400010420000A
20:19:39.908 -> HCI ACLDATA TX -> 0200000900050004000420002300
20:19:39.950 -> HCI EVENT RX <- 0413050104000100
20:19:39.950 -> HCI EVENT RX <- 0413050100000100
20:19:39.950 -> HCI ACLDATA RX <- 020020160012000400050120000229210008292200032823004B2A
20:19:39.991 -> HCI ACLDATA TX -> 0200000900050004000424002300
20:19:39.991 -> HCI EVENT RX <- 0413050104000100
20:19:39.991 -> HCI EVENT RX <- 0413050100000100
20:19:40.027 -> HCI ACLDATA RX <- 020020090005000400010424000A
20:19:40.027 -> HCI ACLDATA TX -> 0200000900050004000424002500
20:19:40.027 -> HCI EVENT RX <- 0413050104000100
20:19:40.066 -> HCI EVENT RX <- 0413050100000100
20:19:40.066 -> HCI ACLDATA RX <- 0200200E000A00040005012400032825004A2A
20:19:40.066 -> HCI ACLDATA TX -> 0200000900050004000426002500
20:19:40.100 -> HCI EVENT RX <- 0413050104000100
20:19:40.100 -> HCI EVENT RX <- 0413050100000100
20:19:40.100 -> HCI ACLDATA RX <- 020020090005000400010426000A
20:19:40.100 -> HCI ACLDATA TX -> 0200000900050004000426002700
20:19:40.144 -> HCI EVENT RX <- 0413050104000100
20:19:40.144 -> HCI EVENT RX <- 0413050100000100
20:19:40.144 -> HCI ACLDATA RX <- 0200200E000A00040005012600032827004C2A
20:19:40.144 -> HCI ACLDATA TX -> 0200000900050004000428002700
20:19:40.200 -> HCI ACLDATA RX <- 0200200600020006000B01
20:19:40.200 -> HCI ACLDATA TX -> 0200000E000A00050001000600020006000000
20:19:40.200 -> HCI EVENT RX <- 0413050104000100
20:19:40.200 -> HCI EVENT RX <- 0413050100000100
20:19:40.200 -> HCI ACLDATA RX <- 020020090005000400010428000A
20:19:40.240 -> HCI EVENT RX <- 0413050104000100
20:19:40.240 -> HCI EVENT RX <- 0413050100000100
20:19:40.240 -> 0x1812 discovered
20:19:40.240 -> HCI ACLDATA TX -> 0200000900050004001214000100
20:19:40.240 -> HCI EVENT RX <- 0413050104000100
20:19:40.240 -> HCI EVENT RX <- 0413050100000100
20:19:40.282 -> HCI ACLDATA RX <- 02002005000100040013
20:19:40.282 -> HCI ACLDATA TX -> 0200000900050004001218000100
20:19:40.282 -> HCI EVENT RX <- 0413050104000100
20:19:40.282 -> HCI EVENT RX <- 0413050100000100
20:19:40.324 -> HCI ACLDATA RX <- 02002005000100040013
20:19:40.324 -> HCI ACLDATA TX -> 020000090005000400121C000100
20:19:40.324 -> HCI EVENT RX <- 0413050104000100
20:19:40.364 -> HCI EVENT RX <- 0413050100000100
20:19:40.364 -> HCI ACLDATA RX <- 02002005000100040013
20:19:40.364 -> HCI ACLDATA TX -> 0200000900050004001220000100
20:19:40.364 -> HCI EVENT RX <- 0413050104000100
20:19:40.400 -> HCI EVENT RX <- 0413050100000100
20:19:40.400 -> HCI ACLDATA RX <- 02002005000100040013
20:19:40.400 -> HCI ACLDATA TX -> 02000008000400040052110001
20:19:40.400 -> Ready to use!
20:19:42.390 -> HCI EVENT RX <- 0413050104000100
20:19:42.431 -> HCI EVENT RX <- 0413050100000100
20:19:42.431 -> Buttons changed: 0x0000
20:19:43.736 -> HCI ACLDATA RX <- 0200200900050004001B17000100
20:19:43.776 -> Buttons changed: 0x0501
20:19:43.936 -> HCI ACLDATA RX <- 0200200900050004001B17000000
20:19:43.936 -> Buttons changed: 0x0500
20:19:44.644 -> HCI ACLDATA RX <- 0200200900050004001B17000200
20:19:44.684 -> Buttons changed: 0x0502
20:19:44.862 -> HCI ACLDATA RX <- 0200200900050004001B17000000
20:19:44.862 -> Buttons changed: 0x0500
20:19:50.482 -> HCI EVENT RX <- 04050400000008
20:19:50.482 -> HCI EVENT RX <- 0413050100000000                **at this point i disconnet remote control and nano crashed**

The nano also crashes with the three line patch.

But I found something out. When I upload the modified LED example, nothing crashes. However, this only searches for the battery service 0x180F and the characteristic 0x2A19. Unfortunately, my BLE knowledge is not sufficient to establish the HID connection. That's why I wanted to take over the part from bitbank2.

best regards kp

modified LED example

/*
  LED Control

  This example scans for BLE peripherals until one with the advertised service
  "19b10000-e8f2-537e-4f6c-d104768a1214" UUID is found. Once discovered and connected,
  it will remotely control the BLE Peripheral's LED, when the button is pressed or released.

  The circuit:
  - Arduino MKR WiFi 1010, Arduino Uno WiFi Rev2 board, Arduino Nano 33 IoT,
    Arduino Nano 33 BLE, or Arduino Nano 33 BLE Sense board.
  - Button with pull-up resistor connected to pin 2.

  You can use it with another board that is compatible with this library and the
  Peripherals -> LED example.

  This example code is in the public domain.
*/

#include <ArduinoBLE.h>

// variables for button
const int buttonPin = 2;
int oldButtonState = LOW;

void setup() {
  Serial.begin(9600);
  while (!Serial);

  // configure the button pin as input
  pinMode(buttonPin, INPUT);

  // initialize the BLE hardware
  BLE.begin();

  Serial.println("BLE Central - LED control");

  // start scanning for peripherals
  //BLE.scanForUuid("180F");
  BLE.scan();
}

void loop() {
  // check if a peripheral has been discovered
  BLEDevice peripheral = BLE.available();

  if (peripheral) {
    // discovered a peripheral, print out address, local name, and advertised service
    Serial.print("Found ");
    Serial.print(peripheral.address());
    Serial.print(" '");
    Serial.print(peripheral.localName());
    Serial.print("' ");
    Serial.print(peripheral.advertisedServiceUuid());
    Serial.println();

  /*  if (peripheral.localName() != "Long name works now") {
      return;
    }*/
if (peripheral.localName() != "Magicsee R1       ") {
      return;
    }
    // stop scanning
    BLE.stopScan();

    controlLed(peripheral);

    // peripheral disconnected, start scanning again
    BLE.scan();
  }
}

void controlLed(BLEDevice peripheral) {
  // connect to the peripheral
  Serial.println("Connecting ...");

  if (peripheral.connect()) {
    Serial.println("Connected");
  } else {
    Serial.println("Failed to connect!");
    return;
  }

  // discover peripheral attributes
  Serial.println("Discovering attributes ...");
  if (peripheral.discoverAttributes()) {
    Serial.println("Attributes discovered");
  } else {
    Serial.println("Attribute discovery failed!");
    peripheral.disconnect();
    return;
  }

  // retrieve the LED characteristic
//  BLECharacteristic ledCharacteristic = peripheral.characteristic("beb5483e-36e1-4688-b7f5-ea07361b26a8");
  BLECharacteristic ledCharacteristic = peripheral.characteristic("2A19");

  if (!ledCharacteristic) {
    Serial.println("Peripheral does not have LED characteristic!");
    peripheral.disconnect();
    return;
  } else if (!ledCharacteristic.canRead()) {
    Serial.println("Peripheral does not have a writable LED characteristic!");
    peripheral.disconnect();
    return;
  }

  while (peripheral.connected()) {
    // while the peripheral is connected

    // read the button pin
    int buttonState = digitalRead(buttonPin);

    if (oldButtonState != buttonState) {
      // button changed
      oldButtonState = buttonState;

      if (buttonState) {
        Serial.println("button pressed");

        // button is pressed, write 0x01 to turn the LED on
        //ledCharacteristic.writeValue((byte)0x01);
      } else {
        Serial.println("button released");

        // button is released, write 0x00 to turn the LED off
       // ledCharacteristic.writeValue((byte)0x00);
      }
    }
  }

  Serial.println("Peripheral disconnected");
}
cansimkp commented 3 years ago

I changed this to this:

 **DELETED** BLEService hidService;

  if (peripheral.connect()) {
    Serial.println("Connected");
  } else {
    Serial.println("Failed to connect!");
    return;
  }

  if (peripheral.discoverService("1812")) {
    Serial.println("0x1812 discovered");
  } else {
    Serial.println("0x1812 disc failed");
    peripheral.disconnect();

   **DELETED** while (1);
    return;
  }
**added** BLEService hidService     = peripheral.service("1812"); // get the HID service
**DELETED**  hidService = peripheral.service("1812"); // get the HID service`

NOW IT RUN !!!!

Thank u all