espd

ESP BT working - 20250401

Apr 1st, 2025 (edited)
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.53 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 = "98:DA:20:02:BE:A4";                          // This only for printing
  35. uint8_t address[6] = { 0x98, 0xDA, 0x20, 0x02, 0xBE, 0xA4 };  // 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(1000000);  // 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 (SerialBT.available()) {
  88.     // Serial.write(SerialBT.read());
  89.     // char incoming = SerialBT.read();
  90.     // Serial.printf("Received: %02X\n", incoming);  // Print HEX value of received data
  91.     // Serial.write(incoming);
  92.     uint8_t incoming = SerialBT.read();  // Read one byte
  93.     Serial.printf("%02X ", incoming);    // Print as HEX
  94.   }
  95.   //delay(40);
  96. }*/
  97.  
  98.  
  99. void loop() {
  100.     uint8_t frame[5];  // Buffer for one full data frame
  101.  
  102.     // Wait until at least 5 bytes are available
  103.     while (SerialBT.available() >= 5) {
  104.         SerialBT.readBytes(frame, 5);  // Read exactly 5 bytes
  105.  
  106.         // Validate IDCHAR (should always be 0xA3)
  107.       /*  if (frame[1] != 0xA3) {
  108.             Serial.println("Invalid frame: IDCHAR mismatch!");
  109.             return;
  110.         }*/
  111.  
  112.         // Calculate checksum
  113.        /* uint8_t checksum = (frame[0] + frame[1] + frame[2] + frame[3]) % 255;
  114.         if (checksum != frame[4]) {
  115.             Serial.println("Checksum error!");
  116.             return;
  117.         }*/
  118.  
  119.         // Extract values
  120.         uint8_t channel = frame[0];
  121.         uint16_t value = (frame[2] << 8) | frame[3];  // Combine High and Low byte
  122.  
  123.         Serial.printf("Channel: %d, Value: %d\n", channel, value);
  124.     }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment