Trying to write a single set of Serial Read/Writes that can be used for either a Bluetooth or hardware Serial connection after whichever is tested to exist.
Don't want to write duplicate code, one using "Serial.println;" and another "SerialBT.println;"...
Is there a way to make a variable to call whichever Serial connection exists, which can then be used in a single set of serial read/writes???
"serialvariable.println;" My first thought was an array, but can't seem to get it working.
//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"
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
BluetoothSerial SerialBT;
//int SerVar[] = {Serial, SerialBT}; //HOW TO DECLARE ARRAY OF THESE TWO COMMANDS ???
int SerNo;
void setup() {
Serial.begin(115200);
SerialBT.begin("ESP32test"); //Bluetooth device name
Serial.println("The device started, now you can pair it with bluetooth!");
}
void loop() { //This WORKS but requires DUPLICATE code for each type of Serial connection.
if (Serial.available()) { //UART connection
Serial.println("Test");
delay(10);
}
if (SerialBT.available()) { //Bluetooth Serial connection
SerialBT.println("Test");
delay(50);
}
}
void Loop_alternative() { //This attempt is based on an array.
if (SerialBT.available()) { // Tests for Bluetooth Serial connection
SerNo = 1;
} else { // Uses UART if no Bluetooth Serial connection
SerNo = 0;
}
SerVar[SerNo].println("Test"); //BT Serial is priority and UART is secondary.
//Add more universal Serial Read and Write coding for both UART and Bluetooth.
//Duplicate code not necessary.
}
Thank you for responding. When I tried your recommendation it gave the error for both lines: "'class Stream' has no member named 'begin'" Any thoughts?
Update: Thank you for the guidance on this. The links are very interesting and something I'll continue to review. Here is the working code. It'll save a lot of duplication and allow me to connect via BT (phone) or direct UART (PC) when interfacing with the unit. This is why I enjoy this site. Great folks, great help!
//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"
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
BluetoothSerial SerialBT;
Stream *serials[] = {&Serial, &SerialBT};
int SerNo;
void setup() {
Serial.begin(115200);
SerialBT.begin("ESP32test"); //Bluetooth device name
Serial.println("The device started, now you can pair it with bluetooth!");
}
void loop () {
if (SerialBT.available()) { // Tests for Bluetooth Serial connection
SerNo = 1;
} else { // Uses UART if no Bluetooth Serial connection
SerNo = 0;
}
serials[SerNo]->println("Test"); //BT Serial is priority and UART is secondary.
//Add more universal Serial Read and Write coding for both UART and Bluetooth.
//Duplicate code not necessary.
}