Skip to main content
4 of 5
added 257 characters in body
Suri
  • 169
  • 1
  • 2
  • 7

SIM808 module : GSM location command (AT+CIPGSMLOC=1,1) giving wrong value after execution of sending message command(AT+CMGS=+91xxxxxxxx)

I wrote the code that if interrupt generated, get gsm location and send a message. first time interrupt generated, got correct gsm location using (AT+CIPGSMLOC=1,1) and successfully send message using command AT+CMGS=+91xxxxxxxx)

But next time when interrupt generated then AT+CIPGSMLOC=1,1 is giving wrong location. When i commented the code to send message then on every interrupt generated , i am getting correct gsm location. So it seems like problem is occurring after sending message. but no clue what exactly going wrong . following is my code

Note: on restarting of module again getting correct location first time before sending 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 func.

Note: 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.

EDIT2: on interesting thing I found is that at my home it is showing correct results every time, but in office it is showing wrong location on second location call i.e after sms got triggered and then if we do gsm location call using AT+CIPGSMLOC=1,1.

Suri
  • 169
  • 1
  • 2
  • 7