|
1 |
| -//This example code is in the Public Domain (or CC0 licensed, at your option.) |
2 |
| -//By Victor Tchistiak - 2019 |
| 1 | +// This example code is in the Public Domain (or CC0 licensed, at your option.) |
| 2 | +// By Victor Tchistiak - 2019 |
3 | 3 | //
|
4 |
| -//This example demostrates master mode bluetooth connection and pin |
5 |
| -//it creates a bridge between Serial and Classical Bluetooth (SPP) |
6 |
| -//this is an extention of the SerialToSerialBT example by Evandro Copercini - 2018 |
| 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 |
7 | 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 |
8 | 15 |
|
9 | 16 | #include "BluetoothSerial.h"
|
10 | 17 |
|
| 18 | +#define USE_NAME // Comment this to use MAC address instead of a slaveName |
| 19 | +//#define USE_PIN // Uncomment this to use PIN during pairing. The pin is specified on the line below |
| 20 | +const char *pin = "1234"; // Change this to reflect the pin expected by the real slave BT device |
| 21 | + |
| 22 | +#ifndef USE_NAME |
| 23 | + #define USE_MAC |
| 24 | +#endif |
| 25 | + |
11 | 26 | #if !defined(CONFIG_BT_SPP_ENABLED)
|
12 | 27 | #error Serial Bluetooth not available or not enabled. It is only available for the ESP32 chip.
|
13 | 28 | #endif
|
14 | 29 |
|
15 | 30 | BluetoothSerial SerialBT;
|
16 | 31 |
|
17 |
| -String MACadd = "AA:BB:CC:11:22:33"; |
18 |
| -uint8_t address[6] = {0xAA, 0xBB, 0xCC, 0x11, 0x22, 0x33}; |
19 |
| -//uint8_t address[6] = {0x00, 0x1D, 0xA5, 0x02, 0xC3, 0x22}; |
20 |
| -String name = "OBDII"; |
21 |
| -const char *pin = "1234"; //<- standard pin would be provided by default |
22 |
| -bool connected; |
| 32 | +#ifdef USE_NAME |
| 33 | + String slaveName = "ESP32-BT-Slave"; // Change this to reflect the real name of your slave BT device |
| 34 | +#endif |
| 35 | + |
| 36 | +#ifdef USE_MAC |
| 37 | + String MACadd = "AA:BB:CC:11:22:33"; // This only for printing |
| 38 | + uint8_t address[6] = {0xAA, 0xBB, 0xCC, 0x11, 0x22, 0x33}; // Change this to reflect real MAC address of your slave BT device |
| 39 | +#endif |
| 40 | + |
| 41 | +String myName = "ESP32-BT-Master"; |
23 | 42 |
|
24 | 43 | void setup() {
|
| 44 | + bool connected; |
25 | 45 | Serial.begin(115200);
|
26 |
| - //SerialBT.setPin(pin); |
27 |
| - SerialBT.begin("ESP32test", true); |
28 |
| - //SerialBT.setPin(pin); |
29 |
| - Serial.println("The device started in master mode, make sure remote BT device is on!"); |
30 |
| - |
31 |
| - // connect(address) is fast (upto 10 secs max), connect(name) is slow (upto 30 secs max) as it needs |
32 |
| - // to resolve name to address first, but it allows to connect to different devices with the same name. |
33 |
| - // Set CoreDebugLevel to Info to view devices bluetooth address and device names |
34 |
| - connected = SerialBT.connect(name); |
35 |
| - //connected = SerialBT.connect(address); |
36 |
| - |
| 46 | + |
| 47 | + SerialBT.begin(myName, true); |
| 48 | + Serial.printf("The device \"%s\" started in master mode, make sure slave BT device is on!\n", myName.c_str()); |
| 49 | + |
| 50 | + #ifdef USE_PIN |
| 51 | + SerialBT.setPin(pin); |
| 52 | + Serial.println("Using PIN"); |
| 53 | + #endif |
| 54 | + |
| 55 | + // connect(address) is fast (up to 10 secs max), connect(slaveName) is slow (up to 30 secs max) as it needs |
| 56 | + // to resolve slaveName to address first, but it allows to connect to different devices with the same name. |
| 57 | + // Set CoreDebugLevel to Info to view devices Bluetooth address and device names |
| 58 | + #ifdef USE_NAME |
| 59 | + connected = SerialBT.connect(slaveName); |
| 60 | + Serial.printf("Connecting to slave BT device named \"%s\"\n", slaveName.c_str()); |
| 61 | + #endif |
| 62 | + #ifdef USE_MAC |
| 63 | + connected = SerialBT.connect(address); |
| 64 | + Serial.print("Connecting to slave BT device with MAC "); Serial.println(MACadd); |
| 65 | + #endif |
| 66 | + |
37 | 67 | if(connected) {
|
38 |
| - Serial.println("Connected Succesfully!"); |
| 68 | + Serial.println("Connected Successfully!"); |
39 | 69 | } else {
|
40 | 70 | while(!SerialBT.connected(10000)) {
|
41 |
| - Serial.println("Failed to connect. Make sure remote device is available and in range, then restart app."); |
| 71 | + Serial.println("Failed to connect. Make sure remote device is available and in range, then restart app."); |
42 | 72 | }
|
43 | 73 | }
|
44 |
| - // disconnect() may take upto 10 secs max |
| 74 | + // Disconnect() may take up to 10 secs max |
45 | 75 | if (SerialBT.disconnect()) {
|
46 |
| - Serial.println("Disconnected Succesfully!"); |
| 76 | + Serial.println("Disconnected Successfully!"); |
47 | 77 | }
|
48 |
| - // this would reconnect to the name(will use address, if resolved) or address used with connect(name/address). |
| 78 | + // This would reconnect to the slaveName(will use address, if resolved) or address used with connect(slaveName/address). |
49 | 79 | SerialBT.connect();
|
| 80 | + if(connected) { |
| 81 | + Serial.println("Reconnected Successfully!"); |
| 82 | + } else { |
| 83 | + while(!SerialBT.connected(10000)) { |
| 84 | + Serial.println("Failed to reconnect. Make sure remote device is available and in range, then restart app."); |
| 85 | + } |
| 86 | + } |
50 | 87 | }
|
51 | 88 |
|
52 | 89 | void loop() {
|
|
0 commit comments