2018-10-18 09:49:14 +00:00
|
|
|
#include "Bluetooth.h"
|
|
|
|
#include "pins.h"
|
2018-10-20 07:11:23 +00:00
|
|
|
#include "Protocol.h"
|
|
|
|
#include "debugport.h"
|
2018-10-18 09:49:14 +00:00
|
|
|
|
2018-10-20 07:11:23 +00:00
|
|
|
// Bluetooth access via HC-05 Module, using a UART
|
|
|
|
|
|
|
|
#ifndef ESP32
|
2018-10-18 09:49:14 +00:00
|
|
|
// NOTE: ESP32 uses an entirely different mechanism, please refer to BluetoothESP32.cpp/.h
|
|
|
|
|
2018-10-20 07:11:23 +00:00
|
|
|
#ifdef __arm__
|
2018-10-18 09:49:14 +00:00
|
|
|
// for Arduino Due
|
|
|
|
static UARTClass& Bluetooth(Serial2);
|
|
|
|
#else
|
|
|
|
// for Mega
|
2018-10-20 07:11:23 +00:00
|
|
|
static HardwareSerial& Bluetooth(Serial2); // TODO: make proper ESP32 BT client
|
2018-10-18 09:49:14 +00:00
|
|
|
#endif
|
|
|
|
|
2018-10-20 07:11:23 +00:00
|
|
|
bool Bluetooth_ATCommand(const char* cmd);
|
2018-10-18 09:49:14 +00:00
|
|
|
|
|
|
|
String BluetoothRxData;
|
|
|
|
|
|
|
|
const int BTRates[] = {
|
|
|
|
9600, 38400, 115200, 19200, 57600, 2400, 4800
|
|
|
|
};
|
|
|
|
|
|
|
|
bool bHC05Available = false;
|
|
|
|
|
|
|
|
void Bluetooth_Init()
|
|
|
|
{
|
2018-10-20 07:11:23 +00:00
|
|
|
|
2018-10-18 09:49:14 +00:00
|
|
|
// search for BlueTooth adapter, trying the common baud rates, then less common
|
|
|
|
// as the device cannot be guaranteed to power up with the key pin high
|
|
|
|
// we are at the mercy of the baud rate stored in the module.
|
|
|
|
Bluetooth.begin(9600);
|
|
|
|
digitalWrite(KeyPin, HIGH);
|
|
|
|
delay(500);
|
|
|
|
|
2018-10-20 07:11:23 +00:00
|
|
|
DebugPort.println("\r\n\r\nAttempting to detect HC-05 Bluetooth module...");
|
2018-10-18 09:49:14 +00:00
|
|
|
|
|
|
|
int BTidx = 0;
|
|
|
|
int maxTries = sizeof(BTRates)/sizeof(int);
|
|
|
|
for(BTidx = 0; BTidx < maxTries; BTidx++) {
|
2018-10-20 07:11:23 +00:00
|
|
|
DebugPort.print(" @ ");
|
|
|
|
DebugPort.print(BTRates[BTidx]);
|
|
|
|
DebugPort.print(" baud... ");
|
2018-10-18 09:49:14 +00:00
|
|
|
Bluetooth.begin(BTRates[BTidx]); // open serial port at a certain baud rate
|
|
|
|
Bluetooth.print("\r\n");
|
|
|
|
Bluetooth.setTimeout(50);
|
|
|
|
|
2018-10-20 07:11:23 +00:00
|
|
|
if(Bluetooth_ATCommand("AT\r\n")) {
|
|
|
|
DebugPort.println(" OK.");
|
2018-10-18 09:49:14 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
// failed, try another baud rate
|
2018-10-20 07:11:23 +00:00
|
|
|
DebugPort.println("");
|
2018-10-18 09:49:14 +00:00
|
|
|
Bluetooth.flush();
|
|
|
|
}
|
|
|
|
|
2018-10-20 07:11:23 +00:00
|
|
|
DebugPort.println("");
|
2018-10-18 09:49:14 +00:00
|
|
|
if(BTidx == maxTries) {
|
2018-10-20 07:11:23 +00:00
|
|
|
DebugPort.println("FAILED to detect HC-05 Bluetooth module :-(");
|
2018-10-18 09:49:14 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
if(BTRates[BTidx] == 115200) {
|
2018-10-20 07:11:23 +00:00
|
|
|
DebugPort.println("HC-05 found and already set to 115200 baud, skipping Init.");
|
2018-10-18 09:49:14 +00:00
|
|
|
bHC05Available = true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
do {
|
2018-10-20 07:11:23 +00:00
|
|
|
DebugPort.println("HC-05 found");
|
2018-10-18 09:49:14 +00:00
|
|
|
|
2018-10-20 07:11:23 +00:00
|
|
|
DebugPort.print(" Setting Name to \"DieselHeater\"... ");
|
|
|
|
if(!Bluetooth_ATCommand("AT+NAME=\"DieselHeater\"\r\n")) {
|
|
|
|
DebugPort.println("FAILED");
|
2018-10-18 09:49:14 +00:00
|
|
|
break;
|
|
|
|
}
|
2018-10-20 07:11:23 +00:00
|
|
|
DebugPort.println("OK");
|
2018-10-18 09:49:14 +00:00
|
|
|
|
2018-10-20 07:11:23 +00:00
|
|
|
DebugPort.print(" Setting baud rate to 115200N81...");
|
|
|
|
if(!Bluetooth_ATCommand("AT+UART=115200,1,0\r\n")) {
|
|
|
|
DebugPort.println("FAILED");
|
2018-10-18 09:49:14 +00:00
|
|
|
break;
|
|
|
|
};
|
2018-10-20 07:11:23 +00:00
|
|
|
DebugPort.println("OK");
|
2018-10-18 09:49:14 +00:00
|
|
|
|
|
|
|
Bluetooth.begin(115200);
|
|
|
|
bHC05Available = true;
|
|
|
|
|
|
|
|
} while(0);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
digitalWrite(KeyPin, LOW); // leave HC-05 command mode
|
|
|
|
|
|
|
|
delay(500);
|
|
|
|
|
|
|
|
if(!bHC05Available)
|
|
|
|
Bluetooth.end(); // close serial port if no module found
|
|
|
|
|
2018-10-20 07:11:23 +00:00
|
|
|
DebugPort.println("");
|
2018-10-18 09:49:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Bluetooth_Check()
|
|
|
|
{
|
|
|
|
// check for data coming back over Bluetooth
|
|
|
|
if(bHC05Available) {
|
|
|
|
if(Bluetooth.available()) {
|
|
|
|
char rxVal = Bluetooth.read();
|
|
|
|
if(isControl(rxVal)) { // "End of Line"
|
|
|
|
BluetoothRxData += '\0';
|
2018-10-20 07:11:23 +00:00
|
|
|
Command_Interpret(BluetoothRxData);
|
2018-10-18 09:49:14 +00:00
|
|
|
BluetoothRxData = "";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
BluetoothRxData += rxVal; // append new char to our Rx buffer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-10-20 07:11:23 +00:00
|
|
|
void Bluetooth_SendFrame(const char* pHdr, const CProtocol& Frame)
|
2018-10-18 09:49:14 +00:00
|
|
|
{
|
|
|
|
if(bHC05Available) {
|
2018-10-20 07:11:23 +00:00
|
|
|
if(Frame.verifyCRC()) {
|
|
|
|
Bluetooth.print(pHdr);
|
|
|
|
Bluetooth.write(Frame.Data, 24);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
DebugPort.print("Bluetooth data not sent, CRC error ");
|
|
|
|
DebugPort.println(pHdr);
|
|
|
|
}
|
2018-10-18 09:49:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// local function, typically to perform Hayes commands with HC-05
|
2018-10-20 07:11:23 +00:00
|
|
|
bool Bluetooth_ATCommand(const char* cmd)
|
2018-10-18 09:49:14 +00:00
|
|
|
{
|
|
|
|
if(bHC05Available) {
|
|
|
|
Bluetooth.print(cmd);
|
|
|
|
char RxBuffer[16];
|
|
|
|
memset(RxBuffer, 0, 16);
|
|
|
|
int read = Bluetooth.readBytesUntil('\n', RxBuffer, 16); // \n is not included in returned string!
|
|
|
|
if((read == 3) && (0 == strcmp(RxBuffer, "OK\r")) ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|