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
相关推荐
~yY…s<#>10 分钟前
【计算机网络】传输层协议UDP
网络协议·计算机网络·udp
椰椰椰耶28 分钟前
【HTTP】请求“报头”(Host、Content-Length/Content-Type、User-Agent(简称 UA))
网络·网络协议·http
嘻嘻仙人5 小时前
【网络通信基础与实践第四讲】用户数据报协议UDP和传输控制协议TCP
网络·网络协议·udp·tcp·三次握手·流量控制·拥塞控制
极客小张5 小时前
基于正点原子Linux开发板的智能监控与家电控制系统设计:深度解析Video4Linux和TCP/IP技术栈
linux·运维·c++·物联网·网络协议·tcp/ip·算法
城南云小白13 小时前
web基础+http协议+httpd详细配置
前端·网络协议·http
Tony聊跨境14 小时前
什么是 SSL 代理?
网络·网络协议·ssl
汀、人工智能15 小时前
报错error: RPC failed,curl 16 Error in the HTTP2 framing layer解决方法
网络·git·网络协议·rpc
一叶飘零_sweeeet17 小时前
为什么 Feign 要用 HTTP 而不是 RPC?
java·网络协议·http·spring cloud·rpc·feign
KookeeyLena717 小时前
动态IP与静态IP:哪种更适合用户使用?
网络·网络协议·tcp/ip