0
#include <DFRobot_sim808.h>
#include <SoftwareSerial.h>

//#define PIN_TX    1
//#define PIN_RX    0
//SoftwareSerial mySerial(PIN_TX,PIN_RX);
//DFRobot_SIM808 sim808(&mySerial);//Connect RX,TX,PWR,

//make sure that the baud rate of SIM900 is 9600!
//you can use the AT Command(AT+IPR=9600) to set it through SerialDebug

DFRobot_SIM808 sim808(&Serial);

char buffer[512];
int i = 0;

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

  //******** Initialize sim808 module *************
  while(!sim808.init()) {
      delay(1000);
      Serial.print("Sim808 init error\r\n");
  }
  delay(3000);

  //*********** Attempt DHCP *******************
  while(!sim808.join(F("mobitel3g"))) {
      Serial.println("Sim808 join network error");
      delay(2000);
  }

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


  //************* Turn on the GPS power************
    while(!sim808.attachGPS()) {
      delay(1000);
      Serial.print("Open the GPS power failure\r\n");
  }

  delay(3000);  
  Serial.println("Open the GPS power success");

for(int i = 0; i<=40;i++)
{
  for(int y = 0;y<=i;y++)
  {
  Serial.print(".");
  delay(60);
  }
 Serial.println("");
}
}

void loop(){
  tcp();      
  delay(2000);

  //Calls the function every 2 seconds
}


void tcp()
{
    // Update the GPS data
    float lati, longi;
    if (sim808.getGPS()) {

      Serial.print(sim808.GPSdata.year);
    Serial.print("/");
    Serial.print(sim808.GPSdata.month);
    Serial.print("/");
    Serial.print(sim808.GPSdata.day);
    Serial.print(" ");
    Serial.print(sim808.GPSdata.hour);
    Serial.print(":");
    Serial.print(sim808.GPSdata.minute);
    Serial.print(":");
    Serial.print(sim808.GPSdata.second);
    Serial.print(":");
    Serial.println(sim808.GPSdata.centisecond);

        lati = sim808.GPSdata.lat,6;
        longi = sim808.GPSdata.lon,6;

        //print the variables 
        Serial.print("LAT =");
        Serial.println(lati);
        Serial.print("LON =");
        Serial.println(longi);
        sim808.detachGPS();
    }
    else {
        // No gps, abort
        Serial.println("No GPS");
        return;
    }
    //*********** Establish a TCP connection ************

    if (!sim808.connect(TCP,"http://syntiq.lk", 80)) {
       Serial.println("Connect error");
       return;
    }
    else {
       Serial.println("Connect mbed.org success");
    }

   //*********** Send a GET request *****************

    char http_cmd[100];
    sprintf(http_cmd, "GET /GPS/Addtoserver.php?data2=%f&data1=%f HTTP/1.0\r\n\r\n\0", lati, longi);
    sim808.send(http_cmd, strlen(http_cmd));

    /*
    int ret = sim808.recv(buffer, sizeof(buffer) - 1);
    if (ret <= 0){
        Serial.println("error receiving");
    }
    else {
        buffer[ret] = '\0';
        Serial.print(buffer);
    }
    */

    //************* Close TCP or UDP connections **********
    sim808.close();

    //*** Disconnect wireless connection, Close Moving Scene *******
    sim808.disconnect();
}

This is the code I have used to retrieve GPS data and I send these data to a MySQL server. But I'm not receiving any data from the GPS module.

I'm sure that the module is working because I tried both GPS and TCP codes individually and they worked well.

Can someone please tell me where I went wrong.

2
  • You can't use the same serial interface for both SIM808 and PC communication.
    – gre_gor
    Jan 29, 2019 at 17:43
  • @gre_gor Sorry i didn't get it. Do you mean that I've to use 2 sets of RX and TX pins? Jan 30, 2019 at 2:36

1 Answer 1

0

It will be better if you use two serial ports (By port I mean a set of serial pins), one for the computer (Serial) and one for the GPS module (Say Serial 1), that is assuming the microcontroller is an Arduino Mega. But if it is an Arduino Uno or any other Arduino with one serial port, then you need to use Software Serial. Software Serial comes with examples in the Arduino IDE by default or you can just go here.

I wish you all the best in your project.

2
  • First thank u for your best wishes @race_max. Yes I'm using a arduino mega. If it's not too much trouble can you point me to a good tutorial coz I tried to find some and couldn't find that much help. Feb 1, 2019 at 15:05
  • @MalinkaJayasinghe You can try this one
    – race_max
    Feb 2, 2019 at 21:30

Your Answer

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

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