连接到服务器后没有GPS数据

时间:2018-07-08 13:04:55

标签: arduino gps arduino-uno at-command

我正在尝试获取GPS数据并将其通过套接字发送到服务器。
我正在使用Arduino Uno和GPS / GSM模块A7 Ai-Thinker。
对于Internet,我使用连接到Wi-Fi路由器的以太网屏蔽。

但是当我加入获取GPS数据并发送数据时遇到了麻烦。
另外,功能正常。我如何启动A7,存在一个循环,在其中输入AT命令并将其发送到A7。关于套接字,我想在启动功能上设置连接并一直保持连接状态。

但是问题是,当我初始化A7并设置套接字连接时,我进入了循环功能,并且在第一次迭代之后,A7上没有任何数据。
如果我尝试先设置与服务器的连接,然后再启动它,则A7不会响应AT命令。

如何保持与服务器的连接并且不失去与A7的连接?

代码:

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

#include <SPI.h>
#include <Ethernet.h>

//-------------------------------------------------------
static const int RXPin = 10, TXPin = 11;
static const uint32_t GPSBaud = 4800;
const String START_GPS = "START_GPS";

TinyGPSPlus gps;
SoftwareSerial gps_serial(RXPin, TXPin);

//-------------------------------------------------------
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
char server[] = "192.168.0.100";
int port = 8090;

IPAddress ip(192, 168, 0, 177);
EthernetClient client;
//-------------------------------------------------------

void initGpsConnection();
void startGps();

void initSocketConnection();
void sendData();


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

  initSocketConnection();
  initGpsConnection();

  sendData();
}

void loop()
{
  while (gps_serial.available() > 0) {
    if (gps.encode(gps_serial.read()))  {
      displayInfo();
      //delay(1000);
    }
  }

  if (millis() > 120000 && gps.charsProcessed() < 10)
  {
    Serial.println(F("No GPS detected: check wiring."));
    //while(true);
  }

  if (!client.connected()) {
    Serial.println();
    Serial.println("Socket connection has been lost.");
    client.stop();

    //while (true);
  }
}

void displayInfo()
{
  Serial.print(F("Location: ")); 
  if (gps.location.isValid())
  {
    Serial.print(gps.location.lat(), 6);
    Serial.print(F(","));
    Serial.print(gps.location.lng(), 6);
  }
  else
  {
    Serial.print(F("INVALID"));
  }

  Serial.print(F("  Date/Time: "));
  if (gps.date.isValid()) {
    Serial.print(gps.date.month() + "/" gps.date.day() + "/" + gps.date.year();
  } else {
    Serial.print(F("INVALID"));
  }

  Serial.print(F(" "));
  if (gps.time.isValid()) { 
    Serial.print(gps.time.hour() + ":" + gps.time.minute() + ":" + gps.time.second());
  } else {
    Serial.print(F("INVALID"));
  }
  Serial.println();
}



void initGpsConnection() {
  Serial.println("In init gps");
  gps_serial.begin(GPSBaud);

  Serial.println("after gps begin");
  delay(1000);
  startGps();

  Serial.println("GPS connection has been set");
}


void startGps() {
  Serial.println("Enter at commands, after that enter `start_gps`");
  String response = "";

  while (true) {
    if (gps_serial.available()) {
      Serial.write(gps_serial.read());
    }
    if (Serial.available()) {
      char c = Serial.read();
      response += c;

      if (response.indexOf(START_GPS) > 0)
        break;

      gps_serial.write(c);
    }
  }
}

void initSocketConnection() {
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    Ethernet.begin(mac, ip);
  }

  delay(1000);
  Serial.println("connecting...");

  if (client.connect(server, port)) {
    Serial.println("Connection with server has been set");
  } else {
    Serial.println("Connection with server has been failed");
  }
}


void sendData(){
  client.print("TEst ard");
}

1 个答案:

答案 0 :(得分:0)

问题出在A7连接处。它通过Ethernet shield处的10、11针连接。但是A7保留10-13个引脚。只需连接A7 vie 8、9或其他引脚即可。