Read file from SD card to Virtual pin?

Hi friends. I want to reading a file from SD card to virtual pins (V5 in the blow sketch). for test I wrote Hello world in a text file that’s named add.txt but when reading it from SD card, in the virtual pins shows numbers instead of letters. any idea to fix ?

here the sketch:

#define TINY_GSM_MODEM_SIM800
#define CS_PIN  15

#include <SoftwareSerial.h>
#include <TinyGsmClient.h>
#include <BlynkSimpleSIM800.h>
#include <TimeLib.h>
#include <WidgetRTC.h>
#include <SPI.h>
#include <SD.h>

char auth[] = "iPeGEj1JIvNqY32488987-hLMo8Ik3pj";
char apn[]  = "mcinet";
char user[] = "";
char pass[] = "";
char server[] = "10.5.51.5";

SoftwareSerial SerialAT(5, 4);

TinyGsm modem(SerialAT);

BlynkTimer timer;
WidgetRTC rtc;

File myFile;

String currentTime;
String currentDate;

void setup()
{
  SerialAT.begin(9600);
  delay(3000);
  modem.restart();
  Blynk.begin(auth, modem, apn, user, pass, server, 8080);
  rtc.begin();
  if (!SD.begin(CS_PIN)) {
  }
  timer.setInterval(10000L, clockDisplay);
}

void clockDisplay()
{
  currentTime = String(hour()) + ":" + minute() + ":" + second();
  currentDate = String(day()) + "-" + month() + "-" + year();
  Blynk.virtualWrite(V1, currentTime);
  Blynk.virtualWrite(V2, currentDate);
  Blynk.virtualWrite(V3, "Fault Detected");
  Blynk.virtualWrite(V4, 100);
  myFile = SD.open("add.txt", FILE_READ);
  if (myFile) {
    while (myFile.available())
    {
      Blynk.virtualWrite(V5, myFile.read());
      myFile.close();
    }
    myFile = SD.open("faults.txt", FILE_WRITE);
    if (myFile) {
      myFile.println(currentTime);
      myFile.println(currentDate);
      myFile.println("---------------");
      myFile.close();
      ESP.deepSleep(0);
    }
  }
}

void loop()
{
  Blynk.run();
  timer.run();
}

We’re the numbers 72 101 108 108 111 32 119 111 114 108 100 ?

Pete.

1 Like

showing 55

I’d try putting some serial print statements in there and see what’s coming out of your file.

Pete.

1 Like
[27364] Connected to GPRS
[27426] Connecting to 37.245.193.215:8080
[29112] Ready (ping: 873ms).
[30136] Time sync: OK
55

hmmm. I’m edit the sketch and it worked on serial print but in app showing number 1

  myFile = SD.open("add.txt");
  if (myFile) {
    while (myFile.available()) {
      Serial.write(myFile.read());
      Blynk.virtualWrite(V5, myFile);
    }
    myFile.close();


[10080] Modem init...
[11331] Connecting to network...
[27794] Network: 43235
[27794] Connecting to mtnirancell ...
[35842] Connected to GPRS
[35903] Connecting to 37.245.193.215:8080
[38326] Ready (ping: 876ms).
[39359] Time sync: OK
hello world

That’s what I’d expect.

Blynk.virtualWrite(V5, myFile.read()); would be the matching command to Serial.write(myFile.read());

Pete.

1 Like

I do that like below but nothing

  myFile = SD.open("add.txt");
  if (myFile) {
    while (myFile.available()) {
      Serial.write(myFile.read());
      Blynk.virtualWrite(V5, myFile.read());
    }
    myFile.close();

Maybe comment-out the Serial.write(myFile.read()); as perhaps this reading he whole of the file.
If not then maybe read the file into a variable and serial.print/virtualWrite that?

Pete.

1 Like

like below ? but not working

  myFile = SD.open("add.txt");
  if (myFile) {
    while (myFile.available()) {
      Serial.write(myFile.read());
      char ADD = myFile.read();
      Blynk.virtualWrite(V5, ADD);
    }
    myFile.close();

You have to read the whole file into a buffer, then send the buffer to Serial or Blynk, as follows:

  // Change the buffer size as necessary
  #define BUF_SIZE    256
  char temp_buffer[BUF_SIZE];
  
  myFile = SD.open("add.txt", FILE_READ);

  if (myFile) 
  {
    uint16_t len = (uint16_t) myFile.size();
  
    if (len < BUF_SIZE - 1 )
    {
      // To make sure NULL terminated
      memset(temp_buffer, 0, sizeof(temp_buffer));
      myFile.read((uint8_t*) temp_buffer, len);
    }
    
    Serial.println(temp_buffer);
    Blynk.virtualWrite(V5, temp_buffer);
  
    myFile.close();
  }

1 Like

thanks for reply. but not showing text in blynk V5. just showing number 104

here the code:

#define TINY_GSM_MODEM_SIM800
#define CS_PIN  15
#define BUF_SIZE    256

#include <SoftwareSerial.h>
#include <TinyGsmClient.h>
#include <BlynkSimpleSIM800.h>
#include <TimeLib.h>
#include <WidgetRTC.h>
#include <SPI.h>
#include <SD.h>

char auth[] = "iPeGEj1JIvNqY32FRPZ4x-hLMo8Ik3pj";
char apn[]  = "mtnirancell";
char user[] = "";
char pass[] = "";
char server[] = "37.245.193.215";

char temp_buffer[BUF_SIZE];

SoftwareSerial SerialAT(5, 4);

TinyGsm modem(SerialAT);

BlynkTimer timer;
WidgetRTC rtc;

File myFile;

String currentTime;
String currentDate;
String string;

void setup()
{
  SerialAT.begin(9600);
  delay(3000);
  modem.restart();
  Blynk.begin(auth, modem, apn, user, pass, server, 8080);
  rtc.begin();
  if (!SD.begin(CS_PIN)) {
  }
  timer.setInterval(10000L, clockDisplay);
}

void clockDisplay()
{
  currentTime = String(hour()) + ":" + minute() + ":" + second();
  currentDate = String(day()) + "-" + month() + "-" + year();
  Blynk.virtualWrite(V1, currentTime);
  Blynk.virtualWrite(V2, currentDate);
  Blynk.virtualWrite(V3, "Fault Detected");
  Blynk.virtualWrite(V4, 100);
  myFile = SD.open("add.txt", FILE_READ);

  if (myFile)
  {
    uint16_t len = (uint16_t) myFile.size();

    if (len < BUF_SIZE - 1 )
    {
      // To make sure NULL terminated
      memset(temp_buffer, 0, sizeof(temp_buffer));
      myFile.read((uint8_t*) temp_buffer, len);
    }

    Serial.println(temp_buffer);
    Blynk.virtualWrite(V5, temp_buffer);

    myFile.close();
  }
}


void loop()
{
  Blynk.run();
  timer.run();
}

There is something strange, could you try again these 2 code snippets, one by one, then post the terminal output to see what’s happening

  myFile = SD.open("add.txt", FILE_READ);

  if (myFile) 
  {
    uint16_t len = (uint16_t) myFile.size();
  
    if (len < BUF_SIZE - 1 )
    {
      memset(temp_buffer, 0, sizeof(temp_buffer));
      myFile.read((uint8_t*) temp_buffer, len);
    }
    
    Serial.println("Temp Buffer = " + String(temp_buffer));
    Blynk.virtualWrite(V5, String(temp_buffer));
  
    myFile.close();
  }

and

  myFile = SD.open("add.txt", FILE_READ);

  if (myFile) 
  {
    uint16_t pos = 0;
    
    memset(temp_buffer, 0, sizeof(temp_buffer));
    
    while (myFile.available()) 
    {    
      Serial.write(myFile.read());

      if (pos < BUF_SIZE)
        temp_buffer[pos++] = myFile.read();
    }
 
    Serial.println("Temp Buffer = " + String(temp_buffer));
    Blynk.virtualWrite(V5, String(temp_buffer));
    
    myFile.close();
  }  

1 Like

the serial print and no hello world

[10616] Modem init...
[11861] Connecting to network...
[15685] Network: 43235
[15685] Connecting to mtnirancell ...
[23210] Connected to GPRS
[23271] Connecting to 37.245.193.215:8080
[26638] Ready (ping: 2378ms).
[30128] Time sync: OK

hmmm. the problem solved with below code thanks @khoih
now showing in V5

  myFile = SD.open("add.txt");
  if (myFile) {
    while (myFile.available()) {
      uint16_t len = (uint16_t) myFile.size();
      memset(temp_buffer, 0, sizeof(temp_buffer));
      myFile.read((uint8_t*) temp_buffer, len);
      Serial.write(myFile.read());
      Serial.println("Temp Buffer = " + String(temp_buffer));
      Blynk.virtualWrite(V5, String(temp_buffer));
    }
    myFile.close();


[13953] Modem init...
[15201] Connecting to network...
[20155] Network: 43235
[20155] Connecting to mtnirancell ...
[27270] Connected to GPRS
[27332] Connecting to 37.245.193.215:8080
[30855] Ready (ping: 1393ms).
[31864] Time sync: OK
⸮Temp Buffer = hello world
2 Likes

Post too late. You’re so fast :wink:

Can you check these following items

  1. Add Serial.begin(115200); at the beginning of setup(). Be sure the file still has hello world and not erased yet by running your original code which showed the hello world
  2. If OK, add the seek() before read()
myFile = SD.open("add.txt", FILE_READ);

  if (myFile) 
  {
    uint16_t len = (uint16_t) myFile.size();
  
    if (len < BUF_SIZE - 1 )
    {
      memset(temp_buffer, 0, sizeof(temp_buffer));
      myFile.seek(0);
      myFile.read((uint8_t*) temp_buffer, len);
    }
    
    Serial.println("File size = " + String( myFile.size()));
    Serial.println("Temp Buffer = " + String(temp_buffer));
    Blynk.virtualWrite(V5, String(temp_buffer));
  
    myFile.close();
  }

and

 myFile = SD.open("add.txt", FILE_READ);

  if (myFile) 
  {
    uint16_t pos = 0;
    
    memset(temp_buffer, 0, sizeof(temp_buffer));

    myFile.seek(0);
    
    while (myFile.available()) 
    {    
      Serial.write(myFile.read());
    }
 
    myFile.seek(0);
    
    while (myFile.available()) 
    {    
      if (pos < BUF_SIZE)
        temp_buffer[pos++] = myFile.read();
    }

    Serial.println("File size = " + String( myFile.size()));
    Serial.println("Temp Buffer = " + String(temp_buffer));
    Blynk.virtualWrite(V5, String(temp_buffer));
    
    myFile.close();
  }  
  1. If still has issue, post the terminal output again. Inform which board (ESP32??) and SD card you are using.so that we can duplicate the problem.
1 Like

thanks. you saved my day :heart_eyes: I’m using ESP8266 Nodemcu with SD module and SIM800L.

I had de same problem here. I’ll try this code. Thx