Beefed up Bluetooth handling to include tuning parameters.
Changed Tx headers to [OEM] and [BTC] for the OEM controller and this Bluetooth controller. Added #ifdefs to accomodate Mega and ESP32. Built and tested on ESP32, Serial1 talks to heater OK using standard pin numbering :-)
This commit is contained in:
parent
18571ae857
commit
baed4b3079
5 changed files with 99 additions and 25 deletions
7
Arduino/SenderTrial2/.vscode/arduino.json
vendored
7
Arduino/SenderTrial2/.vscode/arduino.json
vendored
|
@ -1,6 +1,7 @@
|
||||||
{
|
{
|
||||||
"board": "arduino:sam:arduino_due_x_dbg",
|
"board": "esp32:esp32:esp32",
|
||||||
"port": "COM4",
|
"port": "COM9",
|
||||||
"sketch": "SenderTrial2.ino",
|
"sketch": "SenderTrial2.ino",
|
||||||
"output": "..\\build"
|
"output": "..\\build",
|
||||||
|
"configuration": "PSRAM=disabled,PartitionScheme=default,FlashMode=qio,FlashFreq=80,FlashSize=4M,UploadSpeed=921600,DebugLevel=none"
|
||||||
}
|
}
|
|
@ -3,12 +3,12 @@
|
||||||
{
|
{
|
||||||
"name": "Win32",
|
"name": "Win32",
|
||||||
"includePath": [
|
"includePath": [
|
||||||
|
"C:\\Users\\ray\\AppData\\Local\\Arduino15\\packages\\esp32\\tools\\**",
|
||||||
|
"C:\\Users\\ray\\AppData\\Local\\Arduino15\\packages\\esp32\\hardware\\esp32\\1.0.0\\**",
|
||||||
"C:\\Users\\ray\\AppData\\Local\\Arduino15\\packages\\arduino\\tools\\**",
|
"C:\\Users\\ray\\AppData\\Local\\Arduino15\\packages\\arduino\\tools\\**",
|
||||||
"C:\\Users\\ray\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\sam\\1.6.11\\**"
|
"C:\\Users\\ray\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\sam\\1.6.11\\**"
|
||||||
],
|
],
|
||||||
"forcedInclude": [
|
"forcedInclude": [],
|
||||||
"C:\\Users\\ray\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\sam\\1.6.11\\cores\\arduino\\Arduino.h"
|
|
||||||
],
|
|
||||||
"intelliSenseMode": "msvc-x64",
|
"intelliSenseMode": "msvc-x64",
|
||||||
"compilerPath": "C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.15.26726/bin/Hostx64/x64/cl.exe",
|
"compilerPath": "C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.15.26726/bin/Hostx64/x64/cl.exe",
|
||||||
"cStandard": "c11",
|
"cStandard": "c11",
|
||||||
|
|
|
@ -69,6 +69,7 @@ void SerialReport(const char* hdr, const unsigned char* pData, const char* ftr);
|
||||||
void BluetoothDetect();
|
void BluetoothDetect();
|
||||||
bool BlueToothCommand(const char* cmd);
|
bool BlueToothCommand(const char* cmd);
|
||||||
void BlueToothReport(const char* pHdr, const unsigned char Data[24]);
|
void BlueToothReport(const char* pHdr, const unsigned char Data[24]);
|
||||||
|
void BluetoothInterpret();
|
||||||
|
|
||||||
class CommStates {
|
class CommStates {
|
||||||
public:
|
public:
|
||||||
|
@ -95,16 +96,35 @@ private:
|
||||||
int m_Count;
|
int m_Count;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#if defined(__arm__)
|
||||||
|
// for Arduino Due
|
||||||
UARTClass& USB(Serial);
|
UARTClass& USB(Serial);
|
||||||
UARTClass& BlueWire(Serial1);
|
UARTClass& BlueWire(Serial1);
|
||||||
UARTClass& BlueTooth(Serial2);
|
UARTClass& BlueTooth(Serial2);
|
||||||
|
#else
|
||||||
|
// for ESP32, Mega
|
||||||
|
HardwareSerial& USB(Serial);
|
||||||
|
HardwareSerial& BlueWire(Serial1);
|
||||||
|
#if defined(__ESP32__)
|
||||||
|
// ESP32
|
||||||
|
HardwareSerial& BlueTooth(Serial2); // TODO: make proper ESP32 BT client
|
||||||
|
#else
|
||||||
|
// Mega
|
||||||
|
HardwareSerial& BlueTooth(Serial2);
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(__ESP32__)
|
||||||
|
const int TxEnbPin = 22;
|
||||||
|
#else
|
||||||
const int TxEnbPin = 20;
|
const int TxEnbPin = 20;
|
||||||
|
#endif
|
||||||
const int ListenOnlyPin = 21;
|
const int ListenOnlyPin = 21;
|
||||||
const int KeyPin = 15;
|
const int KeyPin = 15;
|
||||||
|
const int Tx1Pin = 18;
|
||||||
|
const int Rx1Pin = 19;
|
||||||
const int Tx2Pin = 16;
|
const int Tx2Pin = 16;
|
||||||
|
const int Rx2Pin = 17;
|
||||||
|
|
||||||
const int BTRates[] = {
|
const int BTRates[] = {
|
||||||
9600, 38400, 115200, 19200, 57600, 2400, 4800
|
9600, 38400, 115200, 19200, 57600, 2400, 4800
|
||||||
|
@ -118,6 +138,7 @@ CProtocol Heater2; // data packet received from heater in response to our
|
||||||
CProtocol SelfParams(CProtocol::CtrlMode); // holds our local parameters, used in case of no OEM controller
|
CProtocol SelfParams(CProtocol::CtrlMode); // holds our local parameters, used in case of no OEM controller
|
||||||
long lastRxTime; // used to observe inter character delays
|
long lastRxTime; // used to observe inter character delays
|
||||||
bool bBlueToothAvailable = false;
|
bool bBlueToothAvailable = false;
|
||||||
|
String BluetoothRxData;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -132,8 +153,15 @@ void setup()
|
||||||
digitalWrite(KeyPin, LOW);
|
digitalWrite(KeyPin, LOW);
|
||||||
// digitalWrite(Tx2Pin, HIGH);
|
// digitalWrite(Tx2Pin, HIGH);
|
||||||
|
|
||||||
|
#if defined(__arm__) || defined(__AVR__)
|
||||||
BlueWire.begin(25000);
|
BlueWire.begin(25000);
|
||||||
pinMode(19, INPUT_PULLUP); // required for MUX to work properly
|
pinMode(19, INPUT_PULLUP); // required for MUX to work properly
|
||||||
|
#else if defined(__ESP32__)
|
||||||
|
// ESP32
|
||||||
|
#define RXD2 16
|
||||||
|
#define TXD2 17
|
||||||
|
BlueWire.begin(25000, SERIAL_8N1, Rx1Pin, Tx1Pin);
|
||||||
|
#endif
|
||||||
|
|
||||||
// initialise serial monitor on serial port 0
|
// initialise serial monitor on serial port 0
|
||||||
USB.begin(115200);
|
USB.begin(115200);
|
||||||
|
@ -170,6 +198,18 @@ void loop()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// check for data coming back over Bluetooth
|
||||||
|
if(BlueTooth.available()) {
|
||||||
|
char rxVal = BlueTooth.read();
|
||||||
|
if(isControl(rxVal)) { // "End of Line"
|
||||||
|
BluetoothRxData += '\0';
|
||||||
|
BluetoothInterpret();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
BluetoothRxData += rxVal; // append new char to our Rx buffer
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// Handle time interval where we send data to the blue wire
|
// Handle time interval where we send data to the blue wire
|
||||||
if(CommState.is(CommStates::SelfTx)) {
|
if(CommState.is(CommStates::SelfTx)) {
|
||||||
|
@ -191,8 +231,7 @@ void loop()
|
||||||
CommState.set(CommStates::SelfTx);
|
CommState.set(CommStates::SelfTx);
|
||||||
bool bOurParams = true;
|
bool bOurParams = true;
|
||||||
TxManage.Start(SelfParams, timenow, bOurParams);
|
TxManage.Start(SelfParams, timenow, bOurParams);
|
||||||
if(bBlueToothAvailable)
|
BlueToothReport("[BTC]", SelfParams.Data); // BTC => Bluetooth Controller :-)
|
||||||
BlueToothReport("[SLF]", SelfParams.Data);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// precautionary action if all 24 bytes were not received whilst expecting them
|
// precautionary action if all 24 bytes were not received whilst expecting them
|
||||||
|
@ -240,7 +279,7 @@ void loop()
|
||||||
|
|
||||||
if( CommState.is(CommStates::ControllerReport) ) {
|
if( CommState.is(CommStates::ControllerReport) ) {
|
||||||
// filled controller frame, report
|
// filled controller frame, report
|
||||||
BlueToothReport("[CTL]", Controller.Data);
|
BlueToothReport("[OEM]", Controller.Data);
|
||||||
SerialReport("Ctrl ", Controller.Data, " ");
|
SerialReport("Ctrl ", Controller.Data, " ");
|
||||||
CommState.set(CommStates::HeaterRx1);
|
CommState.set(CommStates::HeaterRx1);
|
||||||
}
|
}
|
||||||
|
@ -263,7 +302,9 @@ void loop()
|
||||||
else if( CommState.is(CommStates::HeaterReport2) ) {
|
else if( CommState.is(CommStates::HeaterReport2) ) {
|
||||||
// received heater frame (after our control message), report
|
// received heater frame (after our control message), report
|
||||||
SerialReport("Htr2 ", Heater2.Data, "\r\n");
|
SerialReport("Htr2 ", Heater2.Data, "\r\n");
|
||||||
BlueToothReport("[HTR]", Heater2.Data);
|
// if(!digitalRead(ListenOnlyPin)) {
|
||||||
|
BlueToothReport("[HTR]", Heater2.Data); // pin not grounded, suppress duplicate to BT
|
||||||
|
// }
|
||||||
CommState.set(CommStates::Idle);
|
CommState.set(CommStates::Idle);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -282,6 +323,8 @@ void SerialReport(const char* hdr, const unsigned char* pData, const char* ftr)
|
||||||
|
|
||||||
void BluetoothDetect()
|
void BluetoothDetect()
|
||||||
{
|
{
|
||||||
|
#if defined(__ESP32__)
|
||||||
|
#else
|
||||||
// search for BlueTooth adapter, trying the common baud rates, then less common
|
// 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
|
// 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.
|
// we are at the mercy of the baud rate stored in the module.
|
||||||
|
@ -352,17 +395,20 @@ void BluetoothDetect()
|
||||||
BlueTooth.end(); // close serial port if no module found
|
BlueTooth.end(); // close serial port if no module found
|
||||||
|
|
||||||
USB.println("");
|
USB.println("");
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
bool BlueToothCommand(const char* cmd)
|
bool BlueToothCommand(const char* cmd)
|
||||||
{
|
{
|
||||||
BlueTooth.print(cmd);
|
if(bBlueToothAvailable) {
|
||||||
char RxBuffer[16];
|
BlueTooth.print(cmd);
|
||||||
memset(RxBuffer, 0, 16);
|
char RxBuffer[16];
|
||||||
int read = BlueTooth.readBytesUntil('\n', RxBuffer, 16); // \n is not included in returned string!
|
memset(RxBuffer, 0, 16);
|
||||||
if((read == 3) && (0 == strcmp(RxBuffer, "OK\r")) ) {
|
int read = BlueTooth.readBytesUntil('\n', RxBuffer, 16); // \n is not included in returned string!
|
||||||
return true;
|
if((read == 3) && (0 == strcmp(RxBuffer, "OK\r")) ) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -370,10 +416,37 @@ bool BlueToothCommand(const char* cmd)
|
||||||
void BlueToothReport(const char* pHdr, const unsigned char Data[24])
|
void BlueToothReport(const char* pHdr, const unsigned char Data[24])
|
||||||
{
|
{
|
||||||
if(bBlueToothAvailable) {
|
if(bBlueToothAvailable) {
|
||||||
/* for(int i=0; i<24; i++) {
|
|
||||||
BlueTooth.write(Data[i]);
|
|
||||||
}*/
|
|
||||||
BlueTooth.print(pHdr);
|
BlueTooth.print(pHdr);
|
||||||
BlueTooth.write(Data, 24);
|
BlueTooth.write(Data, 24);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void BluetoothInterpret()
|
||||||
|
{
|
||||||
|
if(BluetoothRxData.startsWith("[CMD]") ) {
|
||||||
|
USB.write("BT command Rx'd: ");
|
||||||
|
// incoming command from BT app!
|
||||||
|
BluetoothRxData.remove(0, 5); // strip away "[CMD]" header
|
||||||
|
if(BluetoothRxData.startsWith("ON")) {
|
||||||
|
USB.write("ON\n");
|
||||||
|
TxManage.RequestOn();
|
||||||
|
}
|
||||||
|
else if(BluetoothRxData.startsWith("OFF")) {
|
||||||
|
USB.write("OFF\n");
|
||||||
|
TxManage.RequestOff();
|
||||||
|
}
|
||||||
|
else if(BluetoothRxData.startsWith("Pmin")) {
|
||||||
|
USB.write("Pmin\n");
|
||||||
|
}
|
||||||
|
else if(BluetoothRxData.startsWith("Pmax")) {
|
||||||
|
USB.write("Pmax\n");
|
||||||
|
}
|
||||||
|
else if(BluetoothRxData.startsWith("Fmin")) {
|
||||||
|
USB.write("Fmin\n");
|
||||||
|
}
|
||||||
|
else if(BluetoothRxData.startsWith("Fmax")) {
|
||||||
|
USB.write("Fmax\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
BluetoothRxData = ""; //flush string, ready for new data
|
||||||
|
}
|
|
@ -3,7 +3,7 @@
|
||||||
|
|
||||||
extern void SerialReport(const char* hdr, const unsigned char* pData, const char* ftr);
|
extern void SerialReport(const char* hdr, const unsigned char* pData, const char* ftr);
|
||||||
|
|
||||||
CTxManage::CTxManage(int TxEnbPin, USARTClass& serial) :
|
CTxManage::CTxManage(int TxEnbPin, HardwareSerial& serial) :
|
||||||
m_Serial(serial),
|
m_Serial(serial),
|
||||||
m_Frame(CProtocol::CtrlMode)
|
m_Frame(CProtocol::CtrlMode)
|
||||||
{
|
{
|
||||||
|
|
|
@ -8,7 +8,7 @@ class CTxManage
|
||||||
const int m_nFrontPorch = 2;
|
const int m_nFrontPorch = 2;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
CTxManage(int TxEnbPin, USARTClass& serial);
|
CTxManage(int TxEnbPin, HardwareSerial& serial);
|
||||||
void RequestOn();
|
void RequestOn();
|
||||||
void RequestOff();
|
void RequestOff();
|
||||||
void Start(const CProtocol& ref, unsigned long timenow, bool self);
|
void Start(const CProtocol& ref, unsigned long timenow, bool self);
|
||||||
|
@ -23,7 +23,7 @@ private:
|
||||||
bool m_bTxPending;
|
bool m_bTxPending;
|
||||||
int m_nTxEnbPin;
|
int m_nTxEnbPin;
|
||||||
unsigned long m_nStartTime;
|
unsigned long m_nStartTime;
|
||||||
USARTClass& m_Serial;
|
HardwareSerial& m_Serial;
|
||||||
|
|
||||||
void _send();
|
void _send();
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue