I'm using SIM900A and GY NEO6MV2. I'm supposed to receive device location continuously after sending "Staff" to GSM and will stop receiving the location if I send "Stop" to GSM. This work on Serial monitor but not on my cellphone.

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

    float flat, flon; // create variable for latitude and longitude object
    int temp=0,i;

    SoftwareSerial mySerial(10, 11); // create gps sensor connection
    TinyGPS gps; // create gps object

    void setup(){
       Serial.begin(9600); // connect serial
       mySerial.begin(9600); // connect gps sensor

     void loop()

     void get_gps() // run over and over
        unsigned long start = millis();

        while (millis() - start < 10000) {
        if (mySerial.available()) {
             char c = mySerial.read();
             if (gps.encode(c)) {

     void gpsdump(TinyGPS &gps)
         Serial.print("Location?\nRight here:\n");
         Serial.print("Google Maps: https://google.com/maps/place//@");
         printFloat(flat, 5); Serial.print(",");
         printFloat(flon, 5); Serial.print('\n');
         Serial.print("Location as metioned above\nTQVM");

      void init_sms()

    void printFloat(double number, int digits)
     // Handle negative numbers
     if (number < 0.0) {
     number = -number;

      // Round correctly so that print(1.999, 2) prints as "2.00"
      double rounding = 0.5;
      for (uint8_t i=0; i<digits; ++i)
      rounding /= 10.0;

      number += rounding;

    // Extract the integer part of the number and print it
     unsigned long int_part = (unsigned long)number;
     double remainder = number - (double)int_part;

    // Print the decimal point, but only if there are digits beyond
    if (digits > 0)

     // Extract digits from the remainder one at a time
     while (digits-- > 0) {
     remainder *= 10.0;
     int toPrint = int(remainder);
     remainder -= toPrint;

1 Answer 1

void loop() {

That's an instant death warrant right there. You can kiss your stack goodbye as it crashes mercilessly into your heap. Never ever ever call loop() from anywhere.

Your decision as to whether or not to get the GPS data is completely flawed. It will run the function once when it finds "Staff" and never again (unless it sees "Staff" again on serial). It doesn't keep finding the same thing over and over again unless you keep sending it over and over again.

And come to that - how exactly are you sending "Staff" and "Stop" through GSM? I assume by SMS message, however I see no code there to actively retrieve the SMS messages from the SIM. So you'll never see anything anyway.

You need to completely re-think your whole program methodology:

  1. Look for new SMS messages
  2. If any exist then read one and delete it.
  3. If the message contained "Staff" then enable sending mode. If the message contained "Stop" then disable sending mode.
  4. If sending mode is enabled and enough time has passed since the last sending then get the GPS data and send it.

You notice the sending of the GPS data is separated from the SMS reception. It's not the reception of the SMS that controls the GSM but the "mode" your program is running in. The SMS just controls that mode.

This is a crude form of Finite State Machine.

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.