【问题标题】:There are no GPS data after connecting to the server连接服务器后没有GPS数据
【发布时间】:2018-07-10 03:10:00
【问题描述】:

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

但是当我加入获取 GPS 数据并发送数据时遇到了麻烦。
另外,这些功能可以正常工作。我如何启动 A7,有一个循环,我输入 AT 命令并将其发送到 A7。关于so​​cket,我想在启动功能时设置连接并一直保持活动状态。

但问题是,当我初始化 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");
}

【问题讨论】:

    标签: arduino gps arduino-uno at-command


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-12
      • 1970-01-01
      • 2013-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-24
      • 1970-01-01
      相关资源
      最近更新 更多