problem to Send SMS when interrupt occurs

Hello,

I’m using arduino mega 2560 and GSM module(SIM9000)…
i want whenever i press button SMS will send on particular no. which is stored at 1st place in SIM card.
for this i’m using in interrupt to send SMS(event based).

i used <attachInterrupt(interrupt, ISR, mode)> for interrupt.

AND to send SMS i’m using lib. SIM908/sms… and method to send SMS is…< SendSMS(byte sim_phonebook_position,const char *message_str) >

But in this method lots of delay occurs, which r not good with interrupt (if i use methods with delays or wait resp. when i press button my device stop working. and same problem is hpng with the below described condition)… most of delay i removed or replaced by some other code, but i’m not able to remove one specific delay used in this method… which is …

char GSM::GetPhoneNumber(byte position, char *phone_number)
{
	char ret_val = -1;

	char *p_char;
	char *p_char1;

	if (position == 0) return (-3);
	if (CLS_FREE != GetCommLineStatus()) return (ret_val);
	SetCommLineStatus(CLS_ATCMD);
	ret_val = 0; // not found yet
	phone_number[0] = 0; // phone number not found yet => empty string

	//send "AT+CPBR=XY" - where XY = position
	_cell.print("AT+CPBR=");
	_cell.print((int)position);
	_cell.print("\r");

	// 5000 msec. for initial comm tmout
	// 50 msec. for inter character timeout
	switch (WaitResp(5000, 50, "+CPBR")) {
	case RX_TMOUT_ERR:
		// response was not received in specific time
		ret_val = -2;
		break;

	case RX_FINISHED_STR_RECV:
		// response in case valid phone number stored:
		// <CR><LF>+CPBR: <index>,<number>,<type>,<text><CR><LF>
		// <CR><LF>OK<CR><LF>

		// response in case there is not phone number:
		// <CR><LF>OK<CR><LF>
		p_char = strchr((char *)(comm_buf), '"');
		if (p_char != NULL) {
			p_char++;       // we are on the first phone number character
			// find out '"' as finish character of phone number string
			p_char1 = strchr((char *)(p_char), '"');
			if (p_char1 != NULL) {
				*p_char1 = 0; // end of string
			}
			// extract phone number string
			strcpy(phone_number, (char *)(p_char));
			// output value = we have found out phone number string
			ret_val = 1;
		}
		break;

	case RX_FINISHED_STR_NOT_RECV:
		// only OK or ERROR => no phone number
		ret_val = 0;
		break;
	}

	SetCommLineStatus(CLS_FREE);
	return (ret_val);
}

to get the phone number used this method but here wait resp. used in switch case… and i tried a lot to replace it but didn’t get any solution yet…

It takes a long time to send an SMS so you CAN’T DO IT IN AN ISR.

In the ISR set a flag. In loop() check for the flag and send the SMS if the flag is set.

boolean TimeToSendSMS = false;
void ISR() {
  TimeToSendSMS = true;
}
void loop() {
.
.
.
  if (TimeToSendSMS) {
    TimeToSendSMS = false;
    SendTheSMS();
  }
.
.
.
}

Thank-you Sir,

i try that ....

hello sir,

i tried that and it is working properly (but individually), when i add some other functions with it.its not working properly.

i found reason behind this problem.. in above code of wait resp. switch case is giving ret_val = -2;

i.e, response was not received in specific time..

PLZ make me know how to solve this prob.

Thanku!!!

Sounds like your GSM module is not responding as expected. Are you able to write a sketch that does nothing but send the SMS without error?

yes Sir....

when i only send msg it is working properly in case of both number string and SIM_PhoneBookPosition_number by using these methods....

SendSMS(char *number_str, const char *message_str) ... for number string and SendSMS(byte sim_phonebook_position,const char *message_str) ... for SIM_PhoneBookPosition_number

and with my sketch when i use number string instead of SIM_PhoneBookPosition_number, my sketch is working properly with other functions, but i want to send msg on SIM_PhoneBookPosition_number.

Plz make me know........ How to solve this prob.

Thank-you!!!!!

in this sketch,i'm using gps with gsm... may be problem is with use of gps..

no, i was wrong that problem is with use of gps, coz when i try to connect to GPRS or internet connection with it same problem is occurring.. gsm not able to send sms at SIM_phonebook_number). :slightly_frowning_face:

Nikhat:
in this sketch,i’m using gps with gsm… may be problem is with use of gps…

What sketch?

hello sir, i explained already above about sketch.Also explained what and where i m facing problem.

when i add some other functions with it.its not working properly.

The sketch with the other functions.

Please edit your post, select the code, and put it between [code][/code] tags.

You can do that by hitting the “Code” icon above the posting area. It is the first icon, with the symbol: </>

How to use this forum

#include <SoftwareSerial.h>
#include <TinyGPS.h>
#include<SIM900.h>
#include <sms.h>

SoftwareSerial ss(12,13);
TinyGPS gps;
SMSGSM sms;
float flon;
float flat;
boolean started=false;
char str_lat[20],str_lon[20];
const int buttonPin = 2;                                                                  // the number of the pushbutton pin   
int buttonState = 0; 
char data[20];
void setup() {
   Serial.begin(9600);                            
   Serial.println("GSM Shield testing.");     
   if (gsm.begin(4800)) {
   Serial.println("\nstatus=READY");
   gsm.forceON();                                       //To ensure that SIM900 is not only in charge mode
   started=true;
   attachInterrupt(0,SMS,HIGH);
}
   else Serial.println("\nstatus=IDLE");
        if(started){  
         read_GPS();                           
         }     
};
void loop(){  
       if (TimeToSendSMS) {
TimeToSendSMS = false;
read_GPS();
SendTheSMS();
}
};

void read_GPS(){
 ss.begin(9600);
 bool newData = false;
 unsigned long chars;
 unsigned short sentences, failed;

 // For one second we parse GPS data and report some key values
 for (unsigned long start = millis(); millis() - start < 1000;){      
   while (ss.available()){
        char c = ss.read();
        if (gps.encode(c))                             // Did a new valid sentence come in?
          newData = true;
}
if (newData){
   float lat,lon;
   gps.f_get_position(&flat, &flon);
   Serial.print("LAT=");
   Serial.println(lat=flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flat,6);
   Serial.print("LON=");
   Serial.println(lon=flon == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flon,6);
   dtostrf(lat,3,6,str_lat);                         // double to string - dtostrf(double, signed char, unsigned char, char*)
   dtostrf(lon,3,6,str_lon);                         // double to string
 }
 gps.stats(&chars, &sentences, &failed);
 if (chars == 0)
   Serial.println(F("** No characters received from GPS: check wiring **"));
 }  
};
void SMS() {
TimeToSendSMS = true;
}

void SendTheSMS(){
buttonState = digitalRead(buttonPin);
   if (buttonState == HIGH){
       pinMode(buttonPin,INPUT);
sprintf(data, "***********/**********,%s,%s", str_lat, str_lon);
delay(100);
sms.SendSMS(1, data);                // msg send on number at sim phonebook position 1.  
//sms.SendSMS("**********",data);    // msg on a 10 digits number string.
}
}

in this sketch see last two lines to send sms… second last line is for SIM phonebook position 1.
and last is for number string.
sms.SendSMS("**********",data); is working properly …but i want to use sms.SendSMS(1, data);, which is not properly working… i explained above what problem i’m facing with it.

Anyone suggest solution of this problem???

no one has any solution or suggestion of my problem? :slightly_frowning_face:

in this sketch see last two lines to send sms.... second last line is for SIM phonebook position 1. and last is for number string. sms.SendSMS("**********",data); is working properly ...but i want to use sms.SendSMS(1, data);, which is not properly working... i explained above what problem i'm facing with it.

I don't understand this. You are going to have to try harder, a lot harder, to make a coherent explanation of the problem.

ok.. i'll try my best to explain u.. see points with sketch posted.

  1. i'm using SIM908 lib. in my sketch.(SIM908/SIM900/sms)

  2. I want to send SMS (stored in a variable "data") on a number which is saved in SIM card.. But don't now why this method (sms.SendSMS(SIM_phonebook_position,text SMS)) is creating problem. and not sending SMS

3.when i use (sms.SendSMS(number string,text SMS)) in my sketch then my code works properly and give output as i want and send SMS on that number string.. but this method is not useful for my purpose. i just use this method in sketch to check where is problem.

  1. When i comment(//) to read_GPS() .... sms.SendSMS(SIM_phonebook_position,text SMS ) is working properly and sending msg without any issue.

i have explained all the points.. now Please can u tell me why (sms.SendSMS(SIM_phonebook_position,text SMS)) is not working with read_GPS()?

no one understand?