1

I'm trying to get GPS coordinates and send them to my API through the GPRS SIM900 and GPS NEO-6M shields.

In setup(), I first start the GPRS shield and call the function that connects to the internet. Then in loop(), I start to capture GPS coordinates and finally I send to my API, but my code doesn't work with this logic. I'd like to connect once and get GPS coordinates and send them to the API many times making a tracking.

The app stops if I run the code. connectToInternet() in setup() and getGpsPosition() in loop(). The SIM900 shield connects but the NEO6-M shield does not display the coordinates. The GPS only captures the coordinates if I do not connect to the internet.

Could someone help me? Following is my code and printout of the response to AT commands.

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

static const int RXPin = 4, TXPin = 3;
InetGSM inet;
TinyGPSPlus gps;
SoftwareSerial serialGps(RXPin, TXPin);
char msg[50];
boolean started = false;
const char* dataCoord = "";

void setup() {
  // Serial connection.
  Serial.begin(19200);
  Serial.print(F("------------------------------------------------- \r\n"));
  Serial.println(F("Starting App Tracking Arduino"));
  Serial.print(F("------------------------------------------------- \r\n"));
  // Turn on GPRS
  powerUpGprs();
  // Start configuration of shield SIM900 with baudrate.
  if (gsm.begin(2400)) {
    Serial.println(F("\nstatus=READY"));
    started = true;
  } else Serial.println(F("\nstatus=IDLE"));
  if (started)
    connectToInternet();
  // Start configuration of shield GPS NEO-6M with baudrate.
  serialGps.begin(9600);
}

void loop() {
  // This sketch displays information every time a new sentence is correctly encoded.
  while (serialGps.available() > 0)
    if (gps.encode(serialGps.read())) getGpsPosition();
}

/**
  Power up
*/
void powerUpGprs() {
  pinMode(9, OUTPUT);
  delay(100);
  digitalWrite(9, HIGH);
  Serial.println(F("\nPower Up SIM900!"));
}

/**
  Connect to Internet via GSM
*/
void connectToInternet() {
  Serial.print(F("\r\n"));
  Serial.print(F("------------------------------------------------- \r\n"));
  Serial.print(F(" CONNECT TO INTERNET \r\n"));
  Serial.print(F("------------------------------------------------- \r\n"));
  //GPRS attach, put in order APN, username and password.
  if (inet.attachGPRS("zap.vivo.com.br", "vivo", "vivo"))
    Serial.println(F("status=ATTACHED"));
  else
    Serial.println(F("status=ERROR"));
  delay(1000);
  //Read IP address.
  gsm.SimpleWriteln("AT+CIFSR");
}


/**
  Get GPS position
*/
void getGpsPosition() {
  Serial.print(F("\r\n\r\n"));
  Serial.print(F("------------------------------------------------- \r\n"));
  Serial.print(F(" GET POSITON \r\n"));
  Serial.print(F("------------------------------------------------- \r\n"));
  // geolocation
  TinyGPSDate &d = gps.date;
  TinyGPSTime &t = gps.time;
  char* timestamp;
  if (!d.isValid()) {
    timestamp = "00/00/0000 00:00:00";
  } else {
    char sz[32];
    sprintf(sz, "%02d/%02d/%02d %02d:%02d:%02d",
      d.day(), d.month(), d.year(),
      t.hour(), t.minute(), t.second());
    timestamp = sz;
  }
  String position = String("");
  position.concat(String("timestamp=") + String(timestamp));
  position.concat(String("&latitude=") + String(gps.location.lat(), 6));
  position.concat(String("&longitude=") + String(gps.location.lng(), 6));
  position.concat(String("&altitude=") + String(gps.altitude.meters()));
  position.concat(String("&speed=") + String(gps.speed.kmph()));
  Serial.println(dataCoord);
  send2Api();
}

/**
  Send To API, The function inet.httpPOST(),
  this is submit method POST via http request
*/
void send2Api() {
  Serial.print(F("\r\n\r\n"));
  Serial.print(F("------------------------------------------------- \r\n"));
  Serial.print(F(" SEND TO API\r\n"));
  Serial.print(F("------------------------------------------------- \r\n"));
  // Read until serial buffer is emapty.
  gsm.WhileSimpleRead();
  // TCP Client HTTP, send a POST request to the server and save the reply.
  int numdata = inet.httpPOST("api.myapp.com.br", 80, "/geolocations ", dataCoord, msg, 100);
  // Print the results.
  Serial.print(F("\nNumber of data received: "));
  Serial.println(numdata);
  Serial.print(F("\nData received: "));
  Serial.println(msg);
};

If I connect to internet the GPS does not display the coordinates.

Run app with  connectToInternet() and getGpsPosition

But if I don't connect to internet the GPS displays the coordinates.

connectToInternet() function omitted

2

You are using 2 SoftwareSerial instances and you're losing bytes when you aren't listening to the other port. Also your inetGSM library uses pins 3 for RX with the SIM900 and in your Arduino sketch, pin 3 is also the Arduino TX pin to the GPS module. Therefore, anything sent by the Arduino over pin 3 is received by both modules. Confusion.

  • Use the GPS module with another pair of pins, maybe, 7 for RX and 8 for TX. Connect the GPS TX to 7 and its RX to 8.
  • Try adding: gsm._cell.listen(); right before this line:

    // Read until serial buffer is empty. gsm.WhileSimpleRead();

  • Also add serialGps.listen(); right before this line:

    // Print the results. Serial.print(F("\nNumber of data received: "));

Refer to this answer for more details.

protected by Community Aug 9 '18 at 0:06

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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