espd

BT new 11 with board manager downgrade

Mar 21st, 2025 (edited)
68
0
Never
2
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.61 KB | None | 0 0
  1. // This example code is in the Public Domain (or CC0 licensed, at your option.)
  2. // By Victor Tchistiak - 2019
  3. //
  4. // This example demonstrates master mode Bluetooth connection to a slave BT device using PIN (password)
  5. // defined either by String "slaveName" by default "OBDII" or by MAC address
  6. //
  7. // This example creates a bridge between Serial and Classical Bluetooth (SPP)
  8. // This is an extension of the SerialToSerialBT example by Evandro Copercini - 2018
  9. //
  10. // DO NOT try to connect to phone or laptop - they are master
  11. // devices, same as the ESP using this code - it will NOT work!
  12. //
  13. // You can try to flash a second ESP32 with the example SerialToSerialBT - it should
  14. // automatically pair with ESP32 running this code
  15.  
  16. // Downgrade to esp 2.0.17 in board manager
  17.  
  18. #include "BluetoothSerial.h"
  19.  
  20. #define CORE_DEBUG_LEVEL ARDUHAL_LOG_LEVEL_INFO // show logs
  21.  
  22. //#define USE_NAME // Comment this to use MAC address instead of a slaveName
  23. const char *pin = "1234"; // Change this to reflect the pin expected by the real slave BT device
  24.  
  25. #if !defined(CONFIG_BT_SPP_ENABLED)
  26. #error Serial Bluetooth not available or not enabled. It is only available for the ESP32 chip.
  27. #endif
  28.  
  29. BluetoothSerial SerialBT;
  30.  
  31. #ifdef USE_NAME
  32. String slaveName = "EMUCANBT_SPP"; // Change this to reflect the real name of your slave BT device
  33. #else
  34. String MACadd = "AA:BB:CC:11:22:33"; // This only for printing
  35. uint8_t address[6] = { 0xAA, 0xBB, 0xCC, 0x11, 0x22, 0x33 }; // Change this to reflect real MAC address of your slave BT device
  36. #endif
  37.  
  38. String myName = "ESP32-BT-Master";
  39.  
  40. void setup() {
  41. bool connected;
  42. Serial.begin(921600); // Faster serial. Increase serial monitor speed
  43.  
  44. SerialBT.begin(myName, true);
  45. Serial.printf("The device \"%s\" started in master mode, make sure slave BT device is on!\n", myName.c_str());
  46.  
  47. #ifndef USE_NAME
  48. SerialBT.setPin(pin);
  49. Serial.println("Using PIN");
  50. #endif
  51.  
  52. // connect(address) is fast (up to 10 secs max), connect(slaveName) is slow (up to 30 secs max) as it needs
  53. // to resolve slaveName to address first, but it allows to connect to different devices with the same name.
  54. // Set CoreDebugLevel to Info to view devices Bluetooth address and device names
  55. #ifdef USE_NAME
  56. connected = SerialBT.connect(slaveName);
  57. Serial.printf("Connecting to slave BT device named \"%s\"\n", slaveName.c_str());
  58. #else
  59. connected = SerialBT.connect(address);
  60. Serial.print("Connecting to slave BT device with MAC ");
  61. Serial.println(MACadd);
  62. #endif
  63.  
  64. if (connected) {
  65. Serial.println("Connected Successfully!");
  66. } else {
  67. while (!SerialBT.connected(30000)) {
  68. Serial.println("Failed to connect. Make sure remote device is available and in range, then restart app.");
  69. }
  70. }
  71. // Disconnect() may take up to 10 secs max
  72. if (SerialBT.disconnect()) {
  73. Serial.println("Disconnected Successfully!");
  74. }
  75. // This would reconnect to the slaveName(will use address, if resolved) or address used with connect(slaveName/address).
  76. SerialBT.connect();
  77. if (connected) {
  78. Serial.println("Reconnected Successfully!");
  79. } else {
  80. while (!SerialBT.connected(10000)) {
  81. Serial.println("Failed to reconnect. Make sure remote device is available and in range, then restart app.");
  82. }
  83. }
  84. }
  85.  
  86. void loop() {
  87. // if (Serial.available()) {
  88. // SerialBT.write(Serial.read());
  89. // }
  90. if (SerialBT.available()) {
  91. // Serial.write(SerialBT.read());
  92. // char incoming = SerialBT.read();
  93. // Serial.printf("Received: %02X\n", incoming); // Print HEX value of received data
  94. // Serial.write(incoming);
  95. uint8_t incoming = SerialBT.read(); // Read one byte
  96. Serial.printf("%02X ", incoming); // Print as HEX
  97. }
  98. //delay(40);
  99. }
  100.  
  101. //Option #2
  102. /*
  103. void loop() {
  104. uint8_t frame[5]; // Buffer for one full data frame
  105.  
  106. // Wait until at least 5 bytes are available
  107. while (SerialBT.available() >= 5) {
  108. SerialBT.readBytes(frame, 5); // Read exactly 5 bytes
  109.  
  110. // Validate IDCHAR (should always be 0xA3)
  111. if (frame[1] != 0xA3) {
  112. Serial.println("Invalid frame: IDCHAR mismatch!");
  113. return;
  114. }
  115.  
  116. // Calculate checksum
  117. uint8_t checksum = (frame[0] + frame[1] + frame[2] + frame[3]) % 255;
  118. if (checksum != frame[4]) {
  119. Serial.println("Checksum error!");
  120. return;
  121. }
  122.  
  123. // Extract values
  124. uint8_t channel = frame[0];
  125. uint16_t value = (frame[2] << 8) | frame[3]; // Combine High and Low byte
  126.  
  127. Serial.printf("Channel: %d, Value: %d\n", channel, value);
  128. }
  129. }
  130. */
Advertisement
Comments
Add Comment
Please, Sign In to add comment