0

I want to extract longitude and latitude from GSM/GPS module and then upload it to thingspeak. I am using sim808 GSM/GPS module and AT+CGPSINF=0 to get GPS location information sentence (CGPSINF: 0,6.823375,80.001038,36.300000,20170705124133.000,0,10,0.037040,47.410000 ). I want to extract longitude and latitude from it. Does anyone know how to do it? I am not an expert in programming. someone told me to use strtok. But I don't know how to do it. Does anyone have any Arduino code to do this?

I tried DFRobot SIM808 library to collect GPS data. it works like charm. But the problem was, my upload code didn't work when i was using the library to collect longitude and latitude in the same code.

This is my web upload code.

#include <SoftwareSerial.h>
#include <String.h>

SoftwareSerial mySerial(10, 11);//(2,3)


void setup()
{
  mySerial.begin(2400);   // the GPRS baud rate 9600  
  Serial.begin(2400);    // the GPRS baud rate 9600

  delay(1000);

  ShowSerialData();

}

void loop()
{
      Send2Pachube();
  if (mySerial.available())
    Serial.write(mySerial.read());
}
void Send2Pachube()
{
  mySerial.println("AT");//start with attention
  delay(1000);

  mySerial.println("AT+CPIN?");//check for the sim card
  delay(1000);

  mySerial.println("AT+CREG?");//check for registration and access technology of cell
  delay(1000);

  mySerial.println("AT+CGATT?");//check whether the device is attached to GPRS.0Detch,1Attch
  delay(1000);

  mySerial.println("AT+CIPSHUT");//shut packet data protocol context
  delay(1000);

  mySerial.println("AT+CIPSTATUS");// returns the current connection status
  delay(2000);

  mySerial.println("AT+CIPMUX=0"); //create multi-IP connection(0=single connection)
  delay(2000);

  ShowSerialData();

  mySerial.println("AT+CSTT=\"hutch3g\"");//start task and setting the APN,
  delay(1000);

  ShowSerialData();

  mySerial.println("AT+CIICR");//bring up wireless connection
  delay(3000);
  ShowSerialData();

  mySerial.println("AT+CIFSR");//get local IP adress
  delay(2000);
  ShowSerialData();



  mySerial.println("AT+CGPSINF=0");//get GPS
  delay(2000);
  ShowSerialData();

  mySerial.println("AT+CIPSPRT=0");//starts a TCP or UDP connection
  delay(3000);

  ShowSerialData();

  mySerial.println("AT+CIPSTART=\"TCP\",\"api.thingspeak.com\",\"80\"");//start up the connection
  delay(6000);

  ShowSerialData();

  mySerial.println("AT+CIPSEND");//begin send data to remote server
  delay(4000);
  ShowSerialData();

  String str="GET https://api.thingspeak.com/update?api_key=FF19NVWMPZEWX0XK&field1=100";
  mySerial.println(str);//begin send data to remote server
  delay(4000);
  ShowSerialData();

  mySerial.println((char)26);//sending
  delay(5000);//waitting for reply, important! the time is base on the condition of internet 
  mySerial.println();

  ShowSerialData();

  mySerial.println("AT+CIPSHUT");//close the connection
  delay(100);
  ShowSerialData();
} 
void ShowSerialData()
{
  while(mySerial.available()!=0)
    Serial.write(mySerial.read());
}
1
  • Since the library works in getting the data, maybe you should focus on getting that to work with your code.
    – 001
    Jul 5, 2017 at 18:39

1 Answer 1

1

First rather than just dumping it to the screen store the data in a char array

char dataBuffer[64];
int dataCount=0;

while ( (mySerial.available()!=0) && (dataCount<63) ) {
    dataBuffer[dataCount] = mySerial.read());
    dataCount++;
}
dataBuffer[dataCount] = 0;

then use strtok to split the string on the : and , characters

char *output;
output = strtok(dataBuffer,":");
// output will now point to everything up to the first : so "CGPSINF" for gps
int field = 0;
while (output != null) {
  field++;
  output = strtok(null,",");
// output will now point to each value in turn
// for CGPSINF:0,6.823375,80.001038,36.300000,
// when field == 1 output=="0"
// when field == 2 output=="6.823375" etc...
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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