GPS add multiple gps values to sms

Hello everyone,
I am using the code below for my sim808 gsm/gprs and gps module to get gps location and send at text every 20seconds. Unfortunately the gps outputs the correct value the first time the operation is performed then the next time it merges the former values with the new values in serial monitor which makes the coordinate incorrect. Any suggestion on how to fix it

#include <SoftwareSerial.h>
SoftwareSerial sim808(7,8);

char phone_no[] = “xxxxxxx”; // replace with your phone no.
String data[5];
#define DEBUG true
String state,timegps,latitude,longitude;

void setup() {
sim808.begin(9600);
Serial.begin(9600);
delay(50);

sim808.print(“AT+CSMP=17,167,0,0”); // set this parameter if empty SMS received
delay(100);
sim808.print(“AT+CMGF=1\r”);
delay(400);

sendData(“AT+CGNSPWR=1”,1000,DEBUG);
delay(50);
sendData(“AT+CGNSSEQ=RMC”,1000,DEBUG);
delay(150);

}

void loop() {
sendTabData(“AT+CGNSINF”,1000,DEBUG);
if (state !=0) {
Serial.println(“State :”+state);
Serial.println(“Time :”+timegps);
Serial.println(“Latitude :”+latitude);
Serial.println(“Longitude :”+longitude);

sim808.print(“AT+CMGS=\””);
sim808.print(phone_no);
sim808.println(“\””);

delay(300);

sim808.print(“[http://maps.google.com/maps?q=loc:”);](http://maps.google.com/maps?q=loc:%22);)
sim808.print(latitude);
sim808.print(“,”);
sim808.print (longitude);
delay(200);
sim808.println((char)26); // End AT command with a ^Z, ASCII code 26
delay(200);
sim808.println();
delay(20000);
sim808.flush();

} else {
Serial.println(“GPS Initializing…”);
}
}

void sendTabData(String command , const int timeout , boolean debug){

sim808.println(command);
long int time = millis();
int i = 0;

while((time+timeout) > millis()){
while(sim808.available()){
char c = sim808.read();
if (c != ‘,’) {
data[i] +=c;
delay(100);
} else {
i++;
}
if (i == 5) {
delay(100);
goto exitL;
}
}
}exitL:
if (debug) {
state = data[1];
timegps = data[2];
latitude = data[3];
longitude =data[4];
}
}
String sendData (String command , const int timeout ,boolean debug){
String response = “”;
sim808.println(command);
long int time = millis();
int i = 0;

while ( (time+timeout ) > millis()){
while (sim808.available()){
char c = sim808.read();
response +=c;
}
}
if (debug) {
Serial.print(response);
}
return response;
}

The response String is initially set to ""
Then you add characters to it
Then you print it
but you never set it back to "" before adding new characters to it

Please can you give me a sample of what you want me to do to effect the change

Looking more closely at your code it is very confusing

The problem is not with the response String after all but I cannot immediately untangle what is going on and it is not helped by your use of goto

Where exactly in the code does it send the SMS and what data is being sent ?

I deliberately did not include the number I am sending to because this code is going public. The message sends under the void loop section after printing the coordinates. Look at the AT+CMGS command.

The data that you send is held in the data array. For instance, the latitude is held in data[3]

In the sendTabData() function you read characters and add them to the levels in the data array but I don't see where you ever remove the characters previously saved to the data array before adding a new set

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.