Added RemoteDebug and a define to change debugging from serial to telnet depening on telnet define at top of .ino

This commit is contained in:
James 2018-10-28 14:03:44 +11:00
parent f3d6d8d6d2
commit 15a3b6d3ef
6 changed files with 225 additions and 79 deletions

View file

@ -1,19 +1,110 @@
#include "BTCWifi.h"
//this is for telnet only
// 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();
bool portalRunning = false;
bool startCP = false; // start AP and webserver if true, else start only webserver
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.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)
{
TRIG_PIN = initpin;
pinMode(initpin, INPUT_PULLUP);
//reset settings - wipe credentials for testing
//wm.resetSettings();
// Automatically connect using saved credentials,
// if connection fails, it starts an access point with the specified name ( "AutoConnectAP"),
// if empty will auto generate SSID, if password is blank it will be anonymous AP (wm.autoConnect())
// then goes into a blocking loop awaiting configuration and will return success result
res = wm.autoConnect(); // auto generated AP name from chipid
if(!res) {
Serial.println("Failed to connect");
// ESP.restart();
}
else {
//if you get here you have connected to the WiFi
Serial.println("connected...yeey :)");
}
}
void doWiFiManager(){
@ -33,15 +124,17 @@ void doWiFiManager(){
}
// is configuration portal requested?
if(digitalRead(TRIG_PIN) == LOW && (!portalRunning)) {
if(TRIG_PIN == 1 && (!portalRunning)) {
if(startCP){
Serial.println("Button Pressed, Starting Config Portal");
wm.setConfigPortalBlocking(false);
wm.startConfigPortal();
TRIG_PIN = 0; // reset the flag
}
else{
Serial.println("Button Pressed, Starting Web Portal");
wm.startWebPortal();
TRIG_PIN = 0; // reset the flag
}
portalRunning = true;
startTime = millis();

View file

@ -1,8 +1,12 @@
#include <Arduino.h>
#include <WiFiManager.h>
#include "RemoteDebug.h" //https://github.com/JoaoLopesF/RemoteDebug
extern RemoteDebug Debug;
void doWiFiManager();
void initWifi(int initpin);
void DoDebug();
void inittelnetdebug(String HOST_NAME);

View file

@ -3,6 +3,15 @@
#include "Protocol.h"
#include "debugport.h"
#ifdef TELNET
#define PRNT Debug
#endif
#ifndef TELNET
#define PRNT Serial
#endif
#ifdef ESP32
#define ESP32_USE_HC05
@ -52,14 +61,14 @@ void Bluetooth_Init()
// Open Serial2, explicitly specify pins for pin multiplexer!);
Serial2.begin(9600, SERIAL_8N1, Rx2Pin, Tx2Pin);
DebugPort.println("\r\n\r\nAttempting to detect HC-05 Bluetooth module...");
PRNT.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... ");
PRNT.print(" @ ");
PRNT.print(BTRates[BTidx]);
PRNT.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!
@ -67,31 +76,31 @@ void Bluetooth_Init()
Serial2.setTimeout(100);
if(Bluetooth_ATCommand("AT\r\n")) { // probe with a simple "AT"
DebugPort.println(" OK."); // got a response - woo hoo found the module!
PRNT.println(" OK."); // got a response - woo hoo found the module!
break;
}
if(Bluetooth_ATCommand("AT\r\n")) { // sometimes a second try is good...
DebugPort.println(" OK.");
PRNT.println(" OK.");
break;
}
// failed, try another baud rate
DebugPort.println("");
PRNT.println("");
Serial2.flush();
Serial2.end();
delay(100);
}
DebugPort.println("");
PRNT.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 :-(");
PRNT.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)");
PRNT.println("ASSUMING a HC-05 module @ 9600baud (Unknown name)");
Serial2.begin(9600, SERIAL_8N1, Rx2Pin, Tx2Pin);
}
else {
@ -99,22 +108,22 @@ void Bluetooth_Init()
// now program it's name and force a 9600 baud data interface.
// this is the defacto standard as shipped!
DebugPort.println("HC-05 found");
PRNT.println("HC-05 found");
do { // so we can break!
DebugPort.print(" Setting Name to \"Diesel Heater\"... ");
PRNT.print(" Setting Name to \"Diesel Heater\"... ");
if(!Bluetooth_ATCommand("AT+NAME=\"Diesel Heater\"\r\n")) {
DebugPort.println("FAILED");
PRNT.println("FAILED");
break;
}
DebugPort.println("OK");
PRNT.println("OK");
DebugPort.print(" Setting baud rate to 9600N81...");
PRNT.print(" Setting baud rate to 9600N81...");
if(!Bluetooth_ATCommand("AT+UART=9600,1,0\r\n")) {
DebugPort.println("FAILED");
PRNT.println("FAILED");
break;
};
DebugPort.println("OK");
PRNT.println("OK");
Serial2.begin(9600, SERIAL_8N1, Rx2Pin, Tx2Pin);
@ -125,7 +134,7 @@ void Bluetooth_Init()
delay(50);
DebugPort.println("");
PRNT.println("");
}
void Bluetooth_Check()
@ -146,8 +155,8 @@ void Bluetooth_Check()
void Bluetooth_SendFrame(const char* pHdr, const CProtocol& Frame, bool lineterm)
{
DebugPort.print(millis());
DebugPort.print("ms ");
PRNT.print(millis());
PRNT.print("ms ");
// DebugReportFrame(pHdr, Frame, lineterm ? "\r\n" : " ");
DebugReportFrame(pHdr, Frame, " ");
@ -160,16 +169,16 @@ void Bluetooth_SendFrame(const char* pHdr, const CProtocol& Frame, bool lineterm
digitalWrite(LED, !digitalRead(LED)); // toggle LED
}
else {
DebugPort.print("Bluetooth data not sent, CRC error ");
PRNT.print("Bluetooth data not sent, CRC error ");
}
}
else {
DebugPort.print("No Bluetooth client");
PRNT.print("No Bluetooth client");
// force LED off
digitalWrite(LED, 0);
}
if(lineterm)
DebugPort.println("");
PRNT.println("");
}
// local function, typically to perform Hayes commands with HC-05
@ -204,7 +213,7 @@ void Bluetooth_Init()
pinMode(LED, OUTPUT);
if(!SerialBT.begin("ESPHEATER")) {
DebugPort.println("An error occurred initialising Bluetooth");
PRNT.println("An error occurred initialising Bluetooth");
}
}
@ -226,7 +235,7 @@ void Bluetooth_SendFrame(const char* pHdr, const CProtocol& Frame, bool lineterm
{
char fullMsg[32];
DebugPort.print(millis());
PRNT.print(millis());
DebugReportFrame(pHdr, Frame, lineterm ? "\r\n" : " ");
delay(40);
if(SerialBT.hasClient()) {
@ -245,11 +254,11 @@ void Bluetooth_SendFrame(const char* pHdr, const CProtocol& Frame, bool lineterm
delay(10);
}
else {
DebugPort.println("Data not sent to Bluetooth, CRC error!");
PRNT.println("Data not sent to Bluetooth, CRC error!");
}
}
else {
DebugPort.println("No Bluetooth client");
PRNT.println("No Bluetooth client");
digitalWrite(LED, 0);
}
}
@ -357,7 +366,7 @@ void Bluetooth_Init()
pService->start();
// start advertising
pServer->getAdvertising()->start();
DebugPort.println("Awaiting a client to notify...");
PRNT.println("Awaiting a client to notify...");
}
void Bluetooth_Report(const char* pHdr, const CProtocol& Frame)
@ -380,7 +389,7 @@ void Bluetooth_Check()
if (!deviceConnected && oldDeviceConnected) {
delay(500); // give the bluetooth stack the chance to get things ready
pServer->startAdvertising(); // restart advertising
DebugPort.println("start advertising");
PRNT.println("start advertising");
oldDeviceConnected = deviceConnected;
}
// connecting

View file

@ -37,54 +37,54 @@ void Bluetooth_Init()
digitalWrite(KeyPin, HIGH);
delay(500);
DebugPort.println("\r\n\r\nAttempting to detect HC-05 Bluetooth module...");
PRNT.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... ");
PRNT.print(" @ ");
PRNT.print(BTRates[BTidx]);
PRNT.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")) {
DebugPort.println(" OK.");
PRNT.println(" OK.");
break;
}
// failed, try another baud rate
DebugPort.println("");
PRNT.println("");
Bluetooth.flush();
}
DebugPort.println("");
PRNT.println("");
if(BTidx == maxTries) {
DebugPort.println("FAILED to detect HC-05 Bluetooth module :-(");
PRNT.println("FAILED to detect HC-05 Bluetooth module :-(");
}
else {
if(BTRates[BTidx] == 115200) {
DebugPort.println("HC-05 found and already set to 115200 baud, skipping Init.");
PRNT.println("HC-05 found and already set to 115200 baud, skipping Init.");
bHC05Available = true;
}
else {
do {
DebugPort.println("HC-05 found");
PRNT.println("HC-05 found");
DebugPort.print(" Setting Name to \"DieselHeater\"... ");
PRNT.print(" Setting Name to \"DieselHeater\"... ");
if(!Bluetooth_ATCommand("AT+NAME=\"DieselHeater\"\r\n")) {
DebugPort.println("FAILED");
PRNT.println("FAILED");
break;
}
DebugPort.println("OK");
PRNT.println("OK");
DebugPort.print(" Setting baud rate to 115200N81...");
PRNT.print(" Setting baud rate to 115200N81...");
if(!Bluetooth_ATCommand("AT+UART=115200,1,0\r\n")) {
DebugPort.println("FAILED");
PRNT.println("FAILED");
break;
};
DebugPort.println("OK");
PRNT.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
DebugPort.println("");
PRNT.println("");
}
void Bluetooth_Check()
@ -130,8 +130,8 @@ void Bluetooth_SendFrame(const char* pHdr, const CProtocol& Frame)
Bluetooth.write(Frame.Data, 24);
}
else {
DebugPort.print("Bluetooth data not sent, CRC error ");
DebugPort.println(pHdr);
PRNT.print("Bluetooth data not sent, CRC error ");
PRNT.println(pHdr);
}
}
}

View file

@ -1,6 +1,15 @@
#include <Arduino.h>
#include "Protocol.h"
#include "debugport.h"
#ifdef TELNET
#define PRNT Debug
#endif
#ifndef TELNET
#define PRNT Serial
#endif
unsigned short
CProtocol::CalcCRC(int len) const
@ -52,10 +61,10 @@ CProtocol::verifyCRC() const
unsigned short FrameCRC = getCRC();
bool bOK = (FrameCRC == CRC);
if(!bOK) {
DebugPort.print("verifyCRC FAILED: calc:");
DebugPort.print(CRC, HEX);
DebugPort.print(" data:");
DebugPort.println(FrameCRC, HEX);
PRNT.print("verifyCRC FAILED: calc:");
PRNT.print(CRC, HEX);
PRNT.print(" data:");
PRNT.println(FrameCRC, HEX);
}
return bOK; // does it match the stored values?
}

View file

@ -1,3 +1,17 @@
//comment this out to remove TELNET
#define TELNET
#ifdef TELNET
#define PRNT Debug
#endif
#ifndef TELNET
#define PRNT Serial
#endif
/*
Chinese Heater Half Duplex Serial Data Sending Tool
@ -68,8 +82,22 @@
#include "NVStorage.h"
#include "debugport.h"
#include "BTCWifi.h"
#define HOST_NAME "remotedebug-sample"
#define TRIGGER_PIN 0
#define TRIGGER_PIN 12
//comment this out to remove TELNET
#define TELNET
#ifdef TELNET
#define PRNT Debug
#endif
#ifndef TELNET
#define PRNT DebugPort
#endif
#define DEBUG_BTRX
#include "Bluetooth.h"
@ -155,6 +183,7 @@ void PrepareTxFrame(const CProtocol& basisFrame, CProtocol& TxFrame, bool isBTCm
void setup()
{
initWifi(TRIGGER_PIN);
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,
@ -212,7 +241,9 @@ void loop()
{
unsigned long timenow = millis();
doWiFiManager();
DoDebug();
// check for test commands received from PC Over USB
if(DebugPort.available()) {
char rxval = DebugPort.read();
if(rxval == '+') {
@ -266,9 +297,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)) {
DebugPort.print("Re-sync'd with OEM Controller. ");
DebugPort.print(RxTimeElapsed);
DebugPort.println("ms Idle time.");
PRNT.print("Re-sync'd with OEM Controller. ");
PRNT.print(RxTimeElapsed);
PRNT.println("ms Idle time.");
hasOEMController = true;
CommState.set(CommStates::OEMCtrlRx); // we must add this new byte!
}
@ -364,13 +395,13 @@ void loop()
void DebugReportFrame(const char* hdr, const CProtocol& Frame, const char* ftr)
{
DebugPort.print(hdr); // header
PRNT.print(hdr); // header
for(int i=0; i<24; i++) {
char str[16];
sprintf(str, " %02X", Frame.Data[i]); // build 2 dig hex values
DebugPort.print(str); // and print
PRNT.print(str); // and print
}
DebugPort.print(ftr); // footer
PRNT.print(ftr); // footer
}
@ -383,7 +414,7 @@ void Command_Interpret(const char* pLine)
return;
#ifdef DEBUG_BTRX
DebugPort.println(pLine);
PRNT.println(pLine);
#endif
if(strncmp(pLine, "[CMD]", 5) == 0) {
@ -393,61 +424,61 @@ void Command_Interpret(const char* pLine)
pLine += 5; // skip past "[CMD]" header
if(strncmp(pLine, "ON", 2) == 0) {
TxManage.queueOnRequest();
DebugPort.println("Heater ON");
PRNT.println("Heater ON");
}
else if(strncmp(pLine, "OFF", 3) == 0) {
TxManage.queueOffRequest();
DebugPort.println("Heater OFF");
PRNT.println("Heater OFF");
}
else if(strncmp(pLine, "Pmin", 4) == 0) {
pLine += 4;
cVal = (unsigned char)((atof(pLine) * 10.0) + 0.5);
pNVStorage->setPmin(cVal);
DebugPort.print("Pump min = ");
DebugPort.println(cVal);
PRNT.print("Pump min = ");
PRNT.println(cVal);
}
else if(strncmp(pLine, "Pmax", 4) == 0) {
pLine += 4;
cVal = (unsigned char)((atof(pLine) * 10.0) + 0.5);
pNVStorage->setPmax(cVal);
DebugPort.print("Pump max = ");
DebugPort.println(cVal);
PRNT.print("Pump max = ");
PRNT.println(cVal);
}
else if(strncmp(pLine, "Fmin", 4) == 0) {
pLine += 4;
sVal = atoi(pLine);
pNVStorage->setFmin(sVal);
DebugPort.print("Fan min = ");
DebugPort.println(sVal);
PRNT.print("Fan min = ");
PRNT.println(sVal);
}
else if(strncmp(pLine, "Fmax", 4) == 0) {
pLine += 4;
sVal = atoi(pLine);
pNVStorage->setFmax(sVal);
DebugPort.print("Fan max = ");
DebugPort.println(int(sVal));
PRNT.print("Fan max = ");
PRNT.println(int(sVal));
}
else if(strncmp(pLine, "save", 4) == 0) {
pNVStorage->save();
DebugPort.println("NV save");
PRNT.println("NV save");
}
else if(strncmp(pLine, "degC", 4) == 0) {
pLine += 4;
cVal = atoi(pLine);
pNVStorage->setTemperature(cVal);
DebugPort.print("degC = ");
DebugPort.println(cVal);
PRNT.print("degC = ");
PRNT.println(cVal);
}
else if(strncmp(pLine, "Mode", 4) == 0) {
pLine += 4;
cVal = !pNVStorage->getThermostatMode();
pNVStorage->setThermostatMode(cVal);
DebugPort.print("Mode now ");
DebugPort.println(cVal ? "Thermostat" : "Fixed Hz");
PRNT.print("Mode now ");
PRNT.println(cVal ? "Thermostat" : "Fixed Hz");
}
else {
DebugPort.print(pLine);
DebugPort.println(" ????");
PRNT.print(pLine);
PRNT.println(" ????");
}
}