111浏览
查看: 111|回复: 0

[讨论] ESP8266 和 Ai-GP-02-Kit 实现 GPS 时钟

[复制链接]
本帖最后由 无垠的广袤 于 2025-4-14 14:33 编辑

ESP8266 和 Ai-GP-02-Kit 实现 GPS 时钟

本文介绍了安信可 GP-02-Kit 开发板结合 Arduino IDE 实现 GPS 时钟的项目设计流程。

硬件连接
  1. OLED_SCL -> 14
  2. OLED_SDA -> 02
  3. GP-02-Kit_RX -> 04
  4. GP-02-Kit_TX -> 05
复制代码


示意图
ESP8266 和 Ai-GP-02-Kit 实现 GPS 时钟图1


代码

  1. #include <TinyGPSPlus.h>
  2. #include <Wire.h>
  3. #include <Adafruit_GFX.h>
  4. #include <Adafruit_SSD1306.h>
  5. #include <SoftwareSerial.h>
  6. #include <ESP8266WiFi.h>
  7. // I2C款接线说明
  8. // NodeMCU开发板         0.96寸OLED 引脚连接对应关系
  9. // GND                  GND
  10. // 3V3                  VCC
  11. // SCL                  D1 (GPIO 5)
  12. // SDA                  D2 (GPIO 4)
  13. #define SCREEN_WIDTH 128                                                   // OLED display width, in pixels
  14. #define SCREEN_HEIGHT 64                                                   // OLED display height, in pixels
  15. #define OLED_SDA 02                       // SDA引脚,gpio2(D4)
  16. #define OLED_SCL 14                       // SCL引脚,gpio14(D5)
  17. #define OLED_RESET 13                                                      // 重置引脚
  18. #define SCREEN_ADDRESS 0x3C                                                // OLED 显示屏的地址,固化在芯片上
  19. Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);  // 创建实例
  20. // gps模块引脚定义
  21. #define RXPin 4  // GPIO 12 对应nodemcu D6
  22. #define TXPin 5  // GPIO 14 对应nodemcu D5
  23. // The serial connection to the GPS device
  24. SoftwareSerial ss(RXPin, TXPin);
  25. // The TinyGPSPlus object
  26. TinyGPSPlus gps;
  27. // 一些需要使用的变量
  28. int Year, Month, Date, Hour, Minute, Second, Yea, Mon, Dat, Hou;
  29. double Lat, Lng;
  30. String sMonth, sDate, sHour, sMinute, sSecond;
  31. void setup() {
  32.   Wire.begin(OLED_SDA, OLED_SCL);
  33.   Serial.begin(9600);
  34.   WiFi.mode(WIFI_OFF);  //关闭WIFI模块省电
  35.   WiFi.forceSleepBegin();
  36.   ss.begin(9600);  //GPS模块虚拟串口
  37.   // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  38.   if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
  39.     Serial.println(F("SSD1306 allocation failed"));
  40.     for (;;)
  41.       ;  // Don't proceed, loop forever
  42.   }
  43.   display.clearDisplay();       // 清屏
  44.   display.setTextColor(WHITE);  // 设置字体颜色为白色
  45.   display.display();            // 显示
  46.   //OLED屏初始化代码
  47. }
  48. void loop() {
  49.   boolean newData = false;
  50.   for (unsigned long start = millis(); millis() - start < 300;) {
  51.     while (ss.available()) {
  52.       if (gps.encode(ss.read())) {
  53.         newData = true;
  54.       }
  55.     }
  56.   }  //上面是GPS数据接收的固定代码
  57.   Yea = gps.date.year();       //年
  58.   Mon = gps.date.month();      //月
  59.   Dat = gps.date.day();        //日
  60.   Hou = gps.time.hour();       //时
  61.   Minute = gps.time.minute();  //分
  62.   Second = gps.time.second();  //秒
  63.   Lng = gps.location.lng();    //经度
  64.   Lat = gps.location.lat();    //纬度
  65.   //年月日时转换部分,将UTC时间转换为北京时间,并消除错误
  66.   Hour = Hou + 8;  //修正时区
  67.   if (Hour >= 24) {
  68.     Hour = Hour - 24;  //修正小时超程
  69.   }
  70.   if (Hou + 8 >= 24) {
  71.     Date = Dat + 1;
  72.     if ((Mon == 1 || Mon == 3 || Mon == 5 || Mon == 7 || Mon == 8 || Mon == 10 || Mon == 12) && (Date > 31)) {
  73.       Date = Date - 30;
  74.       Month = Mon + 1;  //大月进位
  75.     } else {
  76.       Month = Mon;
  77.       Year = Yea;
  78.     }
  79.     if ((Mon == 4 || Mon == 6 || Mon == 9 || Mon == 11) && (Date > 30)) {
  80.       Date = Date - 29;
  81.       Month = Mon + 1;  //小月进位
  82.     } else {
  83.       Month = Mon;
  84.       Year = Yea;
  85.     }
  86.     if ((Yea % 4 == 0) && (Date > 29)) {
  87.       Date = Date - 28;
  88.       Month = Mon + 1;  //闰月判定并进位
  89.     } else {
  90.       Month = Mon;
  91.       Year = Yea;
  92.     }
  93.     if ((Yea % 4 != 0) && (Date > 28)) {
  94.       Date = Date - 27;
  95.       Month = Mon + 1;  //非闰月判定并进位
  96.     } else {
  97.       Month = Mon;
  98.       Year = Yea;
  99.     }
  100.     if (Month > 12) {
  101.       Month = Month - 12;
  102.       Year = Yea + 1;  //年超程进位
  103.     }
  104.   } else {
  105.     Date = Dat;
  106.     Month = Mon;
  107.     Year = Yea;
  108.   }
  109.   //结果显示部分
  110.   display.setTextColor(SSD1306_WHITE);
  111.   display.setCursor(38, 0);
  112.   display.setTextSize(1);
  113.   display.print(Year);
  114.   display.setCursor(63, 0);
  115.   display.setTextSize(1);
  116.   display.print("-");
  117.   display.setTextSize(1);
  118.   display.setCursor(71, 0);
  119.   sMonth = formatNumber(Month, 2);
  120.   display.print(sMonth);
  121.   display.setCursor(83, 0);
  122.   display.setTextSize(1);
  123.   display.print("-");
  124.   display.setTextSize(1);
  125.   display.setCursor(91, 0);
  126.   sDate = formatNumber(Date, 2);
  127.   display.print(sDate);
  128.   display.setTextSize(2);
  129.   display.setCursor(26, 13);
  130.   sHour = formatNumber(Hour, 2);
  131.   display.print(sHour);
  132.   display.setCursor(46, 13);
  133.   display.setTextSize(2);
  134.   display.print(":");
  135.   display.setTextSize(2);
  136.   display.setCursor(56, 13);
  137.   sMinute = formatNumber(Minute, 2);
  138.   display.print(sMinute);
  139.   display.setCursor(76, 13);
  140.   display.setTextSize(2);
  141.   display.print(":");
  142.   display.setTextSize(2);
  143.   display.setCursor(86, 13);
  144.   sSecond = formatNumber(Second, 2);
  145.   display.print(sSecond);
  146.   display.setTextSize(1);
  147.   display.setCursor(35, 33);
  148.   display.print(gps.location.lng(), 8);
  149.   display.setTextSize(1);
  150.   display.setCursor(35, 43);
  151.   display.print(gps.location.lat(), 8);
  152.   display.setCursor(105, 53);
  153.   display.setTextSize(1);
  154.   display.print("m");
  155.   display.setCursor(50, 53);
  156.   display.setTextSize(1);
  157.   display.print("km/h");
  158.   display.setTextSize(1);
  159.   display.setCursor(80, 53);
  160.   display.print(gps.speed.mps());
  161.   display.setTextSize(1);
  162.   display.setCursor(25, 53);
  163.   display.print(gps.speed.kmph());
  164.   display.display();
  165.   delay(500);
  166.   display.clearDisplay();
  167. }
  168. // 格式化数字的函数
  169. String formatNumber(int number, int digits) {
  170.   String formatted = "";
  171.   if (number < pow(10, digits - 1)) {
  172.     formatted = String(number, DEC);
  173.     while (formatted.length() < digits) {
  174.       formatted = "0" + formatted;
  175.     }
  176.   } else {
  177.     formatted = String(number, DEC);
  178.   }
  179.   return formatted;
  180. }
复制代码



编译代码,选择端口号并上传固件,短按 RST 复位运行程序。

参考:CSDN博客 .

效果

选择室外或窗边放置天线,保持连接状态,上电首次通信需 30 秒左右便能接收并识别出时钟、坐标和速度等信息
ESP8266 和 Ai-GP-02-Kit 实现 GPS 时钟图2
开发板详见:立创开源硬件平台 .

OLED 显示
ESP8266 和 Ai-GP-02-Kit 实现 GPS 时钟图3

动态演示

ESP8266 和 Ai-GP-02-Kit 实现 GPS 时钟图4

总结

本文介绍了安信可 GP-02-Kit 开发板实现 GPS 时钟的项目设计流程,为 GP-02 模组的开发和应用提供了参考。


您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4 备案 沪公网安备31011502402448

© 2013-2025 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail