ESP32-S3 BLE Issues

xcnavug
Posts: 7
Joined: Sat Sep 16, 2023 11:45 pm

ESP32-S3 BLE Issues

Postby xcnavug » Wed Dec 27, 2023 3:53 am

Hi,
I got a custom made ESP32-S3 pcb. I tried my sketch wich is running on an ESP32-Wroom 32D Devboard. I can uoload it and got it running via USB with some modifications, but i can not make it advertice ble, even with using nimBLE.
What could be the reason?
Thank you

  1. //Changelog:
  2. //made center doubleklick toggle between STF/Vario (toggle sending S/V)
  3. // Reset XCREMOTE with 5sec X-Hold added
  4. //New BLEKeyboard Library keeps BLE Advertising when disconnected
  5.  
  6.  
  7.  
  8.  
  9. // uses libraries from
  10. // https://github.com/r89m/PushButton
  11. // https://github.com/r89m/Button
  12. // https://github.com/thomasfredericks/Bounce2
  13.  
  14. #include <Button.h>
  15. #include <ButtonEventCallback.h>
  16. #include <PushButton.h>
  17. #include <Bounce2.h>
  18. #include <BluetoothSerial.h>
  19. #include <BleKeyboard.h>
  20.  
  21. // Updating server
  22. #include <WiFi.h>
  23. #include <WiFiClient.h>
  24. #include <WebServer.h>
  25. #include <ESPmDNS.h>
  26. #include <Update.h>
  27. #include "OneButton.h"
  28.  
  29. // Version
  30. #define VERSION "2.3.2"
  31. #define USE_NIMBLE
  32.  
  33. // Init BLE
  34. BleKeyboard bleKeyboard("XCREMOTE", "XCNAV UG", 100);
  35.  
  36.  
  37. // Pin Assignments
  38.  
  39. const int LED_Pin = 02;
  40.  
  41. const int Up_Pin = 15;            // UP         (Joystick Up)
  42. const int Down_Pin = 26;          // Down       (Joystick Down)
  43. const int Left_Pin = 25;          // Left       (Joystick Left)
  44. const int Right_Pin = 27;         // Right      (Joystick Right)
  45. const int Center_Pin = 17;        // Enter      (Joystick Press)
  46. const int Rectangle_Pin = 18;     // Rectangle  (Button 2)
  47. const int Triangle_Pin = 05;      // Triangle   (Button 3)
  48. const int Circle_Pin = 33;        // Circle     (Button 4)
  49. const int Cancel_Pin = 04;        // X          (Button 5)
  50.  
  51. // Button's keys
  52. const char Up_Press_Key = KEY_UP_ARROW;
  53. const char Down_Press_Key = KEY_DOWN_ARROW;
  54. const char Left_Press_Key = KEY_LEFT_ARROW;
  55. const char Right_Press_Key = KEY_RIGHT_ARROW;
  56. const char Rectangle_Press_Key = KEY_F4;
  57. const char Rectangle_Hold_Key = KEY_F3;
  58. const char Triangle_Press_Key = KEY_F6;
  59. const char Triangle_Hold_Key = KEY_F2;
  60. const char Circle_Press_Key = 'M';
  61. const char Circle_Hold_Key = KEY_F1;
  62. const char Cancel_Press_Key = KEY_ESC;
  63. const char Cancel_Hold_Key = 'T';
  64.  
  65.  
  66.  
  67. const int Joy_Rebounce_Interval = 3;
  68. const int Joy_Rebounce_Threshold = 20;
  69. const int Joy_Active_Threshold = 100;
  70. const int Button_Hold_Threshold = 500;
  71. const int Button_Rebounce_Interval = 500;
  72. const int Reset_Threshold = 5000;
  73. const int Joy_Hold_Threshold = 1;
  74.  
  75. // PushButton's instances
  76. PushButton Up = PushButton(Up_Pin);
  77. PushButton Down = PushButton(Down_Pin);
  78. PushButton Left = PushButton(Left_Pin);
  79. PushButton Right = PushButton(Right_Pin);
  80. PushButton Center = PushButton(Center_Pin);
  81. PushButton Rectangle = PushButton(Rectangle_Pin);
  82. PushButton Triangle = PushButton(Triangle_Pin);
  83. PushButton Circle = PushButton(Circle_Pin);
  84. PushButton Cancel = PushButton(Cancel_Pin);
  85.  
  86.  
  87. // Setup a new OneButton on pin PIN_INPUT
  88. // The 2. parameter activeLOW is true, because external wiring sets the button to LOW when pressed.
  89. OneButton button(Center_Pin, true);
  90.  
  91. // save the millis when a press has started.
  92. unsigned long HoldCenterTime;
  93.  
  94. // Variables
  95. int bt_first_connected = false;
  96. boolean Joy_Inactive = true;
  97. int Joy_Active_Counter =   0;
  98.  
  99.  
  100.  
  101.  
  102. // Updating server
  103. const char* host = "esp32";
  104. const char* ssid = "XCREMOTE";
  105. //const char* ssid = "xcremote";
  106. const char* password = "xcremote";
  107. boolean server_running = 0;
  108. WebServer server(80);
  109.  
  110.  
  111. // current Cruise_Climb (Joy Center) state, staring with LOW (0)
  112. int Cruise_Climb = LOW;
  113.  
  114. void setup() {
  115.  
  116.    pinMode(Up_Pin, INPUT_PULLUP);
  117.    pinMode(Down_Pin, INPUT_PULLUP);
  118.    pinMode(Left_Pin, INPUT_PULLUP);
  119.    pinMode(Right_Pin, INPUT_PULLUP);
  120.    pinMode(Center_Pin, INPUT_PULLUP);
  121.    pinMode(Rectangle_Pin, INPUT_PULLUP);
  122.    pinMode(Triangle_Pin, INPUT_PULLUP);
  123.    pinMode(Circle_Pin, INPUT_PULLUP);
  124.    pinMode(Cancel_Pin, INPUT_PULLUP);
  125.  
  126.    Serial.begin(115200);
  127.    Serial.println("Version: " VERSION);
  128.    Serial.print("XCRemote MAC Address:  ");
  129.    Serial.println(WiFi.macAddress());
  130.  
  131.    // link the xxxclick functions to be called on xxxclick event.
  132.    button.attachClick(SingleClick);
  133.    button.attachDoubleClick(DoubleClick);
  134.    button.setPressTicks(1000); // that is the time when LongHoldCenter is called
  135.    button.attachLongPressStart(HoldCenter);
  136.  
  137.  
  138.  
  139.    // pinMode(LED_Pin, OUTPUT);
  140.  
  141.    if (digitalRead(Center_Pin) == 0) updating_server_start();
  142.    else {
  143.       Serial.println("Starting BLE work!");
  144.       bleKeyboard.begin();
  145.      
  146.    
  147.     Up.onRelease(Joy_onRelease);
  148.     Down.onRelease(Joy_onRelease);
  149.     Left.onRelease(Joy_onRelease);
  150.     Right.onRelease(Joy_onRelease);
  151.  
  152.     Up.onHoldRepeat(Joy_Hold_Threshold, Joy_Rebounce_Interval, Joy_onHoldRepeat);
  153.     Down.onHoldRepeat(Joy_Hold_Threshold, Joy_Rebounce_Interval, Joy_onHoldRepeat);
  154.     Left.onHoldRepeat(Joy_Hold_Threshold, Joy_Rebounce_Interval, Joy_onHoldRepeat);
  155.     Right.onHoldRepeat(Joy_Hold_Threshold, Joy_Rebounce_Interval, Joy_onHoldRepeat);
  156.    
  157.     Center.onRelease(0, Button_Hold_Threshold-1, Button_onRelease);
  158.     Rectangle.onRelease(0, Button_Hold_Threshold-1, Button_onRelease);
  159.     Triangle.onRelease(0, Button_Hold_Threshold-1, Button_onRelease);
  160.     Circle.onRelease(0, Button_Hold_Threshold-1, Button_onRelease);
  161.     Cancel.onRelease(0, Button_Hold_Threshold-1, Button_onRelease);
  162.      
  163.     Center.onHold(Button_Hold_Threshold, Button_onHold);
  164.     Rectangle.onHold(Button_Hold_Threshold, Button_onHold);
  165.     Triangle.onHold(Button_Hold_Threshold, Button_onHold);
  166.     Circle.onHold(Button_Hold_Threshold, Button_onHold);
  167.     Cancel.onHoldRepeat(Button_Hold_Threshold, Button_Rebounce_Interval,Button_onHoldRepeat);
  168.  
  169.    
  170.     Joy_Active_Counter = 0;
  171.   }
  172.  
  173. }
  174.  
  175.  
  176.  
  177. void loop() {
  178.   // keep watching the push button:
  179.   button.tick();
  180.   if (server_running) {
  181.     server.handleClient();
  182.     delay(1);
  183.   }
  184.   else {
  185.     if(bleKeyboard.isConnected()) {
  186.         Serial.println("ok");
  187.       if (not bt_first_connected) {
  188.                 bt_first_connected = true;
  189.         Serial.println("BT connected");
  190.         delay (2000);
  191.         bleKeyboard.print("C");
  192.       }
  193.       Up.update();
  194.       Down.update();
  195.       Left.update();
  196.       Right.update();
  197.       Center.update();
  198.       Rectangle.update();
  199.       Triangle.update();
  200.       Circle.update();
  201.       Cancel.update();
  202.     }
  203.    
  204.   }
  205. }
  206.  
  207.  
  208. void keyboardPress(char key) {
  209.   bleKeyboard.press(key);
  210. }
  211.  
  212. void Button_onRelease(Button& btn, uint16_t duration){
  213.   if (btn.is(Rectangle)) keyboardPress(Rectangle_Press_Key);
  214.   if (btn.is(Triangle)) keyboardPress(Triangle_Press_Key);
  215.   if (btn.is(Circle)) keyboardPress(Circle_Press_Key);
  216.   if (btn.is(Cancel)) keyboardPress(Cancel_Press_Key);
  217.   bleKeyboard.releaseAll();
  218. }
  219.  
  220. void Button_onHold(Button& btn, uint16_t duration){
  221.   if (btn.is(Rectangle)) keyboardPress(Rectangle_Hold_Key);
  222.   if (btn.is(Triangle)) keyboardPress(Triangle_Hold_Key);
  223.   if (btn.is(Circle)) keyboardPress(Circle_Hold_Key);
  224.   bleKeyboard.releaseAll();
  225. }
  226.  
  227. void Joy_onHoldRepeat(Button& btn, uint16_t duration, uint16_t repeat_count){
  228.   if(btn.isPressed() && Joy_Active_Counter == 5) {
  229.     if(btn.is(Up)) keyboardPress(Up_Press_Key);
  230.     if(btn.is(Down)) keyboardPress(Down_Press_Key);
  231.     if(btn.is(Left)) keyboardPress(Left_Press_Key);
  232.     if(btn.is(Right)) keyboardPress(Right_Press_Key);
  233.     bleKeyboard.releaseAll();
  234.   }
  235.   Joy_Active_Counter = Joy_Active_Counter +1;
  236.   if (Joy_Inactive && Joy_Active_Counter > Joy_Active_Threshold){
  237.     Joy_Active_Counter = 0;
  238.     Joy_Inactive = false;
  239.   }
  240.   if (!Joy_Inactive && Joy_Active_Counter > Joy_Rebounce_Threshold) Joy_Active_Counter = 0;  
  241. }
  242.  
  243. void Joy_onRelease(Button& btn, uint16_t duration){
  244.   Joy_Active_Counter = 0;
  245.   Joy_Inactive = true;
  246. }
  247.  
  248. void SingleClick() {    // this function will be called when the Joy center button is pressed 1 time only.
  249.   bleKeyboard.write(KEY_RETURN);
  250.   Serial.println("SingleClick() detected.");
  251. } // SingleClick
  252.  
  253. void DoubleClick() {    // this function will be called when the Joy center button was pressed 2 times in a short timeframe.
  254.   if(Cruise_Climb == LOW) {
  255.     bleKeyboard.print("V");
  256.     Serial.println("Vario");
  257.   }
  258.   else {
  259.     bleKeyboard.print("S");
  260.     Serial.println("Speed to fly");
  261.   }
  262.   Cruise_Climb = !Cruise_Climb; // reverse the Cruise_Climb
  263. } // DoubleClick
  264.  
  265. void HoldCenter() {     // this function will be called when the Joy center button is held down for 0.5 second or more.
  266.   bleKeyboard.print("P");
  267.   Serial.println("PAN()");
  268.   HoldCenterTime = millis() - 500; // as set in setPressTicks()
  269. } // HoldCenter()
  270.  
  271.  
  272. void Button_onHoldRepeat(Button& btn, uint16_t duration, uint16_t repeat_count){  // this function will be called when the Cancel button is held down for a longer time
  273.   if (btn.is(Cancel)) {
  274.     if (repeat_count == 1) {
  275.       keyboardPress(Cancel_Hold_Key);
  276.       bleKeyboard.releaseAll();
  277.     }
  278.     if (duration > 5000){
  279.       bleKeyboard.print("E");
  280.       delay(1000);
  281.       ESP.restart();  // ESP32_Restart
  282.     }
  283.   }
  284. }

lbernstone
Posts: 826
Joined: Mon Jul 22, 2019 3:20 pm

Re: ESP32-S3 BLE Issues

Postby lbernstone » Wed Dec 27, 2023 7:13 pm

Most likely interference on the antenna. Can it connect to WiFi? Load up the WiFiScan example and see if you can find an AP, and if the rssi is what you would expect (should be -30-40db if you are right next to the AP).

xcnavug
Posts: 7
Joined: Sat Sep 16, 2023 11:45 pm

Re: ESP32-S3 BLE Issues

Postby xcnavug » Thu Dec 28, 2023 3:15 am

I do the upload directly via the onboard USB. After uploading and resetting I can not acces the serial communication as I do not have acces to RX/TX pins. After resseting the Arduino IDE does not recognize the board via USB as long as I won´t bring it in download mode by pressing the bootand reset button. I tried some wifi server examples, but it seems like i does not create a wifi signal either.

lbernstone
Posts: 826
Joined: Mon Jul 22, 2019 3:20 pm

Re: ESP32-S3 BLE Issues

Postby lbernstone » Thu Dec 28, 2023 5:29 am


Who is online

Users browsing this forum: No registered users and 74 guests