Go Down

Topic: Gsm Shield + Sd card (Read 2116 times) previous topic - next topic

taramarasarahlara

Hi,

I am trying to receive sms and then stock them in a sd card. The initialization of the card is working but then my code stop just before the "Waiting for messages" serial! 

I would be so grateful if someone already tried to do this or have some tips for me.

Code: [Select]
#include <SoftwareSerial.h>  // Needed by the EMIC2 library
#include <SD.h>  // Needed by the EMIC2 library, though not utilized in this example
#include "EMIC2.h"




// include the GSM library
#include <GSM.h>

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

// for the available pins on your board
#define RX_PIN 9  // Connect SOUT pin of the Emic 2 module to the RX pin
#define TX_PIN 6  // Connect SIN pin of the Emic 2 module to the TX pin

EMIC2 emic;  // Creates an instance of the EMIC2 library

File myFile;

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



// Array to hold the number a SMS is retreived from
char senderNumber[20];



void setup()
{
  // initialize serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
 
  //Initializing card
 Serial.println("Initializing SD card...");

   pinMode(10, OUTPUT);
   
  if (!SD.begin(10)) {
    Serial.println("initialization failed!");
   
    return;
  }
  Serial.println("initialization done.");
 
 // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
 // myFile = SD.open("test.txt", FILE_WRITE);

 //SMS
 
 
  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("Waiting for messages");
 
  // Initializes the EMIC2 instance
    // The library sets up a SoftwareSerial port
    // for the communication with the Emic 2 module
    emic.begin(RX_PIN, TX_PIN);
   
    emic.setVoice(8);  // Sets the voice (9 choices: 0 - 8)*/
   
   
   
     
}

void loop()
{
 
 
  char c;
 
 
 
  // If there are any SMSs available() 
   if (sms.available()){
   
 
    Serial.println("Message received from:");
   
    // Get remote number
    sms.remoteNumber(senderNumber, 20);
    Serial.println(senderNumber);

    // An example of message disposal   
    // Any messages starting with # should be discarded
    if(sms.peek()=='#')
    {
      Serial.println("Discarded SMS");
      sms.flush();
    }
   
    String textToSay = "";
    // Read message bytes and print them
    while(c=sms.read()){
      Serial.print(c);
      textToSay = textToSay + String(c);
    }
//    emic.setRate(100);
//    emic.speak(textToSay);
//    emic.resetRate(); 
   
   
   
   
   
    Serial.println("\nEND OF MESSAGE");
   
    // Delete message from modem memory
    sms.flush();
    Serial.println("MESSAGE DELETED");
   
   
   myFile = SD.open("test.txt", FILE_WRITE);
   
     // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.print(textToSay);
    myFile.println("/");
// close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
   
     delay(1000);
         
  }

}

ronaldgadget

I have a GSM shield with TFT and Sd card working with Arduino Mega.

I discovered that the GSM shield appears very sensitive to power supply: I can hardly start the GSM module when running from power over USB. When I power from a 2A/5V USB adapter it works better, but also has issues.

I will attach the relevant pieces of code below.
Note that I do not use the millis() function for timeout--> this is unreliable on my arduinos (don't know why)

The includes /variables I use:
Code: [Select]
// %%%%%%%%%% library includes %%%%%%%%%%%%%%
#include <SPI.h>
#include <SD.h>
#include <TFT.h>            // Arduino LCD library
#include <EEPROM.h>
#include <GSM.h>

// display stuff
char str[50],WrkStr[50];       // strings to play with
TFT screen = TFT(cs, dc, rst); // define the display
GSM_SMS sms;

.... init code:
  screen.begin();
  if (!SD.begin(SD_CS)) {
      screen.text("SD Card fail or not present", 10, 70);
  }
gsmAccess.begin(PinCode)==GSM_READY //program hangs here when GSM shield power is bad


code to read SMS, show on display and write to Sdcard
Code: [Select]
if (sms.available())
  {
    // Get remote number
    sms.remoteNumber(phoneNumber, 20);
       
    strcpy(WrkStr,oTimeS);strcat(WrkStr,", ");
    itoa(day,str,10);strcat(WrkStr,str);strcat(WrkStr,"/");
    itoa(month,str,10);strcat(WrkStr,str);strcat(WrkStr,"/");
    itoa(year,str,10);strcat(WrkStr,str);strcat(WrkStr," from ");
    screen.text(WrkStr,0,20);screen.text(phoneNumber,32,30);
    strcat(WrkStr,phoneNumber);     

    // Any messages starting with # should be discarded
    if(sms.peek()=='#') sms.flush();
   
    // Read message bytes into char array
    i=0;
    while(c=sms.read()) {
      smsTxt[i]=char(c); i++;
    }
    smsTxt[i]='\0'; // mark the end
   
    //show SMS on display
    dispTxt(smsTxt,50);
   
    // Delete message from modem memory
    sms.flush();
   
    //write this to logfile
    logFile = SD.open(smsFile, FILE_WRITE);
    if (logFile) {
      logFile.println(WrkStr);
      logFile.println(smsTxt);     
      logFile.close();
    } else screen.text("*** Error writing to Log file ***",10,120);
 


used procedure
Code: [Select]
// **********************************************************************************
// **********************************************************************************
// **** show char array inTxt at screen at y pos y, in as many lines as needed   ****
// **********************************************************************************
// **********************************************************************************
int dispTxt(char *inTxt, int y) {
    int maxx=26,c;   // max chars per line
    while (strlen(inTxt)>maxx) {
      strncpy(WrkStr,inTxt,maxx); WrkStr[maxx] = '\0';
      screen.text(WrkStr,0,y);y=y+9;  // show this part; next line
      c=strlen(inTxt)-maxx;
      strncpy(inTxt,inTxt+maxx,c);
      inTxt[c]='\0';
    }
    screen.text(inTxt,0,y);
    return y;
}


Good Luck!
Ronald Gadget - discovered Arduino in Sept 2014 - built 4 systems with it. Professionally building uServers - see www.zurich.ibm.com/microserver

Go Up