CoAP and IOT system return JSON, coap returns json

Source: Internet
Author: User

CoAP and IOT system return JSON, coap returns json

After adding JSON support to IoT CoAP, it becomes very interesting. At least we can get the expected results. In the previous article, we introduced some common tools-the CoAP command line tool set.

CoAP client code example

Before starting, we need a client code so that our server can return the correct data and parse it.

var coap = require('coap');var requestURI = 'coap://localhost/';var url = require('url').parse(requestURI + 'id/1/');console.log("Request URL: " + url.href);var req = coap.request(url);var bl = require('bl');req.setHeader("Accept", "application/json");req.on('response', function(res) {    res.pipe(bl(function(err, data) {        var json = JSON.parse(data);        console.log(json);    }));});req.end();

The Code is a little long and has a lot of content, but the core is this sentence:

req.setHeader("Accept", "application/json");

In this case, we only need to judge on our server,

if(req.headers['Accept'] == 'application/json') {     //do something };

In this way, data can be returned. (Reprinted and retained: CoAP and IOT system return JSON)

CoAP Server code

Server-side code is relatively simple. Let's take a look.

if (req.headers['Accept'] == 'application/json') {        parse_url(req.url, function(result){            res.end(result);        });        res.code = '2.05';    }

Whether the request is in JSON format, and then returns a 205, that is, Content, which is designed to request a URL to return the corresponding data. For example

 coap://localhost/id/1/

In this case, the request is for data with ID 1, that is

[ { id: 1, value: 'is id 1', sensors1: 19, sensors2: 20 }]

Parse_url only reads data from the database.

function parse_url(url ,callback) {    var db = new sqlite3.Database(config["db_name"]);    var result = [];    db.all("SELECT * FROM basic;", function(err, rows) {        callback(JSON.stringify(rows));    })}

And all of them are displayed. The design is not good, but it is almost done now.


Iot engineering learning route

1. Programming: C. Assembly
2. Embedded System Basics
3. hardware devices and platforms: node devices (such as T-Mote Sky and TI MSP430) and platforms (such as Arduino and Raspberry Pi)
3. Wireless Sensor Networks: Basic knowledge, protocol stack (ZigBee, IETF 6 LowPan, CoAP, etc)
4. Wireless Sensor Network Operating System (TinyOS, Contiki, etc)
5. Find typical application cases and Academic Review on the Internet. This work is not done at the end, but is carried out in the previous four steps.

I am a freshman, majoring in Iot applications. What should I learn if I want to learn something?

We recommend that you study the contiki system. ipv6 is the mainstream for a while, and the contiki operating system is specially designed for IOT, including the latest ipv6 protocol, rpl protocol, and coap protocol. Query the information by yourself, basically in English.
 

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.