驴友花雕
发表于 2022-5-25 12:59:43
(2)实验串口返回情况
驴友花雕
发表于 2022-5-27 13:05:57
程序六:静态解码NMEA数据流的示例(1)Arduino参考开源代码
/*
【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
程序六:静态解码NMEA数据流的示例
*/
#include <TinyGPS++.h>//导入驱动库
#include <TinyGPSPlus.h>
// 一个示例 NMEA 流。
const char *gpsStream =
"$GPRMC,045103.000,A,3014.1984,N,09749.2872,W,0.67,161.46,030913,,,A*7C\r\n"
"$GPGGA,045104.000,3014.1985,N,09749.2873,W,1,09,1.2,211.6,M,-22.5,M,,0000*62\r\n"
"$GPRMC,045200.000,A,3014.3820,N,09748.9514,W,36.88,65.02,030913,,,A*77\r\n"
"$GPGGA,045201.000,3014.3864,N,09748.9411,W,1,10,1.2,200.8,M,-22.5,M,,0000*6C\r\n"
"$GPRMC,045251.000,A,3014.4275,N,09749.0626,W,0.51,217.94,030913,,,A*7D\r\n"
"$GPGGA,045252.000,3014.4273,N,09749.0628,W,1,09,1.3,206.9,M,-22.5,M,,0000*6F\r\n";
// TinyGPSPlus 对象
TinyGPSPlus gps;
void setup(){
Serial.begin(115200);
Serial.println(F("BasicExample.ino"));
Serial.println(F("TinyGPSPlus 的基本演示(无需设备)"));
Serial.print(F("测试 TinyGPSPlus 库 v. ")); Serial.println(TinyGPSPlus::libraryVersion());
Serial.println(F("准备就绪"));
Serial.println();
while (*gpsStream)
if (gps.encode(*gpsStream++))
displayInfo();
Serial.println();
Serial.println(F("完成"));
}
void loop(){
}
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());
Serial.print(F("/"));
Serial.print(gps.date.day());
Serial.print(F("/"));
Serial.print(gps.date.year());
}
else
{
Serial.print(F("INVALID"));
}
Serial.print(F(" "));
if (gps.time.isValid())
{
if (gps.time.hour() < 10) Serial.print(F("0"));
Serial.print(gps.time.hour());
Serial.print(F(":"));
if (gps.time.minute() < 10) Serial.print(F("0"));
Serial.print(gps.time.minute());
Serial.print(F(":"));
if (gps.time.second() < 10) Serial.print(F("0"));
Serial.print(gps.time.second());
Serial.print(F("."));
if (gps.time.centisecond() < 10) Serial.print(F("0"));
Serial.print(gps.time.centisecond());
}
else
{
Serial.print(F("INVALID"));
}
Serial.println();
}
驴友花雕
发表于 2022-5-27 13:08:57
(2)实验串口返回情况
驴友花雕
发表于 2022-5-27 13:10:43
7、程序七:TinyGPSPlus 对象的常规使用 (1)Arduino参考开源代码
/*
【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
程序七:TinyGPSPlus 对象的常规使用
*/
#include <TinyGPSPlus.h>//导入驱动库
#include <SoftwareSerial.h>
static const int RXPin = 9, TXPin = 8;//定义软串口接脚
static const uint32_t GPSBaud = 9600;
// TinyGPSPlus 对象
TinyGPSPlus gps;
//与 GPS 设备的串行连接
SoftwareSerial ss(RXPin, TXPin);
void setup(){
Serial.begin(9600);//初始化串口
ss.begin(GPSBaud);
Serial.println(F("DeviceExample.ino"));
Serial.println(F("一个简单的 TinyGPSPlus 演示,带有 GPS 模块"));
Serial.print(F("测试 TinyGPSPlus 库 v."));
Serial.println(TinyGPSPlus::libraryVersion());
Serial.println(F("GY-NEO6MV2模块准备就绪"));
Serial.println();
}
void loop(){
//每次正确编码新数据时,此程序都会显示信息。
while (ss.available() > 0)
if (gps.encode(ss.read()))
displayInfo();
if (millis() > 5000 && gps.charsProcessed() < 10)
{
Serial.println(F("未检测到 GPS:检查接线。"));
while (true);
}
}
void displayInfo(){
Serial.print(F("位置: "));
if (gps.location.isValid())
{
Serial.print(gps.location.lat(), 6);
Serial.print(F(","));
Serial.print(gps.location.lng(), 6);
}
else
{
Serial.print(F("无效"));
}
Serial.print(F("日期/时间:"));
if (gps.date.isValid())
{
Serial.print(gps.date.month());
Serial.print(F("/"));
Serial.print(gps.date.day());
Serial.print(F("/"));
Serial.print(gps.date.year());
}
else
{
Serial.print(F("无效"));
}
Serial.print(F(" "));
if (gps.time.isValid())
{
if (gps.time.hour() < 10) Serial.print(F("0"));
Serial.print(gps.time.hour());
Serial.print(F(":"));
if (gps.time.minute() < 10) Serial.print(F("0"));
Serial.print(gps.time.minute());
Serial.print(F(":"));
if (gps.time.second() < 10) Serial.print(F("0"));
Serial.print(gps.time.second());
Serial.print(F("."));
if (gps.time.centisecond() < 10) Serial.print(F("0"));
Serial.print(gps.time.centisecond());
}
else
{
Serial.print(F("无效"));
}
Serial.println();
}
驴友花雕
发表于 2022-5-27 13:12:48
(2)实验串口返回情况
驴友花雕
发表于 2022-5-29 13:37:54
程序八:TinyGPSPlus 很多有趣功能的综合测试(1)Arduino参考开源代码
/*
【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
程序八:TinyGPSPlus 很多有趣功能的综合测试
*/
#include <TinyGPSPlus.h>//导入驱动库
#include <SoftwareSerial.h>
static const int RXPin = 9, TXPin = 8;//定义软串口接脚
static const uint32_t GPSBaud = 9600;
// TinyGPSPlus 对象
TinyGPSPlus gps;
//与 GPS 设备的串行连接
SoftwareSerial ss(RXPin, TXPin);
void setup() {
Serial.begin(9600);
ss.begin(GPSBaud);
Serial.println(F("FullExample.ino"));
Serial.println(F("TinyGPSPlus 很多有趣功能的综合测试"));
Serial.print(F("测试 TinyGPSPlus 库 v."));
Serial.println(TinyGPSPlus::libraryVersion());
Serial.println(F("GY-NEO6MV2模块准备就绪"));
Serial.println();
Serial.println(F("Sats HDOPLatitude Longitude FixDate Time Date Alt Course Speed CardDistance Course CardChars Sentences Checksum"));
Serial.println(F(" (deg) (deg) Age Age(m) --- from GPS -------- to London----RX RX Fail"));
Serial.println(F("----------------------------------------------------------------------------------------------------------------------------------------"));
}
void loop() {
static const double LONDON_LAT = 51.508131, LONDON_LON = -0.128002;
printInt(gps.satellites.value(), gps.satellites.isValid(), 5);
printFloat(gps.hdop.hdop(), gps.hdop.isValid(), 6, 1);
printFloat(gps.location.lat(), gps.location.isValid(), 11, 6);
printFloat(gps.location.lng(), gps.location.isValid(), 12, 6);
printInt(gps.location.age(), gps.location.isValid(), 5);
printDateTime(gps.date, gps.time);
printFloat(gps.altitude.meters(), gps.altitude.isValid(), 7, 2);
printFloat(gps.course.deg(), gps.course.isValid(), 7, 2);
printFloat(gps.speed.kmph(), gps.speed.isValid(), 6, 2);
printStr(gps.course.isValid() ? TinyGPSPlus::cardinal(gps.course.deg()) : "*** ", 6);
unsigned long distanceKmToLondon =
(unsigned long)TinyGPSPlus::distanceBetween(
gps.location.lat(),
gps.location.lng(),
LONDON_LAT,
LONDON_LON) / 1000;
printInt(distanceKmToLondon, gps.location.isValid(), 9);
double courseToLondon =
TinyGPSPlus::courseTo(
gps.location.lat(),
gps.location.lng(),
LONDON_LAT,
LONDON_LON);
printFloat(courseToLondon, gps.location.isValid(), 7, 2);
const char *cardinalToLondon = TinyGPSPlus::cardinal(courseToLondon);
printStr(gps.location.isValid() ? cardinalToLondon : "*** ", 6);
printInt(gps.charsProcessed(), true, 6);
printInt(gps.sentencesWithFix(), true, 10);
printInt(gps.failedChecksum(), true, 9);
Serial.println();
smartDelay(1000);
if (millis() > 5000 && gps.charsProcessed() < 10)
Serial.println(F("没有接收到 GPS 数据:检查接线"));
}
//这个自定义版本的 delay() 确保 gps 对象
//正在被“喂”有效的数据。
static void smartDelay(unsigned long ms)
{
unsigned long start = millis();
do
{
while (ss.available())
gps.encode(ss.read());
} while (millis() - start < ms);
}
static void printFloat(float val, bool valid, int len, int prec)
{
if (!valid)
{
while (len-- > 1)
Serial.print('*');
Serial.print(' ');
}
else
{
Serial.print(val, prec);
int vi = abs((int)val);
int flen = prec + (val < 0.0 ? 2 : 1); // . and -
flen += vi >= 1000 ? 4 : vi >= 100 ? 3 : vi >= 10 ? 2 : 1;
for (int i = flen; i < len; ++i)
Serial.print(' ');
}
smartDelay(0);
}
static void printInt(unsigned long val, bool valid, int len)
{
char sz = "*****************";
if (valid)
sprintf(sz, "%ld", val);
sz = 0;
for (int i = strlen(sz); i < len; ++i)
sz = ' ';
if (len > 0)
sz = ' ';
Serial.print(sz);
smartDelay(0);
}
static void printDateTime(TinyGPSDate &d, TinyGPSTime &t)
{
if (!d.isValid())
{
Serial.print(F("********** "));
}
else
{
char sz;
sprintf(sz, "%02d/%02d/%02d ", d.month(), d.day(), d.year());
Serial.print(sz);
}
if (!t.isValid())
{
Serial.print(F("******** "));
}
else
{
char sz;
sprintf(sz, "%02d:%02d:%02d ", t.hour(), t.minute(), t.second());
Serial.print(sz);
}
printInt(d.age(), d.isValid(), 5);
smartDelay(0);
}
static void printStr(const char *str, int len)
{
int slen = strlen(str);
for (int i = 0; i < len; ++i)
Serial.print(i < slen ? str : ' ');
smartDelay(0);
}
驴友花雕
发表于 2022-5-29 13:42:37
(2)实验串口返回情况
驴友花雕
发表于 2022-5-29 13:44:59
程序九:测试 TinyGPSPlus 的几乎所有功能(1)Arduino参考开源代码
/*
【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
程序九:测试 TinyGPSPlus 的几乎所有功能
*/
#include <TinyGPSPlus.h>//导入驱动库
#include <SoftwareSerial.h>
static const int RXPin = 9, TXPin = 8;//定义软串口接脚
static const uint32_t GPSBaud = 9600;
// TinyGPSPlus 对象
TinyGPSPlus gps;
//与 GPS 设备的串行连接
SoftwareSerial ss(RXPin, TXPin);
// 对于每 5 秒发生一次的统计数据
unsigned long last = 0UL;
void setup() {
Serial.begin(9600);
ss.begin(GPSBaud);
Serial.println(F("KitchenSink.ino"));
Serial.println(F("测试 TinyGPSPlus 的几乎所有功能"));
Serial.print(F("测试 TinyGPSPlus 库 v."));
Serial.println(TinyGPSPlus::libraryVersion());
Serial.println(F("GY-NEO6MV2模块准备就绪"));
Serial.println();
}
void loop(){
// 调度传入的字符
while (ss.available() > 0)
gps.encode(ss.read());
if (gps.location.isUpdated())
{
Serial.print(F("LOCATION Fix Age="));
Serial.print(gps.location.age());
Serial.print(F("ms Raw Lat="));
Serial.print(gps.location.rawLat().negative ? "-" : "+");
Serial.print(gps.location.rawLat().deg);
Serial.print("[+");
Serial.print(gps.location.rawLat().billionths);
Serial.print(F(" billionths],Raw Long="));
Serial.print(gps.location.rawLng().negative ? "-" : "+");
Serial.print(gps.location.rawLng().deg);
Serial.print("[+");
Serial.print(gps.location.rawLng().billionths);
Serial.print(F(" billionths],Lat="));
Serial.print(gps.location.lat(), 6);
Serial.print(F(" Long="));
Serial.println(gps.location.lng(), 6);
}
else if (gps.date.isUpdated())
{
Serial.print(F("DATE Fix Age="));
Serial.print(gps.date.age());
Serial.print(F("ms Raw="));
Serial.print(gps.date.value());
Serial.print(F(" Year="));
Serial.print(gps.date.year());
Serial.print(F(" Month="));
Serial.print(gps.date.month());
Serial.print(F(" Day="));
Serial.println(gps.date.day());
}
else if (gps.time.isUpdated())
{
Serial.print(F("TIME Fix Age="));
Serial.print(gps.time.age());
Serial.print(F("ms Raw="));
Serial.print(gps.time.value());
Serial.print(F(" Hour="));
Serial.print(gps.time.hour());
Serial.print(F(" Minute="));
Serial.print(gps.time.minute());
Serial.print(F(" Second="));
Serial.print(gps.time.second());
Serial.print(F(" Hundredths="));
Serial.println(gps.time.centisecond());
}
else if (gps.speed.isUpdated())
{
Serial.print(F("SPEED Fix Age="));
Serial.print(gps.speed.age());
Serial.print(F("ms Raw="));
Serial.print(gps.speed.value());
Serial.print(F(" Knots="));
Serial.print(gps.speed.knots());
Serial.print(F(" MPH="));
Serial.print(gps.speed.mph());
Serial.print(F(" m/s="));
Serial.print(gps.speed.mps());
Serial.print(F(" km/h="));
Serial.println(gps.speed.kmph());
}
else if (gps.course.isUpdated())
{
Serial.print(F("COURSE Fix Age="));
Serial.print(gps.course.age());
Serial.print(F("ms Raw="));
Serial.print(gps.course.value());
Serial.print(F(" Deg="));
Serial.println(gps.course.deg());
}
else if (gps.altitude.isUpdated())
{
Serial.print(F("ALTITUDE Fix Age="));
Serial.print(gps.altitude.age());
Serial.print(F("ms Raw="));
Serial.print(gps.altitude.value());
Serial.print(F(" Meters="));
Serial.print(gps.altitude.meters());
Serial.print(F(" Miles="));
Serial.print(gps.altitude.miles());
Serial.print(F(" KM="));
Serial.print(gps.altitude.kilometers());
Serial.print(F(" Feet="));
Serial.println(gps.altitude.feet());
}
else if (gps.satellites.isUpdated())
{
Serial.print(F("SATELLITES Fix Age="));
Serial.print(gps.satellites.age());
Serial.print(F("ms Value="));
Serial.println(gps.satellites.value());
}
else if (gps.hdop.isUpdated())
{
Serial.print(F("HDOP Fix Age="));
Serial.print(gps.hdop.age());
Serial.print(F("ms raw="));
Serial.print(gps.hdop.value());
Serial.print(F(" hdop="));
Serial.println(gps.hdop.hdop());
}
else if (millis() - last > 5000)
{
Serial.println();
if (gps.location.isValid())
{
static const double LONDON_LAT = 51.508131, LONDON_LON = -0.128002;
double distanceToLondon =
TinyGPSPlus::distanceBetween(
gps.location.lat(),
gps.location.lng(),
LONDON_LAT,
LONDON_LON);
double courseToLondon =
TinyGPSPlus::courseTo(
gps.location.lat(),
gps.location.lng(),
LONDON_LAT,
LONDON_LON);
Serial.print(F("伦敦 距离="));
Serial.print(distanceToLondon / 1000, 6);
Serial.print(F(" km Course-to="));
Serial.print(courseToLondon, 6);
Serial.print(F(" degrees ["));
Serial.print(TinyGPSPlus::cardinal(courseToLondon));
Serial.println(F("]"));
}
Serial.print(F("DIAGS Chars="));
Serial.print(gps.charsProcessed());
Serial.print(F(" Sentences-with-Fix="));
Serial.print(gps.sentencesWithFix());
Serial.print(F(" Failed-checksum="));
Serial.print(gps.failedChecksum());
Serial.print(F(" Passed-checksum="));
Serial.println(gps.passedChecksum());
if (gps.charsProcessed() < 10)
Serial.println(F("警告:没有 GPS 数据,检查接线。"));
last = millis();
Serial.println();
}
}
驴友花雕
发表于 2022-5-29 13:46:23
(2)实验串口返回情况
驴友花雕
发表于 2022-5-29 17:59:46
程序十:使用 TinyGPSCustom 对象跟踪卫星高度(1)Arduino参考开源代码
/*
【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
程序十:使用 TinyGPSCustom 对象跟踪卫星高度
*/
#include <TinyGPSPlus.h>//导入驱动库
#include <SoftwareSerial.h>
static const int RXPin = 9, TXPin = 8;//定义软串口接脚
static const uint32_t GPSBaud = 9600;
static const int MAX_SATELLITES = 40;
static const int PAGE_LENGTH = 40;
// TinyGPSPlus 对象
TinyGPSPlus gps;
//与 GPS 设备的串行连接
SoftwareSerial ss(RXPin, TXPin);
TinyGPSCustom totalGPGSVMessages(gps, "GPGSV", 1); // $GPGSV 句子,第一个元素
TinyGPSCustom messageNumber(gps, "GPGSV", 2); // $GPGSV 句子,第二个元素
TinyGPSCustom satNumber; // 稍后初始化
TinyGPSCustom elevation;
bool anyChanges = false;
unsigned long linecount = 0;
struct
{
int elevation;
bool active;
} sats;
void setup(){
Serial.begin(9600);
ss.begin(GPSBaud);
Serial.println(F("SatElevTracker.ino"));
Serial.println(F("显示 GPS 卫星高度变化"));
Serial.print(F("测试 TinyGPSPlus 库 v."));
Serial.println(TinyGPSPlus::libraryVersion());
Serial.println(F("GY-NEO6MV2模块准备就绪"));
Serial.println();
// 初始化所有未初始化的 TinyGPSCustom 对象
for (int i=0; i<4; ++i)
{
satNumber.begin(gps, "GPGSV", 4 + 4 * i); // offsets 4, 8, 12, 16
elevation.begin(gps, "GPGSV", 5 + 4 * i); // offsets 5, 9, 13, 17
}
}
void loop(){
// 调度传入的字符
if (ss.available() > 0)
{
gps.encode(ss.read());
if (totalGPGSVMessages.isUpdated())
{
for (int i=0; i<4; ++i)
{
int no = atoi(satNumber.value());
if (no >= 1 && no <= MAX_SATELLITES)
{
int elev = atoi(elevation.value());
sats.active = true;
if (sats.elevation != elev)
{
sats.elevation = elev;
anyChanges = true;
}
}
}
int totalMessages = atoi(totalGPGSVMessages.value());
int currentMessage = atoi(messageNumber.value());
if (totalMessages == currentMessage && anyChanges)
{
if (linecount++ % PAGE_LENGTH == 0)
printHeader();
TimePrint();
for (int i=0; i<MAX_SATELLITES; ++i)
{
Serial.print(F(" "));
if (sats.active)
IntPrint(sats.elevation, 2);
else
Serial.print(F(" "));
sats.active = false;
}
Serial.println();
anyChanges = false;
}
}
}
}
void IntPrint(int n, int len){
int digs = n < 0 ? 2 : 1;
for (int i=10; i<=abs(n); i*=10)
++digs;
while (digs++ < len)
Serial.print(F(" "));
Serial.print(n);
Serial.print(F(" "));
}
void TimePrint(){
if (gps.time.isValid())
{
if (gps.time.hour() < 10)
Serial.print(F("0"));
Serial.print(gps.time.hour());
Serial.print(F(":"));
if (gps.time.minute() < 10)
Serial.print(F("0"));
Serial.print(gps.time.minute());
Serial.print(F(":"));
if (gps.time.second() < 10)
Serial.print(F("0"));
Serial.print(gps.time.second());
Serial.print(F(" "));
}
else
{
Serial.print(F("(未知的)"));
}
}
void printHeader(){
Serial.println();
Serial.print(F("时间 "));
for (int i=0; i<MAX_SATELLITES; ++i)
{
Serial.print(F(" "));
IntPrint(i+1, 2);
}
Serial.println();
Serial.print(F("---------"));
for (int i=0; i<MAX_SATELLITES; ++i)
Serial.print(F("----"));
Serial.println();
}
驴友花雕
发表于 2022-5-29 18:01:29
(2)实验串口返回情况
驴友花雕
发表于 2022-5-31 21:09:11
程序十一:使用 TinyGPSCustom 对象数组监视所有可见的卫星(1)Arduino参考开源代码
/*
【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
程序十一:使用 TinyGPSCustom 对象数组监视所有可见的卫星
*/
#include <TinyGPSPlus.h>//导入驱动库
#include <SoftwareSerial.h>
static const int RXPin = 9, TXPin = 8;//定义软串口接脚
static const uint32_t GPSBaud = 9600;
// TinyGPSPlus 对象
TinyGPSPlus gps;
//与 GPS 设备的串行连接
SoftwareSerial ss(RXPin, TXPin);
/*
来自 http://aprs.gids.nl/nmea/:
$GPGSV
视野中的 GPS 卫星
例如:
$GPGSV,3,1,11,03,03,111,00,04,15,270,00,06,01,010,00,13,06,292,00*74 $GPGSV,3,2,11,14,25,170,00,16,57,208,39,18,67,296,40,19,40,246,00*74
$GPGSV,3,3,11,22,42,067,42,24,14,311,43,27,05,244,00,,,,*4D
1 = 此周期中此类消息的总数
2 = 消息编号
3 = 视图中的 SV 总数
4 = SV PRN 号码
5 = 以度为单位的仰角,最大 90
6 = 方位角,从真北的度数,000 到 359
7 = SNR,00-99 dB(不跟踪时为空)
8-11 = 关于第二个 SV 的信息,与字段 4-7 相同
12-15=关于第三个 SV 的信息,与字段 4-7 相同
16-19= 关于第四个 SV 的信息,与字段 4-7 相同
*/
static const int MAX_SATELLITES = 40;
TinyGPSCustom totalGPGSVMessages(gps, "GPGSV", 1); // $GPGSV 句子,第一个元素
TinyGPSCustom messageNumber(gps, "GPGSV", 2); // $GPGSV 句子,第二个元素
TinyGPSCustom satsInView(gps, "GPGSV", 3); // $GPGSV 句子,第三个元素
TinyGPSCustom satNumber; // 稍后初始化
TinyGPSCustom elevation;
TinyGPSCustom azimuth;
TinyGPSCustom snr;
struct{
bool active;
int elevation;
int azimuth;
int snr;
} sats;
void setup(){
Serial.begin(9600);
ss.begin(GPSBaud);
Serial.println(F("SatelliteTracker.ino"));
Serial.println(F("使用 TinyGPSCustom 监控卫星位置和信号强度"));
Serial.print(F("测试 TinyGPSPlus 库 v."));
Serial.println(TinyGPSPlus::libraryVersion());
Serial.println(F("GY-NEO6MV2模块准备就绪"));
Serial.println();
// 初始化所有未初始化的 TinyGPSCustom 对象
for (int i=0; i<4; ++i)
{
satNumber.begin(gps, "GPGSV", 4 + 4 * i); // offsets 4, 8, 12, 16
elevation.begin(gps, "GPGSV", 5 + 4 * i); // offsets 5, 9, 13, 17
azimuth.begin(gps, "GPGSV", 6 + 4 * i); // offsets 6, 10, 14, 18
snr.begin( gps, "GPGSV", 7 + 4 * i); // offsets 7, 11, 15, 19
}
}
void loop(){
// 初始化所有未初始化的 TinyGPSCustom 对象
if (ss.available() > 0)
{
gps.encode(ss.read());
if (totalGPGSVMessages.isUpdated())
{
for (int i=0; i<4; ++i)
{
int no = atoi(satNumber.value());
// Serial.print(F("SatNumber is ")); Serial.println(no);
if (no >= 1 && no <= MAX_SATELLITES)
{
sats.elevation = atoi(elevation.value());
sats.azimuth = atoi(azimuth.value());
sats.snr = atoi(snr.value());
sats.active = true;
}
}
int totalMessages = atoi(totalGPGSVMessages.value());
int currentMessage = atoi(messageNumber.value());
if (totalMessages == currentMessage)
{
Serial.print(F("Sats="));
Serial.print(gps.satellites.value());
Serial.print(F(" Nums="));
for (int i=0; i<MAX_SATELLITES; ++i)
if (sats.active)
{
Serial.print(i+1);
Serial.print(F(" "));
}
Serial.print(F(" 海拔="));//海拔
for (int i=0; i<MAX_SATELLITES; ++i)
if (sats.active)
{
Serial.print(sats.elevation);
Serial.print(F(" "));
}
Serial.print(F(" 方位角="));//方位角
for (int i=0; i<MAX_SATELLITES; ++i)
if (sats.active)
{
Serial.print(sats.azimuth);
Serial.print(F(" "));
}
Serial.print(F(" 信噪比="));//信噪比
for (int i=0; i<MAX_SATELLITES; ++i)
if (sats.active)
{
Serial.print(sats.snr);
Serial.print(F(" "));
}
Serial.println();
for (int i=0; i<MAX_SATELLITES; ++i)
sats.active = false;
}
}
}
}
驴友花雕
发表于 2022-5-31 21:10:46
(2)实验串口返回情况
驴友花雕
发表于 2022-6-2 07:44:47
程序十二:使用 TinyGPSCustom 提取任何 NMEA 字段(1)Arduino参考开源代码
/*
【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
程序十二:使用 TinyGPSCustom 提取任何 NMEA 字段
*/
#include <TinyGPSPlus.h>//导入驱动库
#include <SoftwareSerial.h>
static const int RXPin = 9, TXPin = 8;//定义软串口接脚
static const uint32_t GPSBaud = 9600;
// TinyGPSPlus 对象
TinyGPSPlus gps;
//与 GPS 设备的串行连接
SoftwareSerial ss(RXPin, TXPin);
/*
通过像这样声明 TinyGPSCustom 对象,我们宣布我们
对 $GPGSA 中的第 15、16 和 17 字段感兴趣
句子,分别是PDOP(F(“位置精度的稀释”)),
HDOP (F("horizontal...")) 和 VDOP (F("vertical..."))。
(计数从紧跟句子名称的字段开始,
即 $GPGSA。有关 NMEA 语句的更多信息,请咨询您的
GPS 模块的文档和/或 http://aprs.gids.nl/nmea/。)
如果你的 GPS 模块不支持 $GPGSA 语句,那么你
不会从此程序获得任何输出。
*/
TinyGPSCustom pdop(gps, "GPGSA", 15); // $GPGSA 句子,第 15 个元素
TinyGPSCustom hdop(gps, "GPGSA", 16); // $GPGSA 句子,第 16 个元素
TinyGPSCustom vdop(gps, "GPGSA", 17); // $GPGSA 句子,第 17 个元素
void setup() {
Serial.begin(9600);
ss.begin(GPSBaud);
Serial.println(F("UsingCustomFields.ino"));
Serial.println(F("使用 TinyGPSCustom 提取任何 NMEA 字段"));
Serial.print(F("测试 TinyGPSPlus 库 v."));
Serial.println(TinyGPSPlus::libraryVersion());
Serial.println(F("GY-NEO6MV2模块准备就绪"));
Serial.println();
}
void loop() {
// 每次更新任何内容时,打印所有内容。
if (gps.altitude.isUpdated() || gps.satellites.isUpdated() ||
pdop.isUpdated() || hdop.isUpdated() || vdop.isUpdated())
{
Serial.print(F("ALT=")); Serial.print(gps.altitude.meters());
Serial.print(F(" PDOP=")); Serial.print(pdop.value());
Serial.print(F(" HDOP=")); Serial.print(hdop.value());
Serial.print(F(" VDOP=")); Serial.print(vdop.value());
Serial.print(F(" SATS=")); Serial.println(gps.satellites.value());
}
while (ss.available() > 0)
gps.encode(ss.read());
}
驴友花雕
发表于 2022-6-2 07:46:51
(2)实验串口返回情况
驴友花雕
发表于 2022-6-2 07:48:43
(3)实验场景图
驴友花雕
发表于 2022-6-4 08:47:07
程序十三:在OLED上显示日期、时间、纬度和经度(1)Arduino参考开源代码
/*
【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
程序十三:在OLED上显示日期、时间、纬度和经度
*/
#include <TimeLib.h>//导入驱动库
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
#include <U8glib.h>
//用于连接GY-NEO6MV2模块的引脚D9/D8
static const int RXPin = 9, TXPin = 8;
// TinyGPS++ 对象
TinyGPSPlus gps;
// GPS模块的串口连接
SoftwareSerial Serial_GPS(RXPin, TXPin);
//OLED显示设置
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_FAST);
void draw() {
//设置时间/时区
GPS_Timezone_Adjust();
//显示的图形命令必须放在这里aqui
u8g.setFont(u8g_font_8x13B);
u8g.drawRFrame(0, 0, 128, 64, 4);
//日期线
u8g.drawStr(10, 14, "D: ");
u8g.setPrintPos(34, 14);
//显示日期
if (day() < 10)
{
u8g.print("0");
}
u8g.print(day());
u8g.drawStr(51, 14, "/");
u8g.setPrintPos(60, 14);
//显示月份
if (month() < 10)
{
u8g.print("0");
}
u8g.print(month());
u8g.drawStr(76, 14, "/");
u8g.setPrintPos(85, 14);
//显示年份
u8g.print(year());
// 时间线
u8g.drawStr(10, 29, "H: ");
u8g.setPrintPos(45, 29);
//开始时间
if (hour() < 10)
{
u8g.print("0");
}
u8g.print(hour());
u8g.drawStr(61, 28, ":");
u8g.setPrintPos(70, 29);
//显示分钟
if (minute() < 10)
{
u8g.print("0");
}
u8g.print(minute());
u8g.drawStr(86, 28, ":");
u8g.setPrintPos(95, 29);
//显示秒数
if (second() < 10)
{
u8g.print("0");
}
u8g.print(second());
//纬度
u8g.drawStr(10, 44, "Lat: ");
u8g.setPrintPos(45, 44);
u8g.print(gps.location.lat(), 5);
//经度
u8g.drawStr(10, 59, "Lon: ");
u8g.setPrintPos(45, 59);
u8g.print(gps.location.lng(), 5);
}
//根据地区调整时区
const int UTC_offset = -3;
void setup() {
// Arduino 波特率
Serial.begin(9600);
//波特率GPS模块
Serial_GPS.begin(9600);
//在串口监视器上显示初始信息
Serial.println(F("日期、时间、纬度和经度"));
Serial.println(F("GPS 模块 GY-NEO6MV2"));
Serial.print(F("TinyGPS++ 驱动库 v."));
Serial.println(TinyGPSPlus::libraryVersion());
Serial.println();
//OLED显示设置
if ( u8g.getMode() == U8G_MODE_R3G3B2 )
{
u8g.setColorIndex(255); // 白色的
}
else if ( u8g.getMode() == U8G_MODE_GRAY2BIT ) {
u8g.setColorIndex(3); // 最大强度
}
else if ( u8g.getMode() == U8G_MODE_BW ) {
u8g.setColorIndex(1); //像素开启
}
else if ( u8g.getMode() == U8G_MODE_HICOLOR ) {
u8g.setHiColorByRGB(255, 255, 255);
}
}
void loop() {
//与GPS模块连接
while (Serial_GPS.available() > 0)
if (gps.encode(Serial_GPS.read()))
displayInfo();
if (millis() > 5000 && gps.charsProcessed() < 10)
{
Serial.println(F("未检测到 GPS:检查接线。"));
while (true);
}
//调用屏幕上的绘图例程
u8g.firstPage();
do
{
draw();
}
while ( u8g.nextPage() );
delay(1000);
}
void displayInfo() {
//在串口监视器中显示信息
Serial.print(F("位置:"));
if (gps.location.isValid())
{
Serial.print(gps.location.lat(), 6); //latitude
Serial.print(F(","));
Serial.print(gps.location.lng(), 6); //longitude
}
else
{
Serial.print(F("无效"));
}
Serial.print(F("日期/时间: "));
if (gps.date.isValid())
{
Serial.print(gps.date.day()); //天
Serial.print(F("/"));
Serial.print(gps.date.month()); //月
Serial.print(F("/"));
Serial.print(gps.date.year());//年
}
else
{
Serial.print(F("无效"));
}
Serial.print(F(" "));
if (gps.time.isValid())
{
if (gps.time.hour() < 10) Serial.print(F("0"));
Serial.print(gps.time.hour()); //小时
Serial.print(F(":"));
if (gps.time.minute() < 10) Serial.print(F("0"));
Serial.print(gps.time.minute()); //分钟
Serial.print(F(":"));
if (gps.time.second() < 10) Serial.print(F("0"));
Serial.print(gps.time.second()); //第二
Serial.print(F("."));
if (gps.time.centisecond() < 10) Serial.print(F("0"));
Serial.print(gps.time.centisecond());
}
else
{
Serial.print(F("无效"));
}
Serial.println();
}
void GPS_Timezone_Adjust() {
while (Serial_GPS.available())
{
if (gps.encode(Serial_GPS.read()))
{
int Year = gps.date.year();
byte Month = gps.date.month();
byte Day = gps.date.day();
byte Hour = gps.time.hour();
byte Minute = gps.time.minute();
byte Second = gps.time.second();
//根据GPS数据调整日期和时间
setTime(Hour, Minute, Second, Day, Month, Year);
//应用偏移来设置日期和时间
//根据时区
adjustTime(UTC_offset * SECS_PER_HOUR);
}
}
}
驴友花雕
发表于 2022-6-4 08:48:55
(2)实验串口返回情况(放在电脑桌上,没有GPS信号)
驴友花雕
发表于 2022-6-4 08:54:08
(3)实验场景图 (放在电脑桌上,没有GPS信号)
驴友花雕
发表于 2022-6-4 08:55:34
(4)实验串口返回情况(模块放在窗口,接收到GPS信号)
驴友花雕
发表于 2022-6-4 08:57:34
(5)实验场景图(模块放在窗口,接收到GPS信号)