Customer:
I have an Inkplate 6 and want to write something to a text file on an SD card and then read that same file. I combined your write and read examples into a single sketch.
Writing to the text file works, but reading from it does not.
Specifically this line fails:
file.open("/text.txt", O_RDONLY)
Here is the code:
#if !defined(ARDUINO_ESP32_DEV) && !defined(ARDUINO_INKPLATE6V2)
#error "Wrong board selection for this example, please select e-radionica Inkplate6 or Soldered Inkplate6 in the boards menu."
#endif
#include "Inkplate.h" //Include Inkplate library to the sketch
#include "SdFat.h" //Include library for SD card
Inkplate display(INKPLATE_1BIT); // Create an object on Inkplate library and also set library into 1 Bit mode (BW)
SdFile file; // Create SdFile object used for accessing files on SD card
char *fileName = "test.txt"; // The name of a file with the extension.
boolean fileRead = false;
int numOfAttempts = 0;
// The text you want to write in the file
char *dataToWrite = "Hello! This is the file writing example for Inkplate 6.\n"; // "\n" represents a new line
void setup()
{
display.begin(); // Init Inkplate library (you should call this function ONLY ONCE)
display.clearDisplay(); // Clear frame buffer of display
display.display(); // Put clear image on display
display.setTextSize(3);
// Init SD card. Display if SD card is init propery or not.
if (display.sdCardInit())
{
display.println("SD Card ok! Reading data…");
display.partialUpdate();
// Try to create text.txt file.
if (!file.open(fileName, FILE_WRITE))
{ // If it fails to create, send an error message to display, otherwise write to file.
display.println("Error while creating the file!");
display.partialUpdate();
display.sdCardSleep();
}
}
}
void loop() {
if (!fileRead) {
if (!display.sdCardInit()) {
display.println("Unable to initialize SD Card!");
}
if (!file.open("/text.txt", O_RDONLY))
{ // If it fails to open, send error message to display and put sd card in sleep mode, otherwise read the file.
display.println("File open error");
display.display();
numOfAttempts++;
if (numOfAttempts >= 5) {
fileRead = true;
}
}
else
{
fileRead = true;
display.clearDisplay(); // Clear everything that is stored in frame buffer of epaper
display.setCursor(0, 0); // Set print position at the begining of the screen
char text[200]; // Array where data from SD card is stored (max 200 chars here)
int len = file.fileSize(); // Read how big is file that we are opening
if (len > 200)
len = 200; // If it's more than 200 bytes (200 chars), limit to max 200 bytes
file.read(text, len); // Read data from file and save it in text array
text[len] = 0; // Put null terminating char at the and of data
display.print(text); // Print data/text
display.sdCardSleep(); // Put sd card in sleep mode
display.display(); // Do a full refresh of display
}
sleep(1);
}
}
Original message (Croatian)
Imam Inkplate 6 i zelio bih snimiti nesto u tekstualnu datoteku na SD karticu odnosno nakon toga procitati tu istu datoteku. Vase primjere za pisanje i citanje sam stavio u jedan te isti Sketch.
Zapisivanje u tekstualnu datoteku prolazi medjutim citanje iste ne prolazi.
Konkretno ovaj dio:
file.open(“/text.txt”, O_RDONLY)
Ako ce pomoci evo i koda:
#if !defined(ARDUINO_ESP32_DEV) && !defined(ARDUINO_INKPLATE6V2)
#error “Wrong board selection for this example, please select e-radionica Inkplate6 or Soldered Inkplate6 in the boards menu.”
#endif
#include “Inkplate.h” //Include Inkplate library to the sketch
#include “SdFat.h” //Include library for SD card
Inkplate display(INKPLATE_1BIT); // Create an object on Inkplate library and also set library into 1 Bit mode (BW)
SdFile file; // Create SdFile object used for accessing files on SD card
char *fileName = “test.txt”; // The name of a file with the extension.
boolean fileRead = false;
int numOfAttempts = 0;
// The text you want to write in the file
char *dataToWrite = “Hello! This is the file writing example for Inkplate 6.\n”; // “\n” represents a new line
void setup()
{
display.begin(); // Init Inkplate library (you should call this function ONLY ONCE)
display.clearDisplay(); // Clear frame buffer of display
display.display(); // Put clear image on display
display.setTextSize(3);
// Init SD card. Display if SD card is init propery or not.
if (display.sdCardInit())
{
display.println(“SD Card ok! Reading data…”);
display.partialUpdate();
// Try to create text.txt file.
if (!file.open(fileName, FILE_WRITE))
{ // If it fails to create, send an error message to display, otherwise write to file.
display.println(“Error while creating the file!”);
display.partialUpdate();
display.sdCardSleep();
}
}
}
void loop() {
if (!fileRead) {
if (!display.sdCardInit()) {
display.println(“Unable to initialize SD Card!”);
}
if (!file.open(“/text.txt”, O_RDONLY))
{ // If it fails to open, send error message to display and put sd card in sleep mode, otherwise read the file.
display.println(“File open error”);
display.display();
numOfAttempts++;
if (numOfAttempts >= 5) {
fileRead = true;
}
}
else
{
fileRead = true;
display.clearDisplay(); // Clear everything that is stored in frame buffer of epaper
display.setCursor(0, 0); // Set print position at the begining of the screen
char ; // Array where data from SD card is stored (max 200 chars here)
int len = file.fileSize(); // Read how big is file that we are opening
if (len > 200)
len = 200; // If it’s more than 200 bytes (200 chars), limit to max 200 bytes
file.read(text, len); // Read data from file and save it in text array
= 0; // Put null terminating char at the and of data
display.print(text); // Print data/text
display.sdCardSleep(); // Put sd card in sleep mode
display.display(); // Do a full refresh of display
}
sleep(1);
}
}
Originally submitted via email support on 24 November 2024. Migrated to the forum for community visibility.