Hello, world for Iot systems and coap

Source: Internet
Author: User
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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.