1

I have created a GPS logger code using DFRobot SIM808 library. The tcp code and getGPS codes are working fine. but when integrating the both the code, it failed to upload GPS data to thingspeak. It gets the IP connect to the server, but the get command is not working; and also the GPS data shown as a string. Can anyone please tell me where the problem is?

/*
### Connect TCP and send GET request.
1. This example is used to test DFRobot_SIM808 GPS/GPRS/GSM Shield's connect TCP and send GET request.
2. Open the SIM808_TCPConnection example or copy these code to your project
3. Download and dial the function switch to Arduino
4. Open serial helper
5. Waiting for a few minutes, until serial has sent "Connect mbed.org success"
6. Serial will send "Hello world!"

create on 2016/09/23, version: 1.0
by jason
*/
#include <DFRobot_sim808.h>
#include <SoftwareSerial.h>

#define PIN_TX    10
#define PIN_RX    11
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];

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

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

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

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


  //************* Turn on the GPS power************
  if( sim808.attachGPS())
      Serial.println("Open the GPS power success");
  else 
      Serial.println("Open the GPS power failure");
  delay(2000);  
}

void loop(){
//  gps();
  delay(2000);
  tcp();
  delay(2000);
}

void tcp(){
    //*********** Establish a TCP connection ************
  if(!sim808.connect(TCP,"api.thingspeak.com", 80)) {
      Serial.println("Connect error");
  }else{
      Serial.println("Connect mbed.org success");
  }

  //*********** Send a GET request *****************
    float lati=sim808.GPSdata.lat;
    float longi=sim808.GPSdata.lon;

    char http_cmd[100];
    sprintf(http_cmd, "GET https://api.thingspeak.com/update?api_key=FF19NVWMPZEWX0XK&field2=%s&field3=%s HTTP/1.0\r\n\r\n\0", lati, longi);

    boolean retValue = true;
    sim808.send(http_cmd, strlen(http_cmd));
    int ret = sim808.recv(buffer, sizeof(buffer)-1);
    if (ret <= 0){
        Serial.println("error receiving");
        retValue = false;
    }
    buffer[ret] = '\0';
    Serial.print(buffer);


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

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

//void gps() {
//   //************** Get GPS data *******************
//   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);
//    Serial.print("latitude :");
//    Serial.println(sim808.GPSdata.lat, 6);
//    Serial.print("longitude :");
//    Serial.println(sim808.GPSdata.lon, 6);
//    Serial.print("speed_kph :");
//    Serial.println(sim808.GPSdata.speed_kph);
//    Serial.print("heading :");
//    Serial.println(sim808.GPSdata.heading);
//    Serial.println();
//
//    //************* Turn off the GPS power ************
//    sim808.detachGPS();
//  }
//}
1

First, I would assume you need to call sim808.getGPS() to update the GPS data. Not sure why you commented that out.

Normally, when building a GET command, you don't include the url (unless going through a proxy).

Last, you need to use %f with sprintf to format a floating point value.

Something like this might work:

void tcp()
{
    // Update the GPS data
    float lati, longi;
    if (sim808.getGPS()) {
        lati = sim808.GPSdata.lat;
        longi = sim808.GPSdata.lon;
        sim808.detachGPS();
    }
    else {
        // No gps, abort
        return;
    }

    //*********** Establish a TCP connection ************

    if (!sim808.connect(TCP,"api.thingspeak.com", 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 /update?api_key=FF19NVWMPZEWX0XK&field2=%f&field3=%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();
}
  • Thanks a lot, man! I 'll check this and let you know! Thanks again! – heshan deeyagaha Jul 8 '17 at 13:49

Your Answer

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

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