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?