2.2.init_env.md.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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="#laravel"><span class="toc-section-number">1.1</span> Laravel</a><ul>
  64. <li><a href="#为什么是-laravel"><span class="toc-section-number">1.1.1</span> 为什么是 Laravel</a></li>
  65. </ul></li>
  66. <li><a href="#安装-laravel"><span class="toc-section-number">1.2</span> 安装 Laravel</a><ul>
  67. <li><a href="#gnulinux安装composer"><span class="toc-section-number">1.2.1</span> GNU/Linux安装Composer</a></li>
  68. </ul></li>
  69. <li><a href="#mysql"><span class="toc-section-number">1.3</span> MySQL</a><ul>
  70. <li><a href="#安装mysql"><span class="toc-section-number">1.3.1</span> 安装MySQL</a></li>
  71. <li><a href="#配置mysql"><span class="toc-section-number">1.3.2</span> 配置MySQL</a></li>
  72. </ul></li>
  73. </ul></li>
  74. </ul>
  75. </div>
  76. <h1 id="环境准备"><span class="header-section-number">1</span> 环境准备</h1>
  77. <h2 id="laravel"><span class="header-section-number">1.1</span> Laravel</h2>
  78. <blockquote>
  79. <p>Laravel是一套简洁、优雅的PHP Web开发框架(PHP Web Framework)。它可以让你从面条一样杂乱的代码中解脱出来;它可以帮你构建一个完美的网络APP,而且每行代码都可以简洁、富于表达力。</p>
  80. </blockquote>
  81. <ul>
  82. <li>RESTful 路由: 通过简单的闭包就能响应HTTP请求。帮你快速开始构建非凡的应用。</li>
  83. <li>强大的数据操纵能力: Laravel自带了强大的Eloquent ORM 和迁移工具。能够完美的与MySQL、Postgres、SQL Server 和 SQLite协同工作。</li>
  84. <li>优雅的模版引擎: PHP代码或轻量级的Blade模版引擎都可无缝融合。Blade模版可以继承,并且拥有极快的解析速度。相信你会喜欢它的。</li>
  85. <li>为明天做准备: 构建大型的企业级应用或者只是提供简单的JSON API;书写强大的控制器或轻巧的RESTful路由,Laravel适应所有级别的开发工作。</li>
  86. <li>可靠的基石: Laravel 的基石是数个Symfony组件,这些经过千锤百炼、可靠的组件为你的应用提供坚实的基础。</li>
  87. <li>基于Composer管理器: Composer 是一套帮你管理第三方扩展包的工具。能够让你迅速在 Packagist 中找到需要的扩展包。</li>
  88. <li>强大的社区支持: 无论你是一个PHP新手还是经验丰富的架构师,都能在社区中找到需要的知识。你可以在IRC中讨论Idea,或者在论坛中发布问题。</li>
  89. <li>测试、重构: Laravel 从开始就将测试作为重点功能。我们提供了灵活的IoC容器,集成了PHPUnit 测试工具。不用担心,这些都很容易上手。</li>
  90. </ul>
  91. <h3 id="为什么是-laravel"><span class="header-section-number">1.1.1</span> 为什么是 Laravel</h3>
  92. <ul>
  93. <li>因为个人喜爱,你也可以用 Ruby On Rails来搭建这样一个功能,或者是Java。</li>
  94. <li>PHP在我的服务器上运行得挺不错的,而且我又不需要重新去写配置那些配置。</li>
  95. <li>Laravel 可以简单的开发我们所需要的功能,换句话说他是 PHP 世界的 Ruby On Rails。</li>
  96. </ul>
  97. <p>这里不会再重述之前的问题,这里只是将需要的步骤一个个写下来,然后丢到这里好好说一下。至于RESTful是什么,前面已经介绍了,就不再重复了。那么下面,我们就用Laravel来搭建一个平台给物联网用的。</p>
  98. <h2 id="安装-laravel"><span class="header-section-number">1.2</span> 安装 Laravel</h2>
  99. <h3 id="gnulinux安装composer"><span class="header-section-number">1.2.1</span> GNU/Linux安装Composer</h3>
  100. <p>GNU/Linux Ubuntu/OpenSUSE下可以执行</p>
  101. <pre><code>$ curl -sS https://getcomposer.org/installer | php</code></pre>
  102. <h4 id="windows安装composer"><span class="header-section-number">1.2.1.1</span> Windows安装Composer</h4>
  103. <p>请直接下载</p>
  104. <p><a href="https://getcomposer.org/Composer-Setup.exe">Composer-Setup</a></p>
  105. <h4 id="mac-os"><span class="header-section-number">1.2.1.2</span> Mac OS</h4>
  106. <p>1.安装Composer</p>
  107. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">brew</span> install homebrew/php/composer</code></pre></div>
  108. <p>2.安装Laravel</p>
  109. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">composer</span> global require <span class="st">&quot;laravel/installer=~1.1&quot;</span></code></pre></div>
  110. <p>3.创建Laravel工程</p>
  111. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">composer</span> create-project laravel/laravel your-project-name --prefer-dist </code></pre></div>
  112. <h4 id="mac-os-1"><span class="header-section-number">1.2.1.3</span> Mac OS</h4>
  113. <p>1.下载laravel.phar</p>
  114. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">wget</span> http://laravel.com/laravel.phar</code></pre></div>
  115. <p>2.重命名</p>
  116. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">mv</span> laravel.phar laravel</code></pre></div>
  117. <p>3.移动到bin中</p>
  118. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">sudo</span> mv laravel /usr/local/bin</code></pre></div>
  119. <p>4.创建项目</p>
  120. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">laravel</span> new blog</code></pre></div>
  121. <h2 id="mysql"><span class="header-section-number">1.3</span> MySQL</h2>
  122. <h3 id="安装mysql"><span class="header-section-number">1.3.1</span> 安装MySQL</h3>
  123. <p><code>出于某些原因,我建议用MariaDB替换MySQL,如果你&quot;真正&quot;需要mysql,将mariadb替换为mysql</code></p>
  124. <p>ps: 在下文中我会继续用MySQL,而不是MariaDB,MairaDB是MySQL的一个分支,真正的开源分支。</p>
  125. <p>Ubuntu/Debian/Mint</p>
  126. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">sudo</span> apt-get install mariadb-server</code></pre></div>
  127. <p>Fedora/Centos</p>
  128. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">sudo</span> yum install mariadb-server </code></pre></div>
  129. <p>openSUSE</p>
  130. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">sudo</span> zypper install mariadb-server</code></pre></div>
  131. <p>Mac OS</p>
  132. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">brew</span> install mariadb</code></pre></div>
  133. <h3 id="配置mysql"><span class="header-section-number">1.3.2</span> 配置MySQL</h3>
  134. <p>修改database.php</p>
  135. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">app/config/database.php</span></code></pre></div>
  136. <p>要修改的就是这个</p>
  137. <div class="sourceCode"><pre class="sourceCode php"><code class="sourceCode php"><span class="st">&#39;mysql&#39;</span> =&gt; <span class="fu">array</span><span class="ot">(</span>
  138. <span class="st">&#39;driver&#39;</span> =&gt; <span class="st">&#39;mysql&#39;</span><span class="ot">,</span>
  139. <span class="st">&#39;host&#39;</span> =&gt; <span class="st">&#39;localhost&#39;</span><span class="ot">,</span>
  140. <span class="st">&#39;database&#39;</span> =&gt; <span class="st">&#39;iot&#39;</span><span class="ot">,</span>
  141. <span class="st">&#39;username&#39;</span> =&gt; <span class="st">&#39;root&#39;</span><span class="ot">,</span>
  142. <span class="st">&#39;password&#39;</span> =&gt; <span class="st">&#39;940217&#39;</span><span class="ot">,</span>
  143. <span class="st">&#39;charset&#39;</span> =&gt; <span class="st">&#39;utf8&#39;</span><span class="ot">,</span>
  144. <span class="st">&#39;collation&#39;</span> =&gt; <span class="st">&#39;utf8_unicode_ci&#39;</span><span class="ot">,</span>
  145. <span class="st">&#39;prefix&#39;</span> =&gt; <span class="st">&#39;&#39;</span><span class="ot">,</span>
  146. <span class="ot">),</span></code></pre></div>
  147. <p>如果你已经有phpmyadmin,似乎对你来说已经很简单了,如果没有的话,就直接用</p>
  148. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">mysql</span> -uroot -p</code></pre></div>
  149. <p>来创建一个新的</p>
  150. <div class="sourceCode"><pre class="sourceCode sql"><code class="sourceCode sql"><span class="kw">CREATE</span> <span class="kw">DATABASE</span> <span class="kw">IF</span> <span class="kw">NOT</span> <span class="kw">EXISTS</span> iot <span class="kw">default</span> charset utf8 COLLATE utf8_general_ci;</code></pre></div>
  151. <p>数据库的目的在于存储数据等等的闲话这里就不多说了,创建一个RESTful的目的在于产生下面的JSON格式数据,以便于我们在Android、Java、Python、jQuery等语言框架或者平台上可以调用,最主要的是可以直接用Ajax来产生更炫目的效果。</p>
  152. <div class="sourceCode"><pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="op">{</span>
  153. <span class="st">&quot;id&quot;</span><span class="op">:</span> <span class="dv">1</span><span class="op">,</span>
  154. <span class="st">&quot;temperature&quot;</span><span class="op">:</span> <span class="dv">14</span><span class="op">,</span>
  155. <span class="st">&quot;sensors1&quot;</span><span class="op">:</span> <span class="dv">12</span><span class="op">,</span>
  156. <span class="st">&quot;sensors2&quot;</span><span class="op">:</span> <span class="dv">12</span><span class="op">,</span>
  157. <span class="st">&quot;led1&quot;</span><span class="op">:</span> <span class="dv">0</span>
  158. <span class="op">}</span></code></pre></div>
  159. </body>
  160. </html>