Iot system and coap Hello, world
We will give a brief introduction to coap and IOT systems in the previous article (PS: coap and IOT system). Then we will try the application of coap protocol.
Coap Application
Before getting started, we need to access coap: // localhost/, so we need to install a Firefox plug-in copper.
Firefox Copper
: Https://addons.mozilla.org/en-US/firefox/addon/copper-270430/
For test, we can access coap: // vs0.inf. ethz. ch: 5683/
Node coap
Node-coap is a client and server library for coap modelled after the HTTP module.
Node-coap is a library of clients and servers used for coap module modeling. Create a package. JSON file and add our library
{ "dependencies":{ "coap": "0.7.2" }}
Then execute
npm install
We can install our dependencies.
Coap example
So we can create such an app. js file.
const coap = require(‘coap‘) , server = coap.createServer()server.on(‘request‘, function(req, res) { res.end(‘Hello ‘ + req.url.split(‘/‘)[1] + ‘\n‘)})server.listen(function() { console.log(‘server started‘)})
Then execute
node app.js
We can access it in a browser, but now there is nothing. Then we create a client-side JS and run it.
const coap = require(‘coap‘) , req = coap.request(‘coap://localhost/World‘)req.on(‘response‘, function(res) { res.pipe(process.stdout)})req.end()
You can output
Hello World
In this way, we can use the coap protocol to create a service, and then we should use it to create more things, such as generating JSON data and restful.
Others
Iot system coap in progress
Https://github.com/gmszone/iot-coap