基于arduino cloud的GPS定位系统

成果概览

在这里插入图片描述

现在可以在arduino cloud官网提供的IOT物联网平台查看实时数据,目前的功能包括:利用arduino R4 Wi-Fi版开发板自带的Wi-Fi联网功能,将GPS信号上传,并利用谷歌地图显示位置信息;同时,该系统辅助性的加入了控制LED亮灭以及显示Wi-Fi的IP地址功能,用以检测系统正确性。
在这里插入图片描述

系统整体图片,可见,所需外设很少,对于学习arduino物联网开发入门很友好。

所需物料

  • Arduino UNO R4 Wi-Fi开发板
  • 面包板
  • LED
  • 220Ω电阻
  • GY-NEO-8M GPS芯片(搭配陶瓷天线)
  • 导线若干
  • 另外,需申请arduino cloud账号(免费版即可)

开发流程

在arduino cloud中新建thing,连接自己的device,创建variable变量后与dashboard进行连接。

核心代码

/* 
  Sketch generated by the Arduino IoT Cloud Thing "Untitled"
  https://create.arduino.cc/cloud/things/b28f3724-a84b-4973-babe-1ae2e4a7aac1 

  Arduino IoT Cloud Variables description

  The following variables are automatically generated and updated when changes are made to the Thing

  String iPaddress;
  float latitude;
  float longitude;
  CloudLocation location;
  bool led;

  Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
  which are called when their values are changed from the Dashboard.
  These functions are generated with the Thing and added at the end of this sketch.
*/

#include "thingProperties.h"
#include <WiFiS3.h>

#include <TinyGPS++.h>
#include <SoftwareSerial.h>

#define gpsTX 7
#define gpsRX 8
#define gpsBaud 9600  // 确保这个波特率与你的GPS模块设置一致

SoftwareSerial gpsSerial(gpsRX, gpsTX);
TinyGPSPlus gps;

unsigned long lastUpdateTime = 0;

const int timeZoneOffset = 8; // 假设你在东八区

const int ledPIN = 2;

void setup() {
  // Initialize serial and wait for port to open:
  Serial.begin(9600);

  gpsSerial.begin(gpsBaud);
  Serial.println("GPS Module Testing with TinyGPS++ library");
  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  delay(1500); 

  // Defined in thingProperties.h
  initProperties();

  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  
  /*
     The following function allows you to obtain more information
     related to the state of network and IoT Cloud connection and errors
     the higher number the more granular information you’ll get.
     The default is 0 (only errors).
     Maximum is 4
 */
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();
  pinMode(ledPIN,OUTPUT);

  // 初始化Wi-Fi
  Serial.print("Connecting to ");
  Serial.println(SSID);

  WiFi.begin(SSID, PASS);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  // 连接成功
  Serial.println("");
  Serial.println("WiFi connected.");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP()); // 打印IP地址
  iPaddress = WiFi.localIP().toString(); // 将本地IP地址转换为String并赋值给iPaddress
}

void loop() {
  ArduinoCloud.update();
  // Your code here 
  if (millis() - lastUpdateTime >= 60000) {
    lastUpdateTime = millis(); // 更新上次更新时间

    // 读取GPS数据
    while (gpsSerial.available()) {
      char c = gpsSerial.read();
      gps.encode(c); // 处理接收到的字符
    }

    // 检查并显示GPS信息
    if (gps.location.isValid() || gps.date.isValid() || gps.time.isValid()) {
      displayGPSInfo();

      latitude = gps.location.lat();
      longitude = gps.location.lng();

      location = {latitude,longitude};
    }
  }
}

/*
  Since Led is READ_WRITE variable, onLedChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onLedChange()  {
  // Add your code here to act upon Led change
  digitalWrite(ledPIN,led);
}

void displayGPSInfo() {
  if (gps.location.isValid()) {
    Serial.print("Latitude: ");
    Serial.println(gps.location.lat(), 6);
    Serial.print("Longitude: ");
    Serial.println(gps.location.lng(), 6);
  }

  if (gps.date.isValid()) {
    Serial.print("Date: ");
    Serial.print(gps.date.month());
    Serial.print("/");
    Serial.print(gps.date.day());
    Serial.print("/");
    Serial.println(gps.date.year());
  }

  if (gps.time.isValid()) {
    int localHour = (gps.time.hour() + 24 + timeZoneOffset) % 24; // 处理时区并避免负数
    Serial.print("Time: ");
    Serial.print(localHour);
    Serial.print(":");
    Serial.print(gps.time.minute());
    Serial.print(":");
    Serial.println(gps.time.second());
  }
}

thing配置

在这里插入图片描述
本系统共创建了五个variables,除led变量设置为读/写皆可外,其余变量均为只读类型。此外,thing中要配置好开发板连接的Wi-Fi:
在这里插入图片描述
只需要输入Wi-Fi名称和密码就好了,注意,我在试验中发现,开发板接收到的Wi-Fi信号只能是2.4GHz,使用5GHz的Wi-Fi信号连接不上,不知道是不是我自己哪里没有设置对。
以上内容设置完成,cloud编辑环境会自动生成包含这些变量以及Wi-Fi密码的头文件,很方便,不需要我们自己配置:
在这里插入图片描述
之后,我们就可以进入.ino文件进行核心功能的搭建,本项目的核心代码已经放在上面了,我现在简要记录几个关键的修改位置。

#include <WiFiS3.h>

实测R4 Wi-Fi开发板只能使用WiFiS3的Wi-Fi库文件,另一个wifinina库用不了(不知道是不是我没用对)。

if (gps.location.isValid() || gps.date.isValid() || gps.time.isValid()) {
      displayGPSInfo();

      latitude = gps.location.lat();
      longitude = gps.location.lng();

      location = {latitude,longitude};
    }

这里是GPS信号输出的关键步骤,当检测到GPS输出信息有效时,更新变量latitudelongitudelocation的信息。这也是如何在dashboard中显示数据的关键,我们只需要将自己想要知道的数据赋值给在thing中定义的变量,后面就可以显示它了。其中location变量属于CloudLocation类,要想使用和更新它,需要location = {Lat, Long};这样使用。参考信息:https://forum.arduino.cc/t/displaying-location-on-iot-cloud-map-widget/906503/4。这个变量是专门为接下来dashboard中的map准备的。
所有程序编写好后,要将程序下载进开发板。

dashboard配置

在这里插入图片描述

以latitude变量为例,我们只需要将自己定义的按钮或者别的显示value的模块与先前在variable定义的变量连接起来,我们就可以显示需要的数据了。其余变量以此类推。

  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这是一篇关于动手制作基于arduino 和12864液晶模块的图形化显示GPS导航系统的帖子,作品效果和成本可能无法和TB上的导航产品媲美,但是动手过程会带给你乐趣和知识。 该设计根据功能分为三个版本。(具体详见附件内容) 最终版本视频演示: 动手之前最好能具备一些背景知识: 1. arduino 相关基础; 2. 能使用12864 液晶模块; 3. 能够用arduino 通过串口通信获取GPS模块定位信息; 4. 能够用arduino 操作SD卡模块; 制作所需主要硬件: arduino UNO 1片; 12864 液晶模块 1片; GPS模块1片; SD卡模块及SD卡 1套; 如截图: 原理: 将地图数据依据瓦片算法存储在SD卡中,通过串口获取GPS定位信息并从中解析出经纬度坐标,依据经纬度坐标读取相应地图数据显示在12864液晶模块上,同时显示定位坐标点。 1. 地图存储算法——瓦片系统(Maps Tile System) 本制作采用的地图数据和地图存储算法来源于微软的bing maps并做了相应修改。 具体可参考: Bing Maps Tile System[1*] Virtual Earth Tile Image URI 参数解析 Goolge and Microsoft Map Url Parameters 在瓦片系统中地图采用金字塔式的分层存储结构,不同层具有不同级别的地图分辨率(地图精细程度),每一层地图被分割成等像素大小(256X256)的瓦片,算法要解决的问题就是给定经纬度坐标和缩放级别(层索引)得到具体相应的瓦片编号。 在连接[1*]的最后有算法实现的代码可共参考。 2. 针对12864液晶模块的设计 12864液晶模块是128像素宽64像素高的单色液晶显示模块,本制作为了适应模块显示做出了两个设计。 1). 将256X256像素的瓦片裁切成128X64像素大小的8份 子瓦片。 如下图所示: 2). 通过阈值方法将8位png索引图像(bing maps 的道路数据)转换成二进制地图数据文件,为了能够显示原图中的文字信息,采用多阈值提取求或方法提取原地图中背景、地物和标注文字数据,由于标注文字和背景之间的扰动,提取效果有待改进。 显示效果: 生成地图程序(需要连接互联网): 通过设置地图范围经纬度信息获取要使用定位的区域,可通过google earth 等能显示经纬度的软件或网页获取经纬度的最大最小值(上大下小,右大左小)。缩放级别建议设置范围1~15,较大地图范围和较高缩放级别会增加地图下载、显示加载的时间。 注意:上面是对GPS导航V0.1版本的具体介绍,其他的V0.2和V0.3版本详见附件内容。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值