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
相关推荐
Aric_Jones8 小时前
HTTP和HTTPS的区别
网络协议·http·https
Ll13045252989 小时前
如何在校园网让虚拟机联网并固定IP
网络·网络协议·tcp/ip
科技块儿10 小时前
【IP】公有&私有IP地址?
服务器·网络协议·tcp/ip
2501_9159184110 小时前
移动端 HTTPS 抓包实战,多工具组合分析与高效排查指南
数据库·网络协议·ios·小程序·https·uni-app·iphone
3***499611 小时前
前端WebSocket教程,实时通信案例
网络·websocket·网络协议
雪芽蓝域zzs14 小时前
uni-app 将 base64 图片编码转为 Blob 本地文件路径
网络协议·udp·uni-app
Eloudy17 小时前
节点内 cuda GPU 之间 P2P IPC 通信的硬件机制参考
网络协议·p2p
atsec17 小时前
atsec完成Newland NPT的P2PE PA评估
服务器·网络协议·npt·p2pe
奥吼吼~~18 小时前
标准输入输出stdio和JSON-RPC
网络协议·rpc·json
我命由我1234520 小时前
Java 开发 - 粘包处理器 - 基于消息头 + 消息体(魔数验证、长度验证)
java·网络·后端·网络协议·java-ee·intellij-idea·intellij idea