CoAP——Libcoap安装和使用(Ubuntu22.04)

1、简介

  • **CoAP(Constrained Application Protocol)**是一种专为受限设备和网络设计的应用层协议。它类似于HTTP,但具有更轻量级的特性,适合用于物联网(IoT)环境中的低功耗和低带宽设备。
  • Libcoap 是一个轻量级的C语言实现的CoAP库,专为资源受限设备和嵌入式系统设计。支持CoAP核心功能:包括GET、POST、PUT、DELETE等基本方法,满足大多数应用场景的需求。
    • GET:检索资源的当前状态或数据。
    • PUT:更新现有资源的状态或数据。
    • POST:创建新的资源或提交数据进行处理。
    • DELETE:删除现有资源。

2、安装

  • 获得Libcoap。
    *

    bash 复制代码
    git clone https://github.com/obgm/libcoap.git
  • 生成configure文件。
    *

    bash 复制代码
    cd libcoap
    ./autogen.sh
  • 生成makefile文件。
    *

    bash 复制代码
    ./configure
    • 报错:
      • Install the package that contains doxygen or disable the doxygen documentation using '--disable-doxygen'.

      • 解决办法:
        *

        bash 复制代码
        ./configure --disable-doxygen
      • 又报错:Install the package that contains a2x (mostly asciidoc) or disable the build of the manpages using '--disable-manpages'.

      • 解决办法:
        *

        bash 复制代码
        ./configure --disable-doxygen --disable-dtls
      • 又报错:Option '--enable-dtls' is set but none of the needed cryptography libraries GnuTLS, OpenSSL, wolfSSL, Mbed TLS or TinyDTLS could be found!

      • 解决办法:
        *

        bash 复制代码
         ./configure --disable-doxygen --disable-manpages --disable-dtls
  • 编译安装。
    *

    bash 复制代码
    make
    sudo make install

3、使用

  • 进入example文件夹,在该文件夹中还有两个可执行文件,coap-client和coap-server。coap-client提供非常丰富的客户端测试指令,而 coap-server提供一个较为简单的服务器端功能。

  • 打开两个终端,分别启动coap-client和coap-server。首先启动服务器。
    *

    bash 复制代码
    ./coap-server
  • 启动客户端。
    *

    bash 复制代码
    ./coap-client -m get coap://localhost
    • 【-m】表示coap访问方法,默认为get方法,此处设置为get方法。除了get方法之外还包括put、post和delete。
  • 查看内置资源。
    *

    bash 复制代码
    ./coap-client -m get coap://[::1]/.well-known/core
    • 解析内容如下:
    XML 复制代码
    </>;title="General Info";ct=0,
    </time>;if="clock";rt="ticks";title="Internal Clock";ct=0;obs,
    </async>;ct=0,
    </example_data>;title="Example Data";ct=0;obs
    • 每个资源用 "," 分隔,具体信息如下:
      • General Info
        • 路径: /
        • 标题: "General Info"
        • 内容格式: ct=0(表示默认格式,如文本)
      • Internal Clock
        • 路径: /time
        • 接口描述: if="clock"
        • 资源类型: rt="ticks"
        • 标题: "Internal Clock"
        • 内容格式: ct=0
        • 可观察: obs(表示该资源是可观察的)
      • Async
        • 路径: /async
        • 内容格式: ct=0
      • Example Data
        • 路径: /example_data
        • 标题: "Example Data"
        • 内容格式: ct=0
        • 可观察: obs
  • 查看/example_data。
    *

    bash 复制代码
    ./coap-client -m get coap://[::1]/example_data
  • 更新/example_data资源的内容。
    *

    bash 复制代码
    ./coap-client -m put -e "hello world" coap://[::1]/example_data
相关推荐
Remember_9931 天前
一文吃透Java WebSocket:原理、实现与核心特性解析
java·开发语言·网络·websocket·网络协议·http·p2p
王码码20351 天前
Flutter for OpenHarmony:stomp_dart_client 打造实时消息引擎(企业级 WebSocket 通信标准) 深度解析与鸿蒙适配指南
网络·websocket·网络协议·flutter·ui·华为·harmonyos
王码码20351 天前
Flutter for OpenHarmony:web_socket 纯 Dart 标准 WebSocket 客户端(跨平台兼容性之王) 深度解析与鸿蒙
android·前端·websocket·网络协议·flutter·华为·harmonyos
中北萌新程序员1 天前
WebSocket(看这一篇就够了)
网络·websocket·网络协议
EmmaXLZHONG1 天前
分布式系统概念与设计笔记(Notes of Distributed Systems Concepts and Design)
笔记·分布式·网络协议·计算机网络
cur1es1 天前
【TCP 协议的相关特性】
java·网络·网络协议·tcp/ip·tcp·滑动窗口·连接管理
!chen1 天前
WebSocket长连接保持与心跳机制
python·websocket·网络协议
一路往蓝-Anbo2 天前
第 7 章:内存地图 (Memory Map) 深度设计——DDR 与 SRAM
linux·stm32·单片机·嵌入式硬件·网络协议
一路往蓝-Anbo2 天前
第 8 章:M33 领航——引导 A35 加载 U-Boot 与 Linux 内核
linux·运维·服务器·stm32·单片机·嵌入式硬件·网络协议
hoududubaba2 天前
ORAN C平面传输和基本功能——基于DMRS的波束赋形和SINR报告
网络·网络协议