Skip to content
/ luacoap Public
forked from rosds/luacoap

lua binding for smcp coap library

Notifications You must be signed in to change notification settings

vwout/luacoap

 
 

Repository files navigation

luacoap

This project is a simple lua binding to the Nyoci CoAP stack. The current version only implements some client calls.

Building

To build this project, cmake and lua are required, plus a few development libraries:

export LUA_VERSION=$(lua -e 'print(string.sub(_VERSION, 5))')

sudo apt update
sudo apt install -y git cmake libtool autoconf-archive openssl libssl-dev lua${LUA_VERSION}-dev

To perform the actual build of luacoap, execute:

git clone https://github.com/vwout/luacoap
mkdir -p luacoap/build
cd luacoap/build
cmake ..
make

the output is coap.so, a shared library that can be loaded into lua.

To use it in lua using require("coap") install this module with:

sudo make install

and you can use it independently of your location.

Alternatively, you can download and install the debian package.

Usage

Currently it is only possible to send GET, PUT and POST request using the CoAP client.

Example

coap = require("coap")
client = coap.Client()

function callback(playload)
  print(playload)
end

client:get(coap.CON, "coap://coap.me/test", callback)

The library also supports DTLS. Use the scheme çoaps and the identity and psk in the url, e.g. coaps://identity:psk@server/path.

The current available functions are

client:get([ ConnectionType ,] url [, ContentType, Payload ], [ callback ])
client:put([ ConnectionType ,] url [, ContentType, Payload ], [ callback ])
client:post([ ConnectionType ,] url [, ContentType, Payload ], [ callback ])
client:observe([ ConnectionType ,] url [, ContentType, Payload ], [ callback ])

where:

  • ConnectionType is either coap.CON or coap.NON for confirmable or non-confirmable,
  • url is the address to the resource
  • ContentType is any of the CoAP supported content types
  • Payload is the data you want to send
  • callback is a function that will be executed when the response arrives
Observe Request

The observe request is different from the others since it returns a listener object. This object can be used to control the subscription to the target resource. The listener object implements the following methods.

listener:callback()   -- Executes the callback function provided to the client
listener:listen()     -- Starts observing the resource
listener:stop()       -- Stops the observation
listener:pause()      -- Suspends the observation
listener:continue()   -- Resumes the observation

About

lua binding for smcp coap library

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C 66.7%
  • CMake 27.4%
  • C++ 4.9%
  • Lua 1.0%