How to print SD Card Success / Fail to OLED

Good day particle people

I can’t seem to get the code figured out to convert the log.error, log.warn messages converted to a format that I can print to my oled screen.

I have been trying different formats for a few days now and just can’t seem to get it. This is using rickkas7 SDcard tutorial found here.

I am also using the SSD1306 OLED used in this tutorial.

I can print all my data, lines, date, etc. I just can’t get the following printed which would be very helpful to confirm that the Sd Card is behaving properly without pulling it out or being connected to a serial connection. I have tried searching the threads for combinations of print and log but not really finding what I need. Any help is appreciated greatly.

Here are the goal messages I want to print to the oled, or a version of the following.

display.print()

Writing to SD Success
Writing to SD Failed
No Card Found,
Or Card Ejected.
Wow your really handsome… ok that last one is a joke.

Thank you in advance for your time.
Tom

You’d need to create a custom logger that does redirect the Log.####() output to your display.
The library you are refering is actually demonstrating how to create a data sink for Log output.
It can also be used to actually log to SD.

If I’m not mistaken this library is not meant as a substitute for the SDFat library (which it uses to do the actual SD stuff) that focuses on how to use an SD Card in your own project.

However, AFAICT you can instantiate multiple log handlers to have multiple targets for your log messages.

2 Likes

Good day sir.
I do have the SDFat library included in my project. Are you suggesting I search all the files in that directory for the proper conversion of the message I am trying to post to the OLED?

I can successfully print my own data and messages to the OLED. I just can’t seem to find the code to convert the following messages, I am also assuming these are the messages that I am trying to convert to get the results I listed above. Ugh.

Log.warn(“This is warning message”);
Log.error(“This is error message”);

Thanks again for taking the time to read all my noob questions. I really appreciate it.

While you could make a custom logger, I would just take the source to the SdCardLogHandlerRK and copy it directly into your project and edit it to write to your OLED at the appropriate locations. It’s only two files.

2 Likes

I was afraid you were going to say something like that. :joy:

I have been pouring over your two files for days trying to find the right parts to change. Thinking I could just cut and paste, make some changes and be good to go. At this point, I think I am just too green for this level of coding. Maybe I have been staring at it for too long or tried too many variables. But I have hit the proverbial wall of frustration.

Thanks for taking the time to comment, it is appreciated.

To keep your furstration contained, some pointers.

  • You can find @rickkas7’s code here
  • You can download the sources and copy/past the contents of the .h and .cpp files in src as new tabs into your project in Web IDE as shown here (add files, name them exactly as the originals, copy/past original code to respective tab).
  • Add the include statements required to talk to the display
  • Add a line of extern Adafruit_SSD1306 display; (or however you’ve called it in your main .ino)
  • Find the Log.xxx() (or DEBUG_xxx) statements that produce the messages you want to redirect (see bellow)
  • Add the code to print your message to the display

Alternatively you could introduce a new SDCARD_LOGHANDLER_DEBUG_LEVEL which doesn’t send to Serial but rather to your display.

Some code lines that wouldn’t be hard to locate

Lines to have a closer look at in SdCardLogHandlerRK.cpp

  • Writing to SD Success or Writing to SD Failed could most likely be injected here
  • No Card Found or Card Ejected could most likely be injected here
4 Likes

Thank you so much for your time with this Sir. I will try it again on Monday and see how it goes. I really appreciate you taking the time to point out the points of interest along with the added direction. As soon as I figure it out I will post the final code… it may be in 2020 but I will get it. :smile:

3 Likes

Man oh man that was a struggle… for me at least but I think I have it working.
Thank you so much @ScruffR for spelling it out for me. It really helped. Thank you @rickkas7 and @gusgonnet for the encouragement. For those that are looking for something similar here is what I did… Note this may or may not be correct but it is working so far.

I added the following to both my .ino file along with @rickkas7 SdCardLogHanlderRK.cpp file.
#include <Adafruit_SSD1306.h>
extern Adafruit_SSD1306 display;

Note that this was the kicker to get things working for me. Adding the include to the .CPP file along with the extern, I had not seen that yet in code examples, or just did not notice it before.

Not sure if they need to be in both the .ino and the .cpp but hey it worked so we will see. Now the display on my OLED running just the simple counter test shows “Write to Card Sucess” at all times, unless I pull the card. Then it will show “NO CARD” but only for a second, then it will disappear, then reappear for a second, disappear, and so on, and so on. I am guessing that is a timing thing with the debug delay, but that is a whole other thing to work on later if I want “NO CARD” to display at all times when there is no card in the reader.

Ok, thanks to everyone here and especially @ScruffR for pointing me in the right direction and spelling it out in Crayon for a noob to read.
This is the part of the code that I modified… or I guess just inserted my little bit.

In the SdCardLogHandlerRK.cpp file around lines 66-67 as was pointed out above right under the DEBUG_HIGH. I added the part in bold.

		DEBUG_HIGH(("sd.begin failed (no card or no reader)"));

display.print(“NO CARD”);

Now for success message at around lines 161-162 right under “if (curLogFile.write(buf, bufOffset) > 0) {”
I added the part in bold.

		if (curLogFile.write(buf, bufOffset) > 0) {

display.println(“Write to Card Sucess”);

Last is the fail message around lines 180-181
Right under this “DEBUG_NORMAL((“write to sd card failed”));”
My part in bold

			DEBUG_NORMAL(("write to sd card failed"));

display.println(“write to Card Failed”);

This was all done on a test unit and separate test app. Now to see if I can pull it all back together on my main app. Thank you all again and let me know you have any questions or suggestions. I am happy to help in the little capacity that I can… if I can. :slight_smile:

I had to snap the pic of the no card message quick as it disappears faster than a glass of wine on this desk.

4 Likes