一步步搭建物联网系统

 主页   资讯   文章   代码   电子书 

使用IoT-CoAP构建物联网

(注意:windows系统npm install失败时,需要自己建立一个C:\Documents and Settings[USERNAME]\Application Data\npm 文件)

npm install iot-coap

1.新建index.js

注意: 如果已经存在一个index.js文件,请将下面内容添加到文件末尾(create index.js, and add)

var iotcoap         = require('iot-coap');

iotcoap.run();
iotcoap.rest.run();

注意:在db配置可以选择mongodb和sqlite3,替换所需要的数据库即可。(you can choice db on iot.js with 'sqlite' or 'mongodb')

2.创建iot.js

exports.config  = {
    "db_name": "iot.db",
    "mongodb_name": "iot",
    "mongodb_documents": "iot",
    "db": "mongodb",
    "table_name": "basic",
    "keys":[
        "id",
        "value",
        "sensors1",
        "sensors2"
    ],
    "db_table": "id integer primary key, value text, sensors1 float, sensors2 float",
    "mongodb_init":[
        {
            id: 1,
            value: "is id 1",
            sensors1: 19,
            sensors2: 20
        },
        {
            id: 2,
            value: "is id 2",
            sensors1: 20,
            sensors2: 21
        }
    ],
    "init_table":[
        "insert or replace into basic (id,value,sensors1,sensors2) VALUES (1, 'is id 1', 19, 20);",
        "insert or replace into basic (id,value,sensors1,sensors2) VALUES (2, 'is id 2', 20, 21);"
    ],
    "query_table":"select * from basic;",
    "rest_url": "/id/:id",
    "rest_post_url": "/",
    "rest_port": 8848
};

3.运行(run)

node index.js

show:

coap listening at coap://0.0.0.0:5683
restify listening at http://0.0.0.0:8848