Initial cut at getting HC05 into classes for the ESP32 or Due environments

Runs on ESP32 OK.
This commit is contained in:
rljonesau 2018-10-30 22:05:29 +11:00
parent f8f70058f9
commit 756ebdd888
6 changed files with 232 additions and 44 deletions

View file

@ -1,2 +1,4 @@
// Place Holder Config File - User config vars and defines to be moved here
// Jimmy C
#define USE_HC05_BLUETOOTH

View file

@ -9,25 +9,3 @@ void Bluetooth_SendACK();
extern void Command_Interpret(const char* pLine); // decodes received command lines, implemented in main .ino file!
struct sRxLine {
char Line[64];
int Len;
sRxLine() {
clear();
}
bool append(char val) {
if(Len < (sizeof(Line) - 1)) {
Line[Len++] = val;
Line[Len] = 0;
return true;
}
return false;
}
void clear() {
Line[0] = 0;
Len = 0;
}
};

View file

@ -2,6 +2,7 @@
#include "pins.h"
#include "Protocol.h"
#include "debugport.h"
#include "BluetoothESP32.h"
#ifdef TELNET
#define DebugPort Debug
@ -21,7 +22,7 @@ const int LED = 2;
// ESP32
sRxLine RxLine;
/*
#ifdef ESP32_USE_HC05
//static HardwareSerial& Bluetooth(Serial2); // TODO: make proper ESP32 BT client
@ -55,9 +56,9 @@ void Bluetooth_Init()
// attach to the SENSE line from the HC-05 module
// this line goes high when a BT client is connected :-)
pinMode(HC05_Sense, INPUT);
pinMode(HC05_SensePin, INPUT);
digitalWrite(KeyPin, HIGH); // request HC-05 module to enter command mode
digitalWrite(HC05_KeyPin, HIGH); // request HC-05 module to enter command mode
// Open Serial2, explicitly specify pins for pin multiplexer!);
Serial2.begin(9600, SERIAL_8N1, Rx2Pin, Tx2Pin);
@ -127,7 +128,7 @@ void Bluetooth_Init()
Serial2.begin(9600, SERIAL_8N1, Rx2Pin, Tx2Pin);
// leave HC-05 command mode, return to data mode
digitalWrite(KeyPin, LOW);
digitalWrite(HC05_KeyPin, LOW);
} while (0); // yeah lame, allows break prior though :-)
}
@ -159,7 +160,7 @@ void Bluetooth_SendFrame(const char* pHdr, const CProtocol& Frame, bool lineterm
// DebugReportFrame(pHdr, Frame, lineterm ? "\r\n" : " ");
DebugReportFrame(pHdr, Frame, " ");
if(digitalRead(HC05_Sense)) {
if(digitalRead(HC05_SensePin)) {
if(Frame.verifyCRC()) {
// send data frame to HC-05
Serial2.print(pHdr);
@ -193,7 +194,7 @@ bool Bluetooth_ATCommand(const char* cmd)
return false;
}
#else // ESP32_USE_HC05
#else // ESP32_USE_HC05*/
#ifndef ESP32_USE_BLE_RLJ
/////////////////////////////////////////////////////////////////////////////////////////
@ -419,6 +420,19 @@ void BLE_Send(std::string Data)
#endif // ESP32_USE_BLE_RLJ
#endif // ESP32_USE_HC05
/*#endif // ESP32_USE_HC05*/
CBluetoothHC05onESP32::CBluetoothHC05onESP32(int keyPin, int sensePin, int rxPin, int txPin) : CBluetoothHC05(keyPin, sensePin)
{
_rxPin = rxPin;
_txPin = txPin;
}
void
CBluetoothHC05onESP32::OpenSerial(int baudrate)
{
Serial2.begin(baudrate, SERIAL_8N1, _rxPin, _txPin);
}
#endif // __ESP32__

View file

@ -2,6 +2,7 @@
#include "pins.h"
#include "Protocol.h"
#include "debugport.h"
#include "BluetoothHC05.h"
// Bluetooth access via HC-05 Module, using a UART
@ -26,6 +27,7 @@ const int BTRates[] = {
bool bHC05Available = false;
void Bluetooth_Init()
{
RxLine.clear();
@ -155,3 +157,173 @@ bool Bluetooth_ATCommand(const char* cmd)
#endif
CBluetoothHC05::CBluetoothHC05(int keyPin, int sensePin)
{
_keyPin = keyPin;
_sensePin = sensePin;
}
void
CBluetoothHC05::Init()
{
const int BTRates[] = {
9600, 38400, 115200, 19200, 57600, 2400, 4800, 1200
};
RxLine.clear();
// attach to the SENSE line from the HC-05 module
// this line goes high when a BT client is connected :-)
pinMode(_sensePin, INPUT);
digitalWrite(_keyPin, HIGH); // request HC-05 module to enter command mode
// Open Serial2, explicitly specify pins for pin multiplexer!);
OpenSerial(9600); // Serial2.begin(9600, SERIAL_8N1, _rxPin, _txPin);
DebugPort.println("\r\n\r\nAttempting to detect HC-05 Bluetooth module...");
int BTidx = 0;
int maxTries = sizeof(BTRates)/sizeof(int);
for(BTidx = 0; BTidx < maxTries; BTidx++) {
DebugPort.print(" @ ");
DebugPort.print(BTRates[BTidx]);
DebugPort.print(" baud... ");
OpenSerial(BTRates[BTidx]); // Serial2.begin(BTRates[BTidx], SERIAL_8N1, _rxPin, _txPin); // open serial port at a std.baud rate
delay(10);
Serial2.print("\r\n"); // clear the throat!
delay(100);
Serial2.setTimeout(100);
if(ATCommand("AT\r\n")) { // probe with a simple "AT"
DebugPort.println(" OK."); // got a response - woo hoo found the module!
break;
}
if(ATCommand("AT\r\n")) { // sometimes a second try is good...
DebugPort.println(" OK.");
break;
}
// failed, try another baud rate
DebugPort.println("");
Serial2.flush();
Serial2.end();
delay(100);
}
DebugPort.println("");
if(BTidx == maxTries) {
// we could not get anywhere with teh AT commands, but maybe this is the other module
// plough on and assume 9600 baud, but at the mercy of whatever the module name is...
DebugPort.println("FAILED to detect a HC-05 Bluetooth module :-(");
// leave the EN pin high - if other style module keeps it powered!
// assume it is 9600, and just (try to) use it like that...
// we will sense the STATE line to prove a client is hanging off the link...
DebugPort.println("ASSUMING a HC-05 module @ 9600baud (Unknown name)");
OpenSerial(9600); // Serial2.begin(9600, SERIAL_8N1, _rxPin, _txPin);
}
else {
// found a HC-05 module at one of its supported baud rates.
// now program it's name and force a 9600 baud data interface.
// this is the defacto standard as shipped!
DebugPort.println("HC-05 found");
do { // so we can break!
DebugPort.print(" Setting Name to \"Diesel Heater\"... ");
if(!ATCommand("AT+NAME=\"Diesel Heater\"\r\n")) {
DebugPort.println("FAILED");
}
else {
DebugPort.println("OK");
}
DebugPort.print(" Setting baud rate to 9600N81...");
if(!ATCommand("AT+UART=9600,1,0\r\n")) {
DebugPort.println("FAILED");
}
else {
DebugPort.println("OK");
}
OpenSerial(9600); // Serial2.begin(9600, SERIAL_8N1, _rxPin, _txPin);
// leave HC-05 command mode, return to data mode
digitalWrite(_keyPin, LOW);
} while (0); // yeah lame, allows break prior though :-)
}
delay(50);
DebugPort.println("");
}
void
CBluetoothHC05::Check()
{
// check for data coming back over Bluetooth
if(Serial2.available()) {
char rxVal = Serial2.read();
if(isControl(rxVal)) { // "End of Line"
Command_Interpret(RxLine.Line);
RxLine.clear();
}
else {
RxLine.append(rxVal); // append new char to our Rx buffer
}
}
}
void
CBluetoothHC05::SendFrame(const char* pHdr, const CProtocol& Frame, bool lineterm)
{
DebugPort.print(millis());
DebugPort.print("ms ");
DebugReportFrame(pHdr, Frame, " ");
if(digitalRead(_sensePin)) {
if(Frame.verifyCRC()) {
// send data frame to HC-05
Serial2.print(pHdr);
Serial2.write(Frame.Data, 24);
// toggle LED
#ifdef BT_LED
digitalWrite(LED, !digitalRead(LED)); // toggle LED
#endif
}
else {
DebugPort.print("Bluetooth data not sent, CRC error ");
}
}
else {
DebugPort.print("No Bluetooth client");
// force LED off
#ifdef BT_LED
digitalWrite(LED, 0);
#endif
}
if(lineterm)
DebugPort.println("");
}
// protected function, typically to perform Hayes commands with HC-05
bool
CBluetoothHC05::ATCommand(const char* cmd)
{
Serial2.print(cmd);
char RxBuffer[16];
memset(RxBuffer, 0, 16);
int read = Serial2.readBytesUntil('\n', RxBuffer, 16); // \n is not included in returned string!
if((read == 3) && (0 == strcmp(RxBuffer, "OK\r")) ) {
return true;
}
return false;
}
void
CBluetoothHC05::OpenSerial(int baudrate)
{
Serial2.begin(baudrate);
}

View file

@ -55,7 +55,7 @@
The circuit:
- a Tx Rx multiplexer is required to combine the Arduino's Tx1 And Rx1 pins onto the blue wire.
- a Tx Enable signal from pin 20 controls the multiplexer
- a Tx Enable signal from pin 22 controls the multiplexer, high for Tx, low for Rx
- Serial logging software on Serial0 via USB link
created 23 Sep 2018 by Ray Jones
@ -70,6 +70,8 @@
#include "debugport.h"
#include "SmartError.h"
#include "BTCWifi.h"
#include "BTCConfig.h"
#define HOST_NAME "remotedebug-sample"
#define TRIGGER_PIN 0
@ -88,8 +90,11 @@
#define DebugPort DebugPort
#endif
#define DEBUG_BTRX
#include "Bluetooth.h"
#ifdef ESP32
#include "BluetoothESP32.h"
#else
#include "BluetoothHC05.h"
#endif
#if defined(__arm__)
// Required for Arduino Due, UARTclass is derived from HardwareSerial
@ -165,6 +170,18 @@ CModeratedFrame HeaterFrame1; // data packet received from heater in resp
CProtocol HeaterFrame2; // data packet received from heater in response to our packet
CProtocol DefaultBTCParams(CProtocol::CtrlMode); // defines the default parameters, used in case of no OEM controller
CSmartError SmartError;
#ifdef USE_HC05_BLUETOOTH
#ifdef ESP32
CBluetoothHC05onESP32 Bluetooth(HC05_KeyPin, HC05_SensePin, Rx2Pin, Tx2Pin);
#else
CBluetoothHC05 Bluetooth(HC05_KeyPin, HC05_SensePin);
#endif
#else
CBluetoothAbstract Bluetooth;
#endif
long lastRxTime; // used to observe inter character delays
bool hasOEMController = false;
@ -179,16 +196,15 @@ CHeaterStorage* pNVStorage = NULL;
void PrepareTxFrame(const CProtocol& basisFrame, CProtocol& TxFrame, bool isBTCmaster);
void setup()
{
void setup() {
initWifi(TRIGGER_PIN, FAILEDSSID, FAILEDPASSWORD);
pinMode(Tx2Pin, OUTPUT);
digitalWrite(Tx2Pin, HIGH);
pinMode(Rx2Pin, INPUT_PULLUP);
pinMode(ListenOnlyPin, INPUT_PULLUP);
pinMode(KeyPin, OUTPUT);
digitalWrite(KeyPin, LOW);
pinMode(HC05_KeyPin, OUTPUT);
digitalWrite(HC05_KeyPin, LOW);
// initialize serial port to interact with the "blue wire"
// 25000 baud, Tx and Rx channels of Chinese heater comms interface:
@ -221,7 +237,8 @@ void setup()
DefaultBTCParams.setFan_Min(1680);
DefaultBTCParams.setFan_Max(4500);
Bluetooth_Init();
// Bluetooth_Init();
Bluetooth.Init();
// create pointer to CHeaterStorage
// via the magic of polymorphism we can use this to access whatever
@ -253,7 +270,8 @@ void loop()
}
}
Bluetooth_Check(); // check for Bluetooth activity
// Bluetooth_Check(); // check for Bluetooth activity
Bluetooth.Check(); // check for Bluetooth activity
// calc elapsed time since last rxd byte
// used to detect no OEM controller, or the start of an OEM frame sequence
@ -339,7 +357,8 @@ void loop()
// filled OEM controller frame, report
// echo received OEM controller frame over Bluetooth, using [OEM] header
if(OEMCtrlFrame.elapsedTime() > 700) {
Bluetooth_SendFrame("[OEM]", OEMCtrlFrame, true);
// Bluetooth_SendFrame("[OEM]", OEMCtrlFrame, true);
Bluetooth.SendFrame("[OEM]", OEMCtrlFrame, true);
OEMCtrlFrame.setTime();
}
else {
@ -366,7 +385,8 @@ void loop()
// echo heater reponse data to Bluetooth client
if(HeaterFrame1.elapsedTime() > 700) {
Bluetooth_SendFrame("[HTR]", HeaterFrame1);
// Bluetooth_SendFrame("[HTR]", HeaterFrame1);
Bluetooth.SendFrame("[HTR]", HeaterFrame1);
HeaterFrame1.setTime();
}
else {
@ -411,8 +431,10 @@ void loop()
delay(5);
if(!hasOEMController) {
// only convey these frames to Bluetooth when NOT using an OEM controller!
Bluetooth_SendFrame("[BTC]", TxManage.getFrame(), true); // BTC => Bluetooth Controller :-)
Bluetooth_SendFrame("[HTR]", HeaterFrame2); // pin not grounded, suppress duplicate to BT
// Bluetooth_SendFrame("[BTC]", TxManage.getFrame(), true); // BTC => Bluetooth Controller :-)
// Bluetooth_SendFrame("[HTR]", HeaterFrame2); // pin not grounded, suppress duplicate to BT
Bluetooth.SendFrame("[BTC]", TxManage.getFrame(), true); // BTC => Bluetooth Controller :-)
Bluetooth.SendFrame("[HTR]", HeaterFrame2); // pin not grounded, suppress duplicate to BT
}
CommState.set(CommStates::Idle);

View file

@ -1,8 +1,8 @@
const int TxEnbPin = 22;
const int ListenOnlyPin = 21;
const int KeyPin = 15;
const int Tx1Pin = 18;
const int Rx1Pin = 19;
const int Tx2Pin = 16;
const int Rx2Pin = 17;
const int HC05_Sense = 4;
const int HC05_KeyPin = 15;
const int HC05_SensePin = 4;