macca-nz

master_slave_BT_Classic_button_led_switch

Mar 15th, 2024 (edited)
901
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 7.09 KB | Source Code | 0 0
  1. /**************************************************************************************************************************
  2.                         Bluetooth Classic Button MASTER Sketch Uses the BOOT button
  3. **************************************************************************************************************************/
  4.  
  5. // This example code is in the Public Domain (or CC0 licensed, at your option.)
  6. // By Victor Tchistiak - 2019
  7. //
  8. // This example demonstrates master mode Bluetooth connection to a slave BT device using PIN (password)
  9. // defined either by String "slaveName" by default "OBDII" or by MAC address
  10. //
  11. // This example creates a bridge between Serial and Classical Bluetooth (SPP)
  12. // This is an extension of the SerialToSerialBT example by Evandro Copercini - 2018
  13. //
  14. // DO NOT try to connect to phone or laptop - they are master
  15. // devices, same as the ESP using this code - it will NOT work!
  16. //
  17. // You can try to flash a second ESP32 with the example SerialToSerialBT - it should
  18. // automatically pair with ESP32 running this code
  19.  
  20. #include "BluetoothSerial.h"
  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. #define LED 2
  26. #define BUTTON 0
  27.  
  28. #if !defined(CONFIG_BT_SPP_ENABLED)
  29. #error Serial Bluetooth not available or not enabled. It is only available for the ESP32 chip.
  30. #endif
  31.  
  32. BluetoothSerial SerialBT;
  33.  
  34. bool press = 0, lastBtnState = 1, ledState = 0;
  35. uint32_t lastMillis = 0;
  36.  
  37. #ifdef USE_NAME
  38.   String slaveName = "ESP32-BT-Slave"; // Change this to reflect the real name of your slave BT device
  39. #else
  40.   String MACadd = "AA:BB:CC:11:22:33"; // This only for printing
  41.   uint8_t address[6]  = {0xAA, 0xBB, 0xCC, 0x11, 0x22, 0x33}; // Change this to reflect real MAC address of your slave BT device
  42. #endif
  43.  
  44. String myName = "ESP32-BT-Master";
  45.  
  46. String inString;
  47.  
  48. void setup() {
  49.   bool connected;
  50.   Serial.begin(115200);
  51.   pinMode(BUTTON, INPUT_PULLUP);
  52.   pinMode(LED, OUTPUT);
  53.   digitalWrite(LED, ledState);
  54.  
  55.   SerialBT.begin(myName, true);
  56.   Serial.printf("The device \"%s\" started in master mode, make sure slave BT device is on!\n", myName.c_str());
  57.  
  58.   #ifndef USE_NAME
  59.     SerialBT.setPin(pin);
  60.     Serial.println("Using PIN");
  61.   #endif
  62.  
  63.   // connect(address) is fast (up to 10 secs max), connect(slaveName) is slow (up to 30 secs max) as it needs
  64.   // to resolve slaveName to address first, but it allows to connect to different devices with the same name.
  65.   // Set CoreDebugLevel to Info to view devices Bluetooth address and device names
  66.   #ifdef USE_NAME
  67.     connected = SerialBT.connect(slaveName);
  68.     Serial.printf("Connecting to slave BT device named \"%s\"\n", slaveName.c_str());
  69.   #else
  70.     connected = SerialBT.connect(address);
  71.     Serial.print("Connecting to slave BT device with MAC "); Serial.println(MACadd);
  72.   #endif
  73.  
  74.   if(connected) {
  75.     Serial.println("Connected Successfully!");
  76.   } else {
  77.     while(!SerialBT.connected(10000)) {
  78.       Serial.println("Failed to connect. Make sure remote device is available and in range, then restart app.");
  79.     }
  80.   }
  81.   // Disconnect() may take up to 10 secs max
  82.   if (SerialBT.disconnect()) {
  83.     Serial.println("Disconnected Successfully!");
  84.   }
  85.   // This would reconnect to the slaveName(will use address, if resolved) or address used with connect(slaveName/address).
  86.   SerialBT.connect();
  87.   if(connected) {
  88.     Serial.println("Reconnected Successfully!");
  89.   } else {
  90.     while(!SerialBT.connected(10000)) {
  91.       Serial.println("Failed to reconnect. Make sure remote device is available and in range, then restart app.");
  92.     }
  93.   }
  94. }
  95.  
  96. void loop() {
  97.   bool buttonState = digitalRead(BUTTON);
  98.  
  99.   if(buttonState != lastBtnState){
  100.     press = true;
  101.     lastMillis = millis();
  102.   }
  103.  
  104.   if(millis() - lastMillis >= 50){
  105.     if(press){
  106.       if(buttonState == LOW){
  107.         SerialBT.write('1');
  108.         Serial.println("Button 1 Press");
  109.       }
  110.       press = false;
  111.     }
  112.     delay(20);
  113.   }
  114.   if (SerialBT.available() > 0) {
  115.     char inChar = SerialBT.read();
  116.       if (inChar != '\n'){
  117.         inString += String(inChar);
  118.       }else{
  119.         Serial.printf("Reply Received: %s\n\n", inString);
  120.         inString = "";
  121.       }
  122.       if(inString == "R11"){
  123.         digitalWrite(LED, HIGH);
  124.       }else if(inString == "R10"){
  125.         digitalWrite(LED, LOW);
  126.       }
  127.   }
  128.   lastBtnState = buttonState;
  129. }
  130.  
  131.  
  132. /************************************** END MASTER SKETCH *************************************************
  133. **********************************************************************************************************/
  134.  
  135. //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  136.  
  137. /*********************************************************************************************************
  138. **********************************BT CLASSIC SLAVE SKETCH with String reply *********************************
  139. *************************** *****************************************************************************/
  140.  
  141. //This example code is in the Public Domain (or CC0 licensed, at your option.)
  142. //By Evandro Copercini - 2018
  143. //
  144. //This example creates a bridge between Serial and Classical Bluetooth (SPP)
  145. //and also demonstrate that SerialBT have the same functionalities of a normal Serial
  146.  
  147. #include "BluetoothSerial.h"
  148.  
  149. //#define USE_PIN // Uncomment this to use PIN during pairing. The pin is specified on the line below
  150. const char *pin = "1234"; // Change this to more secure PIN.
  151.  
  152. String device_name = "ESP32-BT-Slave";
  153.  
  154. #if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
  155. #error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
  156. #endif
  157.  
  158. #if !defined(CONFIG_BT_SPP_ENABLED)
  159. #error Serial Bluetooth not available or not enabled. It is only available for the ESP32 chip.
  160. #endif
  161.  
  162. BluetoothSerial SerialBT;
  163.  
  164. #define LED 2
  165. bool ledState = 0;
  166.  
  167. void setup() {
  168.   Serial.begin(115200);
  169.   pinMode(LED, OUTPUT);
  170.   digitalWrite(LED, ledState);
  171.   SerialBT.begin(device_name); //Bluetooth device name
  172.   Serial.printf("The device with name \"%s\" is started.\nNow you can pair it with Bluetooth!\n", device_name.c_str());
  173.   //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
  174.   #ifdef USE_PIN
  175.     SerialBT.setPin(pin);
  176.     Serial.println("Using PIN");
  177.   #endif
  178. }
  179.  
  180. void loop() {
  181.   char inComing;
  182.   String out_str = "R";
  183.  
  184.   if (SerialBT.available()) {
  185.     inComing = SerialBT.read();
  186.       if(inComing == '1'){
  187.         digitalWrite(LED, ledState = !ledState);
  188.         ledState = digitalRead(LED);
  189.         out_str += String(inComing);
  190.         out_str += String(ledState);
  191.         uint8_t buf[out_str.length()];
  192.         memcpy(buf, out_str.c_str(), out_str.length());
  193.         SerialBT.write(buf, out_str.length());
  194.         SerialBT.println();
  195.         }
  196.         Serial.printf("Sent Reply: %s\n\n", out_str);
  197.       }
  198.   delay(20);
  199. }
  200.  
  201.  
  202.  
  203.  
Advertisement
Add Comment
Please, Sign In to add comment