-1

I am trying to make a GSM shield work together with an SD shield i.e. everytime a message is received it should be saved to the SD card. However this doesn't seem to work. The strange thing is errors seem to pop up at different places each time. Sometimes the loop stops at sms.flush(); yet other times it encounters problems at lastMess = "";. Also, the characters are often scrambled and formed into strange symbols. I thought the problem might be to do with the communication with the Serial monitor, the GSM shield and the SD card.I.e. SPI/serial protocol problems. I also tried replacing the SMS input by a serial monitor input, and writing this to SD but this gave even more problems. Thank you in advance!

#include <SPI.h>
#include <SD.h>
File dataFile;
const int chipSelect = 4;
// include the GSM library
#include <GSM.h>

// PIN Number for the SIM
#define PINNUMBER ""

// initialize the library instances
GSM gsmAccess;
GSM_SMS sms;

// Array to hold the number a SMS is retreived from
char senderNumber[20];
char Content[20];
String lastMess;
char c;
String txtmsg;
String readString;
void setup()
{
  // initialize serial communications and wait for port to open:
  while (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
     //return;
  }

  Serial.begin(9600);

  Serial.println("SMS Messages Receiver");

  // connection state
  boolean notConnected = true;

  // Start GSM connection
  while (notConnected)
  {
    if (gsmAccess.begin(PINNUMBER) == GSM_READY)
      {
      notConnected = false;
      }
    else
    {
      Serial.println("Not connected");
      delay(1000);
    }
  }

  Serial.println("GSM initialized");
  Serial.println("SD initialized");
  Serial.println("Waiting for messages");
}

void loop()
{

  lastMess = "";
  Serial.println("BeginLoop");
  if (sms.available())
  {
    Serial.println("Message received from:");

    // Get remote number
    sms.remoteNumber(senderNumber, 20);
    Serial.println(senderNumber);

      // Read message bytes and print them
    while (c = sms.read())
      {
      Serial.print(c);      
      String nextChar = String (c);
      String Mess = (lastMess + nextChar);
      lastMess = Mess;
      }
    Serial.println("This is the message content:");  
    Serial.println(lastMess);
    Serial.println("\nEND OF MESSAGE");
    // Delete message from modem memory
    sms.flush();
    Serial.println("MESSAGE DELETED");    

    dataFile = SD.open("datalog.csv", FILE_WRITE);

  if (dataFile)
          {
            dataFile.print(senderNumber);
            dataFile.print(",");
            dataFile.print(lastMess);
            dataFile.print(",");
            dataFile.println("time e.g. 12:00");
            dataFile.close();
            delay(1000);            
            Serial.println("Content written to SD");
          }


  else if (!dataFile)
  {

    Serial.println("SD not opened");
  }
  // if the file isn't open, pop up an error:
  else 
  {
    Serial.println("error opening datalog.csv");
  }
   Serial.println("Is SD being skipped?");
  }

  delay(1000);
  Serial.println("EndLoop");



}
Daan
  • 129
  • 4

1 Answers1

0

The answer turned out to be in the memory of the arduino, I got the following message: "Low memory available, stability problems may occur." This scrambled the characters and caused problems. After reducing the code so that is used less memory, everything worked fine!

Daan
  • 129
  • 4