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
相关推荐
哑巴语天雨10 小时前
前端面试-网络协议篇
websocket·网络协议·http·面试·https
ktkiko1111 小时前
Websocket——心跳检测
网络·websocket·网络协议
小梁不秃捏12 小时前
HTTP 常见状态码技术解析(应用层)
网络·网络协议·计算机网络·http
yourkin66614 小时前
HTTPS(下)
服务器·网络协议·https
元气满满的热码式15 小时前
logstash中的input插件(http插件,graphite插件)
网络·网络协议·http·elasticsearch·云原生
豪宇刘17 小时前
从三个维度了解 RPC(Remote Procedure Call,远程过程调用)
网络·网络协议·rpc
人工干智能1 天前
科普:你的笔记本电脑中有三个IP:127.0.0.1、无线网 IP 和局域网 IP;两个域名:localhost和host.docker.internal
网络协议·tcp/ip·电脑
anddddoooo1 天前
域内证书维权
服务器·网络·网络协议·安全·网络安全·https·ssl
mit6.8241 天前
[实现Rpc] 通信-Muduo库的实现 | && 完美转发 | reserve | unique_lock
c++·网络协议·rpc
IsToRestart1 天前
什么是RPC,和HTTP有什么区别?
网络协议·http·rpc