使用IoT-CoAP构建物联网

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

  1. npm install iot-coap

1.新建index.js

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

  1. var iotcoap = require('iot-coap');
  2. iotcoap.run();
  3. iotcoap.rest.run();

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

2.创建iot.js

  1. exports.config = {
  2. "db_name": "iot.db",
  3. "mongodb_name": "iot",
  4. "mongodb_documents": "iot",
  5. "db": "mongodb",
  6. "table_name": "basic",
  7. "keys":[
  8. "id",
  9. "value",
  10. "sensors1",
  11. "sensors2"
  12. ],
  13. "db_table": "id integer primary key, value text, sensors1 float, sensors2 float",
  14. "mongodb_init":[
  15. {
  16. id: 1,
  17. value: "is id 1",
  18. sensors1: 19,
  19. sensors2: 20
  20. },
  21. {
  22. id: 2,
  23. value: "is id 2",
  24. sensors1: 20,
  25. sensors2: 21
  26. }
  27. ],
  28. "init_table":[
  29. "insert or replace into basic (id,value,sensors1,sensors2) VALUES (1, 'is id 1', 19, 20);",
  30. "insert or replace into basic (id,value,sensors1,sensors2) VALUES (2, 'is id 2', 20, 21);"
  31. ],
  32. "query_table":"select * from basic;",
  33. "rest_url": "/id/:id",
  34. "rest_post_url": "/",
  35. "rest_port": 8848
  36. };

3.运行(run)

  1. node index.js

show:

  1. coap listening at coap://0.0.0.0:5683
  2. restify listening at http://0.0.0.0:8848