未接收到 GPS 数据

Not receiving GPS data

#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[=10=]", 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] = '[=10=]';
        Serial.print(buffer);
    }
    */

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

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

这是我用来检索 GPS 数据的代码,我将这些数据发送到 MySQL 服务器。但是我没有从 GPS 模块接收到任何数据。

我确定该模块可以正常工作,因为我分别尝试了 GPS 和 TCP 代码,它们运行良好。

谁能告诉我哪里做错了。

如果你使用两个串行端口(端口我指的是一组串行引脚)会更好,一个用于计算机(Serial),一个用于 GPS 模块(Say Serial 1),假设微控制器是 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.

祝你在项目中一切顺利。