Home > Communication Circuits >Wireless Communication > DIY a GPS child tracker

DIY a GPS child tracker

Source: InternetPublisher:睡不醒的小壮 Keywords: gps tracker Updated: 2024/02/02

I made a GPS tracker to track my kids' locations after school and allow them to send pick-up requests at any time so I can be ready to pick them up from school.

background

My twins love riding their bikes on the trails and are of an age where they can ride much faster than I can. As a parent, I want to encourage them to explore on their own, but I also want to keep an eye on where they are in case something goes wrong.

My kids aren't cell phone savvy yet, so I try to avoid having to duplicate two data plans as much as possible. While there are many commercial GPS trackers for kids available, almost all of them require an additional monthly subscription fee.

All of the above got me thinking - as a software developer, I should be able to build something like this for myself.

And so this project was born!

My custom tracker takes a GPS reading every five minutes and displays it on the map. Additionally, the tracker includes a button that children can press to send their location to their parents via text message.

In this article, I'll show you how I built my own custom GPS tracker and teach you how to replicate parts of this setup yourself, so let's get started.

Step 1: Assemble the Hardware

Most of the hardware I use comes from Swan's Blues Wireless Feather Starter Kit. The Feather Starter kit contains a Notecard, a Notecarrier AF and a Swan MCU. Let's break down each of them.

Notecard

Notecard is a small system-level module that can add connectivity to almost any IoT project. Blues makes cellular and Wi-Fi notecards, but for GPS tracking, you'll need a cellular notecard.

tracking device

I use Notecard to collect GPS coordinates from an external antenna (which I'll show you in a minute) and send those coordinates to the cloud over a cellular connection.

NoteAF

Notecarriers are companion boards that make it easy to prototype and deploy IoT projects using Notecard. The Notecarrier provides you with a convenient place to connect your Notecard, power it, access the embedded antenna, and connect to I2C peripherals via the Qwiic port.

Notecard

Notecarrier AF also includes a Feather-compatible connector that can be easily embedded into any Feather-compatible device for use with Notecard.

Swan

The Notecarrier can easily connect to Feather-compatible MCUs, and the MCU I used is a Swan. Swan can power almost any IoT project as it has a massive 640KB of RAM, is expandable to 55 pins to access additional I/O and buses, and has built-in support for C/C++, Arduino, and CircuitPython.

Notecarrier

Assemble

To assemble this version, you need to insert both Notecard and Swan into Notecarrier AF. Blues has a great picture guide that I recommend following if this is your first time setting up.

Once you've assembled the parts, you'll have a setup that looks like this.

To complete the build I did add the last two things. First, I added an external Molex antenna to the Notecarrier AF's socket. Although the Notecarrier AF has a built-in antenna, the external antenna picks up the GPS signal faster, which was important to me for this project.

Next I added a small Lipo battery and connected its BATTERY to the JST connector on the Notecarrier AF. When I use it outside the office, the lithium battery powers the device.

Here is the setup after installing the antenna and battery:

Setup after installing antenna and battery

NOTE: The Notecarrier-AF includes a micro-USB port that allows you to charge a connected lithium battery if you power the port.

Finally, since I didn't want a bunch of loose pieces flying around while the kids were using it, I taped the lithium battery to the back of the Notecarrier AF and taped the sticky side of the Molex antenna to the back as well (with a little bit of cardboard in the middle) .

Long term I want to build a nice case for the device, but for prototyping this gives me an item my kids can take with them.

Now that you've seen what the hardware looks like, let's look at setting up the cloud backend.

Step 2: Set up the cloud backend

One of the great things about Notecard is that it knows how to send data to the cloud backend Notehub, right out of the box. Notehub is a hosted service designed to connect to Notecard devices and synchronize data.

If you're following along and want to build this project yourself, you'll need to set up an account on notehub.io and then create a new project.

After creating the project, make sure to copy the ProductUID of the new project (see screenshot below) as you will need that identifier to connect Notecard to the new Notehub project.

Step 3: Write the Code
If you remember from Step 1, I was using a Swan MCU to execute the code that drives the child tracker. Swan supports CircuitPython and Arduino, so you can use either, but I chose Arduino because it's a platform I've used with Swan many times before.

If you are new to Swan, you will want to read the Swan Quick Start first, which will help you set up your development environment. If you want to use the same environment as me, complete the Using the Arduino IDE section.

Once you have your development environment set up, you can move on to writing the code itself. In your code, you need to take care of three main things:

Configure note cards

Configure how often to take GPS readings

Handle button presses

Let's address these questions in turn.

NOTE: The complete source code for this project can be found below.

Configure Notecard

Our first coding task is setting up Notecard. Notecard's API is JSON-based, so configuring Notecard is as simple as sending a few JSON requests.

You can send these requests through the online playground, CLI, or SDK for Arduino, Python, C/C++, or Go. In this case we are running on a Swan MCU, so we will use note-arduino, the official library for communicating with Notecard through Arduino compatible microcontrollers.

Specifically, you need to make sure to install the library in the Arduino IDE using the project's installation instructions. note-arduino

After that, you need to start with the following code in your Arduino sketch:

#include

#define serialDebug Serial
#define productUID "com.blues.tj:kidtracker"

Notecard notecard;

void setup() {
serialDebug.begin(115200);

notecard.begin();
notecard.setDebugOutputStream(serialDebug);

J *req1 = notecard.newRequest("hub.set");
JAddStringToObject(req1, "product", productUID);
JAddStringToObject(req1, "mode", "periodic");
JAddNumberToObject(req1, "outbound", 5);
notecard.sendRequest(req1);

serialDebug.println("Setup complete");
}

注意:如果您继续进行操作,请确保将“com.blues.tj:kidtracker”更改为您在上一步中复制的 ProductUID。

首先,它初始化 Notecard ( notecard.begin()) 并设置调试输出流 ( notecard.setDebugOutputStream(serialDebug)),这允许您使用 Arduino IDE 的串行监视器查看日志。

接下来,代码使用 note-arduino 库格式化新hub.set命令并将其发送到 Notecard

J *req1 = notecard.newRequest("hub.set");
JAddStringToObject(req1, "product", productUID);
JAddStringToObject(req1, "mode", "periodic");
JAddNumberToObject(req1, "outbound", 5);
notecard.sendRequest(req1);

该hub.set命令控制您要将数据发送到哪个 Notehub 项目,并允许您为 Notecard 的工作方式设置一些基本配置。

设置"mode"为"periodic"告诉记事本定期连接到蜂窝网络,设置"outbound"为5告诉记事本每五分钟尝试一次连接。Notecard 也可以在连续模式下工作 ( "mode": "continuous"),但保持蜂窝调制解调器连接的恒定会消耗大量电池。

每五分钟发送一次数据已经相当耗电,但对于这个项目,我希望在孩子们骑自行车时经常看到数据,而且在两次使用之间为设备的锂电池充电也没有问题。如果您的项目需要在电池使用方面更加保守,您需要将该"outbound"值设置为相当高的数字。

此时您可以将您的程序发送给 Swan进行测试。如果一切顺利,您应该在 Arduino 串行监视器中看到以下内容:

注意:如果您没有看到日志,您可以在while (!serialDebug)之后添加serialDebug.begin(),这会强制程序等到调试器可用后再继续。只需确保while (!serialDebug)在准备好部署设备时记得删除,因为如果在不存在串行调试器的情况下运行,它将导致无限循环。

如果您返回到您的 Notehub 项目,您现在应该会看到您的设备已列出:

此时,您的 Notecard 已连接到 Notehub,这意味着您已准备好发送位置数据。接下来让我们看看如何配置 Notecard 以收集该位置信息。

配置获取 GPS 读数的频率

Notecard 有许多请求,可让您准确配置应如何获取 GPS 读数。您需要向记事卡发送两个主要命令:card.location.mode和card.location.track.

下面的代码(您可以在现有hub.set代码下添加)发出这两个请求。

J *req2 = notecard.newRequest("card.location.mode");
JAddStringToObject(req2, "mode", "periodic");
JAddNumberToObject(req2, "seconds", 300);
notecard.sendRequest(req2);

J *req3 = notecard.newRequest("card.location.track");
JAddBoolToObject(req3, "start", true);
JAddBoolToObject(req3, "heartbeat", true);
JAddNumberToObject(req3, "hours", 12);
notecard.sendRequest(req3);

在card.location.mode通话中,设置"mode"to"periodic"告诉 Notecard 定期激活其 GPS 模块(而不是连续),设置"seconds"to300告诉 Notecard 尝试每 300 秒或五分钟读取一次 GPS 读数。

该card.location.track请求启动实际的跟踪过程。在周期性 GPS 模式下,Notecard 足够智能,仅在设备移动时才获取 GPS 读数(它使用内置的加速度计知道这一点)。将heartbeat选项设置为true告诉记事卡每 12 小时读取一次(因为我们设置"hours"为12),即使记事卡没有移动。

简而言之,这些请求告诉 Notecard 开始获取 GPS 读数;如果设备正在移动,则每五分钟读取一次;如果没有,则每十二小时阅读一次。

注意:如果您想了解有关如何将 Notecard 配置为资产跟踪器的更多信息,请务必阅读Notecard 的资产跟踪综合指南。
如果您将此程序发送到您的 Swan,您应该会在串行监视器中看到以下输出:

pYYBAGJ4yb2AGcX-AAD3eRgODCg134.png

更重要的是,如果您查看 Notehub 项目中的“事件”选项卡,您应该会开始看到_track.qo进入的文件。

如果您双击一个事件并转到下一页的 JSON 选项卡,您可以查看捕获的 GPS 信息。

可能需要几分钟才能看到您的第一个事件,因为记事卡可能需要一些时间才能建立 GPS 连接。尽量将您的设备放置在对天空有清晰视线的位置,并尽可能避开地下室等狭窄地方。

此外,请记住,Notecard 仅在设备移动时才会获取新的 GPS 读数,因此您可能需要稍微摇晃设备才能_track.qo在测试期间看到后续文件进入。

稍后我们将看看如何在地图上可视化您的 GPS 读数,但让我们首先结束这个项目的代码,看看如何让按下按钮发送带有位置的 SMS 消息。

通知按钮

我希望我的追踪器有一个孩子可以按下的按钮,并让该按钮向父母发送一条带有追踪器当前位置的短信。这是大多数商业儿童追踪器的一个功能,所以我相信这也是其他父母想要的。

有很多方法可以处理 IoT 应用程序中的按钮按下,但 Swan 包含一个用户按钮 ( USER_BTN),用于处理这种确切的场景。

要使用此按钮,您可以#define先在草图顶部添加以下行。

#define BUTTON_PIN USER_BTN
接下来,添加以下布尔值,您将使用它来跟踪用户是否请求了他们的位置。

bool locationRequested = false;
之后,将以下代码添加到现有setup()函数的末尾。此代码将中断附加到您的 Swan 按钮,并在触发时调用名为ISR.

pinMode(BUTTON_PIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), ISR, FALLING);
您需要定义ISR(中断服务例程),您可以通过将以下函数添加到您的草图来完成。

void ISR(void) {
serialDebug.println("Button pressed");
locationRequested = true;
}
locationRequested当用户按下 Swan 的用户按钮时,您的程序现在会更改布尔值。要响应该更改,您需要在 Arduinoloop()函数中监视该布尔值。您可以使用下面的代码来做到这一点。

void loop() {
if (!locationRequested) {
 return;
}

J *req = notecard.newRequest("card.location");
J *resp = notecard.requestAndResponse(req);
serialDebug.println("Location:");
double lat = JGetNumber(resp, "lat");
double lon = JGetNumber(resp, "lon");
serialDebug.println(lat, 10);
serialDebug.println(lon, 10);

if (lat == 0) {
 serialDebug.println("The Notecard does not yet have a location");
 // Wait a minute before trying again.
 delay(1000 * 60);
 return;
}

// http://maps.google.com/maps?q=,
char buffer[100];
snprintf(
 buffer,
 sizeof(buffer),
 "Your kids are requesting you. https://maps.google.com/maps?q=%.8lf,%.8lf",
 lat,
 lon
 );
serialDebug.println(buffer);

J *req2 = notecard.newRequest("note.add");
JAddStringToObject(req2, "file", "twilio.qo");
JAddBoolToObject(req2, "sync", true);
J *body = JCreateObject();
JAddStringToObject(body, "message", buffer);
JAddItemToObject(req2, "body", body);
notecard.sendRequest(req2);

locationRequested = false;
serialDebug.println("Location sent successfully.");
}
这里有很多,所以让我们分解这段代码。首先,请注意这里的所有逻辑都由if (!locationRequested) { return; }检查门控。也就是,如果用户没有按下按钮,在loop().

如果用户请求了一个位置,我们接下来使用 Notecard 的card.location请求来获取当前位置并将其打印出来。

J *req = notecard.newRequest("card.location");
J *resp = notecard.requestAndResponse(req);
serialDebug.println("Location:");
double lat = JGetNumber(resp, "lat");
double lon = JGetNumber(resp, "lon");
serialDebug.println(lat, 10);
serialDebug.println(lon, 10);
Notecard may not have a location yet (it takes a few minutes to get a GPS location when the project starts), so we next Check if card.location returns a valid location. If not, we log another message and wait a minute before trying again.

if (lat == 0) {
serialDebug.println("The Notecard does not yet have a location");
// Wait a minute before trying again.
delay(1000 * 60);
return;
}
If we do have a valid Location, we next format a message that contains a Google Maps link to the tracker's current location.

char buffer[100];
snprintf(
buffer,
sizeof(buffer),
"Your kids are requesting you. https://maps.google.com/maps?q=%.8lf,%.8lf",
lat,
lon
);
serialDebug.println(buffer);
After that, we use Notecard's note.add request to send the message to Notehub.

J *req2 = notecard.newRequest("note.add");
JAddStringToObject(req2, "file", "twilio.qo");
JAddBoolToObject(req2, "sync", true);
J *body = JCreateObject();
JAddStringToObject (body, "message", buffer);
JAddItemToObject(req2, "body", body);
notecard.sendRequest(req2);
Finally, we flip the boolean back to false because we are now done processing the button press.

locationRequested = false;
serialDebug.println("Location sent successfully.");
With that, you are done writing the code needed to run the project! Before pushing the code to the device for the last time, go into the Tools menu of the Arduino IDE and switch the C runtime library to one that supports floating point printing and scanning, as we are using this in the logic of placing the latitude and longitude into the Google Maps URL .

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号