From baed4b3079645282e75f02585d4f7bb56074b595 Mon Sep 17 00:00:00 2001 From: rljonesau Date: Mon, 15 Oct 2018 18:14:12 +1100 Subject: [PATCH] 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 :-) --- Arduino/SenderTrial2/.vscode/arduino.json | 7 +- .../.vscode/c_cpp_properties.json | 6 +- Arduino/SenderTrial2/SenderTrial2.ino | 105 +++++++++++++++--- Arduino/SenderTrial2/TxManage.cpp | 2 +- Arduino/SenderTrial2/TxManage.h | 4 +- 5 files changed, 99 insertions(+), 25 deletions(-) diff --git a/Arduino/SenderTrial2/.vscode/arduino.json b/Arduino/SenderTrial2/.vscode/arduino.json index a6d66f4..684e9bd 100644 --- a/Arduino/SenderTrial2/.vscode/arduino.json +++ b/Arduino/SenderTrial2/.vscode/arduino.json @@ -1,6 +1,7 @@ { - "board": "arduino:sam:arduino_due_x_dbg", - "port": "COM4", + "board": "esp32:esp32:esp32", + "port": "COM9", "sketch": "SenderTrial2.ino", - "output": "..\\build" + "output": "..\\build", + "configuration": "PSRAM=disabled,PartitionScheme=default,FlashMode=qio,FlashFreq=80,FlashSize=4M,UploadSpeed=921600,DebugLevel=none" } \ No newline at end of file diff --git a/Arduino/SenderTrial2/.vscode/c_cpp_properties.json b/Arduino/SenderTrial2/.vscode/c_cpp_properties.json index 93cb9f4..d7283d2 100644 --- a/Arduino/SenderTrial2/.vscode/c_cpp_properties.json +++ b/Arduino/SenderTrial2/.vscode/c_cpp_properties.json @@ -3,12 +3,12 @@ { "name": "Win32", "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\\hardware\\sam\\1.6.11\\**" ], - "forcedInclude": [ - "C:\\Users\\ray\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\sam\\1.6.11\\cores\\arduino\\Arduino.h" - ], + "forcedInclude": [], "intelliSenseMode": "msvc-x64", "compilerPath": "C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.15.26726/bin/Hostx64/x64/cl.exe", "cStandard": "c11", diff --git a/Arduino/SenderTrial2/SenderTrial2.ino b/Arduino/SenderTrial2/SenderTrial2.ino index e5cfe3b..43c10cb 100644 --- a/Arduino/SenderTrial2/SenderTrial2.ino +++ b/Arduino/SenderTrial2/SenderTrial2.ino @@ -69,6 +69,7 @@ void SerialReport(const char* hdr, const unsigned char* pData, const char* ftr); void BluetoothDetect(); bool BlueToothCommand(const char* cmd); void BlueToothReport(const char* pHdr, const unsigned char Data[24]); +void BluetoothInterpret(); class CommStates { public: @@ -95,16 +96,35 @@ private: int m_Count; }; - - +#if defined(__arm__) +// for Arduino Due UARTClass& USB(Serial); UARTClass& BlueWire(Serial1); 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; +#endif 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 BTRates[] = { 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 long lastRxTime; // used to observe inter character delays bool bBlueToothAvailable = false; +String BluetoothRxData; @@ -132,8 +153,15 @@ void setup() digitalWrite(KeyPin, LOW); // digitalWrite(Tx2Pin, HIGH); +#if defined(__arm__) || defined(__AVR__) BlueWire.begin(25000); 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 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 if(CommState.is(CommStates::SelfTx)) { @@ -191,8 +231,7 @@ void loop() CommState.set(CommStates::SelfTx); bool bOurParams = true; TxManage.Start(SelfParams, timenow, bOurParams); - if(bBlueToothAvailable) - BlueToothReport("[SLF]", SelfParams.Data); + BlueToothReport("[BTC]", SelfParams.Data); // BTC => Bluetooth Controller :-) } // precautionary action if all 24 bytes were not received whilst expecting them @@ -240,7 +279,7 @@ void loop() if( CommState.is(CommStates::ControllerReport) ) { // filled controller frame, report - BlueToothReport("[CTL]", Controller.Data); + BlueToothReport("[OEM]", Controller.Data); SerialReport("Ctrl ", Controller.Data, " "); CommState.set(CommStates::HeaterRx1); } @@ -263,7 +302,9 @@ void loop() else if( CommState.is(CommStates::HeaterReport2) ) { // received heater frame (after our control message), report 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); } @@ -282,6 +323,8 @@ void SerialReport(const char* hdr, const unsigned char* pData, const char* ftr) void BluetoothDetect() { + #if defined(__ESP32__) + #else // 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. @@ -352,17 +395,20 @@ void BluetoothDetect() BlueTooth.end(); // close serial port if no module found USB.println(""); - +#endif } bool BlueToothCommand(const char* cmd) { - 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; + if(bBlueToothAvailable) { + 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; } @@ -370,10 +416,37 @@ bool BlueToothCommand(const char* cmd) void BlueToothReport(const char* pHdr, const unsigned char Data[24]) { if(bBlueToothAvailable) { -/* for(int i=0; i<24; i++) { - BlueTooth.write(Data[i]); - }*/ BlueTooth.print(pHdr); 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 +} \ No newline at end of file diff --git a/Arduino/SenderTrial2/TxManage.cpp b/Arduino/SenderTrial2/TxManage.cpp index 30caf10..19c70b7 100644 --- a/Arduino/SenderTrial2/TxManage.cpp +++ b/Arduino/SenderTrial2/TxManage.cpp @@ -3,7 +3,7 @@ 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_Frame(CProtocol::CtrlMode) { diff --git a/Arduino/SenderTrial2/TxManage.h b/Arduino/SenderTrial2/TxManage.h index b3430ef..fd7f9cb 100644 --- a/Arduino/SenderTrial2/TxManage.h +++ b/Arduino/SenderTrial2/TxManage.h @@ -8,7 +8,7 @@ class CTxManage const int m_nFrontPorch = 2; public: - CTxManage(int TxEnbPin, USARTClass& serial); + CTxManage(int TxEnbPin, HardwareSerial& serial); void RequestOn(); void RequestOff(); void Start(const CProtocol& ref, unsigned long timenow, bool self); @@ -23,7 +23,7 @@ private: bool m_bTxPending; int m_nTxEnbPin; unsigned long m_nStartTime; - USARTClass& m_Serial; + HardwareSerial& m_Serial; void _send(); };