Serial Print from .cpp?

Just wanted to know if there is a way to do Serial.println() from the .cpp file in your project.

Short answer, yes.

Are you trying to print from a class member function?

You can pass a Stream object to your class,

Lots of options.

More generally, make sure you:

#include "Particle.h"

in your .cpp file. It's included automatically for .ino files, but for .cpp files you need to manually include it in order to get all of the standard Particle/Arduino stuff.

1 Like

NO NO NO NO NO NO NO NO!!! Originally I had my functions file in .ino and I then I switched it to .cpp. Unfortunately I spent a good day using the #include <WProgram.h> library and trying to get my code to adapt to the whole .cpp environment. I am guessing that all I had to do was #include "Particle.h" Am I right?

Sorry, yes, probably all you needed was to include Particle.h.

The main differences between .ino and .cpp files are:

  • Particle.h is automatically included in .ino files
  • You can use a function before you’ve declared it in .ino files. .cpp files require forward declaration, like:
int functionHandler(String cmd);

if you use the function, for example in a Particle.function call, before you’ve implemented the function in the .cpp file.

3 Likes

I seem to have hit this same issue with a Xenon build.

//------------------------------------------------------------------
void setup()
//------------------------------------------------------------------
{
    Serial.begin(9600);
  
  LCDinit();
  lcd.print(deviceModel);
  lcd.setCursor(0,1);
  lcd.print(F("Version "));
  lcd.print(codeVersion);
  Serial.print("\nInit");

  showHelp();
}

There is an #include “CMD.h” in the .ino. My CMD.cpp has

// ------------------------------------------------------------------------------
void showHelp()
// ------------------------------------------------------------------------------
{
  const char msg1[] = "[? for help]>";
  Serial.print(msg1);
}

CMD.cpp includes “Particle.h”

The lcd functions all work okay. The initial Serial message in Setup() prints, but the stuff in CMD.cpp doesn’t

What am I doing wrong?

Then it should work.

What IDE are you using?
Can you share the entire project?

Yes - I included “Particle.h”.

Using Web IDE to keep it simple
Here is link https://go.particle.io/shared_apps/5c7fbbf6feadaf000a691246

I’m not entirely sure what the intent of this statement is in void consoleText(const char* textToDisplay)

  Serial.printf(tmpText, consoleWidth, "%s", textToDisplay);

Do you not rather want to do

  snprintf(tmpText, consoleWidth, "%s", textToDisplay); 
  Serial.print(tmpText);

Also for some odd reason the Web IDE is duplicating my .ccp and .h files

That’s a known issue which is annoying but can be ignored.

Thanks

I got this from the reference section of the particle site - if that statement is moved to .ino it works as expected (as a combination of snprintf and print)

https://docs.particle.io/reference/device-os/firmware/xenon/#printf-

I will change and try again anyway.

I don’t quite get the resemblance between the docs sample and your code :wink:

docs:

Serial.printf("the temperature today is %f Kelvin", temp);

Where the first parameter is the format string and the second (and potentially following) are the substitution values for the respective format place holders in the format string.

But your version

Serial.printf(tmpText, consoleWidth, "%s", textToDisplay);

seens to have a target buffer as first paramter, follwed by a not supported length parameter, followed by what should be the first and following parameter.

:slight_smile: Just proves the wood is sometimes too close to see the trees… (But why no compiler error…)

Because dynamic variable lists are quite forgiving.
The function expects a char buffer first parameter (which your tempText is) and then accepts any variable that doesn't explicitly contradict a literal format string (which you don't provide).

But I have tested your code and there seems to be something fishy in deed.
I'll investigate a bit more.

1 Like

@shanevanj, I have now recreated code in an entirely new project and now it works.
Have a test with this
Particle Web IDE

1 Like

That works perfectly - appreciate the help!