From 80d1b4395670b4a99ae66d50507d67aa13ab07c1 Mon Sep 17 00:00:00 2001 From: James Date: Tue, 30 Oct 2018 18:23:22 +1100 Subject: [PATCH] Working wifi - Telnet in testing - Ready for big MERGE --- Arduino/SenderTrial2/BTCConfig.h | 1 + Arduino/SenderTrial2/BTCTelnet.cpp | 85 +++++++++++++++++++++++++ Arduino/SenderTrial2/BTCTelnet.h | 7 ++ Arduino/SenderTrial2/BTCWifi.cpp | 63 ------------------ Arduino/SenderTrial2/BTCWifi.h | 6 +- Arduino/SenderTrial2/BluetoothESP32.cpp | 62 +++++++++--------- Arduino/SenderTrial2/BluetoothHC05.cpp | 38 +++++------ Arduino/SenderTrial2/Protocol.cpp | 12 ++-- Arduino/SenderTrial2/SenderTrial2.ino | 59 +++++++++-------- 9 files changed, 180 insertions(+), 153 deletions(-) create mode 100644 Arduino/SenderTrial2/BTCConfig.h create mode 100644 Arduino/SenderTrial2/BTCTelnet.cpp create mode 100644 Arduino/SenderTrial2/BTCTelnet.h diff --git a/Arduino/SenderTrial2/BTCConfig.h b/Arduino/SenderTrial2/BTCConfig.h new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/Arduino/SenderTrial2/BTCConfig.h @@ -0,0 +1 @@ + diff --git a/Arduino/SenderTrial2/BTCTelnet.cpp b/Arduino/SenderTrial2/BTCTelnet.cpp new file mode 100644 index 0000000..466f630 --- /dev/null +++ b/Arduino/SenderTrial2/BTCTelnet.cpp @@ -0,0 +1,85 @@ +//#define TELNET + +#ifdef TELNET +#define DebugPort Debug +#endif + +#ifndef TELNET +#define DebugPort DebugPort +#endif + +#include "BTCTelnet.h" +#include "debugport.h" + +WiFiServer server(23); +WiFiClient serverClients[MAX_SRV_CLIENTS]; + +void initTelnet() { + server.begin(); + server.setNoDelay(true); + + DebugPort.print("Ready! Use 'telnet "); + DebugPort.print(WiFi.localIP()); + DebugPort.println(" 23' to connect"); +} + +void doTelnet() { + uint8_t i; + //check if there are any new clients + if (server.hasClient()){ + for(i = 0; i < MAX_SRV_CLIENTS; i++){ + //find free/disconnected spot + if (!serverClients[i] || !serverClients[i].connected()){ + if(serverClients[i]) serverClients[i].stop(); + serverClients[i] = server.available(); + if (!serverClients[i]) DebugPort.println("available broken"); + DebugPort.print("New client: "); + DebugPort.print(i); DebugPort.print(' '); + DebugPort.println(serverClients[i].remoteIP()); + break; + } + } + if (i >= MAX_SRV_CLIENTS) { + //no free/disconnected spot so reject + server.available().stop(); + } + } + //check clients for data + for(i = 0; i < MAX_SRV_CLIENTS; i++){ + if (serverClients[i] && serverClients[i].connected()){ + if(serverClients[i].available()){ + //get data from the telnet client and push it to the UART + while(serverClients[i].available()) DebugPort.write(serverClients[i].read()); + } + } + + else { + if (serverClients[i]) { + serverClients[i].stop(); + } + } + } + + + //check UART for data + if(DebugPort.available()){ + size_t len = DebugPort.available(); + uint8_t sbuf[len]; + DebugPort.readBytes(sbuf, len); + //push UART data to all connected telnet clients + for(i = 0; i < MAX_SRV_CLIENTS; i++){ + if (serverClients[i] && serverClients[i].connected()){ + serverClients[i].write(sbuf, len); + delay(1); + } + } + } + else { + DebugPort.println("WiFi not connected!"); + for(i = 0; i < MAX_SRV_CLIENTS; i++) { + if (serverClients[i]) serverClients[i].stop(); + } + delay(1000); + } +} + diff --git a/Arduino/SenderTrial2/BTCTelnet.h b/Arduino/SenderTrial2/BTCTelnet.h new file mode 100644 index 0000000..05d39dc --- /dev/null +++ b/Arduino/SenderTrial2/BTCTelnet.h @@ -0,0 +1,7 @@ +#include +#include "BTCWifi.h" + +#define MAX_SRV_CLIENTS 2 + +void initTelnet(); +void doTelnet(); diff --git a/Arduino/SenderTrial2/BTCWifi.cpp b/Arduino/SenderTrial2/BTCWifi.cpp index 0b074b2..a947dbe 100644 --- a/Arduino/SenderTrial2/BTCWifi.cpp +++ b/Arduino/SenderTrial2/BTCWifi.cpp @@ -2,16 +2,6 @@ // select which pin will trigger the configuration portal when set to LOW WiFiManager wm; -// Time -RemoteDebug Debug; - -uint32_t mLastTime = 0; -uint32_t mTimeSeconds = 0; - -// Buildin Led ON ? - -boolean mLedON = false; - unsigned int timeout = 120; // seconds to run for unsigned int startTime = millis(); @@ -21,60 +11,7 @@ int TRIG_PIN; bool res; -void inittelnetdebug(String HOST_NAME) -{ - pinMode(LED_BUILTIN, OUTPUT); - digitalWrite(LED_BUILTIN, LOW); - - String hostNameWifi = HOST_NAME; - hostNameWifi.concat(".local"); - Debug.begin(HOST_NAME); - Debug.setSerialEnabled(true); - - Debug.setResetCmdEnabled(true); // Enable the reset command - - Serial.println("* Arduino RemoteDebug Library"); - Serial.println("*"); - Serial.print("* WiFI connected. IP address: "); - Serial.println(WiFi.localIP()); - Serial.println("*"); - Serial.println("* Please use the telnet client (telnet for Mac/Unix or putty and others for Windows)"); - Serial.println("*"); - Serial.println("* This sample will send messages of debug in all levels."); - Serial.println("*"); - Serial.println("* Please try change debug level in telnet, to see how it works"); - Serial.println("*"); - -} - -void DoDebug() -{ - // Each second - - if ((millis() - mLastTime) >= 1000) { - - // Time - - mLastTime = millis(); - - mTimeSeconds++; - - // Blink the led - - mLedON = !mLedON; - digitalWrite(LED_BUILTIN, (mLedON)?LOW:HIGH); - - // Debug the time (verbose level) - - rdebugVln("* Time: %u seconds (VERBOSE)", mTimeSeconds); - - if (mTimeSeconds % 5 == 0) { // Each 5 seconds - - Debug.handle(); - } - } -} void initWifi(int initpin,const char *failedssid, const char *failedpassword) { diff --git a/Arduino/SenderTrial2/BTCWifi.h b/Arduino/SenderTrial2/BTCWifi.h index 2e80323..a6b585a 100644 --- a/Arduino/SenderTrial2/BTCWifi.h +++ b/Arduino/SenderTrial2/BTCWifi.h @@ -1,14 +1,12 @@ #include #include #include -#include "RemoteDebug.h" //https://github.com/JoaoLopesF/RemoteDebug +#include + /* const char *failedssid; const char *failedpassword; */ - extern RemoteDebug Debug; void doWiFiManager(); void initWifi(int initpin,const char *failedssid, const char *failedpassword); - void DoDebug(); - void inittelnetdebug(String HOST_NAME); diff --git a/Arduino/SenderTrial2/BluetoothESP32.cpp b/Arduino/SenderTrial2/BluetoothESP32.cpp index 03936a8..604de1c 100644 --- a/Arduino/SenderTrial2/BluetoothESP32.cpp +++ b/Arduino/SenderTrial2/BluetoothESP32.cpp @@ -4,11 +4,11 @@ #include "debugport.h" #ifdef TELNET -#define PRNT Debug +#define DebugPort Debug #endif #ifndef TELNET -#define PRNT Serial +#define DebugPort Serial #endif @@ -61,14 +61,14 @@ void Bluetooth_Init() // Open Serial2, explicitly specify pins for pin multiplexer!); Serial2.begin(9600, SERIAL_8N1, Rx2Pin, Tx2Pin); - PRNT.println("\r\n\r\nAttempting to detect HC-05 Bluetooth module..."); + 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++) { - PRNT.print(" @ "); - PRNT.print(BTRates[BTidx]); - PRNT.printf(" baud... "); + DebugPort.print(" @ "); + DebugPort.print(BTRates[BTidx]); + DebugPort.print(" baud... "); Serial2.begin(BTRates[BTidx], SERIAL_8N1, Rx2Pin, Tx2Pin); // open serial port at a std.baud rate delay(10); Serial2.print("\r\n"); // clear the throat! @@ -76,30 +76,30 @@ void Bluetooth_Init() Serial2.setTimeout(100); if(Bluetooth_ATCommand("AT\r\n")) { // probe with a simple "AT" - PRNT.println(" OK."); // got a response - woo hoo found the module! + DebugPort.println(" OK."); // got a response - woo hoo found the module! break; } if(Bluetooth_ATCommand("AT\r\n")) { // sometimes a second try is good... - PRNT.println(" OK."); + DebugPort.println(" OK."); break; } // failed, try another baud rate - PRNT.println(""); + DebugPort.println(""); Serial2.flush(); Serial2.end(); delay(100); } - PRNT.println(""); + 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... - PRNT.println("FAILED to detect a HC-05 Bluetooth module :-("); + 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... - PRNT.println("ASSUMING a HC-05 module @ 9600baud (Unknown name)"); + DebugPort.println("ASSUMING a HC-05 module @ 9600baud (Unknown name)"); Serial2.begin(9600, SERIAL_8N1, Rx2Pin, Tx2Pin); } else { @@ -107,22 +107,22 @@ void Bluetooth_Init() // now program it's name and force a 9600 baud data interface. // this is the defacto standard as shipped! - PRNT.println("HC-05 found"); + DebugPort.println("HC-05 found"); do { // so we can break! - PRNT.printf(" Setting Name to \"Diesel Heater\"... "); + DebugPort.print(" Setting Name to \"Diesel Heater\"... "); if(!Bluetooth_ATCommand("AT+NAME=\"Diesel Heater\"\r\n")) { - PRNT.println("FAILED"); + DebugPort.println("FAILED"); break; } - PRNT.println("OK"); + DebugPort.println("OK"); - PRNT.printf(" Setting baud rate to 9600N81..."); + DebugPort.print(" Setting baud rate to 9600N81..."); if(!Bluetooth_ATCommand("AT+UART=9600,1,0\r\n")) { - PRNT.println("FAILED"); + DebugPort.println("FAILED"); break; }; - PRNT.println("OK"); + DebugPort.println("OK"); Serial2.begin(9600, SERIAL_8N1, Rx2Pin, Tx2Pin); @@ -133,7 +133,7 @@ void Bluetooth_Init() delay(50); - PRNT.println(""); + DebugPort.println(""); } void Bluetooth_Check() @@ -154,8 +154,8 @@ void Bluetooth_Check() void Bluetooth_SendFrame(const char* pHdr, const CProtocol& Frame, bool lineterm) { - PRNT.print(millis()); - PRNT.printf("ms "); + DebugPort.print(millis()); + DebugPort.print("ms "); // DebugReportFrame(pHdr, Frame, lineterm ? "\r\n" : " "); DebugReportFrame(pHdr, Frame, " "); @@ -168,16 +168,16 @@ void Bluetooth_SendFrame(const char* pHdr, const CProtocol& Frame, bool lineterm digitalWrite(LED, !digitalRead(LED)); // toggle LED } else { - PRNT.printf("Bluetooth data not sent, CRC error "); + DebugPort.print("Bluetooth data not sent, CRC error "); } } else { - PRNT.printf("No Bluetooth client"); + DebugPort.print("No Bluetooth client"); // force LED off digitalWrite(LED, 0); } if(lineterm) - PRNT.println(""); + DebugPort.println(""); } // local function, typically to perform Hayes commands with HC-05 @@ -212,7 +212,7 @@ void Bluetooth_Init() pinMode(LED, OUTPUT); if(!SerialBT.begin("ESPHEATER")) { - PRNT.println("An error occurred initialising Bluetooth"); + DebugPort.println("An error occurred initialising Bluetooth"); } } @@ -234,7 +234,7 @@ void Bluetooth_SendFrame(const char* pHdr, const CProtocol& Frame, bool lineterm { char fullMsg[32]; - PRNT.printf(millis()); + DebugPort.print(millis()); DebugReportFrame(pHdr, Frame, lineterm ? "\r\n" : " "); delay(40); if(SerialBT.hasClient()) { @@ -253,11 +253,11 @@ void Bluetooth_SendFrame(const char* pHdr, const CProtocol& Frame, bool lineterm delay(10); } else { - PRNT.println("Data not sent to Bluetooth, CRC error!"); + DebugPort.println("Data not sent to Bluetooth, CRC error!"); } } else { - PRNT.println("No Bluetooth client"); + DebugPort.println("No Bluetooth client"); digitalWrite(LED, 0); } } @@ -365,7 +365,7 @@ void Bluetooth_Init() pService->start(); // start advertising pServer->getAdvertising()->start(); - PRNT.println("Awaiting a client to notify..."); + DebugPort.println("Awaiting a client to notify..."); } void Bluetooth_Report(const char* pHdr, const CProtocol& Frame) @@ -388,7 +388,7 @@ void Bluetooth_Check() if (!deviceConnected && oldDeviceConnected) { delay(500); // give the bluetooth stack the chance to get things ready pServer->startAdvertising(); // restart advertising - PRNT.println("start advertising"); + DebugPort.println("start advertising"); oldDeviceConnected = deviceConnected; } // connecting diff --git a/Arduino/SenderTrial2/BluetoothHC05.cpp b/Arduino/SenderTrial2/BluetoothHC05.cpp index 3f04988..1ada4e5 100644 --- a/Arduino/SenderTrial2/BluetoothHC05.cpp +++ b/Arduino/SenderTrial2/BluetoothHC05.cpp @@ -37,54 +37,54 @@ void Bluetooth_Init() digitalWrite(KeyPin, HIGH); delay(500); - PRNT.println("\r\n\r\nAttempting to detect HC-05 Bluetooth module..."); + 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++) { - PRNT.print(" @ "); - PRNT.print(BTRates[BTidx]); - PRNT.print(" baud... "); + DebugPort.print(" @ "); + DebugPort.print(BTRates[BTidx]); + DebugPort.print(" baud... "); Bluetooth.end(); Bluetooth.begin(BTRates[BTidx]); // open serial port at a certain baud rate Bluetooth.print("\r\n"); Bluetooth.setTimeout(50); if(Bluetooth_ATCommand("AT\r\n")) { - PRNT.println(" OK."); + DebugPort.println(" OK."); break; } // failed, try another baud rate - PRNT.println(""); + DebugPort.println(""); Bluetooth.flush(); } - PRNT.println(""); + DebugPort.println(""); if(BTidx == maxTries) { - PRNT.println("FAILED to detect HC-05 Bluetooth module :-("); + DebugPort.println("FAILED to detect HC-05 Bluetooth module :-("); } else { if(BTRates[BTidx] == 115200) { - PRNT.println("HC-05 found and already set to 115200 baud, skipping Init."); + DebugPort.println("HC-05 found and already set to 115200 baud, skipping Init."); bHC05Available = true; } else { do { - PRNT.println("HC-05 found"); + DebugPort.println("HC-05 found"); - PRNT.print(" Setting Name to \"DieselHeater\"... "); + DebugPort.print(" Setting Name to \"DieselHeater\"... "); if(!Bluetooth_ATCommand("AT+NAME=\"DieselHeater\"\r\n")) { - PRNT.println("FAILED"); + DebugPort.println("FAILED"); break; } - PRNT.println("OK"); + DebugPort.println("OK"); - PRNT.print(" Setting baud rate to 115200N81..."); + DebugPort.print(" Setting baud rate to 115200N81..."); if(!Bluetooth_ATCommand("AT+UART=115200,1,0\r\n")) { - PRNT.println("FAILED"); + DebugPort.println("FAILED"); break; }; - PRNT.println("OK"); + DebugPort.println("OK"); Bluetooth.end(); Bluetooth.begin(115200); @@ -101,7 +101,7 @@ void Bluetooth_Init() if(!bHC05Available) Bluetooth.end(); // close serial port if no module found - PRNT.println(""); + DebugPort.println(""); } void Bluetooth_Check() @@ -130,8 +130,8 @@ void Bluetooth_SendFrame(const char* pHdr, const CProtocol& Frame) Bluetooth.write(Frame.Data, 24); } else { - PRNT.print("Bluetooth data not sent, CRC error "); - PRNT.println(pHdr); + DebugPort.print("Bluetooth data not sent, CRC error "); + DebugPort.println(pHdr); } } } diff --git a/Arduino/SenderTrial2/Protocol.cpp b/Arduino/SenderTrial2/Protocol.cpp index addc717..f7083de 100644 --- a/Arduino/SenderTrial2/Protocol.cpp +++ b/Arduino/SenderTrial2/Protocol.cpp @@ -3,11 +3,11 @@ #include "debugport.h" #ifdef TELNET -#define PRNT Debug +#define DebugPort Debug #endif #ifndef TELNET -#define PRNT Serial +#define DebugPort Serial #endif @@ -61,10 +61,10 @@ CProtocol::verifyCRC() const unsigned short FrameCRC = getCRC(); bool bOK = (FrameCRC == CRC); if(!bOK) { - PRNT.print("verifyCRC FAILED: calc:"); - PRNT.print(CRC, HEX); - PRNT.print(" data:"); - PRNT.println(FrameCRC, HEX); + DebugPort.print("verifyCRC FAILED: calc:"); + DebugPort.print(CRC, HEX); + DebugPort.print(" data:"); + DebugPort.println(FrameCRC, HEX); } return bOK; // does it match the stored values? } diff --git a/Arduino/SenderTrial2/SenderTrial2.ino b/Arduino/SenderTrial2/SenderTrial2.ino index e3e481a..ef0dd44 100644 --- a/Arduino/SenderTrial2/SenderTrial2.ino +++ b/Arduino/SenderTrial2/SenderTrial2.ino @@ -77,14 +77,14 @@ //comment this out to remove TELNET -#define TELNET +//#define TELNET #ifdef TELNET -#define PRNT Debug +#define DebugPort Debug #endif #ifndef TELNET -#define PRNT DebugPort +#define DebugPort DebugPort #endif #define DEBUG_BTRX @@ -171,7 +171,7 @@ void PrepareTxFrame(const CProtocol& basisFrame, CProtocol& TxFrame, bool isBTCm void setup() { initWifi(TRIGGER_PIN, FAILEDSSID, FAILEDPASSWORD); - inittelnetdebug(HOST_NAME); + // initialize serial port to interact with the "blue wire" // 25000 baud, Tx and Rx channels of Chinese heater comms interface: // Tx/Rx data to/from heater, @@ -229,8 +229,7 @@ void loop() { unsigned long timenow = millis(); doWiFiManager(); - //PRNT.println(WiFi.localIP()); - DoDebug(); + // check for test commands received from PC Over USB if(DebugPort.available()) { @@ -286,9 +285,9 @@ void loop() // This will be the first activity for considerable period on the blue wire // The heater always responds to a controller frame, but otherwise never by itself if(BlueWireData.available() && (RxTimeElapsed > 100)) { - PRNT.printf("Re-sync'd with OEM Controller. "); - PRNT.print(RxTimeElapsed); - PRNT.println("ms Idle time."); + DebugPort.print("Re-sync'd with OEM Controller. "); + DebugPort.print(RxTimeElapsed); + DebugPort.println("ms Idle time."); hasOEMController = true; CommState.set(CommStates::OEMCtrlRx); // we must add this new byte! } @@ -384,13 +383,13 @@ void loop() void DebugReportFrame(const char* hdr, const CProtocol& Frame, const char* ftr) { - PRNT.printf(hdr); // header + DebugPort.print(hdr); // header for(int i=0; i<24; i++) { char str[16]; sprintf(str, " %02X", Frame.Data[i]); // build 2 dig hex values - PRNT.printf(str); // and print + DebugPort.print(str); // and print } - PRNT.printf(ftr); // footer + DebugPort.print(ftr); // footer } @@ -403,7 +402,7 @@ void Command_Interpret(const char* pLine) return; #ifdef DEBUG_BTRX - PRNT.println(pLine); + DebugPort.println(pLine); #endif if(strncmp(pLine, "[CMD]", 5) == 0) { @@ -413,61 +412,61 @@ void Command_Interpret(const char* pLine) pLine += 5; // skip past "[CMD]" header if(strncmp(pLine, "ON", 2) == 0) { TxManage.queueOnRequest(); - PRNT.println("Heater ON"); + DebugPort.println("Heater ON"); } else if(strncmp(pLine, "OFF", 3) == 0) { TxManage.queueOffRequest(); - PRNT.println("Heater OFF"); + DebugPort.println("Heater OFF"); } else if(strncmp(pLine, "Pmin", 4) == 0) { pLine += 4; cVal = (unsigned char)((atof(pLine) * 10.0) + 0.5); pNVStorage->setPmin(cVal); - PRNT.printf("Pump min = "); - PRNT.println(cVal); + DebugPort.print("Pump min = "); + DebugPort.println(cVal); } else if(strncmp(pLine, "Pmax", 4) == 0) { pLine += 4; cVal = (unsigned char)((atof(pLine) * 10.0) + 0.5); pNVStorage->setPmax(cVal); - PRNT.printf("Pump max = "); - PRNT.println(cVal); + DebugPort.print("Pump max = "); + DebugPort.println(cVal); } else if(strncmp(pLine, "Fmin", 4) == 0) { pLine += 4; sVal = atoi(pLine); pNVStorage->setFmin(sVal); - PRNT.printf("Fan min = "); - PRNT.println(sVal); + DebugPort.print("Fan min = "); + DebugPort.println(sVal); } else if(strncmp(pLine, "Fmax", 4) == 0) { pLine += 4; sVal = atoi(pLine); pNVStorage->setFmax(sVal); - PRNT.printf("Fan max = "); - PRNT.println(int(sVal)); + DebugPort.print("Fan max = "); + DebugPort.println(int(sVal)); } else if(strncmp(pLine, "save", 4) == 0) { pNVStorage->save(); - PRNT.println("NV save"); + DebugPort.println("NV save"); } else if(strncmp(pLine, "degC", 4) == 0) { pLine += 4; cVal = atoi(pLine); pNVStorage->setTemperature(cVal); - PRNT.printf("degC = "); - PRNT.println(cVal); + DebugPort.print("degC = "); + DebugPort.println(cVal); } else if(strncmp(pLine, "Mode", 4) == 0) { pLine += 4; cVal = !pNVStorage->getThermostatMode(); pNVStorage->setThermostatMode(cVal); - PRNT.printf("Mode now "); - PRNT.println(cVal ? "Thermostat" : "Fixed Hz"); + DebugPort.print("Mode now "); + DebugPort.println(cVal ? "Thermostat" : "Fixed Hz"); } else { - PRNT.printf(pLine); - PRNT.println(" ????"); + DebugPort.print(pLine); + DebugPort.println(" ????"); } }