Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This example code is in the Public Domain (or CC0 licensed, at your option.)
- // By Victor Tchistiak - 2019
- //
- // This example demonstrates master mode Bluetooth connection to a slave BT device using PIN (password)
- // defined either by String "slaveName" by default "OBDII" or by MAC address
- //
- // This example creates a bridge between Serial and Classical Bluetooth (SPP)
- // This is an extension of the SerialToSerialBT example by Evandro Copercini - 2018
- //
- // DO NOT try to connect to phone or laptop - they are master
- // devices, same as the ESP using this code - it will NOT work!
- //
- // You can try to flash a second ESP32 with the example SerialToSerialBT - it should
- // automatically pair with ESP32 running this code
- // Downgrade to esp 2.0.17 in board manager
- #include "BluetoothSerial.h"
- #define CORE_DEBUG_LEVEL ARDUHAL_LOG_LEVEL_INFO // show logs
- //#define USE_NAME // Comment this to use MAC address instead of a slaveName
- const char *pin = "1234"; // Change this to reflect the pin expected by the real slave BT device
- #if !defined(CONFIG_BT_SPP_ENABLED)
- #error Serial Bluetooth not available or not enabled. It is only available for the ESP32 chip.
- #endif
- BluetoothSerial SerialBT;
- #ifdef USE_NAME
- String slaveName = "EMUCANBT_SPP"; // Change this to reflect the real name of your slave BT device
- #else
- String MACadd = "98:DA:20:02:BE:A4"; // This only for printing
- uint8_t address[6] = { 0x98, 0xDA, 0x20, 0x02, 0xBE, 0xA4 }; // Change this to reflect real MAC address of your slave BT device
- #endif
- String myName = "ESP32-BT-Master";
- void setup() {
- bool connected;
- Serial.begin(1000000); // Faster serial. Increase serial monitor speed
- SerialBT.begin(myName, true);
- Serial.printf("The device \"%s\" started in master mode, make sure slave BT device is on!\n", myName.c_str());
- #ifndef USE_NAME
- SerialBT.setPin(pin);
- Serial.println("Using PIN");
- #endif
- // connect(address) is fast (up to 10 secs max), connect(slaveName) is slow (up to 30 secs max) as it needs
- // to resolve slaveName to address first, but it allows to connect to different devices with the same name.
- // Set CoreDebugLevel to Info to view devices Bluetooth address and device names
- #ifdef USE_NAME
- connected = SerialBT.connect(slaveName);
- Serial.printf("Connecting to slave BT device named \"%s\"\n", slaveName.c_str());
- #else
- connected = SerialBT.connect(address);
- Serial.print("Connecting to slave BT device with MAC ");
- Serial.println(MACadd);
- #endif
- if (connected) {
- Serial.println("Connected Successfully!");
- } else {
- while (!SerialBT.connected(30000)) {
- Serial.println("Failed to connect. Make sure remote device is available and in range, then restart app.");
- }
- }
- // Disconnect() may take up to 10 secs max
- if (SerialBT.disconnect()) {
- Serial.println("Disconnected Successfully!");
- }
- // This would reconnect to the slaveName(will use address, if resolved) or address used with connect(slaveName/address).
- SerialBT.connect();
- if (connected) {
- Serial.println("Reconnected Successfully!");
- } else {
- while (!SerialBT.connected(10000)) {
- Serial.println("Failed to reconnect. Make sure remote device is available and in range, then restart app.");
- }
- }
- }
- /*void loop() {
- if (SerialBT.available()) {
- // Serial.write(SerialBT.read());
- // char incoming = SerialBT.read();
- // Serial.printf("Received: %02X\n", incoming); // Print HEX value of received data
- // Serial.write(incoming);
- uint8_t incoming = SerialBT.read(); // Read one byte
- Serial.printf("%02X ", incoming); // Print as HEX
- }
- //delay(40);
- }*/
- void loop() {
- uint8_t frame[5]; // Buffer for one full data frame
- // Wait until at least 5 bytes are available
- while (SerialBT.available() >= 5) {
- SerialBT.readBytes(frame, 5); // Read exactly 5 bytes
- // Validate IDCHAR (should always be 0xA3)
- /* if (frame[1] != 0xA3) {
- Serial.println("Invalid frame: IDCHAR mismatch!");
- return;
- }*/
- // Calculate checksum
- /* uint8_t checksum = (frame[0] + frame[1] + frame[2] + frame[3]) % 255;
- if (checksum != frame[4]) {
- Serial.println("Checksum error!");
- return;
- }*/
- // Extract values
- uint8_t channel = frame[0];
- uint16_t value = (frame[2] << 8) | frame[3]; // Combine High and Low byte
- Serial.printf("Channel: %d, Value: %d\n", channel, value);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment