Go Down

Topic: Reading Line by line form SD card (Read 460 times) previous topic - next topic

V1shal

Hi Guys,

I am creating a weather station where I am logging all the sensor Data in a text file in SD card with time stamp. Later on I want to read the written data in the SD card and send it via sms using GSM shield.

However, for now I just want to write the Data in a line with the time stamp, and then read the last written line and print on serial monitor.

The data is something like this:

2018/3/12 15:31:26 25.81,41.72,1007.63,12057,25.0,AQ = H,260,257,0.02,0.00,0.00,0.04,0.04,0.63,S


The main purpose why I want to read from the SD card is because in case the SMS is not send I want to go back to the last line that was successfully sent by SMS and then read and resend all data updates in the SD card since last sent.

Else if that is too difficult, then I just want to read the last updated line in the text file from the SD card and send it via SMS ( for now print it in serial monitor).

Here is my code so far

Code: [Select]

#include <SPI.h>
#include <SD.h>
#include "RTClib.h"

RTC_PCF8523 rtc;

const int chipSelect = 9;
int recNum=0;
int recordNumberOfInterest=1;

File myData;

void setup() {
 
  Serial.begin(9600); // Open serial communications and wait for port to open
 
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");
 
  // see if the RTC is present and can be initialized:
  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }

  if (! rtc.initialized()) {
    Serial.println("RTC is NOT running!");
  }
}

void loop() {
  //writing data to SD card in one line
 
  myData= SD.open ("Data.txt", FILE_WRITE); // open file to write
  DateTime now = rtc.now();
   
  myData.print(now.year(), DEC);
  myData.print('/');
  myData.print(now.month(), DEC);
  myData.print('/');
  myData.print(now.day(), DEC);
  myData.print(" ");
  myData.print(now.hour(), DEC);
  myData.print(':');
  myData.print(now.minute(), DEC);
  //myData.print(':');
  //myData.print(now.second(), DEC);
  // This is where I write sensor Data
 /* myData.print(" ");
  myData.print(temp);                            myData.print(",");
  myData.print(hum);                             myData.print(",");
  myData.print(pres);                            myData.print(",");
  myData.print(gas);                             myData.print(",");
  myData.print(air_quality_score,1);             myData.print(",");
  myData.print(CalculateIAQ(air_quality_score)); myData.print(",");
  myData.print(uv.readVisible());                myData.print(",");
  myData.print(uv.readIR());                     myData.print(",");
  myData.print(UVindex);                         myData.print(",");
  myData.print(Rainfall);                        myData.print(",");
  myData.print(Total_Rainfall_24hr);             myData.print(",");
  myData.print(Wind_Speed);                      myData.print(",");
  myData.print(Wind_Speed_Max);                  myData.print(",");
  myData.print(Wind_Gust);                       myData.print(",");
  myData.print(Wind_direction);       
  myData.println("/n");
 
  */   
  myData.close();     
  Serial.println(" Done writing to SD ");
  recNum++;
 
  delay(10);// give some time to close file

 
 // Open SD Card
 myData= SD.open ("Data.txt", FILE_READ);
 //Read the last written line
 //Print on serial monitor
 //close sd card
 myData.close();
 //Delay 5 sec untill next write to SD
 delay(5000); // Delay 5sec before next measurements
}


I have commented the data writing part of the code as it calls some functions which I have not included in this code. However, if it is required I will be happy to post it up.

I tried using this code however this only prints the first line over and over again

Code: [Select]
if(recNum == recordNumberOfInterest)
    { 
      myData= SD.open ("Data.txt", FILE_READ);
      String list = myData.readStringUntil('\n');
      Serial.print(list);
      recordNumberOfInterest++;
    }


Can someone please help me or guide me on the right path.

Your Help is very much appreciated. I am new in using arduino.

Thanks in Advance.

Regards
Vishal







 

robtillaart

#1
Mar 12, 2018, 11:28 am Last Edit: Mar 12, 2018, 11:29 am by robtillaart
First advice for a project this size  - split responsibilities --> use functions

Code: ( not tested) [Select]
#include <SPI.h>
#include <SD.h>
#include "RTClib.h"

RTC_PCF8523 rtc;

const int chipSelect = 9;
int recNum = 0;
int recordNumberOfInterest = 0;

uint32_t positionLPR = 0;  // LastProcessedRecord

File myData;


void setup()
{
  Serial.begin(9600); // Open serial communications and wait for port to open

  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println(F("ERROR: Card failed, or not present"));
    // don't do anything more:
    while (1);
  }
  Serial.println("card initialized.");

  // see if the RTC is present and can be initialized:
  if (! rtc.begin()) {
    Serial.println(F("ERROR: Couldn't find RTC"));
    while (1);
  }

  if (! rtc.initialized()) {
    Serial.println(F("ERROR: RTC is NOT running!"));
    while (1);
  }
}



void loop()
{
  writeLineToSD();

  Serial.println(" Done writing to SD ");
  recNum++;


  positionLPR = procesLineFromSD(positionLPR);


  //Delay 5 sec until next write to SD
  delay(5000); // Delay 5sec before next measurements
}

uit32_t readLineFromSD(uint32_t pos)
{
  FILE data = SD.open ("Data.txt", FILE_READ);

  data.seek(pos);
  while (data.available())
  {
    int c = data.read();
    pos++;
    Serial.print(c);
    if (c == '\n') break;
  }
  myData.close();
  return pos;
}


void writeLineToSD()
{
  //writing data to SD card in one line

  myData = SD.open ("Data.txt", FILE_WRITE); // open file to write
  DateTime now = rtc.now();

  myData.print(now.year(), DEC);
  myData.print('/');
  myData.print(now.month(), DEC);
  myData.print('/');
  myData.print(now.day(), DEC);
  myData.print(" ");
  myData.print(now.hour(), DEC);
  myData.print(':');
  myData.print(now.minute(), DEC);

  //myData.print(':');
  //myData.print(now.second(), DEC);
  // This is where I write sensor Data
  /* myData.print(" ");
  myData.print(temp);                            myData.print(",");
  myData.print(hum);                             myData.print(",");
  myData.print(pres);                            myData.print(",");
  myData.print(gas);                             myData.print(",");
  myData.print(air_quality_score,1);             myData.print(",");
  myData.print(CalculateIAQ(air_quality_score)); myData.print(",");
  myData.print(uv.readVisible());                myData.print(",");
  myData.print(uv.readIR());                     myData.print(",");
  myData.print(UVindex);                         myData.print(",");
  myData.print(Rainfall);                        myData.print(",");
  myData.print(Total_Rainfall_24hr);             myData.print(",");
  myData.print(Wind_Speed);                      myData.print(",");
  myData.print(Wind_Speed_Max);                  myData.print(",");
  myData.print(Wind_Gust);                       myData.print(",");
  myData.print(Wind_direction);
  */

  myData.println();
  myData.close();

  delay(10); // give some time to close file
}



Have a look and give it a try
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

V1shal

Hi Robtillaart,

Thank you for you help, really appreciate it.

A few questions:

Is this suppose to be a function? It seems to be not in the code you gave

Code: ( not tested) [Select]


  positionLPR = procesLineFromSD(positionLPR);



secondly int c=data.read();

Is suppose to read the unline until \n which is new line ? am i correct?

If you have time can you please tell me what these functions do, this is the first time i am using SD card to read and write files

positionLPR = readLineFromSD(positionLPR);

data.seek(pos);

int c = data.read();

Thank you again for your help.

Regards
Vishal


robtillaart

Is this suppose to be a function? It seems to be not in the code you gave

Code: ( not tested) [Select]


  positionLPR = procesLineFromSD(positionLPR);


should be  - readLineFromSD

as my post says the code was not tested, I just copied and modified your code and added stuf.
did not check the names of the functions.

secondly int c=data.read();

Is suppose to read the unline until \n which is new line ? am i correct?
no it reads one char at the time,
* the while loop takes care to repeat it
* the if jumps out of the while loop when \n is encountered

Quote
If you have time can you please tell me what these functions do, this is the first time i am using SD card to read and write files

positionLPR = readLineFromSD(positionLPR);

data.seek(pos);

int c = data.read();

Thank you again for your help.

Regards
Vishal
These functions do (sort of)  what you asked for in your first post.
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Go Up