Example sketches for the new Arduino IDE for ESP8266

Moderator: igrr

User avatar
By debojitk
#66078 Hi Pablo,
Thanks for your response. I replaced all the Serial.print() for strings with F(). Now F() thing does not work with Serial.printf. There is a function printf_P, that takes 1st arg as a pgmspace string. So I wrote a macro as follows:
Code: Select all#define DEBUG_PRINTF_P(x,...)    printf_P(PSTR(x),__VA_ARGS__)

Then in the code I wrote:
Code: Select all   int i=100;
   DEBUG_PRINTF_P("value of i is: %d", i);

It prints:
Code: Select allvalue of i is: 1073692176

Can you tell me where I am going wrong?
User avatar
By Pablo2048
#66079 I've never used printf_P (I use vsnprintf_P), but You can check source code in Print.h & Print.cpp, or You can try String(F("value of i is %d")).c_str() instead . Did You search the internet for this kind of problem?
User avatar
By debojitk
#66083 The source for both printf_P and vsnprintf_P is pgmspace.cpp. Ideally this should work. The purpose of printf_P is to take the static format string from rom.
The alternate solution of wrapping the fmt string using String is a nice one, but could you please confirm whether this would run into any memory leak. I am saying this because some blog advises to use char * whenever possible and avoid String object.
Please clarify my confusion.

Thanks,
Debojit
User avatar
By Pablo2048
#66100 No, this is not true. IMHO You are using Serial object which is derived from Stream which use Print so Serial.printf_P uses Print object and not pgmspace (at least not directly). IMHO there is no reason for memory leak in this case, but You can easily check this by using ESP.getFreeHeap()...