Go Down

Topic: Check for unread sms using timer1 (Read 825 times) previous topic - next topic

lepass_7

Dears,
I am using arduino UNO with this IComSat gsm shield. On top of those I tried to use both libraries:
  • https://github.com/MarcoMartines/GSM-GPRS-GPS-Shield
  • https://github.com/Seeed-Studio/GPRS_SIM900


Even thought libraries are working perfectly (send/receive sms) I cannot use the sms read method based  on timer events.

Please have a look:

Code: [Select]

#include <TimerOne.h>
#include <GPRS_Shield_Arduino.h>
#include <SoftwareSerial.h>
#include <Wire.h>
#define PIN_TX    2
#define PIN_RX    3
#define BAUDRATE  9600
#define MESSAGE_LENGTH 160

char message[MESSAGE_LENGTH];
int messageIndex = 0;
char phone[16];
char datetime[24];

GPRS gprsTest(PIN_TX,PIN_RX,BAUDRATE);//RX,TX,PWR,BaudRate

void setup() {
  Serial.begin(9600);
  while(!gprsTest.init()) {
      Serial.print("init error\r\n");
      delay(1000);
  }
  delay(3000); 
  Serial.println("Init Success, please send SMS message to me!");

  delay(1000);
  /*TIMER 1*/
  Timer1.initialize(500000);         // initialize timer1, and set a 1/2 second period
  Timer1.attachInterrupt(readSMS);  // attaches callback() as a timer overflow interrupt
  Timer1.setPeriod(13000000);
}
void loop() {
 
}
void readSMS()
{
  Serial.print("/");
  messageIndex = gprsTest.isSMSunread();
  Serial.println("=");
   if (messageIndex > 0) { //At least, there is one UNREAD SMS
      gprsTest.readSMS(messageIndex, message, MESSAGE_LENGTH, phone, datetime);           
      //In order not to full SIM Memory, is better to delete it
      gprsTest.deleteSMS(messageIndex);
      Serial.print("From number: ");
      Serial.println(phone); 
      Serial.print("Datetime: ");
      Serial.println(datetime);       
      Serial.print("Recieved Message: ");
      Serial.println(message);   
   }
   else
   {
    Serial.println("No unread sms");
   }
}


The result of the above code on my terminal is to have only one time this:
Code: [Select]

Init Success, please send SMS message to me!
/


My purpose is to check for unread SMS every 13000000 uS, and keep the main loop to handle other routines.

Could you please let me know why the posted code won't work?

SupArdu

The function readSMS() contains Serial.print() which is not allowed within an interrupt.
Probably this is your problem.
You have to move the Serial.print()s to loop(), i.e. within the interrupt routine save all the information you want to get from the SMS to an array and print the content of that array within loop().


I don't know the libraries you are using. But I have a function to check if a SMS is available, IsSMSPresent(), see the attachment.
My buffer for SMS information is not large enough to store the content of several SMS.
_cell is my HW serial link to the SIM900.

You could call this function in the timer interrupt, returning if an SMS is available, and then within loop() reading the SMS and do all the prints.
-------------------
have success
SupArdu

Go Up