Displaying text from file on OLED screen - not working as expected

Hi there, I'm trying to display text read from a file on an OLED screen using ESP-01S. I'm using the ESP-01S Sketch Data Uploader to upload "test.txt" to the ESP01S. I'm than reading that data and storing it in a char array txt, but when I try to display it on the OLED screen, I'm only getting a string of "aaaaaaaaaaa@" instead of the expected "dadadadadadada".

test.txt:
dadadadadadadadadadada

Here is my code:

#include "FS.h"
#include <Arduino.h>
#include <U8g2lib.h>

#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif

U8G2_SSD1306_128X32_UNIVISION_F_SW_I2C u8g2(U8G2_R0, /* clock=SCL*/ 0, /* data=SDA*/ 2);  

void setup() {
  u8g2.begin();
  u8g2.clearBuffer();
  u8g2.setFont(u8g2_font_ncenB08_tr);

  if (!SPIFFS.begin()) {
    u8g2.drawStr(0,10,"An Error has occurred while mounting SPIFFS");
    return;
  }

  File file = SPIFFS.open("/test.txt", "r");
  if (!file) {
    u8g2.drawStr(0,10,"Failed to open file for reading");
    return;
  }

  char txt[100];
  int i = 0;

  while(file.available()){
    txt[i] = file.read();
    i++;
  }
  u8g2.drawStr(0,10, txt);
  u8g2.sendBuffer(); 
  file.close();
}

Also is there better way to read data from file? Something where you don't use while instead read whole file at once?

What is "ESP-01S" for an exact type of microcontroller??
Do you mean this one?
image

How did you connect the OLED-display to your microcontroller?

Do you have the serial monitor available at the same time as you have the OLED display connected?
If you can't use the serial monitor at the same time as having the OLED connectd to your microcontroller do yourself a big favor and buy a ESP32 node-MCU

Beeing able to use the serial monitor for debugging will make the development-process 10 times easier

best regards Stefan

1 Like

You don't terminate the string! You should also check if the limits of your buffer aren't exceeded.

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