我正在构建一个GPS跟踪器,该跟踪器将位置数据发送到带有蓝牙的android并在终端中显示信息。我正在尝试使用TinyGPS ++库将数据解析为更易读的文本。我可以在显示器和蓝牙终端中打印NEO 6M原始数据,但是使用TinyGPSPlus显示数据时我只能得到0值。我想查看位置每5秒更新一次。如何每隔5秒钟在Bluetooth终端中打印一次位置?
此代码在蓝牙终端和显示器上的输出为:
LONG = 0.000
ALT = 0.000
SATs = 0
这是我的代码:
#include "BluetoothSerial.h"
#include <TinyGPS++.h>
BluetoothSerial ESP_BT;
TinyGPSPlus gps;
#define GPS_RXD2 16
#define GPS_TXD2 17
char datGPS=' ';
int incoming;
int OUTLED = 2;
void setup() {
Serial.begin(9600);
Serial2.begin(9600, SERIAL_8N1, GPS_RXD2, GPS_TXD2);
Serial.println("Serial Txd is on pin: "+String(GPS_TXD2));
Serial.println("Serial Rxd is on pin: "+String(GPS_RXD2));
ESP_BT.begin("BLE-GPS-Tracker"); //Name of your Bluetooth Signal
Serial.println("Bluetooth Device is Ready to Pair");
pinMode (OUTLED, OUTPUT);
}
void loop() {
while (Serial2.available()) {
datGPS = char(Serial2.read());
Serial.print(datGPS);
ESP_BT.print(datGPS);
gps.encode(Serial2.read());
static unsigned long lastTime = 0;
if (millis() - lastTime >= 5000) {
lastTime = millis();
Serial.print("LAT="); Serial.println(gps.location.lat(), 3);
Serial.print("LONG="); Serial.println(gps.location.lng(), 3);
Serial.print("ALT="); Serial.println(gps.altitude.meters());
Serial.print("SATs="); Serial.println(gps.satellites.value());
ESP_BT.print("LAT="); ESP_BT.println(gps.location.lat(), 3);
ESP_BT.print("LONG="); ESP_BT.println(gps.location.lng(), 3);
ESP_BT.print("ALT="); ESP_BT.println(gps.altitude.meters());
ESP_BT.print("SATs="); ESP_BT.println(gps.satellites.value());
}
}
if (ESP_BT.available())
{
incoming = ESP_BT.read();
Serial.print("Received:");
Serial.println(incoming);
if (incoming == 49)
{
digitalWrite(OUTLED, HIGH);
ESP_BT.println("LED turned ON");
}
if (incoming == 48)
{
digitalWrite(OUTLED, LOW);
ESP_BT.println("LED turned OFF");
}
if (Serial2.available())
{
ESP_BT.println(char(Serial2.read()));
}
}
delay(5);
}