4.0.easyiot.md.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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="#简单物联网"><span class="toc-section-number">1</span> 简单物联网</a><ul>
  63. <li><a href="#硬件通信"><span class="toc-section-number">1.1</span> 硬件通信</a><ul>
  64. <li><a href="#串口通信"><span class="toc-section-number">1.1.1</span> 串口通信</a></li>
  65. </ul></li>
  66. <li><a href="#硬件"><span class="toc-section-number">1.2</span> 硬件</a><ul>
  67. <li><a href="#arduino"><span class="toc-section-number">1.2.1</span> Arduino</a></li>
  68. <li><a href="#继电器"><span class="toc-section-number">1.2.2</span> 继电器</a></li>
  69. </ul></li>
  70. </ul></li>
  71. </ul>
  72. </div>
  73. <h1 id="简单物联网"><span class="header-section-number">1</span> 简单物联网</h1>
  74. <p>到这时,我们算搭建了一个简单的REST服务了。接着我们可以简单的做一个最小的物联网系统,将我们的单片机、MCU等等连上网。</p>
  75. <div class="figure">
  76. <img src="./images/arch.jpeg" alt="硬件结构图" />
  77. <p class="caption">硬件结构图</p>
  78. </div>
  79. <p>考虑到如果我们只是单一连接各个节点,那么系统的结构图,同下所示</p>
  80. <div class="figure">
  81. <img src="./images/fullconnected.png" alt="全连接" />
  82. <p class="caption">全连接</p>
  83. </div>
  84. <p>下面的星形结构图类似于我们在接下来所要构建的系统</p>
  85. <div class="figure">
  86. <img src="./images/star.png" alt="星形结构图" />
  87. <p class="caption">星形结构图</p>
  88. </div>
  89. <p>一个用于控制真实电器的硬件实物图</p>
  90. <div class="figure">
  91. <img src="./images/hardware.jpg" alt="简单实物图" />
  92. <p class="caption">简单实物图</p>
  93. </div>
  94. <h2 id="硬件通信"><span class="header-section-number">1.1</span> 硬件通信</h2>
  95. <h3 id="串口通信"><span class="header-section-number">1.1.1</span> 串口通信</h3>
  96. <p>Arduino与Raspberry Pi通过串口通信的方式实现通信,相互传输所需要的数据,Raspberry Pi将资源传于互联网上对应的接口,接口可以在互联网上被访问。Laravel框架构架于服务器之上,将Raspbery Pi获取过来的数据存储于MySQL数据,再以REST服务的方式共享数据,互联网上的其他设备便可以通过网络来访问这些设备。Ajax用于将后台的数据以不需要刷新的方式传递到网站前台,通过HighCharts框架显示给终端用户。</p>
  97. <h4 id="python"><span class="header-section-number">1.1.1.1</span> Python</h4>
  98. <p>1.在Windows中的串口通常是<code>COM1</code>,<code>COM0</code>等等</p>
  99. <div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">ser<span class="op">=</span>serial.Serial(<span class="st">&quot;COM0&quot;</span>,<span class="dv">9600</span>)</code></pre></div>
  100. <p>2.Mac OS系统中位于/dev目录下,名字类似于<code>tty.usbmodem1451</code>。</p>
  101. <div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"> serial.Serial(<span class="st">&quot;/dev/tty.usbmodem1451&quot;</span>,<span class="dv">9600</span>)</code></pre></div>
  102. <p>3.在Linux内核的系统中虚拟串口用的节点是ttyACM,位于/dev目录下。</p>
  103. <div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"> serial.Serial(<span class="st">&quot;/dev/ttyACM0&quot;</span>,<span class="dv">9600</span>)</code></pre></div>
  104. <blockquote>
  105. <p>串行接口是一种可以将接受来自CPU的并行数据字符转换为连续的串行数据流发送出去,同时可将接受的串行数据流转换为并行的数据字符供给CPU的器件。一般完成这种功能 的电路,我们称为串行接口电路。</p>
  106. </blockquote>
  107. <p>便是打开这个设备,以9600的速率传输数据。</p>
  108. <p>程序框架如下所示:</p>
  109. <div class="figure">
  110. <img src="./images/raspberrypi_flow.png" alt="Raspberry Pi" />
  111. <p class="caption">Raspberry Pi</p>
  112. </div>
  113. <p>代码如下:</p>
  114. <div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="im">import</span> json
  115. <span class="im">import</span> urllib2
  116. <span class="im">import</span> serial
  117. <span class="im">import</span> time
  118. url<span class="op">=</span><span class="st">&quot;http://www.xianuniversity.com/athome/1&quot;</span>
  119. <span class="cf">while</span> <span class="dv">1</span>:
  120. <span class="cf">try</span>:
  121. date<span class="op">=</span>urllib2.urlopen(url)
  122. result<span class="op">=</span>json.load(date)
  123. status<span class="op">=</span>result[<span class="dv">0</span>][<span class="st">&quot;led1&quot;</span>]
  124. ser<span class="op">=</span>serial.Serial(<span class="st">&quot;/dev/ttyACM0&quot;</span>,<span class="dv">9600</span>)
  125. <span class="cf">if</span> status<span class="op">==</span><span class="dv">1</span> :
  126. ser.write(<span class="st">&quot;1&quot;</span>)
  127. <span class="cf">elif</span> status<span class="op">==</span><span class="dv">0</span>:
  128. ser.write(<span class="st">&quot;0&quot;</span>)
  129. time.sleep(<span class="dv">1</span>)
  130. <span class="cf">except</span> urllib2.URLError:
  131. <span class="bu">print</span> <span class="st">&quot;Bad URL or timeout&quot;</span></code></pre></div>
  132. <div class="figure">
  133. <img src="./images/getjson.png" alt="python返回json数据" />
  134. <p class="caption">python返回json数据</p>
  135. </div>
  136. <p>系统还需要对上面的数据进行处理,只拿其中的结果</p>
  137. <div class="figure">
  138. <img src="./images/origin.png" alt="python处理完后的结果" />
  139. <p class="caption">python处理完后的结果</p>
  140. </div>
  141. <p>当改变led的状态后,便可以得到下面的结果</p>
  142. <div class="figure">
  143. <img src="./images/change.png" alt="改变状态后的结果" />
  144. <p class="caption">改变状态后的结果</p>
  145. </div>
  146. <h4 id="ruby"><span class="header-section-number">1.1.1.2</span> Ruby</h4>
  147. <p>如果你用的是Ruby的话,可以尝试使用<code>serialport</code></p>
  148. <p>安装</p>
  149. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">sudo</span> gem install serialport</code></pre></div>
  150. <p>代码大致如下</p>
  151. <div class="sourceCode"><pre class="sourceCode ruby"><code class="sourceCode ruby">require <span class="st">&#39;serialport&#39;</span>
  152. sp = <span class="dt">SerialPort</span>.new <span class="st">&quot;/dev/ACM0&quot;</span>, <span class="dv">9600</span>
  153. sp.write <span class="st">&quot;1&quot;</span></code></pre></div>
  154. <p>注意: 根据相关的系统修改相关的代码。</p>
  155. <h2 id="硬件"><span class="header-section-number">1.2</span> 硬件</h2>
  156. <h3 id="arduino"><span class="header-section-number">1.2.1</span> Arduino</h3>
  157. <p>这样我们在我们的Arduino上所要做的便是,读取串口的结果并控制IO口。</p>
  158. <div class="sourceCode"><pre class="sourceCode c"><code class="sourceCode c"><span class="dt">int</span> ledPort=<span class="dv">13</span>;
  159. <span class="dt">void</span> setup() {
  160. Serial.begin(<span class="dv">9600</span>);
  161. pinMode(ledPort,OUTPUT);
  162. }
  163. <span class="dt">int</span> serialData;
  164. <span class="dt">void</span> loop() {
  165. String inString = <span class="st">&quot;&quot;</span>;
  166. <span class="kw">while</span> (Serial.available()&gt; <span class="dv">0</span>)
  167. {
  168. <span class="dt">int</span> inChar = Serial.read();
  169. <span class="kw">if</span> (isDigit(inChar)) {
  170. inString += (<span class="dt">char</span>)inChar;
  171. }
  172. serialData=inString.toInt();
  173. Serial.print(serialData);
  174. }
  175. <span class="kw">if</span>(serialData==<span class="dv">1</span>){
  176. digitalWrite(ledPort,HIGH);
  177. }<span class="kw">else</span>{
  178. digitalWrite(ledPort,LOW);
  179. }
  180. }</code></pre></div>
  181. <p>如果结果是1的话,就让13口为高电平,也就是让灯亮起来。</p>
  182. <h3 id="继电器"><span class="header-section-number">1.2.2</span> 继电器</h3>
  183. <blockquote>
  184. <p>继电器(英文名称:relay)是一种电控制器件,是当输入量(激励量)的变化达到规定要求时,在电气输出电路中使被控量发生预定的阶跃变化的一种电器。它具有控制系统(又称输入回路)和被控制系统(又称输出回路)之间的互动关系。通常应用于自动化的控制电路中,它实际上是用小电流去控制大电流运作的一种“自动开关”。故在电路中起着自动调节、安全保护、转换电路等作用。</p>
  185. </blockquote>
  186. <p>在这里我们可以默认为我们想要为单片机的5V电压控制220V的电器。</p>
  187. <p>最后我们便可以通过些来控制灯的开和关。</p>
  188. </body>
  189. </html>