Send GPS Location on Multiple Numbers

Good day,

Please help me in my code, it works in sending to a single mobile number but I need to send it to multiple mobile numbers.

This is my code:

#include <Adafruit_GPS.h>
#include <SoftwareSerial.h>

SoftwareSerial mySerial(3, 2);
SoftwareSerial phoneSerial(5,6);
Adafruit_GPS GPS(&mySerial);
#define GPSECHO  false
boolean usingInterrupt = true;
void useInterrupt(boolean);

void setup()  
{
   
  Serial.begin(115200);
  Serial.println("Adafruit GPS library basic test!");
  GPS.begin(9600);
  
  GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
    GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);   
    GPS.sendCommand(PGCMD_ANTENNA);
  useInterrupt(true);

  delay(1000);
  
}

SIGNAL(TIMER0_COMPA_vect) {
  char c = GPS.read();
#ifdef UDR0
  if (GPSECHO)
    if (c) UDR0 = c;  
#endif
}

void useInterrupt(boolean v) {
  if (v) {
    OCR0A = 0xAF;
    TIMSK0 |= _BV(OCIE0A);
    usingInterrupt = true;
  } else {
    TIMSK0 &= ~_BV(OCIE0A);
    usingInterrupt = false;
  }
}


uint8_t state = 1;
uint32_t start_phone = 0UL;
uint32_t phone_timer = 0UL;

void loop()                    
{

   if (GPS.newNMEAreceived()) {
       if (!GPS.parse(GPS.lastNMEA())) {  
      if (millis() > start_phone) {
        if (millis() - phone_timer > 10000)  {     
          mySerial.end();
          phoneSerial.begin(4800);
      

    phoneSerial.begin(4800);
    delay(5000);
    Serial.print("Sending Text");
   phoneSerial.println("AT");
   updateSerial();
   
    phoneSerial.println("AT+CMGF=1");
    updateSerial();

    phoneSerial.println("AT+CMGS=\"+63*********\"");
    updateSerial();
    
    if(GPS.fix){
    
      phoneSerial.print("http://maps.google.com/maps?q=loc:");
    updateSerial();
    phoneSerial.print(GPS.latitudeDegrees, 4);
    updateSerial();
    phoneSerial.print(",");
     updateSerial();
    phoneSerial.print(GPS.longitudeDegrees, 4);
     updateSerial();
     }
    else{
      phoneSerial.print("No.Fix");
      updateSerial();
    }
    
    phoneSerial.write(26);
    
    Serial.println("Sent");
    delay(1000);

   }
  }
 }
}
}

void updateSerial()
{
  delay(500);
  while (Serial.available()) 
  {
    phoneSerial.write(Serial.read());
  }
  while(phoneSerial.available()) 
  {
    Serial.write(phoneSerial.read());
  }
}

why do you have twice

phoneSerial.begin(4800);
phoneSerial.begin(4800);

you have a part of the code where you send to 1 number… if you duplicate that part and change the phone number (not a good idea to publish your phone number on a public forum by the way) then you’ll send it to multiple phones.

if you were to put the phone numbers in an array, you could iterate over the array to repeat the code and send to multiple numbers

give it a try and post your ideas

Create a function that accepts a phone number and a text (both as const char*). The function handles the communication with the GMS module. Then just call the function with any number of phone numbers you want (pseudo code):

void sendSMS(const char* phoneNumber, const char* text)
{
    // ...
    phoneSerial.print("AT+CMGS=\"");
    phoneSerial.print(phoneNumber);
    phoneSerial.println('\"');
    
    phoneSerial.println(text);
    phoneSerial.write(26);
}

void loop()
{
    sendSMS("+69123456789", "Hello world to phone 1");
    sendSMS("+69987654321", "Text for phone 2");
}

Can I use an if/else condition?

An if/else does not repeat anything

yumenoshian:
Can I use an if/else condition?

Sure. But it doesn't make much sense in this context.

Here is an example of using an array of phone numbers, a function and a for loop to go through the array

const char * phoneNumbers[] = {
  "+63*********1",
  "+63*********2",
  "+63*********3",
  "+63*********4",
  "+63*********5",
  "+63*********6",
  "+63*********7",
  "+63*********8",
};

const uint8_t nbPhoneNumbers = sizeof(phoneNumbers) / sizeof(phoneNumbers[0]);

void doSomethingWithPhoneNumbers()
{
  for (uint8_t index = 0; index < nbPhoneNumbers; index++) {
    Serial.print(F("SMS to "));
    Serial.print(phoneNumbers[index]);
    Serial.print(F("\t-->\tAT+CMGS=\""));
    Serial.print(phoneNumbers[index]);
    Serial.println(F("\""));
  }
}

void setup() {
  Serial.begin(115200);
  doSomethingWithPhoneNumbers();
}

void loop() {}

running this code you’ll see in the Serial Monitor (ensure it’s set at 115200 bauds) the following:

[color=purple]SMS to +63*********1	-->	AT+CMGS="+63*********1"
SMS to +63*********2	-->	AT+CMGS="+63*********2"
SMS to +63*********3	-->	AT+CMGS="+63*********3"
SMS to +63*********4	-->	AT+CMGS="+63*********4"
SMS to +63*********5	-->	AT+CMGS="+63*********5"
SMS to +63*********6	-->	AT+CMGS="+63*********6"
SMS to +63*********7	-->	AT+CMGS="+63*********7"
SMS to +63*********8	-->	AT+CMGS="+63*********8"
[/color]

that should give you an idea on how to structure your code