Skip to content

Commit

Permalink
Merge pull request #164 from eclipse/readme-binding-coap
Browse files Browse the repository at this point in the history
feat: create readme for CoAP binding package
  • Loading branch information
danielpeintner authored Feb 4, 2020
2 parents 808f3b4 + e3bfa11 commit e478b1f
Showing 1 changed file with 90 additions and 0 deletions.
90 changes: 90 additions & 0 deletions packages/binding-coap/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# CoAP Protocol Binding of node-wot

## Getting Started

In the following examples it is shown how to use the CoAP binding of node-wot.

### Prerequisites
* `npm install @node-wot/core`
* `npm install @node-wot/binding-coap`

### Client Example

The client example tries to connect to a TestThing via CoAP and reads a property `string`. The ThingDescription is located under the follwing CoAP uri coap://plugfest.thingweb.io:5683/TestThing.

`node example-client.js`

```
// example-client.js
Servient = require("@node-wot/core").Servient
CoapClientFactory = require("@node-wot/binding-coap").CoapClientFactory
Helpers = require("@node-wot/core").Helpers
// create Servient and add CoAP binding
let servient = new Servient();
servient.addClientFactory(new CoapClientFactory(null));
let wotHelper = new Helpers(servient);
wotHelper.fetch("coap://plugfest.thingweb.io:5683/TestThing").then(async (td) => {
// using await for serial execution (note 'async' in then() of fetch())
try {
servient.start().then((WoT) => {
WoT.consume(td).then((thing) => {
// read a property "string" and print the value
thing.readProperty("string").then((s) => {
console.log(s);
});
});
});
} catch (err) {
console.error("Script error:", err);
}
}).catch((err) => { console.error("Fetch error:", err); });
```

### Server Example

The server example produces a thing that allows for setting a property `count`. The thing is reachable through CoAP. Additional additional handlers could be added.

`node example-server.js`

```
// example-server.js
Servient = require("@node-wot/core").Servient
CoapServer = require("@node-wot/binding-coap").CoapServer
Helpers = require("@node-wot/core").Helpers
// create Servient add HTTP binding
let servient = new Servient();
servient.addServer(new CoapServer());
servient.start().then((WoT) => {
WoT.produce({
"@context": "https://www.w3.org/2019/wot/td/v1",
title: "MyCounter",
properties: {
count: {
type: "integer"
}
}
}).then((thing) => {
console.log("Produced " + thing.title);
thing.writeProperty("count", 0)
thing.expose().then(() => {
console.info(thing.title + " ready");
console.info("TD : " + JSON.stringify(thing.getThingDescription()));
thing.readProperty("count").then((c) => {
console.log("cound is " + c);
});
});
});
});
```


### More Details

see https://github.com/eclipse/thingweb.node-wot/

0 comments on commit e478b1f

Please sign in to comment.