Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**************************************************************************************************************************
- Bluetooth Classic Button MASTER Sketch Uses the BOOT button
- **************************************************************************************************************************/
- // 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
- #include "BluetoothSerial.h"
- #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
- #define LED 2
- #define BUTTON 0
- #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;
- bool press = 0, lastBtnState = 1, ledState = 0;
- uint32_t lastMillis = 0;
- #ifdef USE_NAME
- String slaveName = "ESP32-BT-Slave"; // Change this to reflect the real name of your slave BT device
- #else
- String MACadd = "AA:BB:CC:11:22:33"; // This only for printing
- uint8_t address[6] = {0xAA, 0xBB, 0xCC, 0x11, 0x22, 0x33}; // Change this to reflect real MAC address of your slave BT device
- #endif
- String myName = "ESP32-BT-Master";
- String inString;
- void setup() {
- bool connected;
- Serial.begin(115200);
- pinMode(BUTTON, INPUT_PULLUP);
- pinMode(LED, OUTPUT);
- digitalWrite(LED, ledState);
- 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(10000)) {
- 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() {
- bool buttonState = digitalRead(BUTTON);
- if(buttonState != lastBtnState){
- press = true;
- lastMillis = millis();
- }
- if(millis() - lastMillis >= 50){
- if(press){
- if(buttonState == LOW){
- SerialBT.write('1');
- Serial.println("Button 1 Press");
- }
- press = false;
- }
- delay(20);
- }
- if (SerialBT.available() > 0) {
- char inChar = SerialBT.read();
- if (inChar != '\n'){
- inString += String(inChar);
- }else{
- Serial.printf("Reply Received: %s\n\n", inString);
- inString = "";
- }
- if(inString == "R11"){
- digitalWrite(LED, HIGH);
- }else if(inString == "R10"){
- digitalWrite(LED, LOW);
- }
- }
- lastBtnState = buttonState;
- }
- /************************************** END MASTER SKETCH *************************************************
- **********************************************************************************************************/
- //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
- /*********************************************************************************************************
- **********************************BT CLASSIC SLAVE SKETCH with String reply *********************************
- *************************** *****************************************************************************/
- //This example code is in the Public Domain (or CC0 licensed, at your option.)
- //By Evandro Copercini - 2018
- //
- //This example creates a bridge between Serial and Classical Bluetooth (SPP)
- //and also demonstrate that SerialBT have the same functionalities of a normal Serial
- #include "BluetoothSerial.h"
- //#define USE_PIN // Uncomment this to use PIN during pairing. The pin is specified on the line below
- const char *pin = "1234"; // Change this to more secure PIN.
- String device_name = "ESP32-BT-Slave";
- #if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
- #error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
- #endif
- #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;
- #define LED 2
- bool ledState = 0;
- void setup() {
- Serial.begin(115200);
- pinMode(LED, OUTPUT);
- digitalWrite(LED, ledState);
- SerialBT.begin(device_name); //Bluetooth device name
- Serial.printf("The device with name \"%s\" is started.\nNow you can pair it with Bluetooth!\n", device_name.c_str());
- //Serial.printf("The device with name \"%s\" and MAC address %s is started.\nNow you can pair it with Bluetooth!\n", device_name.c_str(), SerialBT.getMacString()); // Use this after the MAC method is implemented
- #ifdef USE_PIN
- SerialBT.setPin(pin);
- Serial.println("Using PIN");
- #endif
- }
- void loop() {
- char inComing;
- String out_str = "R";
- if (SerialBT.available()) {
- inComing = SerialBT.read();
- if(inComing == '1'){
- digitalWrite(LED, ledState = !ledState);
- ledState = digitalRead(LED);
- out_str += String(inComing);
- out_str += String(ledState);
- uint8_t buf[out_str.length()];
- memcpy(buf, out_str.c_str(), out_str.length());
- SerialBT.write(buf, out_str.length());
- SerialBT.println();
- }
- Serial.printf("Sent Reply: %s\n\n", out_str);
- }
- delay(20);
- }
Advertisement
Add Comment
Please, Sign In to add comment