I wrote the code that if an interrupt is generated, get gsmGSM location and send a message. first
The first time the interrupt is generated, I got the correct gsmGSM location using (AT+CIPGSMLOC=1,1)AT+CIPGSMLOC=1,1
and successfully sendsent a message using command AT+CMGS=+91xxxxxxxx)the AT+CMGS=+91xxxxxxxx
command.
But the next time whenan interrupt was generated then AT+CIPGSMLOC=1,1 is giving AT+CIPGSMLOC=1,1
gave a wrong location.
When i When I commented out the code to send a message then, on every interrupt generated , iI am getting the correct gsmGSM location. So it seems like the problem is occurring after sending a message. butBut I have no clue what exactly is going wrong . followingFollowing is my code.
Note: on restarting ofthe module, I'm again getting the correct location the first time, before sending a message.
void setup() {
mySerial.begin(9600);
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// initialize the pushbutton pin as an input:
pinMode(sensorPin, INPUT);
// Attach an interrupt to the ISR vector
attachInterrupt(0, pin_ISR, FALLING);
}
/////////***GSM RESPONSE***///////////
String DisplayGSMResponse()
{
if(mySerial.available())
{
String gsm_input="";
while(mySerial.available())
{
gsm_input+= (char)mySerial.read();
}
gsm_input.trim();
delay(1000);
Serial.println(gsm_input);
return gsm_input;
}
}
void SendMessage(String mobNum,String Location)
{
Serial.println("Sending message..");
mySerial.println("AT+CMGF=1");
//Sets the GSM Module in Text Mode
delay(2000);
// Delay of 1000 milli seconds or 1 second
DisplayGSMResponse();
mySerial.println("AT+CMGS=\""+mobNum+"\"\r");
// mobile Number
delay(4000);
DisplayGSMResponse();
cleanBuffer();
mySerial.print("test");
// The SMS text you want to send
delay(100);
mySerial.println((char)26);
// ASCII code of CTRL+Z
delay(8000);
DisplayGSMResponse();
}
void gprsInit() {
if (isGPRSInit) {
return;
}
Serial.println("GPRS INITIALIZING");
mySerial.println("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\"");
delay(2000);
DisplayGSMResponse();
mySerial.println("AT+SAPBR=3,1,\"APN\",\"INTERNET\"");
delay(2000);
DisplayGSMResponse();
mySerial.println("AT+SAPBR=1,1");
delay(8000);
DisplayGSMResponse();
mySerial.println("AT+SAPBR=2,1");
delay(2000);
DisplayGSMResponse();
isGPRSInit = true;
}
void gprsterm() {
mySerial.println("AT+SAPBR=0,1");
delay(1000);
DisplayGSMResponse();
isGPRSInit = false;
}
String getGPS() {
Serial.println("GETTING GPS LOCATION");
String loc = "";
String location = "";
mySerial.println("AT+CIPGSMLOC=1,1");
delay(3000);
loc = DisplayGSMResponse();
return location;
}
void loop() {
if (buttonState == 1)
{
detachInterrupt(0);
gprsInit();
location = getGPS();
SendMessage(mobileNumber, location);
gprsterm();
Serial.println("location = " + location);
buttonState = 0;
location = "";
attachInterrupt(0, pin_ISR, FALLING );
}
}
void pin_ISR() {
buttonState = 1;
Serial.println("interrupt");
}
EDIT: added gprsterm funcfunction.
Note: I removed the interrupt and called the code inside the loop function twice. The first time I got a correct result. The second time a wrong result. soSo it interrupt seems like the interrupt is not be causing the problem.
EDIT2:
onOne interesting thing I found is that at my home it is showing correct results every time, but in the office it is showing a wrong location on second location call i.e after sms got triggeredthe SMS was sent and then if we do gsmget the GSM location call using AT+CIPGSMLOC=1,1AT+CIPGSMLOC=1,1
.