1

I wrote the code that if an interrupt is generated, get GSM location and send a message.

The first time the interrupt is generated, I got the correct GSM location using AT+CIPGSMLOC=1,1 and successfully sent a message using the AT+CMGS=+91xxxxxxxxcommand.

But the next time an interrupt was generated, AT+CIPGSMLOC=1,1 gave a wrong location. When I commented out the code to send a message, on every interrupt generated I am getting the correct GSM location. So it seems like the problem is occurring after sending a message. But I have no clue what exactly is going wrong. Following is my code.

Note: on restarting the 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) {
  }
  // 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 function.

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. So it seems like the interrupt is not be causing the problem.

EDIT2: One 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 the SMS was sent and we get the GSM location using AT+CIPGSMLOC=1,1.

4
  • What does your gprsterm() function do? But more importantly, review how you are using interrupts, please consult how to use attachInterrupt() on arduino.cc
    – jogco
    Commented Jul 26, 2016 at 18:48
  • added gprsterm func. I removed the interrupt and called the code inside loop function twice. first time got correct result. second time wrong result. so it interrupt seems not be causing problem.
    – Suri
    Commented Jul 27, 2016 at 9:19
  • 1
    Check the function getGPS(). Especially the usage of the local String variables. You might find something there :) Commented Jul 27, 2016 at 12:17
  • I checked local string variable all are well means intialized with empty string then with data. on interesting thing I found is that at my home it is giving correct results every time, but in office it is showing wrong location on second location call,
    – Suri
    Commented Jul 28, 2016 at 13:03

2 Answers 2

1

It might be due to the command giving the nearest cell tower location. And for sending SMS it might be shifting to a different cell tower depending on your network. This could maybe be easily checked using a different cell provider or as you said its working at your home so I believe I am correct in assuming this is the answer

-1
Serial.println("location = " + location);

I think in upper line you should remove + sign from the code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.