0

I am trying to get GPS data and pass it through GSM (Http Get Request). The GPS codes part works fine alone, also HTTPRequest part lonely. But when i use them together , the module don't get GPS data to send with GSM! I've commented parts for more understanding.

//Include Libraries
#include <DFRobot_sim808.h>
#include <SoftwareSerial.h>

//Define module connect pins
#define PIN_TX    12
#define PIN_RX    13
SoftwareSerial mySerial(PIN_TX,PIN_RX);
DFRobot_SIM808 sim808(&mySerial);//Connect RX,TX,PWR,

//store response from website
char buffer[256];

//store Http request
char datas[83];

//store GPS latitude
float lat;

//store GPS longitude
float lon;

//store GPS latitude as text
char la[8];

//store GPS longitude as text
char lo[8];

void setup() {
  mySerial.begin(9600);
  Serial.begin(9600);  

//Temporary for prevent of connection error from network! added now
  delay(2000);
  sim808.close();
  sim808.disconnect();
  sim808.detachGPS();
  delay(2000);

  while(!sim808.init()) { 
      Serial.print("Sim808 init error\r\n");
    }
    Serial.println("Sim808 init success");

  //************* Turn on the GPS power************
  if( sim808.attachGPS())
      Serial.println("Open the GPS power success");
  else 
      Serial.println("Open the GPS power failure");
 while(!sim808.join(F("mcinet"))) {
      Serial.println("join network error");
  }
  Serial.println("join network success");

  //************ Successful DHCP ****************
  Serial.print("IP Address is ");
  Serial.println(sim808.getIPAddress());

  //*********** Establish a TCP connection ************
  while(!sim808.connect(TCP,"332b8a09.ngrok.io", 80)) {
      Serial.println("Connect error");
  }

}

//this function get latitude and longitude and put them in variable (datas) for http request

void http(float loni, float lati){
    dtostrf(lati, 6, 6, la);
    dtostrf(loni, 6, 6, lo);

    strcpy(datas,"GET /index.php?lat=");
    strcat(datas, la);
    strcat(datas, "&long=");
    strcat(datas, lo);
    strcat(datas," HTTP/1.1\r\nHost: 332b8a09.ngrok.io\r\n\r\n\r\n\r\n");
}

void loop() {
    if(sim808.getGPS()) {
//get GPS data
    lat = sim808.GPSdata.lat;
    lon = sim808.GPSdata.lon;

//send GPS data to http function for put them into http request
    http(lat,lon);

    //*********** Send a GET request *****************
    Serial.println("waiting to fetch...");
    sim808.send(datas, sizeof(datas));
    while (true) {
        int ret = sim808.recv(buffer, sizeof(buffer)-1);
        buffer[ret] = '\0';
        Serial.print("Recv: ");
        Serial.print(ret);
        Serial.print(" bytes: ");
        Serial.println(buffer);
        break;
      }
    }
    delay(3000);
    Serial.println("Another Attemp...");
}

I Use sim808 EVB-V.3.2.4 module and Arduino Uno.

And this is the serial prints of program:

Sim808 init success
Open the GPS power success
join network success
IP Address is
102.168.24.54
Another Attemp...
Another Attemp...
Another Attemp...
Another Attemp...
Another Attemp...
.
.
.
And goes on Infinity Another Attemp...

I must get printed string : "waiting to fetch..." and some other strings before each "Another Attemp..."

  • thanks. i will do that and will comment the result – Pooya Behravesh Sep 15 at 20:05
  • well, any of that ways not worked! the gps lat long is 00.00000 – Pooya Behravesh Sep 15 at 21:26
  • start with the GPS example code ... make sure that it works as expected ... then add the GSM code in sections ... test after each addition ... start by adding all of the declarations before setup() .... then add program lines to setup() one at a time .... that should reveal what causes your code to break – jsotola Sep 15 at 21:49
  • Omg this is continuing on not working! step by step, but fail! – Pooya Behravesh Sep 16 at 10:26
  • what are you doing? .... you don't continue adding code when it fails .... you try to figure out why the added code made it fail ... so, what made the GPS example code fail? – jsotola Sep 16 at 14:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.