Printing SD files in Nextion Display

Hello!
I am currently having problems with a Nextion display and SD module. I am trying to print on a text box the present files in the SD card. However, i have been trying and I can't manage to make it work. Here is the code:


void printDirectory(File dir, int numTabs) {
  root = SD.open("/");
  Serial2.print(F("sdFeedback2.txt=\""));
  while (true) {

    File entry =  dir.openNextFile();
    if (! entry) {
      if (numTabs == 0) {
        Serial2.print("**Done**");
        Serial2.print(F("\""));
        Serial2.print("\xFF\xFF\xFF");
        return;
      }
    }

    Serial2.print(entry.name());

    if (entry.isDirectory()) {
      Serial2.print("/");
      printDirectory(entry, numTabs + 1);
    } else {
      // files have sizes, directories do not
      Serial2.print(entry.size());
      Serial2.print(F("\r\n"));
    }
    entry.close();
  }
  root.close();
}

to call the function I place it in the code as : printDirectory(root, 0); and all I get on the textbox is "Done". Any idea to why this is happening?

Do you mean an SD card in the Nextion or an SD card module attached to your Arduino?

@PerryBebbington it's an SD module attached to an arduino mega and I am using the SD.h library.

There are 2 parts to your question: Dealing with SD cards, about which I know nothing, and printing to a Nextion, which I can help with.

Split your problem in 2 parts, start with getting text on your Nextion, when that works deal with the SD card and what you want to print, and slot that into your now working methods for printing on a Nextion. See Using Nextion displays with Arduino for help printing to a Nextion text box.

Do you really want to get stuck in a while() loop?

You send the start of printing to a text box with:

Serial2.print(F("sdFeedback2.txt=\""));

Then the rest is conditional on other things, which might or might not be true. What do you think happens if after the sdFeeback2.txt etc bit is sent the next part is not sent? You are now in your while() loop with no opportunity to ever send sdFeeback2.txt etc again.

Look at my examples, the complete message to the Nextion is all in one block, not split into bits of code that might or might not do things.

As to whether you ever get to the return in the while() loop I have no idea because getting there is too convoluted. To get to the return numTabs has to equal 0, but I cannot see anything in your while() loop that would change it. I'm not sure anything every changes entry either.

I am actually using the code posted on your guide! Which I personally think it's just great for managing several pages in a Nextion display.

I have split the problem in two as you suggested, and finally got a working code that I think solves the problems you stated.

#include <SD.h>
#include <SPI.h>
File root;

void setup() {
  Serial2.begin(115200);
  SD.begin(53);
  root = SD.open("/");
  printDirectory(root, 0);
}
void printDirectory(File dir, int counter) {

  String entryName;
  
  while (true) {

    File entry =  dir.openNextFile();

    if (!entry || dir.size() <= 0) {

      break;
    }

    if (!entry.isDirectory() && counter == 0) {

      entryName += (String) entry.name() + "           " + (String)entry.size() + (String) "\r\n";
    }

    else
    {
      printDirectory(entry, counter + 1);
    }
    entry.close();
  }
  Serial2.print(F("sdFeedback2.txt=\""));
  Serial2.print(entryName);
  Serial2.print("\"");
  Serial2.print("\xff\xff\xff");
}

(the caracteristics part is a anther code part that is not here)

1 Like

Ok, good :slight_smile:

1 Like

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