Change Serial Port on Arduino Nano 33 iot on-the-fly

Hello, I’m working on a little project with some nano 33 iot’s and I’ve used Serial throughout my code for debugging purposes. I also use serial commands to change some settings in my code. Currently I just have it connected to my computer which is great for debugging the main part of my code but I’d like to be able to check my stream of debug info while my project is in the “field”.
I figure I could hook up one of those cheap serial Bluetooth modules to Serial1 to transmit what I need much more conveniently. My problem however is I’d like to be able to change which serial port I’m using on the fly without doubling the amount of debug code to pick which one in every instance. I’ve tried a few things I’ve seen on the forum for other boards and eventually got some code to compile but only subsequently locked up the nano 33 iot immediately. From what I can tell it’s the difficulty is partly because Serial and Serial1 are from different sources, USB versus Hardware.
I’ve already deleted the code I tried because of how bad it bricked my board temporarily but basically I tried declaring a global object of Stream to use throughout my code and then later setting it with Serial1 or Serial which only succeeded in causing my board to fail to communicate via USB.

I know I could just solely use the Bluetooth and connect that to my computer but I kinda just want to learn if it’s even possible to change it on the fly. Sorta like flip a switch to enable Bluetooth debugging and have all of my serial functions start using the other port so I have the option for either or. And honestly even if there was a way to just print to both at the same time that would be sufficient I think

Do you guys have any suggestions on a simple fix or is this not do-able?

Why not create a function that call when you print and include an additional argument that determines where it is sent. It can simply be a true false parameter. If flag is true print to x else print to y.

I thought of that but I believe I would have to have a ton of overloaded functions to handle the various variable types, I looked at print h for an example and I figured that was just too much work honestly

Right now I can initialize a global variable and use that to print as normal throughout my code, after some research I found a reference made this way can’t be changed so basically still no solution

Stream &debugPort = Serial;
debugPort.println();

But later on when I try to change it with

DebugPort = Serial1;

Nothing changes and it still prints thru serial

This is my current method which allows me to change which serial port all my print statements goto easily at time of compile, basically I would just like to do this at runtime somehow, preferably on the fly. In the rest of my code I have a stream handler so that I can change parameters after it’s uploaded, works from either Serial or from my httpclient

#define SERIAL1DEBUG false

#if SERIAL1DEBUG == true
Stream &debugPort = Serial1;
#elif SERIAL1DEBUG == false
Stream &debugPort = Serial;
#endif
[\code]

The only other solutions I’ve come up with which are less than ideal is to pass along a local stream object to every function to print with like I do in this function

void printConfig(Stream &port){
if(currentConfig.DEBUG) {
  port.println(DEBUGBREAK);
  port.println("Print Config");
  port.println(DEBUGBREAK);
}
port.print("DEBUG: ");port.println(currentConfig.DEBUG);
port.print("sleepEnabled: ");port.println(currentConfig.sleepEnabled);
port.print("LOCALSERVER: ");port.println(currentConfig.LOCALSERVER);
port.print("serial1Debug: ");port.println(currentConfig.serial1Debug);
port.print("serial1BaudRate: ");port.println(currentConfig.serial1BaudRate);
port.print("configPassword: ");port.println(currentConfig.configPassword);
port.print("bootCount: ");port.println(currentConfig.bootCount);
port.print("SERVERHOSTNAME: ");port.println(currentConfig.SERVERHOSTNAME);
// etc,etc,etc
[\code]

But that would be annoying and require me to change every function on several pages of code
Or to add an if else everywhere and basically double the amount of debug code which Feela really unnecessary

Here's my final solution which does appear to work well, allows me to change between Serial, Serial1, and a wifi client (called testClient)

Create a global Print object and set the default method

Print *debugPort = &Serial;

Later on change the print object like this

if(request.equals("debugPort")){
if(value.equals("Serial")) debugPort = &Serial;
else if(value.equals("Serial1")) debugPort = &Serial1;
else if(value.equals("testClient")) debugPort = &testClient;
}

Then to use the object for example

debugPort->print("sleepEnabled: ");
debugPort->println(currentConfig.sleepEnabled);

This seems to work well so far, just a little annoying having to use -> instead of . But other than that it's been working well if this is useful to anyone else

And just a note, I haven't tested using any other methods other than print and println, the begin , connect, etc is all handled independently

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.