0

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
       delay(2000);
       Serial.println("AT+CNMI=2,2,0,0,0");
       temp=0;
     }

     void loop()
     {
        while(Serial.available())
        {
           if(Serial.find("Staff"))
           {
              get_gps();
           }
           if(Serial.find("Stop"))
           {
               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)) {
                 gpsdump(gps);;
             }
            }
          }
        }


     void gpsdump(TinyGPS &gps)
     {
         init_sms();
         gps.f_get_position(&flat,&flon);
         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");
         Serial.write(0x1A);
         delay(2000);
       }

      void init_sms()
     {
       Serial.println("AT+CMGF=1");
       delay(400);
       Serial.println("AT+CMGS=\"+60134484868\"");
       delay(400);
     }

    void printFloat(double number, int digits)
    {
     // Handle negative numbers
     if (number < 0.0) {
     Serial.print('-');
     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;
     Serial.print(int_part);

    // Print the decimal point, but only if there are digits beyond
    if (digits > 0)
    Serial.print("."); 

     // Extract digits from the remainder one at a time
     while (digits-- > 0) {
     remainder *= 10.0;
     int toPrint = int(remainder);
     Serial.print(toPrint);
     remainder -= toPrint;
    }
   }
2
void loop() {
    ....
    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, privacy policy and cookie policy

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