read the fixed length line from sd card

Hi all,

I am having SD card with text file containing:

00000100 15:12:2017 17:18:08
00000200 15:12:2017 17:19:08
00000300 15:12:2017 17:20:08
00000400 15:12:2017 17:21:08
00000500 15:12:2017 17:22:08
00000600 15:12:2017 17:24:08
00000700 15:12:2017 17:27:08
00000800 15:12:2017 17:30:08
00000900 15:12:2017 17:34:08
00001000 15:12:2017 17:37:08
00001100 15:12:2017 17:39:08
00001200 15:12:2017 17:46:08

each line have 24 characters including spaces;i want to send one line at a time to tcp port using gprs module…when sent successfully i am getting OK…i compared with "ok"string…then want to write "sent"at the start of that sent line…and in next iteration it will check first characters if found “sent” then it will skip that line until got line to be processed;i searched on google i found "position(())"and “seek(())” function needs to use and fixed length strings are easy to handle than variable

I am successfully able to send string to the TCP port using GPRS :
This is the code:

#include <SoftwareSerial.h>
/* Create object named SIM900 of the class SoftwareSerial */
SoftwareSerial SIM900(2, 3);
void setup() {
   SIM900.begin(9600);  /* Define baud rate for software serial communication */
   Serial.begin(9600); /* Define baud rate for serial communication */
}

void loop() {
   Serial.println("TCP Receive :");
   Serial.print("AT\\r\\n");
   SIM900.println("AT"); /* Check Communication */
   delay(5000);
   ShowSerialData(); /* Print response on the serial monitor */
   delay(5000);
   Serial.print("AT+CIPMODE=0\\r\\n");
   SIM900.println("AT+CIPMODE=0"); /* Non-Transparent (normal) mode for TCP/IP application */
   delay(5000);
   ShowSerialData();
   delay(5000);
   Serial.print("AT+CIPMUX=0\\r\\n");
   SIM900.println("AT+CIPMUX=0");  /* Single TCP/IP connection mode */
   delay(5000);
   ShowSerialData();
   delay(5000);
   Serial.print("AT+CGATT=1\\r\\n");
   SIM900.println("AT+CGATT=1"); /* Attach to GPRS Service */
   delay(5000);
   ShowSerialData();
   delay(5000);
   Serial.print("AT+CREG?\\r\\n");
   SIM900.println("AT+CREG?"); /* Network registration status */
   delay(5000);
   ShowSerialData();
   delay(5000);
   Serial.print("AT+CGATT?\\r\\n");
   SIM900.println("AT+CGATT?");  /* Attached to or detached from GPRS service */
   delay(5000);
   ShowSerialData();
   delay(5000);
   Serial.print("AT+CGDCONT=1,\"IP\",\"ISAFE\"\r\\n");
   SIM900.println("AT+CGDCONT=1,\"IP\",\"ISAFE\""); /* Start task and set APN */
   delay(5000);
   ShowSerialData();
   delay(5000);
   Serial.print("AT+CGACT=1,1\\r\\n");
   SIM900.println("AT+CGACT=1,1"); /* Bring up wireless connection with GPRS */
   delay(5000);
   ShowSerialData();
   delay(5000);
   Serial.print("AT+CIFSR\\r\\n");
   SIM900.println("AT+CIFSR"); /* Get local IP address */
   delay(5000);
   ShowSerialData();
   delay(5000);
   Serial.print("AT+CIPSTART=\"TCP\",\"122.169.113.165\",1883");
   SIM900.println("AT+CIPSTART=\"TCP\",\"122.169.113.165\",1883");  /* Start up TCP connection */
   delay(5000);
   ShowSerialData();
   delay(5000);
   Serial.print("AT+CIPSEND\\r\\n");
   SIM900.println("AT+CIPSEND"); /* Send data through TCP connection */
   delay(2000);
   ShowSerialData();
   delay(2000);
   sendtemp();
   delay(5000);
   ShowSerialData();
   delay(5000);
   Serial.print("AT+CIPSHUT\\r\\n");
   SIM900.println("AT+CIPSHUT"); /* Deactivate GPRS PDP content */
   delay(5000);
   ShowSerialData();
   delay(5000);
}

void ShowSerialData()
{
   while (SIM900.available() != 0)   /* If data is available on serial port */
     Serial.write(char (SIM900.read())); /* Print character received on to the serial monitor */
}

void sendtemp() {
  String str = "Hello world!";
  delay(500);
  SIM900.println(str);
  delay(300);
  ShowSerialData();
  SIM900.write(0x1A);
  delay(300);
  ShowSerialData();
}

But having trouble with SD card code…help needed in that section

Start by writing a simple sketch that opens the file, then reads the file line by line and prints it on the Serial output. Then you can see what it really does. Do a quick search on Google or this site and you'll find lots of examples on how to open and read files.

Yes i can read first line of the text file and send it to tcp port but after successfully sent i cant skip that line and tag that line is as"sent" and read next line?

code:

#include <SD.h>
#include <SPI.h>
#include <SoftwareSerial.h>
SoftwareSerial SIM900(2, 3);
File printFile;
String buffer;
boolean SDfound;
char val;
String readstr;
static String readstr1;
static int count = 0;
char inData[20];
byte index = 0;
void setup() {
  Serial.begin(9600);
  SIM900.begin(9600);
  if (SDfound == 0) {
    if (!SD.begin(10)) {
      Serial.print("The SD card cannot be found");
      while (1);
    }
  }



}

void loop() {
  SDfound = 1;
  printFile = SD.open("TEST.txt");

  if (!printFile) {
    Serial.print("The text file cannot be opened");
    while (1);
  }
  while (printFile.available()) {
    buffer = printFile.readStringUntil('\n');

    // buffer =  buffer+(char)printFile.read();
    Serial.println(buffer); //Printing for debugging purpose

    Serial.println("TCP Receive :");
    Serial.print("AT\\r\\n");
    SIM900.println("AT"); /* Check Communication */
    delay(5000);
    ShowSerialData(); /* Print response on the serial monitor */


    Serial.print("AT+CIPMUX=0\\r\\n");
    SIM900.println("AT+CIPMUX=0");  /* Single TCP/IP connection mode */
    delay(5000);
    ShowSerialData();

    Serial.print("AT+CGATT=1\\r\\n");
    SIM900.println("AT+CGATT=1"); /* Attach to GPRS Service */
    delay(5000);
    ShowSerialData();

    Serial.print("AT+CREG?\\r\\n");
    SIM900.println("AT+CREG?"); /* Network registration status */
    delay(5000);
    ShowSerialData();

    Serial.print("AT+CGATT?\\r\\n");
    SIM900.println("AT+CGATT?");  /* Attached to or detached from GPRS service */
    delay(5000);
    ShowSerialData();

    Serial.print("AT+CGDCONT=1,\"IP\",\"ISAFE\"\r\\n");
    SIM900.println("AT+CGDCONT=1,\"IP\",\"ISAFE\""); /* Start task and set APN */
    delay(5000);
    ShowSerialData();

    Serial.print("AT+CGACT=1,1\\r\\n");
    SIM900.println("AT+CGACT=1,1"); /* Bring up wireless connection with GPRS */
    delay(5000);
    ShowSerialData();

    Serial.print("AT+CIFSR\\r\\n");
    SIM900.println("AT+CIFSR"); /* Get local IP address */
    delay(5000);
    ShowSerialData();

    Serial.print("AT+CIPSTART=\"TCP\",\"122.169.113.165\",1883");
    SIM900.println("AT+CIPSTART=\"TCP\",\"122.169.113.165\",1883");  /* Start up TCP connection */
    delay(5000);
    ShowSerialData();
    delay(5000);
    Serial.print("AT+CIPSEND\\r\\n");
    SIM900.println("AT+CIPSEND"); /* Send data through TCP connection */
    delay(2000);
    ShowSerialData();


    delay(2000);
    sendtemp();
    delay(5000);
    ShowSerialData();





    Serial.print("AT+CIPCLOSE\\r\\n");
    SIM900.println("AT+CIPCLOSE"); /* Deactivate GPRS PDP content */
    delay(5000);
    ShowSerialData();
    Serial.print("AT+CIPSHUT\\r\\n");
    SIM900.println("AT+CIPSHUT"); /* Deactivate GPRS PDP content */
    delay(5000);
    ShowSerialData();


    break;
  }




  while (printFile.available()) {
    Serial.write(printFile.read());
  }
  printFile.close();
}


void ShowSerialData()
{
  while (SIM900.available() != 0) {
    val = char (SIM900.read());
    readstr += val;
    if (val == '\n') {
      Serial.println(readstr);
      readstr1 = readstr;
      if (readstr1 == "AT+CIPSEND\r\n") {
        delay(50);
        Serial.println("Data Sending Started");
        count++;
        Serial.println(count);
      }
      if ((readstr == "OK\r\n")) {
        Serial.println("correct");
        delay(50);
        if (count == 1) {
          Serial.println("String sent");
          count = 0;

        }
      }
      readstr = "";
    }

  }
}

void sendtemp() {
  delay(500);
  SIM900.println(buffer);
  delay(300);
  ShowSerialData();
  SIM900.write(0x1A);
  delay(300);
  ShowSerialData();
}

You have a lot of crap in the loop function.

If you just read a line, and don’t close the file then the next reading the line will be the next line.

Please follow the advice of wvmarle and write a simple program without all that crap and we could probably tell where you are going wrong. You no not need to use position and seek for what you are trying to do.

i can read the text file line by line

code:

#include <SD.h>
#include <SPI.h>
File printFile;
String buffer;
boolean SDfound;
void setup() {
  Serial.begin(9600);
  if (SDfound == 0) {
    if (!SD.begin(10)) {
      Serial.print("The SD card cannot be found");
      while (1);
    }
  }
}

void loop() {
  SDfound = 1;
  printFile = SD.open("data.txt");

  if (!printFile) {
    Serial.print("The text file cannot be opened");
    while (1);
  }
  while (printFile.available()) {
    buffer = printFile.readStringUntil('\n');
    Serial.println(buffer);
  }
  //printFile.close();
}

There's so much unrelated stuff in that loop() that even upon third skim-reading I couldn't find where in the sketch you try to read from the SD file. That new short one is much better.

Replace the while with an if statement, and you read the next line every time loop() runs.

You may need to add a flag to not try to open the file when it is open already:

bool fileOpen = false;

loop() {
  if (fileOpen == false) {
    printFile = SD.open("data.txt");
    fileOpen = true;
  }
  // Rest of loop functions
}

Thank You sir,Now i am able to send data line by line through GPRS

#include <SD.h>
#include <SPI.h>
#include<SoftwareSerial.h>
SoftwareSerial SIM900(2, 3);
File printFile;
String buffer;
bool fileOpen = false;
boolean SDfound;
void setup() {
  Serial.begin(9600);
  SIM900.begin(9600);
  if (SDfound == 0) {
    if (!SD.begin(10)) {
      Serial.print("The SD card cannot be found");
      while (1);
    }
  }
}

void loop() {
  SDfound = 1;
  if (fileOpen == false) {
    printFile = SD.open("data.txt");
    fileOpen = true;
  }
  delay(3000);
  while (printFile.available()) {
    buffer = printFile.readStringUntil('\n');
    Serial.println(buffer);
    Serial.println("TCP Receive :");
    Serial.print("AT\\r\\n");
    SIM900.println("AT"); /* Check Communication */
    delay(5000);
    ShowSerialData(); /* Print response on the serial monitor */


    Serial.print("AT+CIPMUX=0\\r\\n");
    SIM900.println("AT+CIPMUX=0");  /* Single TCP/IP connection mode */
    delay(5000);
    ShowSerialData();

    Serial.print("AT+CGATT=1\\r\\n");
    SIM900.println("AT+CGATT=1"); /* Attach to GPRS Service */
    delay(5000);
    ShowSerialData();

    Serial.print("AT+CREG?\\r\\n");
    SIM900.println("AT+CREG?"); /* Network registration status */
    delay(5000);
    ShowSerialData();

    Serial.print("AT+CGATT?\\r\\n");
    SIM900.println("AT+CGATT?");  /* Attached to or detached from GPRS service */
    delay(5000);
    ShowSerialData();

    Serial.print("AT+CGDCONT=1,\"IP\",\"ISAFE\"\r\\n");
    SIM900.println("AT+CGDCONT=1,\"IP\",\"ISAFE\""); /* Start task and set APN */
    delay(5000);
    ShowSerialData();

    Serial.print("AT+CGACT=1,1\\r\\n");
    SIM900.println("AT+CGACT=1,1"); /* Bring up wireless connection with GPRS */
    delay(5000);
    ShowSerialData();

    Serial.print("AT+CIFSR\\r\\n");
    SIM900.println("AT+CIFSR"); /* Get local IP address */
    delay(5000);
    ShowSerialData();

    Serial.print("AT+CIPSTART=\"TCP\",\"122.169.113.165\",1883");
    SIM900.println("AT+CIPSTART=\"TCP\",\"122.169.113.165\",1883");  /* Start up TCP connection */
    delay(5000);
    ShowSerialData();
    delay(5000);
    Serial.print("AT+CIPSEND\\r\\n");
    SIM900.println("AT+CIPSEND"); /* Send data through TCP connection */
    delay(2000);
    ShowSerialData();
    delay(2000);
    sendtemp();
    delay(5000);
    ShowSerialData();
    Serial.print("AT+CIPCLOSE\\r\\n");
    SIM900.println("AT+CIPCLOSE"); 
    delay(5000);
    ShowSerialData();
    Serial.print("AT+CIPSHUT\\r\\n");
    SIM900.println("AT+CIPSHUT"); 
    delay(5000);
    ShowSerialData();
    buffer = "";


  }
  //printFile.close();
}
void ShowSerialData()
{
  while (SIM900.available() != 0) /* If data is available on serial port */
    Serial.write(char (SIM900.read())); /* Print character received on to the serial monitor */
}

void sendtemp() {
  delay(500);
  SIM900.println(buffer);
  delay(300);
  ShowSerialData();
  SIM900.write(0x1A);
  delay(300);
  ShowSerialData();
}

But I want to consider them as a “sent” if sent successfully ,And skip all the lines which are being sent successfully and read the next line and send to tcp port like that way…

Two options. 1) create an array of bool, with as many elements as you have lines. Then you can mark line by line whether it's been sent. If you have many lines, you better use bytes for that, and bitwise operations so you can store the status of 8 lines per element. Or if not too many lines, use a single 64-bit integer to store status of up to 64 lines. This way lines can be sent and resent in random order.

2) create a global int and simply keep track of the last line you sent out, if they're always sent in order.

These way might be helpful;but in the process of reading line from sd card and then sending to tcp port ,
if incase GPRS Module fails to connect to that port,that line will be missed out when next time loop runs() and will take next line…that would be loss of data…

Then you just don’t mark the line as sent. You only do that when you have the confirmation that it’s actually done.

Right I have got "OK"response from gprs module after AT+CIPSEND.. I have compared with hardcoded OK"

if matches then write anything at the start of that line to mark it as "sent"...

wvmarle: 2) create a global int and simply keep track of the last line you sent out, if they're always sent in order.

OK i got it now what you are saying.... increment the int counter when actually sent the line successfully(meaning i got "ok"after AT+CIPSEND)..which is global

how to keep track of the line?

As you outlined yourself in #11

The following Things i did to keep track of line: 1.After AT+CIPSEND I increment count by 1 in loop 2.In Reading serial port,when i got (count==1) and "OK", I increment one more variable recNum by 1 Which is global and saves to another variable Named inc

void ShowSerialData()
{
  while (SIM900.available() != 0) {
    val = char (SIM900.read());
    readstr += val;
    if (val == '\n') {
      Serial.println(readstr);
      Serial.println(count);
      if (count == 1) {
        if ((readstr == "OK\r\n")) {
          recNum++;
          Serial.println(F("String sent"));
          Serial.println(recNum);
          inc = recNum;
          count = 0;
        }
      }
      readstr = "";
    }
  }
}

and in loop i did something like this:

list = myFile.readStringUntil('\r');
    if (recNum == inc) {
           sendGprsdata();
}

still not working as expected :(

i want when gprs module failed to send specific line to tcp port ,in next loop it should read that specific line again and try to send to tcp port

The snippets you posted are useless, the error is most likely in the part you didn't post (as so often when it comes to snippets). E.g. this variable count: it is only ever set to 0 in this snippet. Not declared, nowhere it can reach the value 1.

Please create a smallest possible sketch that fully demonstrates the problem at hand (i.e. reading the file and sending it out), then post that as a whole.

okay
code:

#include <SoftwareSerial.h>
SoftwareSerial SIM900(2, 3);
#include <SPI.h>
#include <SD.h>
int recNum = 0, count = 0, inc = 0;

File myFile;
String list;
char val;
String readstr, readstr1;
void setup() {

  Serial.begin(9600);
  while (!Serial) {
    ;
  }
  SIM900.begin(9600);

  Serial.print("Initializing SD card...");

  if (!SD.begin(10)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
}

void loop() {
  myFile = SD.open("Data1.txt");
  while (myFile.available())
  { list = myFile.readStringUntil('\r');

    if (recNum == inc) {
      gprsData();
    }
  }
}


void ShowSerialData()
{
  while (SIM900.available() != 0) {
    val = char (SIM900.read());
    readstr += val;
    if (val == '\n') {
      Serial.println(readstr);
      Serial.println(count);
      if (count == 1) {//if counter =1
        if ((readstr == "OK\r\n")) {//&got OK After AT+CIPSEND
          recNum++;//Increment the counter
          Serial.println(F("String sent"));
          Serial.println(recNum);
          inc = recNum;//save counter value to another variable
          count = 0;
        }
      }
      readstr = "";
    }
  }
}

void gprsData() {
  Serial.println(list);
  Serial.println("TCP Receive :");
  Serial.print("AT\\r\\n");
  SIM900.println("AT");
  delay(5000);
  ShowSerialData();


  Serial.print("AT+CIPMUX=0\\r\\n");
  SIM900.println("AT+CIPMUX=0");
  delay(5000);
  ShowSerialData();

  Serial.print("AT+CGATT=1\\r\\n");
  SIM900.println("AT+CGATT=1");
  delay(5000);
  ShowSerialData();

  Serial.print("AT+CREG?\\r\\n");
  SIM900.println("AT+CREG?");
  delay(5000);
  ShowSerialData();

  Serial.print("AT+CGATT?\\r\\n");
  SIM900.println("AT+CGATT?");
  delay(5000);
  ShowSerialData();

  Serial.print("AT+CGDCONT=1,\"IP\",\"ISAFE\"\r\\n");
  SIM900.println("AT+CGDCONT=1,\"IP\",\"ISAFE\"");
  delay(5000);
  ShowSerialData();

  Serial.print("AT+CGACT=1,1\\r\\n");
  SIM900.println("AT+CGACT=1,1");
  delay(5000);
  ShowSerialData();

  Serial.print("AT+CIFSR\\r\\n");
  SIM900.println("AT+CIFSR");
  delay(5000);
  ShowSerialData();

  Serial.print("AT+CIPSTART=\"TCP\",\"122.169.113.165\",1883");
  SIM900.println("AT+CIPSTART=\"TCP\",\"122.169.113.165\",1883");
  delay(5000);
  ShowSerialData();

  Serial.print("AT+CIPSEND\\r\\n");
  SIM900.println("AT+CIPSEND");
  delay(2000);
  count++;//increment the counter
  ShowSerialData();
  delay(2000);
  SIM900.println(list);
  delay(300);
  ShowSerialData();
  SIM900.write(0x1A);
  delay(300);
  ShowSerialData();
  delay(5000);
  ShowSerialData();

  Serial.print("AT+CIPCLOSE\\r\\n");
  SIM900.println("AT+CIPCLOSE");
  delay(5000);
  ShowSerialData();

  Serial.print("AT+CIPSHUT\\r\\n");
  SIM900.println("AT+CIPSHUT");
  delay(5000);
  ShowSerialData();
}

You're doing weird things, hard to understand, especially for lack of useful comments (the only comments that are there just repeat literally what the code does, instead of explaining why you're doing this).

There's this variable "count" which appears to be used as a bool flag (only allowed values 0 and 1, and though you have a count++ there) telling whether a line has been sent. OTOH recNum is a kind of counter it seems. That's then set equal to another variable called inc, and the only use for inc that I find is a test whether it's equal to recNum?!

Your post did get me thinking on how I would solve this problem. One of the key things is to separate stuff into functions - you did that largely already. Then you get something like this:

void sendLinesInFile() {
  // open file on SD card here, store it in object f.
  nextLine = readLine(f); // read the first line.
  while (nextLine != "") { 
    while (sendLine(nextLine) == false) {} // Repeat until sendLine returns true, i.e. sent successfully.
    nextLine = readLine(f); // returns the next line, or an empty line when finished with the file.
  }
}

char* nextLine(file f) {
  // Reads next line from the file f.
  // Returns that line or an empty string if EOF.
}

bool sendLine(char* line) {
  // sends the line.
}

You may want to add a limit on resends, so it doesn't try in perpetuity when there's no working connection, and other such contingency measures. Also sendLinesInFile() may return the result of the sending.

Hi 90% got it working…created one logic please check in loop:
code:

#include <SoftwareSerial.h>
SoftwareSerial SIM900(2, 3);
#include <SD.h>
#include <SPI.h>
File printFile;
char val;
int recNum = 0;
int recordCount;
int lastRecordStart;
String readstr;
String buffer;
boolean SDfound, SendFound;
bool fileOpen = false;
void setup() {
  Serial.begin(9600);
  if (SDfound == 0) {
    if (!SD.begin(8)) {
      Serial.print(F("The SD card cannot be found"));
      while (1);
    }
  }
  pinMode(9, OUTPUT);
  SIM900.begin(9600);
  SIM900power();
}



void loop() {
  SDfound = 1;
  if (fileOpen == false) {
    printFile = SD.open("Data1.txt");
    fileOpen = true;
  }

  if (!printFile) {
    Serial.print(F("The text file cannot be opened"));
    while (1);
  }
  if (printFile) {
    Serial.println(F("Data1.txt:"));
    while (printFile.available()) {
      Serial.print(F("LineNumber:")); Serial.println(recNum);    //start from line zero
      lastRecordStart = recNum * 10;    //(number of characters in a line including 'r' and '\n'=10)*(number of times data sent successfully)
      printFile.seek(lastRecordStart);    //seek till you got position
      Serial.print(F("Last position till data sent successfully:")); Serial.println(lastRecordStart);
      buffer = printFile.readStringUntil('\n');//read that line
      Serial.print(F("Line to be sent:")); Serial.println(buffer);
      gprsData();
    }
    // printFile.close();
  }

}


void gprsData() {

  Serial.println("TCP Receive :");
  Serial.print(F("AT\\r\\n"));
  SIM900.println("AT");
  delay(5000);
  ShowSerialData();

  Serial.print(F("AT+CIPMUX=0\\r\\n"));
  SIM900.println("AT+CIPMUX=0");
  delay(5000);
  ShowSerialData();

  Serial.print(F("AT+CGATT=1\\r\\n"));
  SIM900.println("AT+CGATT=1");
  delay(5000);
  ShowSerialData();

  Serial.print(F("AT+CREG?\\r\\n"));
  SIM900.println("AT+CREG?");
  delay(5000);
  ShowSerialData();

  Serial.print(F("AT+CGATT?\\r\\n"));
  SIM900.println("AT+CGATT?");
  delay(5000);
  ShowSerialData();

  Serial.print(F("AT+CGDCONT=1,\"IP\",\"ISAFE\"\r\\n"));
  SIM900.println("AT+CGDCONT=1,\"IP\",\"ISAFE\"");
  delay(5000);
  ShowSerialData();

  Serial.print(F("AT+CGACT=1,1\\r\\n"));
  SIM900.println("AT+CGACT=1,1");
  delay(5000);
  ShowSerialData();

  Serial.print(F("AT+CIFSR\\r\\n"));
  SIM900.println("AT+CIFSR");
  delay(5000);
  ShowSerialData();

  Serial.print(F("AT+CIPSTART=\"TCP\",\"122.169.113.165\",1883"));
  SIM900.println("AT+CIPSTART=\"TCP\",\"122.169.113.165\",1883");
  delay(5000);
  ShowSerialData();

  Serial.print(F("AT+CIPSEND\\r\\n"));
  SIM900.println("AT+CIPSEND");
  delay(2000);
  ShowSerialData();
  delay(2000);
  SIM900.println(buffer);
  delay(300);
  ShowSerialData();
  SIM900.write(0x1A);
  delay(300);
  ShowSerialData();
  delay(5000);
  ShowSerialData();

  Serial.print(F("AT+CIPCLOSE\\r\\n"));
  SIM900.println("AT+CIPCLOSE");
  delay(5000);
  ShowSerialData();

  Serial.print(F("AT+CIPSHUT\\r\\n"));
  SIM900.println("AT+CIPSHUT");
  delay(5000);
  ShowSerialData();

}



void ShowSerialData()
{
  /////-----------------------------------need Help in This Function may be better switching of if loops -----------------------------////////
  while (SIM900.available() != 0) {
    val = char (SIM900.read());
    readstr += val;


    if (val == '\n') {
      Serial.println(readstr);


      if (readstr == "AT+CIPSEND\r\n") {// you found AT+CIPSEND COMMAND
        delay(50);
        SendFound = 1;//set flag
      }




      if (SendFound == 1) {    //when command found
        if ((readstr == "OK\r\n")) {  //& OK response got confirmed that data sent successfully
          Serial.println(F("String sent"));
          recNum++;  //increment the global varible
          Serial.print(F("recNum:")); Serial.println(recNum);
        }
      }



      if (readstr == "AT+CIPCLOSE\r\n") {
        SendFound = 0; //reset flag in next command after AT+CIPSEND
        Serial.print(F("recNum:")); Serial.println(recNum);
      }


      readstr = "";
    }
  }
}


void SIM900power()
// software equivalent of pressing the GSM shield "power" button
{
  digitalWrite(9, HIGH);
  delay(1000);
  digitalWrite(9, LOW);
  delay(5000);
}