3.1.iot-coap.md.html 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <meta http-equiv="Content-Style-Type" content="text/css" />
  6. <meta name="generator" content="pandoc" />
  7. <title></title>
  8. <style type="text/css">code{white-space: pre;}</style>
  9. <style type="text/css">
  10. div.sourceCode { overflow-x: auto; }
  11. table.sourceCode, tr.sourceCode, td.lineNumbers, td.sourceCode {
  12. margin: 0; padding: 0; vertical-align: baseline; border: none; }
  13. table.sourceCode { width: 100%; line-height: 100%; }
  14. td.lineNumbers { text-align: right; padding-right: 4px; padding-left: 4px; color: #aaaaaa; border-right: 1px solid #aaaaaa; }
  15. td.sourceCode { padding-left: 5px; }
  16. code > span.kw { color: #007020; font-weight: bold; } /* Keyword */
  17. code > span.dt { color: #902000; } /* DataType */
  18. code > span.dv { color: #40a070; } /* DecVal */
  19. code > span.bn { color: #40a070; } /* BaseN */
  20. code > span.fl { color: #40a070; } /* Float */
  21. code > span.ch { color: #4070a0; } /* Char */
  22. code > span.st { color: #4070a0; } /* String */
  23. code > span.co { color: #60a0b0; font-style: italic; } /* Comment */
  24. code > span.ot { color: #007020; } /* Other */
  25. code > span.al { color: #ff0000; font-weight: bold; } /* Alert */
  26. code > span.fu { color: #06287e; } /* Function */
  27. code > span.er { color: #ff0000; font-weight: bold; } /* Error */
  28. code > span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
  29. code > span.cn { color: #880000; } /* Constant */
  30. code > span.sc { color: #4070a0; } /* SpecialChar */
  31. code > span.vs { color: #4070a0; } /* VerbatimString */
  32. code > span.ss { color: #bb6688; } /* SpecialString */
  33. code > span.im { } /* Import */
  34. code > span.va { color: #19177c; } /* Variable */
  35. code > span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
  36. code > span.op { color: #666666; } /* Operator */
  37. code > span.bu { } /* BuiltIn */
  38. code > span.ex { } /* Extension */
  39. code > span.pp { color: #bc7a00; } /* Preprocessor */
  40. code > span.at { color: #7d9029; } /* Attribute */
  41. code > span.do { color: #ba2121; font-style: italic; } /* Documentation */
  42. code > span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
  43. code > span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
  44. code > span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
  45. </style>
  46. <link rel="stylesheet" href="css/vendor.css" type="text/css" />
  47. </head>
  48. <body>
  49. <html xmlns="http://www.w3.org/1999/xhtml" xmlns:wb="http://open.weibo.com/wb" >
  50. <head>
  51. <title>一步步搭建物联网系统(教你设计物联网系统)</title>
  52. <meta name="keywords" content="设计物联网系统,物联网系统设计">
  53. <meta name="description" content="一份关于如何设计物联网系统的文档">
  54. <link rel="stylesheet" href="../css/vendor.css" />
  55. </head>
  56. <body class="container inner wrap">
  57. <h1>一步步搭建物联网系统(教你设计物联网系统)</h1>
  58. </body>
  59. </html>
  60. <div id="TOC">
  61. <ul>
  62. <li><a href="#使用iot-coap构建物联网"><span class="toc-section-number">0.1</span> 使用IoT-CoAP构建物联网</a></li>
  63. </ul>
  64. </div>
  65. <h2 id="使用iot-coap构建物联网"><span class="header-section-number">0.1</span> 使用IoT-CoAP构建物联网</h2>
  66. <p>(<code>注意</code>:windows系统npm install失败时,需要自己建立一个C:and Settings[USERNAME]Data文件)</p>
  67. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">npm</span> install iot-coap</code></pre></div>
  68. <p>1.新建<strong>index.js</strong></p>
  69. <p><code>注意</code>: 如果已经存在一个index.js文件,请将下面内容添加到文件末尾(create index.js, and add)</p>
  70. <div class="sourceCode"><pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">var</span> iotcoap <span class="op">=</span> <span class="at">require</span>(<span class="st">&#39;iot-coap&#39;</span>)<span class="op">;</span>
  71. <span class="va">iotcoap</span>.<span class="at">run</span>()<span class="op">;</span>
  72. <span class="va">iotcoap</span>.<span class="va">rest</span>.<span class="at">run</span>()<span class="op">;</span></code></pre></div>
  73. <p><code>注意</code>:在db配置可以选择mongodb和sqlite3,替换所需要的数据库即可。(you can choice db on iot.js with ‘sqlite’ or ‘mongodb’)</p>
  74. <p>2.创建<strong>iot.js</strong></p>
  75. <div class="sourceCode"><pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="va">exports</span>.<span class="at">config</span> <span class="op">=</span> <span class="op">{</span>
  76. <span class="st">&quot;db_name&quot;</span><span class="op">:</span> <span class="st">&quot;iot.db&quot;</span><span class="op">,</span>
  77. <span class="st">&quot;mongodb_name&quot;</span><span class="op">:</span> <span class="st">&quot;iot&quot;</span><span class="op">,</span>
  78. <span class="st">&quot;mongodb_documents&quot;</span><span class="op">:</span> <span class="st">&quot;iot&quot;</span><span class="op">,</span>
  79. <span class="st">&quot;db&quot;</span><span class="op">:</span> <span class="st">&quot;mongodb&quot;</span><span class="op">,</span>
  80. <span class="st">&quot;table_name&quot;</span><span class="op">:</span> <span class="st">&quot;basic&quot;</span><span class="op">,</span>
  81. <span class="st">&quot;keys&quot;</span><span class="op">:</span>[
  82. <span class="st">&quot;id&quot;</span><span class="op">,</span>
  83. <span class="st">&quot;value&quot;</span><span class="op">,</span>
  84. <span class="st">&quot;sensors1&quot;</span><span class="op">,</span>
  85. <span class="st">&quot;sensors2&quot;</span>
  86. ]<span class="op">,</span>
  87. <span class="st">&quot;db_table&quot;</span><span class="op">:</span> <span class="st">&quot;id integer primary key, value text, sensors1 float, sensors2 float&quot;</span><span class="op">,</span>
  88. <span class="st">&quot;mongodb_init&quot;</span><span class="op">:</span>[
  89. <span class="op">{</span>
  90. <span class="dt">id</span><span class="op">:</span> <span class="dv">1</span><span class="op">,</span>
  91. <span class="dt">value</span><span class="op">:</span> <span class="st">&quot;is id 1&quot;</span><span class="op">,</span>
  92. <span class="dt">sensors1</span><span class="op">:</span> <span class="dv">19</span><span class="op">,</span>
  93. <span class="dt">sensors2</span><span class="op">:</span> <span class="dv">20</span>
  94. <span class="op">},</span>
  95. <span class="op">{</span>
  96. <span class="dt">id</span><span class="op">:</span> <span class="dv">2</span><span class="op">,</span>
  97. <span class="dt">value</span><span class="op">:</span> <span class="st">&quot;is id 2&quot;</span><span class="op">,</span>
  98. <span class="dt">sensors1</span><span class="op">:</span> <span class="dv">20</span><span class="op">,</span>
  99. <span class="dt">sensors2</span><span class="op">:</span> <span class="dv">21</span>
  100. <span class="op">}</span>
  101. ]<span class="op">,</span>
  102. <span class="st">&quot;init_table&quot;</span><span class="op">:</span>[
  103. <span class="st">&quot;insert or replace into basic (id,value,sensors1,sensors2) VALUES (1, &#39;is id 1&#39;, 19, 20);&quot;</span><span class="op">,</span>
  104. <span class="st">&quot;insert or replace into basic (id,value,sensors1,sensors2) VALUES (2, &#39;is id 2&#39;, 20, 21);&quot;</span>
  105. ]<span class="op">,</span>
  106. <span class="st">&quot;query_table&quot;</span><span class="op">:</span><span class="st">&quot;select * from basic;&quot;</span><span class="op">,</span>
  107. <span class="st">&quot;rest_url&quot;</span><span class="op">:</span> <span class="st">&quot;/id/:id&quot;</span><span class="op">,</span>
  108. <span class="st">&quot;rest_post_url&quot;</span><span class="op">:</span> <span class="st">&quot;/&quot;</span><span class="op">,</span>
  109. <span class="st">&quot;rest_port&quot;</span><span class="op">:</span> <span class="dv">8848</span>
  110. <span class="op">};</span></code></pre></div>
  111. <p>3.运行(run)</p>
  112. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">node</span> index.js</code></pre></div>
  113. <p>show:</p>
  114. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">coap</span> listening at coap://0.0.0.0:5683
  115. <span class="kw">restify</span> listening at http://0.0.0.0:8848</code></pre></div>
  116. </body>
  117. </html>