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
相关推荐
caimouse19 分钟前
TDI (传输驱动接口 Transport Driver Interface) 详细分析
网络协议
shengnan_wsn4 小时前
【协议】【TCP】
网络·网络协议·tcp/ip
消失的旧时光-19435 小时前
第 3 篇:机器人 TCP 长连接如何稳定运行?心跳、掉线检测与自动重连
网络协议·tcp/ip·机器人
不弃君6 小时前
在 Windows 上用 MinGW 搭建 lwIP 调试环境:方法与踩坑实录
windows·网络协议·tcp/ip·嵌入式·mingw·lwip
NeilYuen8 小时前
实现TCP发送HTTP请求
网络协议·tcp/ip·http
ARoger_miu5711 小时前
opsf笔记
网络·网络协议
caimouse11 小时前
tcpip.sys 网络层 (IP) 详细分析
网络·网络协议·tcp/ip
wuhuhuan12 小时前
Day17:HTTP 接口测试与链式调用 — sendRequest 从入门到实战
网络·网络协议·http
m0_5474866612 小时前
《网络协议安全》全套PPT课件(太原理工大学)
网络协议·安全·网络安全
caimouse12 小时前
tcpip.sys 传输层 (TCP) 详细分析
网络·网络协议·tcp/ip