代码:
全选/*
GPS Posi
tion Logger
gps-position-logger.ino
Read GPS data from BN-220 or any serial GPS sensor
Requires TinyGPS++ Library
Save to SD or microSD card
DroneBot Workshop 2021
*/
// Include required libraries
#include
#include
#include
#include
// GPS Connections
static const int RXPin = 4, TXPin = 3;
// GPS Baud rate (change if required)
static const uint32_t GPSBaud = 9600;
// SD Card Select pin
const int chipSelect = 8;
// Write LED
//const int recLED = 7;
// String to hold GPS data
String gpstext;
// GPS write delay counter variables
// Change gpsttlcount as required
int gpscount = 0;
int gpsttlcount = 30;
// TinyGPS++ object
TinyGPSPlus gps;
// SoftwareSerial connection to the GPS device
SoftwareSerial ss(RXPin, TXPin);
void setup()
{
// Set LED pin as output
//pinMode(recLED, OUTPUT);
// Start Serial Monitor for debugging
Serial.begin(115200);
// Start SoftwareSerial
ss.begin(GPSBaud);
// Initialize SD card
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
//don't do anything more:
while (1);
}
Serial.println("card initialized.");
// Blink LED so we know we are ready
// digitalWrite(recLED, HIGH);
// delay(500);
// digitalWrite(recLED, LOW);
// delay(500);
// digitalWrite(recLED, HIGH);
// delay(500);
// digitalWrite(recLED, LOW);
// delay(500);
// digitalWrite(recLED, HIGH);
// delay(500);
// digitalWrite(recLED, LOW);
}
void loop()
{
// Turn off LED
// digitalWrite(recLED, LOW);
// See if data available
while (ss.available() > 0)
if (gps.encode(ss.read()))
// See if we have a complete GPS data string
if (displayInfo() != "0")
{
// Get GPS string
gpstext = displayInfo();
// Check GPS Count
Serial.println(gpscount);
if (gpscount == gpsttlcount) {
// LED On to indicate data to write to SD card
// digitalWrite(recLED, HIGH);
//Open the file on card for writing
File dataFile = SD.open("gpslog.csv", FILE_WRITE);
if (dataFile) {
// If the file is available, write to it and close the file
dataFile.println(gpstext);
dataFile.close();
// Serial print GPS string for debugging
Serial.println(gpstext);
}
// If the file isn't open print an error message for debugging
else {
Serial.println("error opening datalog.txt");
}
}
// Increment GPS Count
gpscount = gpscount + 1;
if (gpscount > gpsttlcount) {
gpscount = 0;
}
}
}
// Function to return GPS string
String displayInfo()
{
// Define empty string to hold output
String gpsdata = "";
// Get latitude and longitude
if (gps.location.isValid())
{
gpsdata = String(gps.location.lat(), 6);
gpsdata += (",");
gpsdata += String(gps.location.lng(), 6);
gpsdata += (",");
}
else
{
return "0";
}
// Get Date
if (gps.date.isValid())
{
gpsdata += String(gps.date.year());
gpsdata += ("-");
if (gps.date.month() < 10) gpsdata += ("0");
gpsdata += String(gps.date.month());
gpsdata += ("-");
if (gps.date.day() < 10) gpsdata += ("0");
gpsdata += String(gps.date.day());
}
else
{
return "0";
}
// Space between date and time
gpsdata += (" ");
// Get time
if (gps.time.isValid())
{
if (gps.time.hour() < 10) gpsdata += ("0");
gpsdata += String(gps.time.hour());
gpsdata += (":");
if (gps.time.minute() < 10) gpsdata += ("0");
gpsdata += String(gps.time.minute());
gpsdata += (":");
if (gps.time.second() < 10) gpsdata += ("0");
gpsdata += String(gps.time.second());
}
else
{
return "0";
}
// Return completed string
return gpsdata;
}
你用的是哪个GPS模块?
什么不起作用?
所以我上传了上面显示的代码,在我将我的 gps 和 sd 卡读卡器连接到它之后,我从串行监视器中得到了这个……gps 是一个 gt-u7,一个非常常见的中国仿制品。当我通过 USB 插入 gps 并打开串行监视器时,我得到了很好的 gps 语句。
20:54:19.498 ->
3,第一个原因:4,引导模式:(3,0)
20:54:19.498 ->
20:54:19.498 -> wdt 重置
20:54:19.498 -> 加载 0x4010f000,len 3460 , room 16
20:54:19.498 -> tail 4
20:54:19.498 -> chksum 0xcc
20:54:19.498 -> load 0x3fff20b8, len 40, room 4
20:54:19.498 -> tail 4
20:54:19.531 -> 校验和 0xc9
20:54:19.531 -> csum 0xc9
20:54:19.531 -> v0004c7a0
20:54:19.531 ->