Skip to Content
物联网编程
book

物联网编程

by Andy King
July 2025
Intermediate to advanced
424 pages
5h 42m
Chinese
O'Reilly Media, Inc.
Content preview from 物联网编程

第 9 章 CoAP 客户端集成 CoAP 客户端集成

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

收到请求。

发送通知。

谁在监听?

本章将继续探讨 CoAP 的客户端实现,特别是在 CDA 中的实现,并对在 GDA 中构建 CoAP 客户端提供一些指导。

本章内容

第 8 章讨论了 CoAP 的基础知识和在 GDA 中托管服务器的开发,以及在 CDA 中启用 CoAP 服务器的可选练习。本章的重点是构建一个将成为 CDA 一部分的 CoAP 客户端,并通过可选练习使 GDA 支持 CoAP 客户端。如果完成本章的练习,你将学会如何将 CoAP 客户端功能集成到 CDA 中(也可选择集成到 GDA 中),以便与 GDA 的 CoAP 服务器进行通信。

设计概念

让我们快速回顾一下第 8 章中讨论的两个客户端连接示例,并对每个示例稍作修改。我们将在本章中更详细地讨论它们--即作为 CoAP 客户端的 CDA 和作为 CoAP 服务器的 GDA。

例 A-1:CDA 向 GDA 的 CoAP 服务器写入遥测更新。

  1. CDA 会将新数据写入 GDA CoAP 服务器托管的单个资源。该资源的名称为 PIOT/ConstrainedDevice/SensorMsg。

  2. CDA 将连接到 GDA 托管的coap://localhost:5683/PIOT/ConstrainedDevice/SensorMsg资源,并发出 PUT 请求,其有效载荷为转换为 JSON 的SensorData 实例。

  3. 成功时,服务器将响应一个响应代码,表明资源已创建或更改。如果失败,客户端需要处理异常或不成功的响应代码,并做出相应反应。

例 A-2:CDA 在 GDA 的 CoAP 服务器上观察执行器命令。

  1. CDA 将通过 OBSERVE 请求(本质上是稍作修改的 GET)监控现有资源的更新。资源名称为 PIOT/ConstrainedDevice/ActuatorCmd。

  2. CDA 将连接到位于coap://localhost:5683/PIOT/ConstrainedDevice/ActuatorCmd 的资源,并发出 OBSERVE 请求。

  3. 只要 GDA 有需要发送到 CDA 的ActuatorData 实例(JSON 有效负载的形式),GDA 就会做出响应。如果没有更新,两个应用程序都不会采取任何行动。

GDA 承载着这两种资源,并且已经设计好支持这些请求类型--例 A-1 中的 OUT 和例 A-2 中的 OBSERVE (GET)。

如果我们想转换角色,让 CDA 托管我们的资源,而由 GDA 发出请求呢?正如第 8 章中所讨论的,这当然是可行的,而且您在进行可选练习时可能已经选择构建一个 CoAP 服务器作为 CDA 的一部分。

让我们来看两个类似的例子,但 CDA 作为服务器,GDA 作为客户端。

例 B-1:GDA 向 CDA 的 CoAP 服务器写入执行器命令。

  1. GDA 会将新数据写入 CDA 的 CoAP 服务器中托管的单个资源。该资源的名称为PIOT/ConstrainedDevice/ActuatorCmd

  2. GDA 将连接到 CDA 托管的coap://localhost:5683/PIOT/ConstrainedDevice/ActuatorCmd ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

What Successful Project Managers Do

What Successful Project Managers Do

W. Scott Cameron, Jeffrey S. Russell, Edward J. Hoffman, Alexander Laufer
How to Overcome a Power Deficit

How to Overcome a Power Deficit

Cyril Bouquet, Jean-Louis Barsoux
The Human Factor in AI-Based Decision-Making

The Human Factor in AI-Based Decision-Making

Philip Meissner, Christoph Keding

Publisher Resources

ISBN: 9798341662896