From c41f95619bdedb8679ac3613a6981c6c99664b16 Mon Sep 17 00:00:00 2001 From: Kemal Hadimli Date: Tue, 26 May 2026 19:22:25 +0100 Subject: [PATCH 1/2] Configurable UART console --- examples/simple_repeater/MyMesh.cpp | 18 +++++++++--------- examples/simple_repeater/main.cpp | 21 ++++++++++++--------- src/MeshCore.h | 10 +++++++--- variants/rak3401/platformio.ini | 2 ++ 4 files changed, 30 insertions(+), 21 deletions(-) diff --git a/examples/simple_repeater/MyMesh.cpp b/examples/simple_repeater/MyMesh.cpp index 1f68c6f2a0..ca31522b35 100644 --- a/examples/simple_repeater/MyMesh.cpp +++ b/examples/simple_repeater/MyMesh.cpp @@ -461,10 +461,10 @@ const char *MyMesh::getLogDateTime() { void MyMesh::logRxRaw(float snr, float rssi, const uint8_t raw[], int len) { #if MESH_PACKET_LOGGING - Serial.print(getLogDateTime()); - Serial.print(" RAW: "); - mesh::Utils::printHex(Serial, raw, len); - Serial.println(); + MESH_CONSOLE_SERIAL.print(getLogDateTime()); + MESH_CONSOLE_SERIAL.print(" RAW: "); + mesh::Utils::printHex(MESH_CONSOLE_SERIAL, raw, len); + MESH_CONSOLE_SERIAL.println(); #endif } @@ -1043,7 +1043,7 @@ void MyMesh::dumpLogFile() { while (f.available()) { int c = f.read(); if (c < 0) break; - Serial.print((char)c); + MESH_CONSOLE_SERIAL.print((char)c); } f.close(); } @@ -1232,14 +1232,14 @@ void MyMesh::handleCommand(uint32_t sender_timestamp, char *command, char *reply } } } else if (sender_timestamp == 0 && strcmp(command, "get acl") == 0) { - Serial.println("ACL:"); + MESH_CONSOLE_SERIAL.println("ACL:"); for (int i = 0; i < acl.getNumClients(); i++) { auto c = acl.getClientByIdx(i); if (c->permissions == 0) continue; // skip deleted (or guest) entries - Serial.printf("%02X ", c->permissions); - mesh::Utils::printHex(Serial, c->id.pub_key, PUB_KEY_SIZE); - Serial.printf("\n"); + MESH_CONSOLE_SERIAL.printf("%02X ", c->permissions); + mesh::Utils::printHex(MESH_CONSOLE_SERIAL, c->id.pub_key, PUB_KEY_SIZE); + MESH_CONSOLE_SERIAL.printf("\n"); } reply[0] = 0; } else if (memcmp(command, "discover.neighbors", 18) == 0) { diff --git a/examples/simple_repeater/main.cpp b/examples/simple_repeater/main.cpp index 297337ab5c..b99ec14572 100644 --- a/examples/simple_repeater/main.cpp +++ b/examples/simple_repeater/main.cpp @@ -29,7 +29,10 @@ static unsigned long userBtnDownAt = 0; #endif void setup() { - Serial.begin(115200); +#if defined(USE_SERIAL1_CONSOLE) && defined(NRF52_PLATFORM) + ((Uart *)&MESH_CONSOLE_SERIAL)->setPins(PIN_SERIAL1_RX, PIN_SERIAL1_TX); +#endif + MESH_CONSOLE_SERIAL.begin(115200); delay(1000); board.begin(); @@ -86,8 +89,8 @@ void setup() { store.save("_main", the_mesh.self_id); } - Serial.print("Repeater ID: "); - mesh::Utils::printHex(Serial, the_mesh.self_id.pub_key, PUB_KEY_SIZE); Serial.println(); + MESH_CONSOLE_SERIAL.print("Repeater ID: "); + mesh::Utils::printHex(MESH_CONSOLE_SERIAL, the_mesh.self_id.pub_key, PUB_KEY_SIZE); MESH_CONSOLE_SERIAL.println(); command[0] = 0; @@ -109,12 +112,12 @@ void setup() { void loop() { int len = strlen(command); - while (Serial.available() && len < sizeof(command)-1) { - char c = Serial.read(); + while (MESH_CONSOLE_SERIAL.available() && len < sizeof(command)-1) { + char c = MESH_CONSOLE_SERIAL.read(); if (c != '\n') { command[len++] = c; command[len] = 0; - Serial.print(c); + MESH_CONSOLE_SERIAL.print(c); } if (c == '\r') break; } @@ -123,12 +126,12 @@ void loop() { } if (len > 0 && command[len - 1] == '\r') { // received complete line - Serial.print('\n'); + MESH_CONSOLE_SERIAL.print('\n'); command[len - 1] = 0; // replace newline with C string null terminator char reply[160]; the_mesh.handleCommand(0, command, reply); // NOTE: there is no sender_timestamp via serial! if (reply[0]) { - Serial.print(" -> "); Serial.println(reply); + MESH_CONSOLE_SERIAL.print(" -> "); MESH_CONSOLE_SERIAL.println(reply); } command[0] = 0; // reset command buffer @@ -141,7 +144,7 @@ void loop() { if (userBtnDownAt == 0) { userBtnDownAt = millis(); } else if ((unsigned long)(millis() - userBtnDownAt) >= USER_BTN_HOLD_OFF_MILLIS) { - Serial.println("Powering off..."); + MESH_CONSOLE_SERIAL.println("Powering off..."); board.powerOff(); // does not return } } else { diff --git a/src/MeshCore.h b/src/MeshCore.h index b4c57faf32..c424481a95 100644 --- a/src/MeshCore.h +++ b/src/MeshCore.h @@ -21,17 +21,21 @@ #define MAX_PATH_SIZE 64 #define MAX_TRANS_UNIT 255 +#ifndef MESH_CONSOLE_SERIAL + #define MESH_CONSOLE_SERIAL Serial +#endif + #if MESH_DEBUG && ARDUINO #include - #define MESH_DEBUG_PRINT(F, ...) Serial.printf("DEBUG: " F, ##__VA_ARGS__) - #define MESH_DEBUG_PRINTLN(F, ...) Serial.printf("DEBUG: " F "\n", ##__VA_ARGS__) + #define MESH_DEBUG_PRINT(F, ...) MESH_CONSOLE_SERIAL.printf("DEBUG: " F, ##__VA_ARGS__) + #define MESH_DEBUG_PRINTLN(F, ...) MESH_CONSOLE_SERIAL.printf("DEBUG: " F "\n", ##__VA_ARGS__) #else #define MESH_DEBUG_PRINT(...) {} #define MESH_DEBUG_PRINTLN(...) {} #endif #if BRIDGE_DEBUG && ARDUINO -#define BRIDGE_DEBUG_PRINTLN(F, ...) Serial.printf("%s BRIDGE: " F, getLogDateTime(), ##__VA_ARGS__) +#define BRIDGE_DEBUG_PRINTLN(F, ...) MESH_CONSOLE_SERIAL.printf("%s BRIDGE: " F, getLogDateTime(), ##__VA_ARGS__) #else #define BRIDGE_DEBUG_PRINTLN(...) {} #endif diff --git a/variants/rak3401/platformio.ini b/variants/rak3401/platformio.ini index 20a8a548b9..1f9472818e 100644 --- a/variants/rak3401/platformio.ini +++ b/variants/rak3401/platformio.ini @@ -36,6 +36,8 @@ build_flags = -D MAX_NEIGHBOURS=50 ;-D MESH_PACKET_LOGGING=1 ;-D MESH_DEBUG=1 + ;-D USE_SERIAL1_CONSOLE + ;-D MESH_CONSOLE_SERIAL=Serial1 build_src_filter = ${rak3401.build_src_filter} + +<../examples/simple_repeater> From 87172fcf294b08f8b5d7cf89c4a173616aaa7efa Mon Sep 17 00:00:00 2001 From: Kemal Hadimli Date: Tue, 26 May 2026 20:21:01 +0100 Subject: [PATCH 2/2] No need to specify Serial1 pins again --- examples/simple_repeater/main.cpp | 3 --- variants/rak3401/platformio.ini | 2 +- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/examples/simple_repeater/main.cpp b/examples/simple_repeater/main.cpp index b99ec14572..d6ddf9c713 100644 --- a/examples/simple_repeater/main.cpp +++ b/examples/simple_repeater/main.cpp @@ -29,9 +29,6 @@ static unsigned long userBtnDownAt = 0; #endif void setup() { -#if defined(USE_SERIAL1_CONSOLE) && defined(NRF52_PLATFORM) - ((Uart *)&MESH_CONSOLE_SERIAL)->setPins(PIN_SERIAL1_RX, PIN_SERIAL1_TX); -#endif MESH_CONSOLE_SERIAL.begin(115200); delay(1000); diff --git a/variants/rak3401/platformio.ini b/variants/rak3401/platformio.ini index 1f9472818e..4675a9eebb 100644 --- a/variants/rak3401/platformio.ini +++ b/variants/rak3401/platformio.ini @@ -36,8 +36,8 @@ build_flags = -D MAX_NEIGHBOURS=50 ;-D MESH_PACKET_LOGGING=1 ;-D MESH_DEBUG=1 - ;-D USE_SERIAL1_CONSOLE ;-D MESH_CONSOLE_SERIAL=Serial1 + ;-UENV_INCLUDE_GPS ; GPS is on Serial1, enabling this line disables it build_src_filter = ${rak3401.build_src_filter} + +<../examples/simple_repeater>