Added bluetooth communication.

This commit is contained in:
Vojtěch Pour 2022-11-18 21:48:14 +01:00
parent 6695e71370
commit 8188e43bab

View file

@ -1,5 +1,13 @@
#define BAUDRATE 57600
#define BAUDRATE 115200
#define DEBUGOUTPUT 0
#include <map> // Bluetooth
#include <BluetoothSerial.h> // Bluetooth
// Bluetooth Serial, variables
BluetoothSerial SerialBT;
#define BT_DISCOVER_TIME 10000
esp_spp_sec_t sec_mask=ESP_SPP_SEC_NONE;
esp_spp_role_t role=ESP_SPP_ROLE_SLAVE;
// checksum variables
byte generatedChecksum = 0;
@ -14,32 +22,93 @@ byte meditation = 0;
long lastReceivedPacket = 0;
boolean bigPacket = false;
//////////////////////////
// Microprocessor Setup //
//////////////////////////
//////////////////////////
// Microprocessor Setup //
//////////////////////////
void setup(){
Serial1.begin(BAUDRATE); // Serial port 1 (ATMEGA2560)
Serial.begin(BAUDRATE); // USB
Serial.begin(BAUDRATE);
// ----------------------------------------------------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------------------------------------------------
//////////////////////////
// BLUETOOTH CONNECTION //
//////////////////////////
if(! SerialBT.begin("ESP32test", true) ) {
Serial.println("========== serialBT failed!");
abort();
}
Serial.println("Starting discoverAsync...");
BTScanResults* btDeviceList = SerialBT.getScanResults();
if (SerialBT.discoverAsync([](BTAdvertisedDevice* pDevice) {
Serial.printf(">>>>>>>>>>>Found a new device asynchronously: %s\n", pDevice->toString().c_str());
} )
) {
delay(BT_DISCOVER_TIME);
Serial.print("Stopping discoverAsync... ");
SerialBT.discoverAsyncStop();
Serial.println("discoverAsync stopped");
delay(5000);
if(btDeviceList->getCount() > 0) {
BTAddress addr;
int channel=0;
Serial.println("Found devices:");
for (int i=0; i < btDeviceList->getCount(); i++) {
BTAdvertisedDevice *device=btDeviceList->getDevice(i);
Serial.printf(" ----- %s %s %d\n", device->getAddress().toString().c_str(), device->getName().c_str(), device->getRSSI());
std::map<int,std::string> channels=SerialBT.getChannels(device->getAddress());
Serial.printf("scanned for services, found %d\n", channels.size());
for(auto const &entry : channels) {
Serial.printf(" channel %d (%s)\n", entry.first, entry.second.c_str());
}
if(channels.size() > 0) {
addr = device->getAddress();
channel=channels.begin()->first;
}
}
if(addr) {
Serial.printf("connecting to %s - %d\n", addr.toString().c_str(), channel);
SerialBT.connect(addr, channel, sec_mask, role);
}
} else {
Serial.println("Didn't find any devices");
}
} else {
Serial.println("Error on discoverAsync f.e. not workin after a \"connect\"");
}
// ----------------------------------------------------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------------------------------------------------
}
////////////////////////////////
// Read data from Serial UART //
////////////////////////////////
/////////////////////////////////////
// Read data from Serial Bluetooth //
/////////////////////////////////////
byte ReadOneByte() {
int ByteRead;
while(!Serial1.available());
ByteRead = Serial1.read();
#if DEBUGOUTPUT
Serial.print((char)ByteRead); // echo the same byte out the USB serial (for debug purposes)
#endif
return ByteRead;
if(! SerialBT.isClosed() && SerialBT.connected()) {
while(!SerialBT.available());
ByteRead = SerialBT.read();
#if DEBUGOUTPUT
Serial.print((char)ByteRead); // echo the same byte out the USB serial (for debug purposes)
#endif
return ByteRead;
} else {
Serial.println("not connected");
}
}
/////////////
//MAIN LOOP//
/////////////
/////////////
//MAIN LOOP//
/////////////
void loop() {
// Look for sync bytes
if(ReadOneByte() == 170) {