From d3134a816c468d6f28846738de86dacf9ebc3585 Mon Sep 17 00:00:00 2001 From: Vladimir Garistov Date: Sun, 26 Nov 2023 02:13:05 +0200 Subject: [PATCH] Added workshop example for bluetooth communication --- .../TUES_bluetooth_demo.ino | 111 ++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 Workshops/TUES_bluetooth_demo/TUES_bluetooth_demo.ino diff --git a/Workshops/TUES_bluetooth_demo/TUES_bluetooth_demo.ino b/Workshops/TUES_bluetooth_demo/TUES_bluetooth_demo.ino new file mode 100644 index 0000000..bb73d6a --- /dev/null +++ b/Workshops/TUES_bluetooth_demo/TUES_bluetooth_demo.ino @@ -0,0 +1,111 @@ +/* + TUES Bluetooth Demo - an example for controlling motors over Bluetooth + Copyright (C) 2023 Vladimir Garistov + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published + by the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see +*/ + +// Board selection. Comment out for Arduino AVR boards. +#define _ESP32_ + +// Dabble configuration options +#define CUSTOM_SETTINGS +#define INCLUDE_TERMINAL_MODULE +#ifdef _ESP32_ + // Does not compile with increased warnings in Arduino IDE because some are treated as errors. + #include +#else + #include +#endif + +#ifdef _ESP32_ + #if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BT_BLUEDROID_ENABLED) + #error Bluetooth is not enabled! Please run `make menuconfig` to enable it. + #endif + + #if !defined(CONFIG_BT_SPP_ENABLED) + #error Serial Port Profile for Bluetooth is not available or not enabled. + #endif +#else + #define RX_PIN 2 + #define TX_PIN 3 + #define BT_BAUDRATE 9600 +#endif + +#define UART_BAUDRATE 38400 +#define UART_TIMEOUT 10 // ms + +static void serial_relay(void); + +#ifdef _ESP32_ + static const char device_name[] = "ESP32-BT-Slave"; +#endif + +void setup() +{ + #ifdef _ESP32_ + Dabble.begin(device_name); + #else + Dabble.begin(BT_BAUDRATE, RX_PIN, TX_PIN); + #endif + + Serial.begin(UART_BAUDRATE); + Serial.setTimeout(UART_TIMEOUT); + Serial.println("Setup complete."); +} + +void loop() +{ + // This function is used to refresh data obtained from smartphone. + // Hence calling this function is mandatory in order to get data properly from your mobile. + Dabble.processInput(); + serial_relay(); +} + +/* + Relays data from the Bluetooth module to the UART interface and vice-versa. + A maximum of 256 characters can be sent from the UART interface to the Bluetooth module. + Otherwise the buffer of the Serial library fills up and characters are silently dropped. + A maximum of 64 characters can be sent from the Bluetooth module to the UART interface. + In this case the buffer of the Dabble library fills up. Result is the same - some + characters are silently dropped. + Does not work correctly for non-ASCII characters. + Tested on Arduino Uno and ESP32, the lenght limits might differ on other boards. +*/ +static void serial_relay(void) +{ + static String serial_data = ""; + static bool data_flag = false; + + if (Serial.available()) + { + // Blocks for UART_TIMEOUT + serial_data = Serial.readString(); + data_flag = true; + } + if (data_flag == true) + { + Terminal.print(serial_data); + serial_data = ""; + data_flag = false; + } + if (Terminal.available()) + { + while (Terminal.available()) + { + Serial.write(Terminal.read()); + } + Serial.println(); + } +} -- 2.39.5