tag:blogger.com,1999:blog-6943065401048601062024-02-19T22:49:58.289+08:00ㄘㄊ的根莖類成長記錄Shiunhttp://www.blogger.com/profile/00430694685762085062noreply@blogger.comBlogger50125tag:blogger.com,1999:blog-694306540104860106.post-34119661114573406842018-10-05T19:41:00.000+08:002018-10-05T19:41:16.831+08:00GCP 刪除用不到的VM筆者在之前測試LINE messaging API是採用GCP架設server, 架設時是參考GCP Always Free方案內容架設, 但要是用不到的話還是趕緊關閉掉避免浪費資源吧<br />
<br />
<h4>
GCP Always Free</h4>
<br />
GCP Always Free在以下的限制下免費(詳情請看參考資料)<br />
<br />
<ul>
<li>每月 1 個 f1-micro 執行個體 (僅限美國地區,不含北維吉尼亞)</li>
<li>每月平均 30 GB 硬碟空間、每月平均 5 GB 快照</li>
<li>每月 1 GB 網路輸出量 (從北美輸出至中國和澳洲以外的所有區域目的地)</li>
</ul>
<h4>
</h4>
<h4>
取消靜態IP</h4>
<div>
<br />
之前架設服務時為綁定domain, 筆者將外部IP設定為靜態IP, 但等等刪除VM後靜態IP就不會綁定在VM上, <span style="color: red;">GCP會針對未使用的IP進行收費避免浪費IP的情況</span></div>
<div>
參考: <a href="https://cloud.google.com/compute/pricing">Google Compute Engine 定價</a> 中的「未使用的 IP 位址價格」</div>
<div>
<ol>
<li>開啟「VM執行個體」頁面<br /><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhg9_pNfdwMenS4PLIP1zq9EasktghgWsSeYhB4HJ6cwhLEldMlXrCtO1Uf2dNyH6yADwXC24IZm9uf__uz-M5pzaLgYl5kjPDrDs4JIEJk0KRXBH4fQz-KB4gj_x2VJoWCTOWRbdv-hmUG/s1600/01+%25E6%258B%25B7%25E8%25B2%259D.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="680" data-original-width="1600" height="268" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhg9_pNfdwMenS4PLIP1zq9EasktghgWsSeYhB4HJ6cwhLEldMlXrCtO1Uf2dNyH6yADwXC24IZm9uf__uz-M5pzaLgYl5kjPDrDs4JIEJk0KRXBH4fQz-KB4gj_x2VJoWCTOWRbdv-hmUG/s640/01+%25E6%258B%25B7%25E8%25B2%259D.png" width="640" /></a></li>
<li>進入準備刪除的VM<br /><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhJfF9m_1dzVIjqW1QhBk8Ei46NjVvmh5tMnnTk-MvXNhyphenhyphenJvBvmlzJQk0C1WkBQG7GyU0GGnXe_tYt86D469cFtmc6yu87_oPTkOPCI9EX3C1NCLSgtWxEmNuE4-XL8iFUcM2p7ScpgRka_/s1600/02+%25E6%258B%25B7%25E8%25B2%259D.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="458" data-original-width="1100" height="165" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhJfF9m_1dzVIjqW1QhBk8Ei46NjVvmh5tMnnTk-MvXNhyphenhyphenJvBvmlzJQk0C1WkBQG7GyU0GGnXe_tYt86D469cFtmc6yu87_oPTkOPCI9EX3C1NCLSgtWxEmNuE4-XL8iFUcM2p7ScpgRka_/s400/02+%25E6%258B%25B7%25E8%25B2%259D.png" width="400" /></a></li>
<li>點擊右上角的編輯按鈕<br /><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj-gA_qBAJVwI8El-JsjyWQP2fFE05lsJ6__aiRiW2bdP05Hnh1Lw5mFHiWsHGqkDngUL5u5K1uxv7J2iAbzJ1-5qiOasQ0oz9BsHDj48TfC4FNd7T952DePzw1JPtviKLKIgdA1b00N4BM/s1600/03+%25E6%258B%25B7%25E8%25B2%259D.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="634" data-original-width="946" height="267" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj-gA_qBAJVwI8El-JsjyWQP2fFE05lsJ6__aiRiW2bdP05Hnh1Lw5mFHiWsHGqkDngUL5u5K1uxv7J2iAbzJ1-5qiOasQ0oz9BsHDj48TfC4FNd7T952DePzw1JPtviKLKIgdA1b00N4BM/s400/03+%25E6%258B%25B7%25E8%25B2%259D.png" width="400" /></a></li>
<li>點擊網路介面<br /><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhYdGV7hH0qDqdmfHtZB9iQxOPRem2mpwwfv-CK9UUGvjmSlQc_8Xx7BPHrMzattxIykK9L5z3tnZPD5xkV2hm4VUs7FHX1-70LOBbm57tHWdT3KMLRwb-KwifcoUzXRpyhCUUfeOuc-igq/s1600/04+%25E6%258B%25B7%25E8%25B2%259D.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="390" data-original-width="988" height="157" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhYdGV7hH0qDqdmfHtZB9iQxOPRem2mpwwfv-CK9UUGvjmSlQc_8Xx7BPHrMzattxIykK9L5z3tnZPD5xkV2hm4VUs7FHX1-70LOBbm57tHWdT3KMLRwb-KwifcoUzXRpyhCUUfeOuc-igq/s400/04+%25E6%258B%25B7%25E8%25B2%259D.png" width="400" /></a></li>
<li>將外部IP改為「無」或是「臨時」, 此時會看到下方警告說未使用的靜態IP會收費, 接著點擊最下方的「儲存」<br /><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgVpueErpg5zYyvcN8tPiqAPKb2WvOchWIY1iCwkYuCONNKs2QDwOSf6Dwe_vPdAhBXxnaup8nSMdHk-ODim0hK4XmGYrz7PwCUsFTkBM46un5EQDBpAqh8SfP8drBkhpnWIYQ-24L8SFtD/s1600/05+%25E6%258B%25B7%25E8%25B2%259D.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="844" data-original-width="1070" height="315" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgVpueErpg5zYyvcN8tPiqAPKb2WvOchWIY1iCwkYuCONNKs2QDwOSf6Dwe_vPdAhBXxnaup8nSMdHk-ODim0hK4XmGYrz7PwCUsFTkBM46un5EQDBpAqh8SfP8drBkhpnWIYQ-24L8SFtD/s400/05+%25E6%258B%25B7%25E8%25B2%259D.png" width="400" /></a></li>
<li>開啟「VPC網路」下的「外部IP位址」<br /><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiUiCzXb96CRZr5WvPGRaAcAH9ZvmbkLWkA8YNuFo_3v-t0TDrHigXCLMDhS96jlThIzK_hXThjki26k0E_i-H8aY1zYROi_57Fjr0xl6vM39qmsMz9p2sjXkzNfHTrmLIUXvS5z-GYE_RZ/s1600/07+%25E6%258B%25B7%25E8%25B2%259D.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="816" data-original-width="1004" height="325" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiUiCzXb96CRZr5WvPGRaAcAH9ZvmbkLWkA8YNuFo_3v-t0TDrHigXCLMDhS96jlThIzK_hXThjki26k0E_i-H8aY1zYROi_57Fjr0xl6vM39qmsMz9p2sjXkzNfHTrmLIUXvS5z-GYE_RZ/s400/07+%25E6%258B%25B7%25E8%25B2%259D.png" width="400" /></a></li>
<li>勾選原來的靜態IP後點擊「釋放靜態位址」, 接著按下「確定」後即可釋放<br /><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEivPmVntCX0U5IYEM3ZznJKLlP0BCV34_pP9saRK0fGPDlXPVgGHASJsnIUXUmfmpTR7DsXOk2_XcgGeNoN0x_vOBlQp5z5dcSN80cTKf3R_-aYOYJ_oQ08vW2XYG5RaDkh2uTQaiGcUkgh/s1600/08+%25E6%258B%25B7%25E8%25B2%259D.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="520" data-original-width="1168" height="176" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEivPmVntCX0U5IYEM3ZznJKLlP0BCV34_pP9saRK0fGPDlXPVgGHASJsnIUXUmfmpTR7DsXOk2_XcgGeNoN0x_vOBlQp5z5dcSN80cTKf3R_-aYOYJ_oQ08vW2XYG5RaDkh2uTQaiGcUkgh/s400/08+%25E6%258B%25B7%25E8%25B2%259D.png" width="400" /></a><br /><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiWBq2sMR3-JgPAt2CoOlon2ILh3L3NRulP8n4jYCXp1DI5_m9AmLPWGLm6nh9Ku-Nvz_iPlAMj9jnMwzN4sMf_VhAK09tbwqM0fBA844s_X17b8D3wqGC-9bbLLLQopy589I-I6b6rO429/s1600/09.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="972" data-original-width="1512" height="255" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiWBq2sMR3-JgPAt2CoOlon2ILh3L3NRulP8n4jYCXp1DI5_m9AmLPWGLm6nh9Ku-Nvz_iPlAMj9jnMwzN4sMf_VhAK09tbwqM0fBA844s_X17b8D3wqGC-9bbLLLQopy589I-I6b6rO429/s400/09.png" width="400" /></a></li>
</ol>
<h4>
刪除VM</h4>
</div>
<div>
<br />
<ol>
<li>回到VM執行個體<br /><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhg9_pNfdwMenS4PLIP1zq9EasktghgWsSeYhB4HJ6cwhLEldMlXrCtO1Uf2dNyH6yADwXC24IZm9uf__uz-M5pzaLgYl5kjPDrDs4JIEJk0KRXBH4fQz-KB4gj_x2VJoWCTOWRbdv-hmUG/s1600/01+%25E6%258B%25B7%25E8%25B2%259D.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="680" data-original-width="1600" height="268" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhg9_pNfdwMenS4PLIP1zq9EasktghgWsSeYhB4HJ6cwhLEldMlXrCtO1Uf2dNyH6yADwXC24IZm9uf__uz-M5pzaLgYl5kjPDrDs4JIEJk0KRXBH4fQz-KB4gj_x2VJoWCTOWRbdv-hmUG/s640/01+%25E6%258B%25B7%25E8%25B2%259D.png" width="640" /></a></li>
<li>勾選要刪除的VM, 接著按下右上角的刪除<br /><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiqARvrkdf_B8oMMefn02EpgljpbUaDtUCNpNDscRzWW8cI4SfVjdJ0c9uMZksi7wPmLnxYDp6fepNjuVQVHdId6AiVBfJbcdps28N_dj563GNv4-5UhfUHeU9vjMxmLet9SHhYj6q6HglP/s1600/10+%25E6%258B%25B7%25E8%25B2%259D.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="516" data-original-width="1600" height="206" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiqARvrkdf_B8oMMefn02EpgljpbUaDtUCNpNDscRzWW8cI4SfVjdJ0c9uMZksi7wPmLnxYDp6fepNjuVQVHdId6AiVBfJbcdps28N_dj563GNv4-5UhfUHeU9vjMxmLet9SHhYj6q6HglP/s640/10+%25E6%258B%25B7%25E8%25B2%259D.png" width="640" /></a></li>
<li>點擊「刪除」按鍵<br /><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEivBrdXq16ramj-QkS-Gulx7Xl3Y-r3RasoQPuFSlEwdn7QbBz_YfMyF5-Gb4kz68dzhtmab3ReNA6KfQHh92yPOTPDYXl5OKR_JO5timY3h5zqZ5qqF9SA6Dmfw7CgR35gqmR2DnRtMEJ-/s1600/11.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="898" data-original-width="1550" height="230" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEivBrdXq16ramj-QkS-Gulx7Xl3Y-r3RasoQPuFSlEwdn7QbBz_YfMyF5-Gb4kz68dzhtmab3ReNA6KfQHh92yPOTPDYXl5OKR_JO5timY3h5zqZ5qqF9SA6Dmfw7CgR35gqmR2DnRtMEJ-/s400/11.png" width="400" /></a></li>
<li>等待一段時間後VM就會被完全刪除</li>
</ol>
</div>
<h3>
<span style="font-family: inherit;">參考資料</span></h3>
<div>
<ol>
<li><a href="https://cloud.google.com/free/docs/frequently-asked-questions#always-free" target="_blank"><span style="font-family: inherit;">Frequently Asked Questions | Google Cloud Platform Free Tier | Google Cloud</span></a></li>
<li><span style="font-family: inherit;"><a href="https://cloud.google.com/free/docs/always-free-usage-limits" target="_blank">Always Free Usage Limits | Google Cloud Platform Free Tier | Google Cloud</a></span></li>
</ol>
</div>
Shiunhttp://www.blogger.com/profile/00430694685762085062noreply@blogger.com0tag:blogger.com,1999:blog-694306540104860106.post-54132868165487127652018-10-04T23:48:00.000+08:002018-10-05T19:53:05.930+08:00LINE BOT 匯率提醒機器人(3/3) - 實作匯率提醒機器人<h4>
前情提要</h4>
<div>
<br />
前一篇文章 <a href="http://blog.alenshiun.tw/2018/10/line-bot-23-gcphttps-server.html" target="_blank">LINE BOT 匯率提醒機器人(2/3) - 使用GCP架設HTTPS server</a> 完成後現在可以用HTTPS和domain name連接上server, 接著就來串接LINE messaging API完成自動推播功能吧<br />
<br />
<h4>
<span style="font-family: inherit;">開發重點</span></h4>
<div>
<ol>
<li><span style="color: #cccccc; font-family: inherit;">使用GCP服務中的GCE(Google Compute Engine)建立一台Linux server</span></li>
<li><span style="color: #cccccc; font-family: inherit;">使用GCE服務架設網站server, 網站server使用Apache架設</span></li>
<li><span style="color: #cccccc; font-family: inherit;">加入HTTPS支援 - 使用Let’s Encrypt免費SSL憑證</span></li>
<li>註冊LINE developers, 並申請LINE messaging API </li>
<li>使用PHP + SQLite實作LINE Bot傳送匯率到LINE群組的頻道上</li>
</ol>
<h4>
實作匯率提醒機器人 - 申請LINE messaging API</h4>
<div>
<br />
實作LINE定期推播訊息給群組前, 先讓我們要先完成LINE messaging API申請與相關設定</div>
<div>
<ol style="font-weight: 400;">
<li>登入<a href="https://developers.line.me/en/" target="_blank">LINE developers</a>網站</li>
<li>完成第一次申請的步驟, 此步驟請照LINE developers網站提示完成, 完成後即可進入以下的console頁面<br /><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgzbYkOIPnDKYEYzBrq_I2suaHl-sNa_w2-4vZF7zh1fZfmkJ065_uA9UWLAFRVfwCvfPSRoQzU137NGxT2Er7p-nx0VhIHGvnbsO0Yn8GGk9xUyXI9PptDZQsKYUg7TSXsUZPqZciEAk4J/s1600/02+%25E6%258B%25B7%25E8%25B2%259D.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="416" data-original-width="1600" height="166" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgzbYkOIPnDKYEYzBrq_I2suaHl-sNa_w2-4vZF7zh1fZfmkJ065_uA9UWLAFRVfwCvfPSRoQzU137NGxT2Er7p-nx0VhIHGvnbsO0Yn8GGk9xUyXI9PptDZQsKYUg7TSXsUZPqZciEAk4J/s640/02+%25E6%258B%25B7%25E8%25B2%259D.png" width="640" /></a></li>
<li>點擊右上方的Create New Provider進入建立provider頁面</li>
<li>根據需求寫上provider後點選confirm和create<br /><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh0HD5anrcY469NVEVhWjxZqHsEVyVuLer9p6lrKpinPbAn0w6bvgrhJ8YPGz-v_j6Pt3SjxkAGfZlUIqg7Eb7LmuIlsSei2slnqIjSCIcHwo-qHifeJi4Kiripoy66UiEm3yuVYC-XPgAb/s1600/03.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="934" data-original-width="1600" height="372" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh0HD5anrcY469NVEVhWjxZqHsEVyVuLer9p6lrKpinPbAn0w6bvgrhJ8YPGz-v_j6Pt3SjxkAGfZlUIqg7Eb7LmuIlsSei2slnqIjSCIcHwo-qHifeJi4Kiripoy66UiEm3yuVYC-XPgAb/s640/03.png" width="640" /></a><br /><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEghU2oF1EZVl2VAFV3HX1hiDcsFKDwBUZNhL72LIBA3eLS-RlOSwORn9jUWDrFcPr1O-uHZp_Xz6RwhOye4KXheZsjjoKv8Volhh2gDCwMX8gUpHAsFWssKN4WWw7q0_IHF9-5r3WyZOkmG/s1600/04.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="942" data-original-width="1600" height="376" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEghU2oF1EZVl2VAFV3HX1hiDcsFKDwBUZNhL72LIBA3eLS-RlOSwORn9jUWDrFcPr1O-uHZp_Xz6RwhOye4KXheZsjjoKv8Volhh2gDCwMX8gUpHAsFWssKN4WWw7q0_IHF9-5r3WyZOkmG/s640/04.png" width="640" /></a></li>
<li>接下來點擊右下的Create Channel<br /><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiGRn0Bl2EL_GNGKG09ioth3HOqTwmnAN1Q6lshAED7Eo80Pu__GNwEoFvCKTdbDW9DOVCqVuLdO-IxGQHarsJkUjjCxPNwyWVJSyQn27PV351JKhzGsiGKfOodqRVQSA5Uudman6WUSjbb/s1600/05+%25E6%258B%25B7%25E8%25B2%259D.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="851" data-original-width="1265" height="430" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiGRn0Bl2EL_GNGKG09ioth3HOqTwmnAN1Q6lshAED7Eo80Pu__GNwEoFvCKTdbDW9DOVCqVuLdO-IxGQHarsJkUjjCxPNwyWVJSyQn27PV351JKhzGsiGKfOodqRVQSA5Uudman6WUSjbb/s640/05+%25E6%258B%25B7%25E8%25B2%259D.png" width="640" /></a></li>
<li>填入App name, App description, Plan選取Developer Trial, Category和Subcategory照需求自己選, 最後再寫上email後即可送出confirm<br /><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhMN0NzLl-tfJ_l9KfkRbeNUofFtR-dud3EkY7Wlq6xf6LeOZVwa_quLxotQI67j2U3wlU39Fx_5pa_iSO-wsSmJc308JuMarv6Qx4Zo3sdISRGHpLpU5Ssv7B-aX98RCS-gjOfuoidZvuf/s1600/07.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="959" data-original-width="1600" height="382" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhMN0NzLl-tfJ_l9KfkRbeNUofFtR-dud3EkY7Wlq6xf6LeOZVwa_quLxotQI67j2U3wlU39Fx_5pa_iSO-wsSmJc308JuMarv6Qx4Zo3sdISRGHpLpU5Ssv7B-aX98RCS-gjOfuoidZvuf/s640/07.png" width="640" /></a><br /><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiT6CcEnOIz0ASNpBnrChh8EeAFJEhopZ_Dk15dl2QYlC3QYs4sg89gxyJ54wbbHqIyW0ZcxMkahUDpNl81pvutXEhNdmU6xFsA7wqBnWJZJFBHTK-Q6d8h3BUYQmiTOKmIpXshF6hdAG-o/s1600/08+%25E6%258B%25B7%25E8%25B2%259D.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="959" data-original-width="1600" height="382" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiT6CcEnOIz0ASNpBnrChh8EeAFJEhopZ_Dk15dl2QYlC3QYs4sg89gxyJ54wbbHqIyW0ZcxMkahUDpNl81pvutXEhNdmU6xFsA7wqBnWJZJFBHTK-Q6d8h3BUYQmiTOKmIpXshF6hdAG-o/s640/08+%25E6%258B%25B7%25E8%25B2%259D.png" width="640" /></a></li>
<li>接著將最下方的LINE@ Terms of Use和Messaging Terms of Use打勾後再按下Create完成<br /><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEggziZlLb1xYm2d3jExVNvKod0O0u5HJbQqsHsIa0GYWOb0De99vdw_tkh8XSj676Pon4Gr6lGzgZAx8rO3unL_e_EQkjt_XqHpcFlZy1GcYWKIrzN4wcJoonAXMR70GO2LvVwNtiKNMqXX/s1600/11.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="733" data-original-width="1600" height="291" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEggziZlLb1xYm2d3jExVNvKod0O0u5HJbQqsHsIa0GYWOb0De99vdw_tkh8XSj676Pon4Gr6lGzgZAx8rO3unL_e_EQkjt_XqHpcFlZy1GcYWKIrzN4wcJoonAXMR70GO2LvVwNtiKNMqXX/s640/11.png" width="640" /></a></li>
<li>建立完成後可以看到剛剛輸入的chanel出現在畫面上<br /><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhWzORt7abeBs6567ZFij1AYvhbVnE34xzzhFmvkK7i5YjkLDp3sWMMxED49RVYLUVdNyL2LNV9QnDhryqXae4ItCYkLMKd5UzpSjky2RvXL71X6LMlo4-XN96A2-BCJOWk3QAjjm3zLxjl/s1600/12.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="1068" data-original-width="1332" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhWzORt7abeBs6567ZFij1AYvhbVnE34xzzhFmvkK7i5YjkLDp3sWMMxED49RVYLUVdNyL2LNV9QnDhryqXae4ItCYkLMKd5UzpSjky2RvXL71X6LMlo4-XN96A2-BCJOWk3QAjjm3zLxjl/s400/12.png" width="400" /></a></li>
<li>點擊剛建立好的channel</li>
<li>找到Allow bot to join group chats並按下edit, 改為enable後按下update<br /><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjXWmEhAwm5Bij5HhWkz9OEfN_MbFsS4rX2sgl8jNB0ku8LUDvZQBrjds7iblacOfmjbEIUl6wzHsq-6DQ7Mx_poVNLjBxcHV2ELJCGau2I3bE-dA1EMdGfN8SMXYQfmrWNP-FiILfkFPix/s1600/13+%25E6%258B%25B7%25E8%25B2%259D.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="519" data-original-width="1600" height="204" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjXWmEhAwm5Bij5HhWkz9OEfN_MbFsS4rX2sgl8jNB0ku8LUDvZQBrjds7iblacOfmjbEIUl6wzHsq-6DQ7Mx_poVNLjBxcHV2ELJCGau2I3bE-dA1EMdGfN8SMXYQfmrWNP-FiILfkFPix/s640/13+%25E6%258B%25B7%25E8%25B2%259D.png" width="640" /></a><br /><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg11E_XaInk01dVebEhXOQb1kQj-gKEdfhdTOQjbJZhhe6bC0kFM6hCdxVu79fJ0xK1u2yosZEIF38njeJvSkChaTJfSTbUyRbnN-SvLy6rmyLXuPDFq0Z5nRY-rMGRBMXeqXMV_pC_G7Wl/s1600/14+%25E6%258B%25B7%25E8%25B2%259D.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="557" data-original-width="1600" height="222" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg11E_XaInk01dVebEhXOQb1kQj-gKEdfhdTOQjbJZhhe6bC0kFM6hCdxVu79fJ0xK1u2yosZEIF38njeJvSkChaTJfSTbUyRbnN-SvLy6rmyLXuPDFq0Z5nRY-rMGRBMXeqXMV_pC_G7Wl/s640/14+%25E6%258B%25B7%25E8%25B2%259D.png" width="640" /></a></li>
<li>找到QR code, 使用手機上的LINE掃描QR code將機器人加入好友<br /><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEinnG6HMD2NM981rNHlV8JJuqqWpY4VTQ9HiZ8njav_e9nqK4JN8c-FdUbE84Eqzl7MmNv7IKbM2O4hRVjbI7OutrWG9H8uGLbVaZbmDQM5Qu0w-PKqVcP3ZEzj7IL32hqcf75XSw0B31p3/s1600/15+%25E6%258B%25B7%25E8%25B2%259D.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="622" data-original-width="1600" height="248" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEinnG6HMD2NM981rNHlV8JJuqqWpY4VTQ9HiZ8njav_e9nqK4JN8c-FdUbE84Eqzl7MmNv7IKbM2O4hRVjbI7OutrWG9H8uGLbVaZbmDQM5Qu0w-PKqVcP3ZEzj7IL32hqcf75XSw0B31p3/s640/15+%25E6%258B%25B7%25E8%25B2%259D.png" width="640" /></a><br /><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi7vUspanO6tYPh-_mKOEUd7m-pm5stXpLgQFsGSbGhvOOyKob7g4QqFLJ4b39TmerpLV4Ck5yYTR1eJ_oqZwqHtT9hI9IChvh_66FKcJIp18ngUoLuutyRtZV-IGzE-AP4-dpQwBu81bbK/s1600/16.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="1600" data-original-width="901" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi7vUspanO6tYPh-_mKOEUd7m-pm5stXpLgQFsGSbGhvOOyKob7g4QqFLJ4b39TmerpLV4Ck5yYTR1eJ_oqZwqHtT9hI9IChvh_66FKcJIp18ngUoLuutyRtZV-IGzE-AP4-dpQwBu81bbK/s320/16.PNG" width="180" /></a></li>
<li>加入好友後會跳出歡迎訊息<br /><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgY7czUghcsC9W09UFMUUPhV4eeJ78m6Llxt9AntrVcSwGCIy1j89GFGT5Kd6JygB2naxJu66yuNHRASx_LZFwb88dy2SI9KYWZxyzvztgZ9bPH_lqOMETuz9Ns26uNxoObwxfIILYxZMVS/s1600/17.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="1600" data-original-width="901" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgY7czUghcsC9W09UFMUUPhV4eeJ78m6Llxt9AntrVcSwGCIy1j89GFGT5Kd6JygB2naxJu66yuNHRASx_LZFwb88dy2SI9KYWZxyzvztgZ9bPH_lqOMETuz9Ns26uNxoObwxfIILYxZMVS/s320/17.PNG" width="180" /></a></li>
<li>接下來找到Channel access token並點擊issue取得access token<br /><div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhX8Pg51eqKHHCuTDeA8BcNfvbhUtPKYfz8d4yd8Gpa_mkvcBQbLZxOVhz35f9bgt1TRCK6UlzKuHdWGLZC8tt_M4i19ErwRihaukJCIJz9awadon20Jxpm7SgqYPCGQS0LE1_iY0JYr0vk/s1600/18.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="744" data-original-width="1600" height="296" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhX8Pg51eqKHHCuTDeA8BcNfvbhUtPKYfz8d4yd8Gpa_mkvcBQbLZxOVhz35f9bgt1TRCK6UlzKuHdWGLZC8tt_M4i19ErwRihaukJCIJz9awadon20Jxpm7SgqYPCGQS0LE1_iY0JYr0vk/s640/18.png" width="640" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi54Q7nokJv0B7OtQNuHRcB2IXAfXRCcTVBjJI7tpsfpKsjQvPygpXOf56dKZ-GUBxGbsmgJZsv3KQ7kQyqJhyphenhyphenX0kxKiaBycz8CeQ3dnoV2S0GMTW_lEcXHy4ng-ffMbGrmvKZGtuQjiVoK/s1600/19.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="216" data-original-width="1600" height="86" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi54Q7nokJv0B7OtQNuHRcB2IXAfXRCcTVBjJI7tpsfpKsjQvPygpXOf56dKZ-GUBxGbsmgJZsv3KQ7kQyqJhyphenhyphenX0kxKiaBycz8CeQ3dnoV2S0GMTW_lEcXHy4ng-ffMbGrmvKZGtuQjiVoK/s640/19.png" width="640" /></a></div>
<span style="color: red;">注意: access token是LINE用來認證的資料, 請勿外流出去</span></li>
<li>將 <a href="http://blog.alenshiun.tw/2018/10/line-bot-23-gcphttps-server.html">LINE BOT 匯率提醒機器人(2/3) - 使用GCP架設HTTPS server</a> 中使用到的domain name後端加上/recv.php輸入到webhooks上, 例如 https://linebottest.alenshiun.tw/recv.php<br />LINE會在機器人被加入好友、加入群組、封鎖等等時傳送個訊息到webhooks上<br /><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj68W4Ns9C9ksawcycssyHJYq39UzqlaaK52LXda9x4t_LZ1lCjxdFBPGHodvGI-iTdJwt4m1zysSLg9pUqdNitr_yRIYZpgXQ88-az7GFZC9hu0rI3XFvpcJqq_u8GGnRYdRAmlKPoGRhJ/s1600/20.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="473" data-original-width="1600" height="188" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj68W4Ns9C9ksawcycssyHJYq39UzqlaaK52LXda9x4t_LZ1lCjxdFBPGHodvGI-iTdJwt4m1zysSLg9pUqdNitr_yRIYZpgXQ88-az7GFZC9hu0rI3XFvpcJqq_u8GGnRYdRAmlKPoGRhJ/s640/20.png" width="640" /></a></li>
</ol>
</div>
<h4>
實作匯率提醒機器人 - 架站目標!</h4>
<div>
<br />
申請完LINE messaging API後即可開始架設網站, 架設目標如下<br />
<br />
<ol>
<li>當LINE機器人被加入群組時, 網站會收到由LINE官方送來的通知</li>
<li>收到LINE官方的通知後將辨識群組用的唯一識別碼存進資料庫</li>
<li>Linux server向政府開放的open data取得匯率資訊</li>
<li>將匯率資訊推播給資料庫中紀錄的全部群組</li>
<li>每天上午10:00重複步驟3~4</li>
</ol>
<div>
為達到以上目標, 筆者採用PHP + SQLite開發匯率機器人<br />
<br /></div>
</div>
<h4>
實作匯率提醒機器人 - 安裝PHP與SQLite</h4>
</div>
</div>
<div>
<br />
首先來安裝PHP和SQLite</div>
<div>
<ol>
<li>更新apt套件資訊<br />
<code><pro>shiun@instance-1:$</pro> sudo apt-get update</code>
</li>
<li>安裝PHP<br />
<code><pro>shiun@instance-1:$</pro> sudo apt-get install php</code>
</li>
<li>安裝PHP apache模組<br />
<code><pro>shiun@instance-1:$</pro> sudo apt-get install libapache2-mod-php</code>
</li>
<li>安裝SQLite指令程式<br />
<code><pro>shiun@instance-1:$</pro> sudo apt-get install sqlite</code>
</li>
<li>重新啟動apache<br />
<code><pro>shiun@instance-1:$</pro> sudo service apache2 restart</code>
</li>
<li>檢查php是否能正常運作<br />
<code><pro>shiun@instance-1:$</pro> sudo vi /var/www/linebottest/web/info.php
<com># 加入以下內容</com>
<?php
phpinfo();
?>
</code>
<br />
使用瀏覽器開網頁確定是不是出現PHP資訊頁面, 有看到下圖頁面就是PHP啟動成功<br /><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiQPoyM88aMdO209ehVUqkycqVsDRuuZ3NcZppepU8ELCY8hjyQShhAl7dAGGVjo4Z3WhHYdHM0Y-hrTGwD7Y3uvFkE5E61IIOcugv_ejCdJx1mOhgp3O6AclY04rruWCk6UUvrOhbdHNP7/s1600/01.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="734" data-original-width="1600" height="292" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiQPoyM88aMdO209ehVUqkycqVsDRuuZ3NcZppepU8ELCY8hjyQShhAl7dAGGVjo4Z3WhHYdHM0Y-hrTGwD7Y3uvFkE5E61IIOcugv_ejCdJx1mOhgp3O6AclY04rruWCk6UUvrOhbdHNP7/s640/01.png" width="640" /></a>
</li>
<li>確認能正常運作後刪除info.php避免PHP資訊外洩<br />
<code><pro>shiun@instance-1:$</pro> sudo rm /var/www/linebottest/web/info.php
</code>
</li>
</ol>
</div>
<div>
<h4>
實作匯率提醒機器人 - 安裝相依套件</h4>
</div>
<div>
<br />
匯率機器人會使用到許多現成網路上的套件, 請先安裝好這些套件</div>
<div>
<div lang="en-US" style="font-family: Calibri; font-size: 11.0pt; margin: 0in;">
</div>
<ol>
<li>composer: PHP相依套件管理程式<br />
<code><pro>shiun@instance-1:$</pro> sudo apt-get install composer</code>
</li>
<li>libfreetype6-dev, <span style="font-size: 11pt;">php-image-text</span><span style="font-size: 11pt;">: 繪圖套件, 畫折線圖會用到</span><br />
<code><pro>shiun@instance-1:$</pro> sudo apt-get install libfreetype6-dev
<pro>shiun@instance-1:$</pro> sudo apt-get install php-image-text
</code>
</li>
<li>php-sqlite3: PHP專用的SQLite套件<br />
<code><pro>shiun@instance-1:$</pro> sudo apt-get install php-sqlite3</code>
</li>
<li>php-xml: PHP的xml parser套件<br />
<code><pro>shiun@instance-1:$</pro> sudo apt-get install php-xml</code>
</li>
<li>php-mbstring: PHP多國語言字串處理套件<br />
<code><pro>shiun@instance-1:$</pro> sudo apt-get install php-mbstring</code>
</li>
<li>php-curl: PHP進行HTTP/HTTPS連線用套件<br />
<code><pro>shiun@instance-1:$</pro> sudo apt-get install php-curl</code></li>
</ol>
<div>
<h4>
實作匯率提醒機器人 - 匯率機器人專案程式建置</h4>
</div>
<div>
<ol>
<li>從GitHub上clone一份LINE BOT專案, 專案名稱為foreignEX<br />
<code><pro>shiun@instance-1:$</pro> https://github.com/AlenShiun/ForeignEX</code>
</li>
<li>將原先的linebottest資料夾整個刪除, 接著把專案檔改名並複製到linebottest原先的位置<br />
<code><pro>shiun@instance-1:$</pro> sudo rm -rf /var/www/linebottest
<pro>shiun@instance-1:$</pro> sudo mv ForeignEX /var/www/linebottest
</code>
</li>
<li>安裝專案相依套件<br />
<code><pro>shiun@instance-1:$</pro> cd /var/www/linebottest/
<pro>shiun@instance-1:$</pro> composer install
</code>
</li>
<li>設定LINE access token, access token為「申請LINE messaging API」步驟中取得的access token<br />
<code><pro>shiun@instance-1:$</pro> cd /var/www/linebottest/
<pro>shiun@instance-1:/var/www/linebottest$</pro> sudo vi config.php
<com>#找到access token並修改</com>
// LINE message API access token
define("LINE_MESSAGE_API_ACCESS_TOKEN", 'Sm6xxxxxxx ...... xxxxxxxxxxxxxxlFU=');
</code>
</li>
<li>回到LINE messaging API channel, 點擊verify驗證LINE是不是可連線上此webhooks<br /><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjaN3fycQZeUwSi4Kt9jLMehhOZPOjNqb3lcF7oDhRnrCPY35vCxNFBHpqNIkR45Mvg89Xbvct3VdpYvLgjbMfxaMBUUsM3J2iPMCnqnnLKv0izThU33XvMmVHznAlafwt8Y6Mgf6oC9J1k/s1600/21.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="295" data-original-width="1600" height="118" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjaN3fycQZeUwSi4Kt9jLMehhOZPOjNqb3lcF7oDhRnrCPY35vCxNFBHpqNIkR45Mvg89Xbvct3VdpYvLgjbMfxaMBUUsM3J2iPMCnqnnLKv0izThU33XvMmVHznAlafwt8Y6Mgf6oC9J1k/s640/21.png" width="640" /></a><br />驗證成功會顯示上圖中的 Success 訊息</li>
<li>設定折線圖存取用的URL<br />
<code><pro>shiun@instance-1:$</pro> cd /var/www/linebottest/
<pro>shiun@instance-1:/var/www/linebottest$</pro> sudo vi config.php
<com>#找到IMAGE_HTTP_PATH_BASE_RATE並修改</com>
// HTTP URL for image files of chart
define('IMAGE_HTTP_PATH_BASE_RATE', "https://linebottest.alenshiun.tw/pic");
</code>
</li>
<li>修改資料夾權限, 讓匯率機器人可以存取資料庫和log資料夾<br />
<code><pro>shiun@instance-1:$</pro> cd /var/www/linebottest/
<pro>shiun@instance-1:/var/www/linebottest$</pro> sudo chown -R www-data:www-data db
<pro>shiun@instance-1:/var/www/linebottest$</pro> sudo chmod -R 755 db
<pro>shiun@instance-1:/var/www/linebottest$</pro> sudo chown -R www-data:www-data log
<pro>shiun@instance-1:/var/www/linebottest$</pro> sudo chmod -R 755 log
</code><br />
修改後結果如下
<code><pro>shiun@instance-1:/var/www/linebottest$</pro> ls -l
total 56
-rw-r--r-- 1 shiun shiun 2381 Oct 4 07:57 chartGenerator.php
-rw-r--r-- 1 shiun shiun 71 Oct 4 07:57 composer.json
-rw-r--r-- 1 shiun shiun 3986 Oct 4 07:57 composer.lock
-rw-r--r-- 1 shiun shiun 765 Oct 4 07:59 config.php
-rw-r--r-- 1 shiun shiun 2862 Oct 4 07:57 dao.php
drwxr-xr-x 2 www-data www-data 4096 Oct 4 08:00 db
drwxr-xr-x 3 shiun shiun 4096 Oct 4 07:58 libs
drwxr-xr-x 2 www-data www-data 4096 Oct 4 08:00 log
-rw-r--r-- 1 shiun shiun 1024 Oct 4 07:57 parser.php
-rw-r--r-- 1 shiun shiun 1120 Oct 4 07:57 README.md
-rw-r--r-- 1 shiun shiun 1495 Oct 4 07:57 sender.php
drwxrwxr-x 4 shiun shiun 4096 Oct 4 07:59 vendor
drwxr-xr-x 3 shiun shiun 4096 Oct 4 08:00 web
-rw-r--r-- 1 shiun shiun 1754 Oct 4 07:57 worker.php
</code>
</li>
<li>在LINE上建立群組, 將匯率機器人加入群組<br /><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjVdrH4kL0AdxiRRRTKbzv_5V-FQp4oeosPwELcG2GKLPsvHgxvKlAbkJZsxcx8KLD8uvsFEcwt2UiMOcwQNO8g82WgrEqo5QKPkIGQayU_y724NJizG1Zaw6KsiR-h72tr8JnG1vcTsDsH/s1600/22+%25E6%258B%25B7%25E8%25B2%259D.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="756" data-original-width="932" height="259" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjVdrH4kL0AdxiRRRTKbzv_5V-FQp4oeosPwELcG2GKLPsvHgxvKlAbkJZsxcx8KLD8uvsFEcwt2UiMOcwQNO8g82WgrEqo5QKPkIGQayU_y724NJizG1Zaw6KsiR-h72tr8JnG1vcTsDsH/s320/22+%25E6%258B%25B7%25E8%25B2%259D.PNG" width="320" /></a></li>
<li>檢查資料庫是否有記錄到群組的唯一識別碼<br />
<code><pro>shiun@instance-1:$</pro> cd /var/www/linebottest/
<pro>shiun@instance-1:/var/www/linebottest$</pro> sqlite3 db/db.sqlite
SQLite version 3.11.0 2016-02-15 17:29:24
Enter ".help" for usage hints.
<com>#輸入.tables, 出現的target代表資料表table有存在</com>
sqlite> .tables
target
<com>#輸入SELECT * FROM target;</com>
sqlite> SELECT * FROM target;
<com>#sqlite指令會列出資料表target下所有的資訊</com>
<com>#以下列為例, 在2018-10-04的8:17分機器人偵測到被加入群組, 群組ID為C0xxxxxxe2b</com>
2|C0xxxxxxe2b|FOREIGN_EX_DB_TYPE_GROUP|2018-10-04 08:17:14|
<com>#按下ctrl+d離開sqlite指令功能</com>
sqlite>
</code>
</li>
<li>執行worker.php, 執行成功後群組會收到匯率資訊<br />
<code><pro>shiun@instance-1:$</pro> cd /var/www/linebottest/
<pro>shiun@instance-1:/var/www/linebottest$</pro> php worker.php
LINE message API response: {}
</code><br /><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj_0S2wQfDq92TB1UyPJmUw0M3cMR8xaSbQeTS-9H_51B9Kcy50LFTlKZxpXI1S6aMURa7uVZ7Q6AT8d8gC6MUxaDxKeb4nue2tyGCnidy0G7IO0C0kMSS3ceM5MTm0INDGJlcYHVLZ5Jpa/s1600/23+%25E6%258B%25B7%25E8%25B2%259D.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="1029" data-original-width="1242" height="265" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj_0S2wQfDq92TB1UyPJmUw0M3cMR8xaSbQeTS-9H_51B9Kcy50LFTlKZxpXI1S6aMURa7uVZ7Q6AT8d8gC6MUxaDxKeb4nue2tyGCnidy0G7IO0C0kMSS3ceM5MTm0INDGJlcYHVLZ5Jpa/s320/23+%25E6%258B%25B7%25E8%25B2%259D.PNG" width="320" /></a></li>
<li>設定crontab讓Linux定期執行worker.php<br />
<code><pro>shiun@instance-1:$</pro> sudo vi /etc/crontab
<com>#在最後一行加入以下內容</com>
00 2 * * * root php /var/www/linebottest/worker.php
</code>
<br />每到UTC時間2:00(台灣時間10:00)Linux會自動啟動worker.php去寄送匯率資料給群組</li>
<li>等待到crontab指定的時間看是否能正常收到匯率資訊</li>
<li>收工~</li>
</ol>
<h4>
後記</h4>
</div>
</div>
<div>
<ol>
<li>GitHub上的專案是採用政府open data的匯率資訊所以並不會有當天資料, 讀者可以自行準備爬蟲去爬銀行網站抓到最新的資料<br />簡單的爬蟲可以用php-html-parser處理DOM物件來做到</li>
<li>GCE的VM預設會採用UTC+0的時間, 若使用台灣時間請自行+8小時, 或是自行研究怎麼修改GCE的時區</li>
</ol>
<div>
<br /></div>
</div>
<h4>
參考資料</h4>
<div>
<ol>
<li><a href="https://blog.alenshiun.tw/2018/10/line-bot-13-gcplinux-server.html" target="_blank">LINE BOT 匯率提醒機器人(1/3) - 使用GCP架設Linux server</a></li>
<li><a href="https://blog.alenshiun.tw/2018/10/line-bot-23-gcphttps-server.html" target="_blank">LINE BOT 匯率提醒機器人(2/3) - 使用GCP架設HTTPS server</a></li>
</ol>
</div>
<div>
<br /></div>
Shiunhttp://www.blogger.com/profile/00430694685762085062noreply@blogger.com0tag:blogger.com,1999:blog-694306540104860106.post-72083653343770275622018-10-03T23:40:00.001+08:002018-10-05T19:52:32.706+08:00LINE BOT 匯率提醒機器人(2/3) - 使用GCP架設HTTPS server<h4>
前情提要</h4>
<div>
<br />
前一篇文章 <a href="http://blog.alenshiun.tw/2018/10/line-bot-13-gcplinux-server.html" target="_blank">LINE BOT 匯率提醒機器人(1/3) - 使用GCP架設Linux server</a> 說明如何使用GCP架設一台Linux server, 但是LINE BOT是需要透過HTTPS協定進行資料傳輸的, 本篇將說明怎麼架設HTTPS server</div>
<div>
<br /></div>
<div>
<h4>
<span style="font-family: inherit;">開發重點</span></h4>
<div>
<ol>
<li><span style="color: #cccccc; font-family: inherit;">使用GCP服務中的GCE(Google Compute Engine)建立一台Linux server</span></li>
<li><span style="font-family: inherit;">使用GCE服務架設網站server, 網站server使用Apache架設</span></li>
<li><span style="font-family: inherit;">加入HTTPS支援 - 使用Let’s Encrypt免費SSL憑證</span></li>
<li><span style="font-family: inherit;">註冊LINE developers, 並申請LINE messaging API</span></li>
<li>使用PHP + SQLite實作LINE Bot傳送匯率到LINE群組的頻道上</li>
</ol>
<div>
<h4>
</h4>
<h4>
使用GCP架設HTTPS server - GCP & GCE固定IP設定</h4>
</div>
</div>
</div>
<div>
<br />
由於LINE BOT需要透過IP與Linux server連線, 但GCP建立出來的GCE預設是採用浮動IP設計會造成些麻煩, 因此我們先將浮動IP改為固定IP</div>
<div>
<ol>
<li>開啟「VPC網路」中的「外部IP位置」<br /><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiEbS5TJsxQe3SwEH11At8pizYnLcm7N0TwxXQ3fG7zL8lP2aMWoCcdl_KjmR-cNWefbXX7jXfB_r6myX5qs-oxMGDpEZctpjjIvkYyJ8f_l7kDxKPNMTYTg3_vhaN6zThWrSs9MeiXeq8D/s1600/17+%25E6%258B%25B7%25E8%25B2%259D.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="826" data-original-width="950" height="278" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiEbS5TJsxQe3SwEH11At8pizYnLcm7N0TwxXQ3fG7zL8lP2aMWoCcdl_KjmR-cNWefbXX7jXfB_r6myX5qs-oxMGDpEZctpjjIvkYyJ8f_l7kDxKPNMTYTg3_vhaN6zThWrSs9MeiXeq8D/s320/17+%25E6%258B%25B7%25E8%25B2%259D.png" width="320" /></a></li>
<li>將instance-1使用到的IP由臨時改為靜態<br /><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjKTcM4aG-ZuIdMx6LQSOMnipoOa1Ku_hprywEeS_BADxcA4UyQ8I6HENNPxC42d18b2l36Zn3BNv-T4T3vut5thT3vYRBypuZebQLjX4xmqz5lhNCe6mrFrqdTTFbsTzvje1ARSEIfIQam/s1600/18+%25E6%258B%25B7%25E8%25B2%259D.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="657" data-original-width="1600" height="260" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjKTcM4aG-ZuIdMx6LQSOMnipoOa1Ku_hprywEeS_BADxcA4UyQ8I6HENNPxC42d18b2l36Zn3BNv-T4T3vut5thT3vYRBypuZebQLjX4xmqz5lhNCe6mrFrqdTTFbsTzvje1ARSEIfIQam/s640/18+%25E6%258B%25B7%25E8%25B2%259D.png" width="640" /></a><br /><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi2twyob_6A_dqn_jZY5ISfyZ3SYCWmJiJCPo5EMF5KZQOGMEq7eQL6CdnNOkiia-D8fX75L2XStOK6bH_ScAoxw5idtHGQxMxP5nsIZFR1y-_4pHNj9B5OT8DIPkLoMsoiSjBa8Gg841S2/s1600/19.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="540" data-original-width="1044" height="206" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi2twyob_6A_dqn_jZY5ISfyZ3SYCWmJiJCPo5EMF5KZQOGMEq7eQL6CdnNOkiia-D8fX75L2XStOK6bH_ScAoxw5idtHGQxMxP5nsIZFR1y-_4pHNj9B5OT8DIPkLoMsoiSjBa8Gg841S2/s400/19.png" width="400" /></a></li>
<li>設定完成, 收工~<br /><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh9gW5gOerZLJXsJ8EL2nQAM2ex7UpBptN646oAqzlAq5rZO3ITiLOLVVwFCf5cTEfG_6zOBYSTVvxZuaWHvu5AERtvHmNqU38jewpGNvM1gTijAds56IZajeV6l9l5iz60_fhMDEbiZW_1/s1600/19_2+%25E6%258B%25B7%25E8%25B2%259D.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="228" data-original-width="676" height="214" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh9gW5gOerZLJXsJ8EL2nQAM2ex7UpBptN646oAqzlAq5rZO3ITiLOLVVwFCf5cTEfG_6zOBYSTVvxZuaWHvu5AERtvHmNqU38jewpGNvM1gTijAds56IZajeV6l9l5iz60_fhMDEbiZW_1/s640/19_2+%25E6%258B%25B7%25E8%25B2%259D.png" width="640" /></a></li>
</ol>
</div>
<div>
<br /></div>
<div>
<h4>
使用GCP架設HTTPS server - 準備domain name</h4>
</div>
<div>
<br />
此處筆者使用過去在GoDaddy購買的DNS服務設定對應的domain name, 請讀者自行根據自己的DNS廠商提供的方式設定DNS type A對應到固定IP</div>
<div>
<ol>
<li>將DNS type A對應到固定IP<br /><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiPRyU2yFmzmPafU9ZkIznKvO8wNhHoU4PmGDzdPlWMlbO8Rjsan-Ve0pY1aa3X83zbZlNlEmFYoK8F46azybtVwQNqu7P3ZrT4pr9HUOyZwuOH78whldxFR2WSzThAZ6KIOLAbCPPo6xv_/s1600/d1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="320" data-original-width="963" height="211" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiPRyU2yFmzmPafU9ZkIznKvO8wNhHoU4PmGDzdPlWMlbO8Rjsan-Ve0pY1aa3X83zbZlNlEmFYoK8F46azybtVwQNqu7P3ZrT4pr9HUOyZwuOH78whldxFR2WSzThAZ6KIOLAbCPPo6xv_/s640/d1.png" width="640" /></a><br /><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhiG56y0GmgAVEg1Rjq-VrzbhiZnQhNPvJRO2W7NpmAAiy_Rl5kVdpqNRMtMvuRnBI7nhIrLTZO2OxwHCZt_f47QvAzGeUFfCXr5fqD3PPaAgWiJT3XrcDOB1WpL3Q5mQPqHqCYqC1pIyRH/s1600/d2+2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="504" data-original-width="1175" height="274" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhiG56y0GmgAVEg1Rjq-VrzbhiZnQhNPvJRO2W7NpmAAiy_Rl5kVdpqNRMtMvuRnBI7nhIrLTZO2OxwHCZt_f47QvAzGeUFfCXr5fqD3PPaAgWiJT3XrcDOB1WpL3Q5mQPqHqCYqC1pIyRH/s640/d2+2.png" width="640" /></a></li>
<li>Ping domain name看看, 看是否能正常ping到</li>
<li>收工~ 之後不需要再用IP進行連線, 只要用domain name連線即可<br /><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj4a6Lgm_RlRgtg37yeYpFMryCiUvjm4kof8ct_NAFnSurGIwqrgNJLGCn3F6w2ffNzPs9Nxdo9-I0H1MyMaSSXtzRiczHWyARdYX0ZFvgBCyfBCQfoVOWnr6BEetegB_3HS8tto76780Yq/s1600/d3.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="236" data-original-width="563" height="266" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj4a6Lgm_RlRgtg37yeYpFMryCiUvjm4kof8ct_NAFnSurGIwqrgNJLGCn3F6w2ffNzPs9Nxdo9-I0H1MyMaSSXtzRiczHWyARdYX0ZFvgBCyfBCQfoVOWnr6BEetegB_3HS8tto76780Yq/s640/d3.png" width="640" /></a></li>
</ol>
<div>
<span style="color: red;">注意: DNS設定完後不一定馬上生效, 適情況可能會需要等待30min以上</span></div>
<div>
<br /></div>
</div>
<h4>
使用GCP架設HTTPS server - HTTP服務架設</h4>
<div>
<br />
固定IP與domain name準備好後即可開始架設HTTP伺服器, 此處以apache為範例進行架設</div>
<div>
<ol>
<li>連線到Linux server<br />
<code><com>#使用ssh連線到Linux server</com>
<pro>shiun@Shiun:~$</pro> ssh shiun@linebottest.alenshiun.tw
The authenticity of host 'linebottest.alenshiun.tw (35.237.204.103)' can't be established.
ECDSA key fingerprint is SHA256:gUSzeVS5z+RYQeJKtFQDX45EF5sGmIek7jMP6SXTNaM.
<com># 回答yes</com>
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'linebottest.alenshiun.tw' (ECDSA) to the list of known hosts.
Welcome to Ubuntu 16.04.5 LTS (GNU/Linux 4.15.0-1021-gcp x86_64)
...
...
Last login: Wed Oct 3 14:23:39 2018 from 118.167.72.55
<pro>shiun@instance-1:~$</pro>
</code>
</li>
<li>更新apt-get的套件資訊, 確保等等下載的套件為最新套件<br />
<code><com>#更新apt-get資訊</com>
<pro>shiun@instance-1:~$</pro> sudo apt-get update
</code>
</li>
<li>使用apt-get進行Ubuntu套件更新<br />
<code><com>#進行upgrade</com>
<pro>shiun@instance-1:~$</pro> sudo apt-get upgrade
</code>
<code>
<com>#upgrade結束後執行dist-upgrade</com>
<pro>shiun@instance-1:~$</pro> sudo apt-get dist-upgrade
</code>
</li>
<li>重開機, 確保重開機後機器不會掛掉............... (筆者過去中招太多次.........<br />
<code><pro>shiun@instance-1:~$</pro> sudo reboot
</code>
</li>
<li>下載apache套件<br />
<code><pro>shiun@instance-1:~$</pro> sudo apt-get install apache2
Reading package lists... Done
Building dependency tree
...
...
...
<com>#請輸入y</com>
Do you want to continue? [Y/n] y
...
...
...
</code>
</li>
<li>使用瀏覽器檢查 http://linebottest.alenshiun.tw <span style="color: red;">(請換成讀者的網址)</span>是否能連線成功</li>
</ol>
</div>
<div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhj0dgHpc4b9MqDJqir0RNclcpy5QdasxwVnsZc8rktlzPpaYF5cCaN9TpPflrOrqPt25WQX8lZBHGkciOWV4PYsAqOAoT9r7qPXJkz1ZJ9ckHF9cXtGsrachIhMNtl6dIZAdpEkNKy02DH/s1600/apache.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="553" data-original-width="924" height="380" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhj0dgHpc4b9MqDJqir0RNclcpy5QdasxwVnsZc8rktlzPpaYF5cCaN9TpPflrOrqPt25WQX8lZBHGkciOWV4PYsAqOAoT9r7qPXJkz1ZJ9ckHF9cXtGsrachIhMNtl6dIZAdpEkNKy02DH/s640/apache.png" width="640" /></a></div>
<br />
<h4>
使用GCP架設HTTPS server - 設定HTTP server</h4>
</div>
<div>
<br />
HTTP服務架設完後我們會看到上圖的Apache2 Ubuntu Default Page代表架設成功, 但是實務上我們會讓domain name連結到專案的網頁, 因此我們需要改變一下apache的設定</div>
<div>
當使用者在網址上輸入 http://linebottest.alenshiun.tw 時會被導向一個顯示以下訊息的頁面</div>
<div>
<br /></div>
<div>
LINE BOT TEST Work!</div>
<div>
<ol>
<li>建立專案的網頁目錄/var/www/linebottest/web<br />
<code><pro>shiun@instance-1:~$</pro> sudo mkdir -p /var/www/linebottest/web
</code>
</li>
<li>移動到專案的網頁目錄/var/www/linebottest/web<br />
<code><pro>shiun@instance-1:~$</pro> cd /var/www/linebottest/web
</code>
</li>
<li>建立預設頁面index.html<br />
<code><pro>shiun@instance-1:/var/www/linebottest/web$</pro> sudo vi index.html
</code>
<code><com>#輸入LINE BOT TEST Work!並存檔離開</com>
LINE BOT TEST Work!
</code>
</li>
<li>移動到apache設定目錄/etc/apache2/sites-available<br />
<code><pro>shiun@instance-1:~$</pro> cd /etc/apache2/sites-available/
</code>
</li>
<li>新增linebottest.conf, 檔案內容如下<br />
<code><pro>shiun@instance-1:/etc/apache2/sites-available$</pro> sudo vi linebottest.conf
<VirtualHost *:80>
ServerName linebottest.alenshiun.tw
ServerAdmin webmaster@localhost
DocumentRoot /var/www/linebottest/web
<Directory /var/www/linebottest/web>
Options FollowSymLinks
Order allow,deny
Allow from all
AllowOverride all
</Directory>
</VirtualHost>
</code>
</li>
<li>建立/etc/apache2/sites-available/linebottest.conf到/etc/apache2/sites-enabled的連結(捷徑)<br />
<code><pro>shiun@instance-1:$</pro> sudo ln -s /etc/apache2/sites-available/linebottest.conf \
/etc/apache2/sites-enabled/linebottest.conf</code>
</li>
<li>檢查連結(捷徑)是否建立成功<br />
<code><pro>shiun@instance-1:/etc/apache2/sites-available$</pro> ls -l /etc/apache2/sites-enabled/
total 0
lrwxrwxrwx 1 root root 35 Oct 3 04:44 000-default.conf -&gt ../sites-available/000-default.conf
lrwxrwxrwx 1 root root 45 Oct 3 14:58 linebottest.conf -&gt /etc/apache2/sites-available/linebottest.conf
<com># 建立連結成功的話就會顯示此行: linebottest.conf -&gt /etc/apache2/sites-available/linebottest.conf</com>
</code>
</li>
<li>重新啟動apache<br />
<code><pro>shiun@instance-1:$</pro> sudo service apache2 restart
</code>
</li>
<li>使用瀏覽器檢查 http://linebottest.alenshiun.tw , 顯示結果應如下圖所示<br /><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg_0pAZ80FwSTv9IBEwsfFPtbVwW3Nu7YAbQXVHYlwvpTclsBmJKjZvZUWSh9aagSPmVa6uB3hQlXdTBIQvQqPBb8tPknhUBUZzZAXcjsd8SssFgoE2lMy4CYPnL53K14Ay8NA7Artz7fbX/s1600/it_work.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="150" data-original-width="472" height="126" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg_0pAZ80FwSTv9IBEwsfFPtbVwW3Nu7YAbQXVHYlwvpTclsBmJKjZvZUWSh9aagSPmVa6uB3hQlXdTBIQvQqPBb8tPknhUBUZzZAXcjsd8SssFgoE2lMy4CYPnL53K14Ay8NA7Artz7fbX/s400/it_work.png" width="400" /></a></li>
</ol>
<div>
<h4>
使用GCP架設HTTPS server - 設定HTTPS</h4>
</div>
</div>
<div>
<br />
為什麼會需要開啟HTTPS?<br />
<br />
<ul>
<li>傳輸有加密保證</li>
<li>確認傳輸的目的地是合法而不是被偽造的</li>
<li>由於LINE BOT使用到的LINE message API限制連線一定要是HTTPS</li>
</ul>
<br />
因此我們需要開啟HTTPS功能, Apache上提供SSL憑證設定功能, 我們只需要取得SSL憑證並設定上去即可<br />
<br />
但是, 過去SSL憑證是必需要花錢買, 且價格不斐, 但近年來有個組職名為Let’s Encrypt提供免費的SSL憑證, 我們透過安裝Let’s Encrypt的憑證啟動HTTPS服務<br />
<br />
<span style="color: red;">注意: Let’s Encrypt會檢查domain name對應到的HTTP必須在<b>80</b> port, HTTPS必須在<b>443</b> port, 如果讀者是自訂port的請先改回80和443完成驗證後再進行port自訂</span><br />
<br />
<ol>
<li>安裝Let’s Encrypt Apache SSL憑證安裝程式相依套件<br />
<code><pro>shiun@instance-1:$</pro> sudo apt-get install software-properties-common
<pro>shiun@instance-1:$</pro> sudo add-apt-repository ppa:certbot/certbot
This is the PPA for packages prepared by Debian Let's Encrypt Team and backported for Ubuntu(s).
More info: https://launchpad.net/~certbot/+archive/ubuntu/certbot
<com>#直接點擊鍵盤上的enter</com>
Press [ENTER] to continue or ctrl-c to cancel adding it
gpg: keyring `/tmp/tmpqf0n3wow/secring.gpg' created
gpg: keyring `/tmp/tmpqf0n3wow/pubring.gpg' created
gpg: requesting key 75BCA694 from hkp server keyserver.ubuntu.com
gpg: /tmp/tmpqf0n3wow/trustdb.gpg: trustdb created
gpg: key 75BCA694: public key "Launchpad PPA for certbot" imported
gpg: Total number processed: 1
gpg: imported: 1 (RSA: 1)
OK
<pro>shiun@instance-1:$</pro> sudo apt-get update
</code>
</li>
<li>安裝Let’s Encrypt Apache SSL憑證安裝程式<br />
<code><pro>shiun@instance-1:$</pro> sudo apt-get install python-certbot-apache
</code>
</li>
<li>執行Let’s Encrypt Apache SSL憑證安裝程式<br />
<code><pro>shiun@instance-1:$</pro> sudo certbot --apache
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator apache, Installer apache
<com># 請輸入你的email</com>
Enter email address (used for urgent renewal and security notices)
(Enter 'c' to cancel): your_email@your_email.com
Starting new HTTPS connection (1): acme-v02.api.letsencrypt.org
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
agree in order to register with the ACME server at
https://acme-v02.api.letsencrypt.org/directory
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
<com># 輸入A</com>
(A)gree/(C)ancel: A
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing to share your email address with the Electronic Frontier
Foundation, a founding partner of the Let's Encrypt project and the non-profit
organization that develops Certbot? We'd like to send you email about our work
encrypting the web, EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
<com># 輸入N</com>
(Y)es/(N)o:
Which names would you like to activate HTTPS for?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: linebottest.alenshiun.tw
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate numbers separated by commas and/or spaces, or leave input
<com># 選擇你的domain name, 正常情況下certbot會自動抓到正確的domain name</com>
blank to select all options shown (Enter 'c' to cancel): 1
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for linebottest.alenshiun.tw
Enabled Apache rewrite module
Waiting for verification...
Cleaning up challenges
Created an SSL vhost at /etc/apache2/sites-available/linebottest-le-ssl.conf
Enabled Apache socache_shmcb module
Enabled Apache ssl module
Deploying Certificate to VirtualHost /etc/apache2/sites-available/linebottest-le-ssl.conf
Enabling available site: /etc/apache2/sites-available/linebottest-le-ssl.conf
Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
<com># 輸入2, 我們要強制把HTTP自動導向到HTTPS</com>
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2
Enabled Apache rewrite module
Redirecting vhost in /etc/apache2/sites-enabled/linebottest.conf to ssl vhost in /etc/apache2/sites-available/linebottest-le-ssl.conf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations! You have successfully enabled https://linebottest.alenshiun.tw
You should test your configuration at:
https://www.ssllabs.com/ssltest/analyze.html?d=linebottest.alenshiun.tw
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/linebottest.alenshiun.tw/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/linebottest.alenshiun.tw/privkey.pem
Your cert will expire on 2019-01-01. To obtain a new or tweaked
version of this certificate in the future, simply run certbot again
with the "certonly" option. To non-interactively renew *all* of
your certificates, run "certbot renew"
- Your account credentials have been saved in your Certbot
configuration directory at /etc/letsencrypt. You should make a
secure backup of this folder now. This configuration directory will
also contain certificates and private keys obtained by Certbot so
making regular backups of this folder is ideal.
- If you like Certbot, please consider supporting our work by:
Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate
Donating to EFF: https://eff.org/donate-le
</code>
</li>
<li>使用瀏覽器檢查 http://linebottest.alenshiun.tw , 設定成功的話瀏覽器會自動導向https://linebottest.alenshiun.tw<br /><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiW57kHqLUk9OYKPJ60LsgCBfIuMPD6eXYlYc_MamNVg0_jdPb2zwfOIGOR4NE8_1obJ2DuaPUWFCVaXs2Mit2douMrimVTOc-kcdM6yGcSH5GwhG0rRkW95cA8AM3u4-OE8mXz_cgvb1WI/s1600/s_it_work.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="194" data-original-width="440" height="176" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiW57kHqLUk9OYKPJ60LsgCBfIuMPD6eXYlYc_MamNVg0_jdPb2zwfOIGOR4NE8_1obJ2DuaPUWFCVaXs2Mit2douMrimVTOc-kcdM6yGcSH5GwhG0rRkW95cA8AM3u4-OE8mXz_cgvb1WI/s400/s_it_work.png" width="400" /></a></li>
</ol>
<div>
以上步驟都結束後HTTPS server架設完成<br />
<br />
<h4>
參考資料</h4>
<ol>
<li><a href="https://blog.alenshiun.tw/2018/10/line-bot-13-gcplinux-server.html" target="_blank">LINE BOT 匯率提醒機器人(1/3) - 使用GCP架設Linux server</a></li>
</ol>
</div>
</div>
Shiunhttp://www.blogger.com/profile/00430694685762085062noreply@blogger.com0tag:blogger.com,1999:blog-694306540104860106.post-2173639002740694292018-10-03T23:40:00.000+08:002018-10-05T19:53:32.439+08:00LINE BOT 匯率提醒機器人(1/3) - 使用GCP架設Linux server<span style="font-family: inherit;">最近開始準備去日本的滑雪計畫, 考慮到滑雪行程會用到不少日幣, 但是又很懶的每天開銀行網站看, 那就乾脆來寫個機器人每天早上自動推播今日匯率吧</span><br />
<div>
<br /></div>
<h4>
<span style="font-family: inherit;">
事前準備</span></h4>
<ul>
<li><span style="font-family: inherit;">網站server一台<br />必須要有固定IP且可由Internet連結進來, 若讀者自有server, 可直接跳到<a href="http://blog.alenshiun.tw/2018/10/line-bot-23-gcphttps-server.html" target="_blank">LINE BOT 匯率提醒機器人(2/3)</a><br />筆者這邊由於預算有限, 這次用GCP(Google Cloud Platform) Always Free方案架站<b><span style="color: red;">(限定狀況下免費!)</span></b></span></li>
<li><span style="font-family: inherit;">Domain name<br />可以找各大DNS廠商購買, 筆者是用過去在GoDaddy買的domain name處理。沒有domain name的人可以Google找找免費的DNS來用</span></li>
<li><span style="font-family: inherit;">SSH連線軟體<br />筆者使用macOS內建的ssh指令連線到GCP上, Windows上的話首推應該就Putty</span></li>
</ul>
<div>
<span style="color: red;">注意: Always Free方案有區域、硬碟空間、網路流量和其他..., 等限制, 目前(2018/10/3)使用是有機會免費, 但使用前請一定要自行確認是否符合Always Free, 若被收費筆者一概不負任何責任......。 詳細請參考</span> <a href="https://cloud.google.com/free/docs/always-free-usage-limits" target="_blank">一律免費用量限制</a></div>
<h4>
<span style="font-family: inherit;"><br /></span></h4>
<h4>
<span style="font-family: inherit;">開發重點</span></h4>
<div>
<ol>
<li><span style="font-family: inherit;">使用GCP服務中的GCE(Google Compute Engine)建立一台Linux server</span></li>
<li><span style="font-family: inherit;">使用GCE服務架設網站server</span></li>
<li><span style="font-family: inherit;">加入HTTPS支援 - 使用Let’s Encrypt免費SSL憑證</span></li>
<li>註冊LINE developers, 並申請LINE messaging API</li>
<li>實作LINE Bot傳送匯率到LINE群組的頻道上</li>
</ol>
</div>
<div>
<h4>
GCP架設Linux server - 在GCP上建立新專案</h4>
<div>
<ol>
<li> 使用Google帳號開通<a href="https://console.cloud.google.com/?hl=zh-TW" target="_blank">GCP服務</a>, 此步驟請跟著GCP網頁的提示申請, 最後綁定完信用卡後即可進入GCP的控制頁面(如下圖)<br /><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEheGVG-4ZhcrtJG_UI3YP7vfw2fafcyjUYhxJ6RzbdjCKfRDtJNfgBFjEivvqUs2oPhnYrNRINUO1ACpddoPMggEyMF9MgRnUS7OoHyeqmk9mRmnIe-CKabzoa3bkVHfVFLJ6GabWlQsZb_/s1600/0+%25E6%258B%25B7%25E8%25B2%259D.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="679" data-original-width="1088" height="398" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEheGVG-4ZhcrtJG_UI3YP7vfw2fafcyjUYhxJ6RzbdjCKfRDtJNfgBFjEivvqUs2oPhnYrNRINUO1ACpddoPMggEyMF9MgRnUS7OoHyeqmk9mRmnIe-CKabzoa3bkVHfVFLJ6GabWlQsZb_/s640/0+%25E6%258B%25B7%25E8%25B2%259D.png" width="640" /></a></li>
<li>點擊畫面上的「選取專案」後接著點選右上角的「新增專案」<br /><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj18xx-utB3aX9thwgio9m9tWfSKPadD2b3SD_84v3xTyOhKL2UdJWOelUwLHQkfPu9_J4KZhQA1AGZOgDOuv0ZpM8R1_56Acwynm2lzsTvAsJmFeyjsRqKraz9jzBzRNDvpG_E1RqB8glq/s1600/1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="148" data-original-width="758" height="77" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj18xx-utB3aX9thwgio9m9tWfSKPadD2b3SD_84v3xTyOhKL2UdJWOelUwLHQkfPu9_J4KZhQA1AGZOgDOuv0ZpM8R1_56Acwynm2lzsTvAsJmFeyjsRqKraz9jzBzRNDvpG_E1RqB8glq/s400/1.png" width="400" /></a><br /><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj4xRzg4MrhGpNtuRITgNdV6aCGS9U0KN1BARsj1A0cRdWaz8r8ytlfiSuYBuqZALDplryMR30LNCvdzmchFNOS6aTyMKyyXGhyphenhyphenUT1TqG6bDlz3poWB9U3yTvGQWlXzYSEOu8lHA19PQjtj/s1600/2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="280" data-original-width="1600" height="112" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj4xRzg4MrhGpNtuRITgNdV6aCGS9U0KN1BARsj1A0cRdWaz8r8ytlfiSuYBuqZALDplryMR30LNCvdzmchFNOS6aTyMKyyXGhyphenhyphenUT1TqG6bDlz3poWB9U3yTvGQWlXzYSEOu8lHA19PQjtj/s640/2.png" width="640" /></a></li>
<li>填寫專案名稱, 接著會看到右上角的通知(下圖中紅框有數字的地方)提示正在建立專案<br /><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjvH0wO4uawcnltgthX_MOGSMwmdWGDQ2JAeRXKfwlUowIkX72SFSovPalDGnBvdWBXWK6otjNqizEoA0FUDrIohGpvLa-CfBCE2vtttDopdleHm2VZK6RFuCHdW9X60kgwr9I5j1FOnWdX/s1600/03.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="992" data-original-width="1160" height="341" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjvH0wO4uawcnltgthX_MOGSMwmdWGDQ2JAeRXKfwlUowIkX72SFSovPalDGnBvdWBXWK6otjNqizEoA0FUDrIohGpvLa-CfBCE2vtttDopdleHm2VZK6RFuCHdW9X60kgwr9I5j1FOnWdX/s400/03.png" width="400" /></a><br /><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiPtEpEbRyOFU8vp305jnc0KzqsSVcivOgpJjisayIYSUxayVFOcnZKBUiqulx-GdUAHldNASff-8A1_wAaMznyXrcZkVYmILZMecx5XlMvRC-MMXliC6tyg7hdvMpXWJ4VRIbKEwKRZrAA/s1600/04+%25E6%258B%25B7%25E8%25B2%259D.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="126" data-original-width="398" height="63" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiPtEpEbRyOFU8vp305jnc0KzqsSVcivOgpJjisayIYSUxayVFOcnZKBUiqulx-GdUAHldNASff-8A1_wAaMznyXrcZkVYmILZMecx5XlMvRC-MMXliC6tyg7hdvMpXWJ4VRIbKEwKRZrAA/s200/04+%25E6%258B%25B7%25E8%25B2%259D.png" width="200" /></a></li>
<li>中場休息, GCP建立專案會需要一點時間</li>
</ol>
<div>
<h4>
GCP架設Linux server - 在GCE上建立VM</h4>
</div>
<ol>
<li>再次點擊「選取專案」, 並選擇剛剛建立的專案<br /><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhvFVsDx8mj2wrllew9Qohz50YOhZ5HUw2vjg01CmeCKnVFNZaTpI3jgVVk14bKjKRZbFxkyCENC8yYt_RftlpgrT9yst-b3PP5ytXJIYhavTanzag_qpveNxXNAY3HsC2zA2hRdRecEgah/s1600/06+%25E6%258B%25B7%25E8%25B2%259D.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="472" data-original-width="878" height="172" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhvFVsDx8mj2wrllew9Qohz50YOhZ5HUw2vjg01CmeCKnVFNZaTpI3jgVVk14bKjKRZbFxkyCENC8yYt_RftlpgrT9yst-b3PP5ytXJIYhavTanzag_qpveNxXNAY3HsC2zA2hRdRecEgah/s320/06+%25E6%258B%25B7%25E8%25B2%259D.png" width="320" /></a></li>
<li>建立VM執行個體, 這邊建立的VM就如同用VirtualBox, VirtualPC或VMWare建立出來的VM一樣, 可以當作一台電腦主機來看待<br /><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgG_ExPIZf7C6S2BYmHyz-SWOy7u1ZaAzLeV_HzJYiyi7EO5Hpt6WPrRKPYwzDA40aDhGMYHegaxzXl9CE_5rnmFGaOyAUOyjRROB199zWpSY63dhZNWnKcWjnfSG6LRxdtBBxcLnRH-92j/s1600/07+%25E6%258B%25B7%25E8%25B2%259D.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="602" data-original-width="974" height="245" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgG_ExPIZf7C6S2BYmHyz-SWOy7u1ZaAzLeV_HzJYiyi7EO5Hpt6WPrRKPYwzDA40aDhGMYHegaxzXl9CE_5rnmFGaOyAUOyjRROB199zWpSY63dhZNWnKcWjnfSG6LRxdtBBxcLnRH-92j/s400/07+%25E6%258B%25B7%25E8%25B2%259D.png" width="400" /></a></li>
<li>中場休息, GCP啟動Google Computer Engine(GCE)需要些時間, 可去喝杯茶小憩片刻</li>
<li>點選下圖的「建立」並在各個欄位中填上與下圖相同的設定<br /><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiJ2nb2Fcv3JaDXmGi5sRx-VQXSHIL5MC5KTuLZR_Zxj1fhyphenhyphenjdnj73zZq9n1v1LQKNXa7qkyIVGyyke3pB1ddNL4V4hSU3U3h8NR_p5uyCLW1hZj6SAHZ6Bo2StURp9OdqpbAMueCDVBcrX/s1600/08+%25E6%258B%25B7%25E8%25B2%259D.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="801" data-original-width="1600" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiJ2nb2Fcv3JaDXmGi5sRx-VQXSHIL5MC5KTuLZR_Zxj1fhyphenhyphenjdnj73zZq9n1v1LQKNXa7qkyIVGyyke3pB1ddNL4V4hSU3U3h8NR_p5uyCLW1hZj6SAHZ6Bo2StURp9OdqpbAMueCDVBcrX/s640/08+%25E6%258B%25B7%25E8%25B2%259D.png" width="640" /></a><br /><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhMiC1bQuGxyVCvhwsQ9EXMYIIqj2aDVKvcTtFIY-m4nQxAgdKxcQ9i71GfE4CI7zPlMyyReu0a-HVgi6eufUXsuR2UegXV-hEv2XxhIEvv2zkJ-fLYFgWSuriYiGtFGKHY9myN5dZGJNJQ/s1600/10.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="957" data-original-width="1600" height="380" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhMiC1bQuGxyVCvhwsQ9EXMYIIqj2aDVKvcTtFIY-m4nQxAgdKxcQ9i71GfE4CI7zPlMyyReu0a-HVgi6eufUXsuR2UegXV-hEv2XxhIEvv2zkJ-fLYFgWSuriYiGtFGKHY9myN5dZGJNJQ/s640/10.png" width="640" /></a><br /><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjJjbXCr0TiCAl_ZWc60yERPENXzPYNv8jAIMp6uUjlZRDn_cKkRWsmn3sIt3Cg0fjtqHstMu6XbK5V-3r_AlfmdBKBny_dWh01zkXUByEE1QGrwel8nVQO3ilNJr4JjoRl37JGluefzjk4/s1600/11.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="1150" data-original-width="1600" height="458" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjJjbXCr0TiCAl_ZWc60yERPENXzPYNv8jAIMp6uUjlZRDn_cKkRWsmn3sIt3Cg0fjtqHstMu6XbK5V-3r_AlfmdBKBny_dWh01zkXUByEE1QGrwel8nVQO3ilNJr4JjoRl37JGluefzjk4/s640/11.png" width="640" /></a><br /><span style="color: red;">注意: 根據目前的Always Free方案限制, 區域請一定要是us-east1, us-west1, and us-central1其中一個, 硬碟請在30GB內</span></li>
<li>中場休息......, 建立一台新的VM需要一點時間, 出門買個咖啡喝休息一下<br /><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh6glRieCi_qV_naI24PIp7KETsJjjGPARN2Oxe1fWkjZ5JjZEuik2HoYws0XxDlBfNTT1omN1JeIoOKie90u8Y7zd4elFHFIhypdCVZNDugEUEgoi_P2JaK7eWb7eUG5MsudX7aTWbhEpb/s1600/12.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="734" data-original-width="1600" height="291" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh6glRieCi_qV_naI24PIp7KETsJjjGPARN2Oxe1fWkjZ5JjZEuik2HoYws0XxDlBfNTT1omN1JeIoOKie90u8Y7zd4elFHFIhypdCVZNDugEUEgoi_P2JaK7eWb7eUG5MsudX7aTWbhEpb/s640/12.png" width="640" /></a></li>
<li>VM建立完成後請點擊剛建立完成的instance-1<br /><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh7sXrCbKsbpKqsxwUbK1XJ11wLsdFp6SLVowaQsMdc5T3Bmg8qPuyJcknuDrPDbFBpJVXtQZK-_xXJ0Rl9JPB7c3aheldhaQn_-bCSZDvwrAeM6dhOGOTfU0Ho49brNFOPCBnvBBmS0kpE/s1600/13.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="182" data-original-width="574" height="101" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh7sXrCbKsbpKqsxwUbK1XJ11wLsdFp6SLVowaQsMdc5T3Bmg8qPuyJcknuDrPDbFBpJVXtQZK-_xXJ0Rl9JPB7c3aheldhaQn_-bCSZDvwrAeM6dhOGOTfU0Ho49brNFOPCBnvBBmS0kpE/s320/13.png" width="320" /></a></li>
<li>點擊畫面上SSH選單的在「瀏覽器視窗中開啟」<br /><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhhxLI3s-PFAaDourrcCD-rf2N9FQkZ4-F0unEgSWYvLvnmitO-w7eoUDTX1SApLdsVY0AvBNC-ACKxeXy9ArgLtfOQ6afFtBcEfA7Dy0QRDf2XwIFqqG7SpskuCvHWbJzPKRCH_BcN-WYZ/s1600/14.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="648" data-original-width="744" height="278" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhhxLI3s-PFAaDourrcCD-rf2N9FQkZ4-F0unEgSWYvLvnmitO-w7eoUDTX1SApLdsVY0AvBNC-ACKxeXy9ArgLtfOQ6afFtBcEfA7Dy0QRDf2XwIFqqG7SpskuCvHWbJzPKRCH_BcN-WYZ/s320/14.png" width="320" /></a></li>
<li>建立成功~ 恭喜你已連上server, 現在可以下一些Linux指令來玩玩看<br /><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjlUGoU_UTFCv1KYW5PAil0E94If_PFJ7U2F97E4oapEOqrbqogTy5fdpFmuZndZ782_DeC_7YtSJbow81DUBUKXt8xAjI4NEVlil6uGK3s7zZCMDu6qByvms7WQMg29GlQ1LGEw5BsM0TO/s1600/16+%25E6%258B%25B7%25E8%25B2%259D.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="656" data-original-width="1600" height="262" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjlUGoU_UTFCv1KYW5PAil0E94If_PFJ7U2F97E4oapEOqrbqogTy5fdpFmuZndZ782_DeC_7YtSJbow81DUBUKXt8xAjI4NEVlil6uGK3s7zZCMDu6qByvms7WQMg29GlQ1LGEw5BsM0TO/s640/16+%25E6%258B%25B7%25E8%25B2%259D.png" width="640" /></a></li>
</ol>
<h4>
GCP架設Linux server - 透過自己電腦上的ssh連上Linux server</h4>
<div>
<br />
身為一個開發者有自己習慣的ssh軟體是很正常的, 因此以下將說明怎麼使用自己的ssh軟體連上GCE上的VM</div>
<div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<ol>
<li>建立ssh的公鑰檔id_rsa.pub與私鑰檔id_rsa, 若已經有此兩個檔案的讀者可略過此步驟<br />指令如下<br />
<code><com>#建立id_rsa與id_rsa.pub</com>
<com>#已有此兩個檔案的使用者請千萬不要執行以下指令, 避免覆蓋掉舊的檔案</com>
<pro> shiun@Shiun:~$</pro> ssh-keygen -t rsa
<com>#中間出現的選項若沒有特殊目的則可以全部按enter使用預設值即可</com>
<pro> shiun@Shiun:~$</pro> cat .ssh/id_rsa.pub
ssh-rsa AAXXXXnX shiun@Shiun
</code>
<br /><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEinLAdHJ2DW0_BXvBPmp_M8DWV44zVm9JIjtzJHvV_C1Rck5L6OHdfTLmaE6gloPa1QjG0A_W3k1CnmhJWlftsdY9Jte4rgg1XsQB9bvEeT3I8eGQq77Lzy289u9W5CLoGh_yb9_06YEt_2/s1600/22_2+%25E6%258B%25B7%25E8%25B2%259D.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="475" data-original-width="752" height="404" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEinLAdHJ2DW0_BXvBPmp_M8DWV44zVm9JIjtzJHvV_C1Rck5L6OHdfTLmaE6gloPa1QjG0A_W3k1CnmhJWlftsdY9Jte4rgg1XsQB9bvEeT3I8eGQq77Lzy289u9W5CLoGh_yb9_06YEt_2/s640/22_2+%25E6%258B%25B7%25E8%25B2%259D.png" width="640" /></a></li>
<li>開啟GCE的「中繼資料」頁面並進入「SSH金鑰頁面」<br /><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg7bS6dfyQwXXW8S-9gSVZqDArAYs7b-0NtazCHJMDi6v_TqyZnIsWw0D5cyhJFPyJJnlpVcq6LEOpIceuo0qRazVahqOFFplbgvhYxMxJBlbTmWHEwcqqCXDEmmvL9wylHsIJaNjYIHP41/s1600/21+%25E6%258B%25B7%25E8%25B2%259D.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="866" data-original-width="1600" height="345" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg7bS6dfyQwXXW8S-9gSVZqDArAYs7b-0NtazCHJMDi6v_TqyZnIsWw0D5cyhJFPyJJnlpVcq6LEOpIceuo0qRazVahqOFFplbgvhYxMxJBlbTmWHEwcqqCXDEmmvL9wylHsIJaNjYIHP41/s640/21+%25E6%258B%25B7%25E8%25B2%259D.png" width="640" /></a></li>
<li>並將SSH金鑰也就是id_rsa.pub中ssh-rsa開頭到user@xxxx結尾的全部文字複製貼到SSH金鑰頁面上</li>
<li>回到GCE頁面找到Linux server的IP位置<br /><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjjPyyHam4ox0ybqocDeMtLT4SmT93Nn382HNWpgfL5-H6iBw5wQGT21kH4iREZfQgRQt9iUwF0Xg6LQhNuO_PcgE6IOJIaK5jJlH9_GwxIfqPdCQmHJT8wyqvKuW6IXmo5kzl2Iem0AkzF/s1600/26+%25E6%258B%25B7%25E8%25B2%259D.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="298" data-original-width="907" height="209" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjjPyyHam4ox0ybqocDeMtLT4SmT93Nn382HNWpgfL5-H6iBw5wQGT21kH4iREZfQgRQt9iUwF0Xg6LQhNuO_PcgE6IOJIaK5jJlH9_GwxIfqPdCQmHJT8wyqvKuW6IXmo5kzl2Iem0AkzF/s640/26+%25E6%258B%25B7%25E8%25B2%259D.png" width="640" /></a></li>
<li>使用終端機介面輸入ssh指令連線到GCE上的Linux server<br /><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh16odXctz8wCQmtRetKBcstHGCc4WnM3RCZZ44TKREEt0G3MIs3uWQveR2lcqmUMFw9fGWnx1rBAcghn6nwBIvNuQSFG5ylcw8JsTuu4k6JSJxyfOmFXyKykbfFfhN9_7M4mOsmdudkQYA/s1600/27.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="347" data-original-width="598" height="370" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh16odXctz8wCQmtRetKBcstHGCc4WnM3RCZZ44TKREEt0G3MIs3uWQveR2lcqmUMFw9fGWnx1rBAcghn6nwBIvNuQSFG5ylcw8JsTuu4k6JSJxyfOmFXyKykbfFfhN9_7M4mOsmdudkQYA/s640/27.png" width="640" /></a></li>
<li>收工~</li>
</ol>
</div>
</div>
<br /></div>
<h3>
<span style="font-family: inherit;">
參考資料</span></h3>
<div>
<ol>
<li><a href="https://cloud.google.com/free/docs/frequently-asked-questions#always-free" target="_blank"><span style="font-family: inherit;">Frequently Asked Questions | Google Cloud Platform Free Tier | Google Cloud</span></a></li>
<li><span style="font-family: inherit;"><a href="https://cloud.google.com/free/docs/always-free-usage-limits" target="_blank">Always Free Usage Limits | Google Cloud Platform Free Tier | Google Cloud</a></span></li>
<li><a href="https://console.cloud.google.com/?hl=zh-TW" target="_blank">Google Cloud Platform</a></li>
</ol>
</div>
Shiunhttp://www.blogger.com/profile/00430694685762085062noreply@blogger.com0tag:blogger.com,1999:blog-694306540104860106.post-68969661880196996072016-05-09T15:45:00.001+08:002016-05-09T15:56:50.269+08:00Android 使用adb的logcat指令抓取對應log level與tag的log資訊由於筆者最近一般消費性平板或專業的開發板上開發的程式有不少麻煩的特性<br />
<br />
例如<br />
<ol>
<li>消費型平板或開發板的廠商自訂log訊息太多</li>
<li>消費型平板上唯一的micro USB port要拿來連結USB to UART轉接板(可用<a href="http://blog.alenshiun.tw/2016/05/android-wi-fi-debug.html" target="_blank">Android WiFi debug</a>方式解決)</li>
<li>Android framework層給的訊息少了log的時間要拿來debug不方便</li>
<li>..., etc.</li>
</ol>
<br />
考慮到以上的問題,筆者使用終端機開啟類似Android Studio上Android Monitor的logcat並將這些log存為文字檔後再進行分析<br />
<br />
<h4>
事前準備</h4>
<ol>
<li>安裝Android開發工具,不然至少要確定有安裝到adb的執行檔和adb的相依套件</li>
<li>確認環境變數<br />Windows使用者請由控制台 -> 系統及安全性 -> 進階系統設定 -> 進階 -> 環境變數中確認使用者變數或是系統變數中的PATH是否有adb執行檔的路徑<br />
<div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhMoTK2prvnw-j9y4MSq2jYiXfDvfM1KkTZYSKPwBzRpCe0nRVmjCi9LLEsQj3KLcnzoYKV4_MSNKKksWTuUxPdRodB3qd9xjpH60m0bwwQeSTjsPuKFODiITC9J6BqzO3-O1YIDDP5EME2/s1600/%25E8%259E%25A2%25E5%25B9%2595%25E5%25BF%25AB%25E7%2585%25A7+2016-05-09+%25E4%25B8%258B%25E5%258D%25882.50.41.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhMoTK2prvnw-j9y4MSq2jYiXfDvfM1KkTZYSKPwBzRpCe0nRVmjCi9LLEsQj3KLcnzoYKV4_MSNKKksWTuUxPdRodB3qd9xjpH60m0bwwQeSTjsPuKFODiITC9J6BqzO3-O1YIDDP5EME2/s320/%25E8%259E%25A2%25E5%25B9%2595%25E5%25BF%25AB%25E7%2585%25A7+2016-05-09+%25E4%25B8%258B%25E5%258D%25882.50.41.png" width="310" /></a></div>
<br />
<br />
<br /></div>
Mac/Linux使用者請確定自己的~/.bashrc或~/.zshrc中是否有加入adb的路徑</li>
<div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhYOjaqz3mgSKJIVg_VcqD6h_dH3b72WdT1RmyNBvuzLZ-rM8UP_mTRug0U1Q4iZTs2D4oRdZTCfx4jppaPQMAgkV_0WoKcUbIGfk_JEbYDkMvcfunRXJJoG_yrttqWSYzo1jlvjuvVdXF5/s1600/111.png" imageanchor="1"><img border="0" height="154" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhYOjaqz3mgSKJIVg_VcqD6h_dH3b72WdT1RmyNBvuzLZ-rM8UP_mTRug0U1Q4iZTs2D4oRdZTCfx4jppaPQMAgkV_0WoKcUbIGfk_JEbYDkMvcfunRXJJoG_yrttqWSYzo1jlvjuvVdXF5/s640/111.png" width="640" /></a></div>
<br /></div>
<li>下指令測試adb是否能用</li>
<code>
<com># 使用adb version指令檢查adb是否可使用並且觀看adb版本 </com>
<pro>shiun@test:~$ </pro>adb version
Android Debug Bridge version 1.0.32
Revision 09a0d98bebce-android
</code></ol>
<div>
<br />
<h4>
正式開工</h4>
</div>
<ol>
<li>開啟終端機</li>
<li>執行adb logcat -v time -s TestMainActivity列出所有以TestMainActivity作為tag的log訊息</li>
<li>下圖為log出來的結果,其中log訊息會包含時間, log level及log message</li>
</ol>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiJCoja1FgbUj_h5fC-vaWL4xDZWzNSl-NoHrSOwyzDl-d_RO6P_rkCxVu_vPod7g8tW0gXk5DAxdoKn_tKaBA0N9XK-OkuRCj_qoUha1v9be6WBOydoEf3Kwbp4iZoA3S3pWx9nqgXj-vI/s1600/%25E8%259E%25A2%25E5%25B9%2595%25E5%25BF%25AB%25E7%2585%25A7+2016-05-09+%25E4%25B8%258B%25E5%258D%25883.42.58.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="154" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiJCoja1FgbUj_h5fC-vaWL4xDZWzNSl-NoHrSOwyzDl-d_RO6P_rkCxVu_vPod7g8tW0gXk5DAxdoKn_tKaBA0N9XK-OkuRCj_qoUha1v9be6WBOydoEf3Kwbp4iZoA3S3pWx9nqgXj-vI/s640/%25E8%259E%25A2%25E5%25B9%2595%25E5%25BF%25AB%25E7%2585%25A7+2016-05-09+%25E4%25B8%258B%25E5%258D%25883.42.58.png" width="640" /></a></div>
<div>
<br /></div>
Shiunhttp://www.blogger.com/profile/00430694685762085062noreply@blogger.com0tag:blogger.com,1999:blog-694306540104860106.post-43247399673390711892016-05-09T14:28:00.002+08:002016-05-09T14:40:00.232+08:00Android Wi-Fi debug功能<h4>
問題</h4>
最近筆者在研究要怎麼使用Android消費型平板上的micro USB與Arduino或其他裝置透過UART進行溝通。但遇到一個非常麻煩的問題: <span style="color: red;">唯一的micro USB已經被UART轉板佔據住,沒辦法透過USB去抓debug訊息.........</span><br />
<br />
為了降低後續開發的痛苦,筆者試著找看看有沒有其他方式可以抓到debug訊息<br />
<br />
<div style="text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhDP1FWz79bZ87r0ruT947SAGY5nzSciysiTeOusIUZjxPPU2IbYA3Kesmaf_CInT9tvIK_hB7BPikF_lYGkL2z-oTlVW2Bo_RVliOI9cclseWTBfNAEAk5kRM1BQabsHXojVjcw6KrH54V/s1600/1.JPG" imageanchor="1"><img border="0" height="480" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhDP1FWz79bZ87r0ruT947SAGY5nzSciysiTeOusIUZjxPPU2IbYA3Kesmaf_CInT9tvIK_hB7BPikF_lYGkL2z-oTlVW2Bo_RVliOI9cclseWTBfNAEAk5kRM1BQabsHXojVjcw6KrH54V/s640/1.JPG" width="640" /></a></div>
<div style="text-align: center;">
Fig 1. Nexus 7 + USB OTG線 + USB to UART轉板</div>
<div style="text-align: center;">
<br /></div>
<h4 style="text-align: left;">
解決方案</h4>
<div style="text-align: left;">
參考Android Developers網頁可以發現Android的設計上有Wireless debug[1]的方案,只需透過adb工具即可開啟Wi-Fi debug功能,詳細說明如下</div>
<div style="text-align: left;">
<br /></div>
<h3 style="text-align: left;">
環境</h3>
<div style="text-align: left;">
</div>
<ul>
<li>Android裝置: Nexus 7 2013 + Android 6.0.1</li>
<li>電腦: Macbook + OSX 10.11.4</li>
<li>adb 1.0.32</li>
</ul>
<br />
<h3 style="text-align: left;">
步驟</h3>
<div style="text-align: left;">
</div>
<ol>
<li>使用USB線連結Android裝置與電腦</li>
<li>確認USB偵錯有開啟</li>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEidRgrFUnh2TBzHkvFbZOVfs-WuHyFj_fSyFny4qRrhCTYLAx3xWDMe376ELT5O3PuXqaiz05WUzxbAACsXZh0qMi17D0zAT-CuzX4oluXNZ9CtM6OAFK34eB6IiGJxs9huo8tjtVRcX7pU/s1600/2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="400" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEidRgrFUnh2TBzHkvFbZOVfs-WuHyFj_fSyFny4qRrhCTYLAx3xWDMe376ELT5O3PuXqaiz05WUzxbAACsXZh0qMi17D0zAT-CuzX4oluXNZ9CtM6OAFK34eB6IiGJxs9huo8tjtVRcX7pU/s640/2.png" width="640" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
Fig 2. 開啟USB偵錯</div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<li>將Android裝置連線到Wi-Fi的AP上,並找到Android裝置的IP</li>
<div>
方法1: 由Android裝置的Wi-Fi設定找到目前IP位置,但並不是所有裝置都可以直接看到IP,例如筆者的Nexus 7就是不能透過此方法看到IP的機器之一.......</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh9AMEr3t40OArSFA1chjuFROTzumoDLiOnRrlD3fKCvkygfdjUZjjIZUadYRw3DOiB8GCSgTmN2QjHBB_rr0DNgAUZ2KbOmgS4MkJ-9I5aK3yVEVc6kDRUEMSfpwgycDoqx_mFeIOhZz08/s1600/3.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="400" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh9AMEr3t40OArSFA1chjuFROTzumoDLiOnRrlD3fKCvkygfdjUZjjIZUadYRw3DOiB8GCSgTmN2QjHBB_rr0DNgAUZ2KbOmgS4MkJ-9I5aK3yVEVc6kDRUEMSfpwgycDoqx_mFeIOhZz08/s640/3.png" width="640" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
Fig 3. 透過Wi-Fi設定找尋IP位置,筆者的Nexus 7不適用QQ.....</div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div>
方法2: 透過adb指令找到Android裝置的IP位置</div>
<code>
<com># 對Android裝置下ifconfig命令</com>
<com># adb shell: 透過adb對Android裝置下指令</com>
<com># ifconfig: 列出所有網卡的資訊</com>
<pro>shiun@test:~$ </pro>adb shell ifconfig
</code>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjp1eXDjOXMPdqpmhNp4sCSjPo1O1Ccj9I3zoZhPId3OnruoWtw8SUXl-6cT9i3FmGkds40b5WZBTokh2rK8qAr69b4OWjjPfejIMjDwOz2gwVedqpLETj7AHNALDfWYzhvdAfGzerKDzL-/s1600/4.png" imageanchor="1"><img border="0" height="593" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjp1eXDjOXMPdqpmhNp4sCSjPo1O1Ccj9I3zoZhPId3OnruoWtw8SUXl-6cT9i3FmGkds40b5WZBTokh2rK8qAr69b4OWjjPfejIMjDwOz2gwVedqpLETj7AHNALDfWYzhvdAfGzerKDzL-/s640/4.png" width="640" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
Fig 4. 透過adb指令找到IP位置為192.168.1.195</div>
<br />
<li>使用adb命令開啟Wi-Fi debug功能</li>
<code>
<com># 開啟Wi-Fi debug功能,並設定連線port 5555</com>
<pro>shiun@test:~$ </pro>adb tcpip 5555
restarting in TCP mode port: 5555
</code>
<br />
<li>拔掉USB連接線</li>
<li>在電腦端下連線指令</li>
<code>
<com># 連線至Android裝置</com>
<pro>shiun@test:~$ </pro>adb connect 192.168.1.195
connected to 192.168.1.195:5555
</code>
<br />
<li>接著到Android Studio上看結果</li>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjMInNriZq2HPKyUSYQu3_Yw6BSeE8aqby4235_A8WB9vm1g9v7UcG9N586_qihS3mBIOUBicFkdIO4CkR-nre4q9OmfECMK0yLDGZHFCa2FSqBQkn5DhQCtUgSU6-HVE7uwdEcOu0QdnM3/s1600/5.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="440" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjMInNriZq2HPKyUSYQu3_Yw6BSeE8aqby4235_A8WB9vm1g9v7UcG9N586_qihS3mBIOUBicFkdIO4CkR-nre4q9OmfECMK0yLDGZHFCa2FSqBQkn5DhQCtUgSU6-HVE7uwdEcOu0QdnM3/s640/5.png" width="640" /></a></div>
<div>
<br />
<br />
<li>最後要收工的時候記得要關閉Wi-Fi debug功能</li>
<code>
<com># 關閉Wi-Fi debug功能</com>
<pro>shiun@test:~$ </pro>adb usb
restarting in USB mode
</code>
</div>
</ol>
<h4>
</h4>
<h4>
補充</h4>
<div>
若讀者的Android裝置為已root裝置(adb shell進入後直接是root身份的開發板也行),則可考慮直接安裝WiFi ADB - Debug Over Air這套app即可</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEht4E0z0sDdU0bczEqB5Q1lqbOj88P1Pm2hKk-XTyBvi-dZL4y_2ZNsPF3EnkS9P-0_ZUbESdZvAUTvuzh0XzGLLgHmJtCjZPvKQsIdOahRtq0_DOeikBzJhPN4Dg9cmHg17nQSs9Aa_aVx/s1600/6.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="334" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEht4E0z0sDdU0bczEqB5Q1lqbOj88P1Pm2hKk-XTyBvi-dZL4y_2ZNsPF3EnkS9P-0_ZUbESdZvAUTvuzh0XzGLLgHmJtCjZPvKQsIdOahRtq0_DOeikBzJhPN4Dg9cmHg17nQSs9Aa_aVx/s640/6.png" width="640" /></a></div>
<div>
<br /></div>
<h4>
參考資料</h4>
<div>
<span style="background-color: white; color: #444444; font-family: "arial" , "tahoma" , "helvetica" , "freesans" , sans-serif; font-size: 13px; line-height: 18.2px;"> 1. <a href="http://developer.android.com/intl/zh-tw/tools/help/adb.html#wireless" target="_blank">Android developers - Android Debug Bridge</a>: </span><span style="color: #444444; font-family: "arial" , "tahoma" , "helvetica" , "freesans" , sans-serif;"><span style="font-size: 13px; line-height: 18.2px;"><a href="http://developer.android.com/intl/zh-tw/tools/help/adb.html#wireless" target="_blank">Wireless usage</a></span></span></div>
Shiunhttp://www.blogger.com/profile/00430694685762085062noreply@blogger.com0tag:blogger.com,1999:blog-694306540104860106.post-88221389089204959962015-11-25T21:38:00.002+08:002015-11-25T21:38:48.766+08:00樹莓派 /etc/network/interface 直接進行WiFi設定auto lo<br />
<br />
iface lo inet loopback<br />
iface eth0 inet dhcp<br />
<br />
allow-hotplug wlan0<br />
auto wlan0<br />
<br />
<br />
iface wlan0 inet dhcp<br />
wpa-ssid "ssid"<br />
wpa-psk "password"<br />
<div>
<br /></div>
<br />
參考<br />
<a href="https://learn.adafruit.com/adafruits-raspberry-pi-lesson-3-network-setup/setting-up-wifi-with-occidentalis" target="_blank">Setting up Wifi with the Command Line</a>Shiunhttp://www.blogger.com/profile/00430694685762085062noreply@blogger.com0tag:blogger.com,1999:blog-694306540104860106.post-87933451348928294602015-11-25T21:38:00.001+08:002016-11-07T17:47:57.727+08:00Mac 使用ssh連限至遠端電腦時中文變亂碼<h3>
環境:</h3>
本地端作業系統: OS X Yosemite ~ Sierra<br />
終端機程式: iTerm2<br />
遠端作業系統: Ubuntu 14.04<br />
(update @2016/11/07, 最近更新Mac OS X同樣的問題又出現了,不過同樣用同樣的方法一樣能解決)<br />
<br />
<h3>
狀況:</h3>
使用ssh連線至遠端電腦後輸入中文變成亂碼, 下圖為亂碼的例子<br />
<br />
1. 輸入「中文」<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi5MKcjixqCzXmXp-jNyQpjkYRxNbM5p04p2KXF8lo57pAVwLc4Z5vJUVsud6IQTmWsF3WH4mEBhNKKljy1HQzPSR2CRKmKSWFVxnyVutOv_j84trNJgYD4kSrMXC1vSdGpLRnHiHoJKV2E/s1600/%25E8%259E%25A2%25E5%25B9%2595%25E5%25BF%25AB%25E7%2585%25A7+2015-08-04+%25E4%25B8%258A%25E5%258D%258811.50.58.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="125" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi5MKcjixqCzXmXp-jNyQpjkYRxNbM5p04p2KXF8lo57pAVwLc4Z5vJUVsud6IQTmWsF3WH4mEBhNKKljy1HQzPSR2CRKmKSWFVxnyVutOv_j84trNJgYD4kSrMXC1vSdGpLRnHiHoJKV2E/s400/%25E8%259E%25A2%25E5%25B9%2595%25E5%25BF%25AB%25E7%2585%25A7+2015-08-04+%25E4%25B8%258A%25E5%258D%258811.50.58.png" width="400" /></a></div>
<br />
2. 結果卻變成下圖<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg0IixvKhrIOp3hu6g7aewA92bsZzRQeNtXc8Hb2Cy3V9kh-UHj5KDhg5hYoDdQHtHT82RQ-qkhkhro8MtbQwb6Wh4xlXJ14cKVtM7CzUo3CTz0NuDHswAe_LWHoOAtHiU1ahhNooaJ-_VK/s1600/%25E8%259E%25A2%25E5%25B9%2595%25E5%25BF%25AB%25E7%2585%25A7+2015-08-04+%25E4%25B8%258A%25E5%258D%258811.51.03.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="126" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg0IixvKhrIOp3hu6g7aewA92bsZzRQeNtXc8Hb2Cy3V9kh-UHj5KDhg5hYoDdQHtHT82RQ-qkhkhro8MtbQwb6Wh4xlXJ14cKVtM7CzUo3CTz0NuDHswAe_LWHoOAtHiU1ahhNooaJ-_VK/s400/%25E8%259E%25A2%25E5%25B9%2595%25E5%25BF%25AB%25E7%2585%25A7+2015-08-04+%25E4%25B8%258A%25E5%258D%258811.51.03.png" width="400" /></a></div>
<br />
<h3>
解決方案:</h3>
修改/etc/ssh/ssh_config檔案,將SendEnv LANG LC_*註解掉即可<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh2r1ZgR8NHZVq-t0CLa_FvgtAprc5K-KjMKxqWAYITC1ct6PESPTlOmgI8A_vuP-pPki5i4iGLxqoRcQZ25RCCwVoQyZS8Xh3eqCPkR0HmDFCEybPb2zwvJTpjX6RL7eHxEEkgyXB5jlMx/s1600/%25E8%259E%25A2%25E5%25B9%2595%25E5%25BF%25AB%25E7%2585%25A7+2015-08-04+%25E4%25B8%258A%25E5%258D%258811.51.38.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="156" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh2r1ZgR8NHZVq-t0CLa_FvgtAprc5K-KjMKxqWAYITC1ct6PESPTlOmgI8A_vuP-pPki5i4iGLxqoRcQZ25RCCwVoQyZS8Xh3eqCPkR0HmDFCEybPb2zwvJTpjX6RL7eHxEEkgyXB5jlMx/s640/%25E8%259E%25A2%25E5%25B9%2595%25E5%25BF%25AB%25E7%2585%25A7+2015-08-04+%25E4%25B8%258A%25E5%258D%258811.51.38.png" width="640" /></a></div>
<br />Shiunhttp://www.blogger.com/profile/00430694685762085062noreply@blogger.com0tag:blogger.com,1999:blog-694306540104860106.post-71509115874629479682015-11-25T21:28:00.000+08:002015-11-25T21:39:12.080+08:00PHP 二進位處理 pack and unpack幾個月前留下的PHP二進位處理方式,加減做個筆記一下<br />
<br />
pack和unpack用來處理二進位資料<br />
範例如下<br />
<br />
<pre class="prettyprint"><?php
// 使用pack將ascii的1~13放到$txData變數中
for($i=1;$i<14;$i++) {
$txData .= pack("C", $i);
}
// 透過rs232模組寫出去,其中rs232的tx/rx短路,因此寫出去的資料可以在$rxData中收到
$rxData = $this->rs232_model->writeAndRead($txData);
// 使用unpack將$rxData中的二進位資料轉回來
$res = unpack("C*", $rxData);
?>
</pre>
Shiunhttp://www.blogger.com/profile/00430694685762085062noreply@blogger.com1tag:blogger.com,1999:blog-694306540104860106.post-67429519205657352322015-11-24T09:33:00.002+08:002015-11-24T10:07:46.523+08:00Nexus 7 二代 (2013版) 觸控失靈DIY維修由於筆者的Nexus 7(2013板,以下簡稱N7)的觸控螢幕狀況每況愈下,滑動螢幕時常常沒反應或卡住,因此在網路上尋找是否有修正方法<br />
<br />
網路上查到的可能原因方法如下:<br />
1. 螢幕排線鬆掉<br />
<br />
2. 濕氣太重<br />
<br />
3. 螢幕太髒<br />
<br />
4. Android軟韌體問題<br />
<br />
首先先用刪去法<br />
3. 螢幕太髒<br />
筆者近期常常拿拭淨布擦,但擦完後也只有一小段時間不會觸控失靈<br />
<br />
4. Android問題<br />
許多人說的4.4.2已經改善,但是筆者的已經是Android 6.......<br />
<br />
因此筆者針對螢幕排線和濕氣太重試著處理看看,處理流程如下<br />
1. 沒事就丟防潮箱,不然至少丟防潮箱一晚<br />
(註: 防潮箱通常只是控制濕度而已,不是專門拿來除濕用)<br />
<br />
2. 拆了N7的排線! 此動作可能造成保固失效或機器整台gg,請找認識的好人幫忙處理,筆者是擅長魔改硬體的強者我學長幫忙拆機<br />
<strike>Step 0: 拍照留個紀念</strike><br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhW1RhZNWpfip7i2Qu0Syw555W4PTfpsnBKhOfuL_NaFr3dNWQ-vI3K_DoMeSTUPYkshCDIFV2kpTyw-gYrRlEmkiQAK-fxf-_jvTb4dpD5ximJv3_EM4Ai1fcuoOay7hjo0Kw9wUtRXYrg/s1600/IMG_3190.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="193" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhW1RhZNWpfip7i2Qu0Syw555W4PTfpsnBKhOfuL_NaFr3dNWQ-vI3K_DoMeSTUPYkshCDIFV2kpTyw-gYrRlEmkiQAK-fxf-_jvTb4dpD5ximJv3_EM4Ai1fcuoOay7hjo0Kw9wUtRXYrg/s320/IMG_3190.JPG" width="320" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
Fig 1. Nexus 7 (2013) 背面</div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEghyhyphenhyphena-3erPzdUA6Z9P6sHQLJmA0vGK2qifJM5L4mbxROlf0TT3IjSBolWvIswtULIhhSmREFiVfCk_H_Nv9OVZ9DViC4t91T571pbLuuahm14xHpzT-fOVAYN9lEClRqSIirFCJi6C9af/s1600/IMG_3191.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="204" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEghyhyphenhyphena-3erPzdUA6Z9P6sHQLJmA0vGK2qifJM5L4mbxROlf0TT3IjSBolWvIswtULIhhSmREFiVfCk_H_Nv9OVZ9DViC4t91T571pbLuuahm14xHpzT-fOVAYN9lEClRqSIirFCJi6C9af/s320/IMG_3191.JPG" width="320" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
Fig 2. Nexus 7 (2013)正面</div>
<strike><br /></strike>
<strike><br /></strike>
Step 1: 先找拆機教學影片<br />
影片連結: <a href="https://www.youtube.com/watch?v=hAlNuGrGDuU" target="_blank">Nexus 7 (2013) Screen Repair & Disassemble</a><br />
<br />
Step 2: 關機<br />
避免出狀況,一定要關機後再開工。因為很重要,所以要說三次: 「要關機!」、「要關機!」、「要關機!」<br />
<br />
Step 3: 拆惹它!!!<br />
大致上如影片中採用吉他撥片拆開,但是強者我學長用指甲就拆開了XDDDDDDD<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjtVOuCOITq7-LeD9LDQsVhMs9AggIV_ymbtS54SLoZnzZ8a0bnYxJgSl91FzGzqxbGIeNxhxBMRHmjixUWaTD_WQAfZ1zRk7F373cn4t0eJKhvUAAD2oiejCZK-a0nhR3CTMqruuRu8L9P/s1600/IMG_3188.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="314" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjtVOuCOITq7-LeD9LDQsVhMs9AggIV_ymbtS54SLoZnzZ8a0bnYxJgSl91FzGzqxbGIeNxhxBMRHmjixUWaTD_WQAfZ1zRk7F373cn4t0eJKhvUAAD2oiejCZK-a0nhR3CTMqruuRu8L9P/s320/IMG_3188.JPG" width="320" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
Fig 3. 拆解結果圖,上方為N7本體,下方為N7的外殼</div>
<br />
Step 4: 接著將下圖中紅色框框的地方如影片中的方式拆開再重插回去即可<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgWvFQq643InYbY7hyphenhyphenaHKsi4SqHq_WN9y2FNAOSQXrtDFjyMsrLGvDwm2Sn_ek-2ppBAFyHEtSkOwLyRS3fu-_ZNoVYxyckbdfbVpt3gYrK4WGLqyu5AJlC7_CRFCoe02nwkpwggqrl05sh/s1600/IMG_3187.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="240" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgWvFQq643InYbY7hyphenhyphenaHKsi4SqHq_WN9y2FNAOSQXrtDFjyMsrLGvDwm2Sn_ek-2ppBAFyHEtSkOwLyRS3fu-_ZNoVYxyckbdfbVpt3gYrK4WGLqyu5AJlC7_CRFCoe02nwkpwggqrl05sh/s320/IMG_3187.JPG" width="320" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
Fig 4. 電源與排線拆解圖</div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
紅框1: 為安全起見,先拆除電源線</div>
<div class="separator" style="clear: both; text-align: left;">
紅框2: </div>
<div class="separator" style="clear: both; text-align: left;">
i. 拆開剛好壓在右下紅框最下面的線,估計是聲音相關的線,但是會卡到螢幕排線</div>
<div class="separator" style="clear: both; text-align: left;">
ii. 拆解排線,此處是照影片結果推估這紅框2的兩端應該都是觸控螢幕的排線,因此把他拆開再裝回去</div>
<div class="separator" style="clear: both; text-align: left;">
最後再將聲音線和電源線裝回去</div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
Step 5: 把N7外殼裝回去,接著開機測試即完工</div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
結果</div>
<div class="separator" style="clear: both; text-align: left;">
經過施工後至今N7的觸控已經回復正常(灑花)</div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
不過3C產品會出問題的原因百百種,此狀況雖然適用於筆者,但適不適用其他人就不得而知了,因此請各位要拆解前先思考一下以下問題</div>
<div class="separator" style="clear: both; text-align: left;">
1. <strike>投資一定有風險,基金申購前應詳閱公開說明書</strike>。DIY的代價就是有機會整台gg,動手之前請三思</div>
<div class="separator" style="clear: both; text-align: left;">
2. 找得到專家幫忙拆或自己是能拆的專家嗎?</div>
<div class="separator" style="clear: both; text-align: left;">
3. 由於拆解了,因此保固可能失效,未來送回原廠也不能怪原廠不受理</div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
Shiunhttp://www.blogger.com/profile/00430694685762085062noreply@blogger.com2tag:blogger.com,1999:blog-694306540104860106.post-43766306951047040682015-09-27T14:01:00.000+08:002015-09-27T14:19:22.074+08:00樹莓派(Linux通用) QR code掃描機由於最近的研究需要用到固定式QR code掃描機,但網路上找到的都要3k~10k不等,礙於荷包太過瘦弱,筆者自己用樹莓派加web cam架設QR code掃描機<br />
<br />
前置作業<br />
1. 準備好安裝完raspbian的Pi 2代<br />
2. 準備支援V4L2的web cam<br />
不知道啥叫V4L2沒關係,反正就把web cam的USB插上Pi以後看/dev/video[N]有沒有出現<br />
例如筆者使用的Logitech, Inc. Webcam C310的就是出現下圖:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgqeajypwbhJffJmzpsjgVyFp4nybnJNJVp4TNzAaOmXkyDcu8vOX1XQa_rM6Ld32hyphenhyphenUQPubAO2Zp_RUx50F7t4Vg4kMaop31OwxqTIOM1TZyn869FTgp4Vt-nTB_6lTfW2Ce0SLDK2e2SG/s1600/1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="142" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgqeajypwbhJffJmzpsjgVyFp4nybnJNJVp4TNzAaOmXkyDcu8vOX1XQa_rM6Ld32hyphenhyphenUQPubAO2Zp_RUx50F7t4Vg4kMaop31OwxqTIOM1TZyn869FTgp4Vt-nTB_6lTfW2Ce0SLDK2e2SG/s640/1.png" width="640" /></a></div>
<div style="text-align: center;">
圖1. 查詢video裝置</div>
<br />
架設說明如下<br />
1. 先更新apt的資料庫<br />
<code><pro>pi@raspberrypi ~ $ </pro>sudo apt-get update
</code>
<br />
<br />
2. 安裝zbar-tools<br />
<code><pro>pi@raspberrypi ~ $ </pro>sudo apt-get install zbar-tools
</code>
<br />
<br />
3. 執行zbarcam,並調整解析度為320x240<br />
注意: 解析度千萬別設太高,否則Pi會很lag.......<br />
<code><pro>pi@raspberrypi ~ $ </pro>zbarcam ---prescale=320x240</code><br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg5U3_yu24NTpGVxblqq39c4osBOpFYSPizakZV2leH4vtv8NOGqa-B6KDN0nyN4_MGsBzXdLdpTI3gg800yRFYdOiU4DUFP2fjYGuccOO5_aM9ketv8EM17REcRyEegT2osKrCnqqOINUa/s1600/2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="275" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg5U3_yu24NTpGVxblqq39c4osBOpFYSPizakZV2leH4vtv8NOGqa-B6KDN0nyN4_MGsBzXdLdpTI3gg800yRFYdOiU4DUFP2fjYGuccOO5_aM9ketv8EM17REcRyEegT2osKrCnqqOINUa/s320/2.png" width="320" /></a>
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjEZuGQUpYL95OCb_exU-zZlzQi9o4XYXwV148BgscWSScqG_WAFx_XL6Gi9ITULKx4VQsYnCSAlwtF9Kgu6xaoSYWFiUg4eVZr8EbnxK8icXK4peoSv0fcD9vSydqj2MFiGgilPctEPfgj/s1600/3.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="274" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjEZuGQUpYL95OCb_exU-zZlzQi9o4XYXwV148BgscWSScqG_WAFx_XL6Gi9ITULKx4VQsYnCSAlwtF9Kgu6xaoSYWFiUg4eVZr8EbnxK8icXK4peoSv0fcD9vSydqj2MFiGgilPctEPfgj/s320/3.png" width="320" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<div style="text-align: center;">
圖2. 開啟web cam後會先看到左圖的zbar logo,接著就會顯示web cam拍攝到的內容</div>
<br />
4. 找張QR code入鏡並拍出結果吧~<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhkXp91IkZOjJoeVjYQwQ9rimQAlUEWLRmYFDdmKcfLt8-9BYLVyxpUAUHTssQ7D9W2V9MELVz6CE37mPItjnDQzpk4cOL4xUhJkYYS2bd3BvC-d6Hxb-GsIxNWBeN-lQrQccNP-Pxlc_UX/s1600/4.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="284" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhkXp91IkZOjJoeVjYQwQ9rimQAlUEWLRmYFDdmKcfLt8-9BYLVyxpUAUHTssQ7D9W2V9MELVz6CE37mPItjnDQzpk4cOL4xUhJkYYS2bd3BvC-d6Hxb-GsIxNWBeN-lQrQccNP-Pxlc_UX/s640/4.png" width="640" /></a></div>
<div style="text-align: center;">
圖3. 透過web cam掃描左側QR code,再由zbarcam顯示掃描結果</div>
<br />
=======================================================<br />
若沒GUI的話跑zbarcam是會失敗的,此時可以改用以下指令<br />
<code><pro>pi@raspberrypi ~ $ </pro>zbarcam --nodisplay ---prescale=320x240</code><br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjt4Jdpb9DEgMYNwkzmEaSpRCp9wjMbnlimTCwLgD7U75ynPzC2zANIZHZNzNMs6TeVaWRWOP3Pi_EqnUVFTqib2NUs0kDbrNujw1c97tPuLzBVHA30bahW4y0uURuYvxVejE8dd2IfaiKS/s1600/5.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="80" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjt4Jdpb9DEgMYNwkzmEaSpRCp9wjMbnlimTCwLgD7U75ynPzC2zANIZHZNzNMs6TeVaWRWOP3Pi_EqnUVFTqib2NUs0kDbrNujw1c97tPuLzBVHA30bahW4y0uURuYvxVejE8dd2IfaiKS/s640/5.png" width="640" /></a></div>
<div style="text-align: center;">
圖4. 由ssh連上pi在純終端機頁面下指令</div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
參考資料</div>
<div style="text-align: left;">
1. <a href="http://linux.die.net/man/1/zbarcam" target="_blank">zbarcam(1) - Linux man page</a></div>
Shiunhttp://www.blogger.com/profile/00430694685762085062noreply@blogger.com0tag:blogger.com,1999:blog-694306540104860106.post-39118926504682352952015-08-11T10:51:00.001+08:002015-08-11T10:52:18.318+08:00Linux Default route remove and add說明:<br />
由於筆者管理中的某台機器裝上雙網卡後怪怪的, 第一個網卡對外掛掉後不會切到第二張網卡, 因此筆者簡單筆記怎麼重設default route<br />
顯示系統的routing table<br />
<code><pro>shiun@test:~$ </pro>route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default 192.168.0.253 0.0.0.0 UG 0 0 0 eth0
192.168.0.0 * 255.255.255.0 U 0 0 0 eth0
192.168.1.0 * 255.255.255.0 U 0 0 0 eth1</code><br />
<br />
將第二張網卡(eth1)改為default<br />
<code><com># 刪除對應到eth0的default</com>
<pro>shiun@test:~$ </pro>sudo route del default
<com># 將192.168.1.1(eth1)改為新的default</com>
<pro>shiun@test:~$ </pro>sudo add default gw 192.168.1.1
</code>
<br />
這樣就OK了Shiunhttp://www.blogger.com/profile/00430694685762085062noreply@blogger.com0tag:blogger.com,1999:blog-694306540104860106.post-16398082241071956772015-08-05T22:33:00.002+08:002015-08-06T00:09:10.927+08:00樹莓派 Raspberry Pi2 John the ripper (開膛手約翰XD?)應觀眾要求測試Pi2跑John的效能, 結果如下列資料所示<br />
<br />
<br />
<code><pro>pi@ShiunPi /etc/john $ </pro>john -te
Benchmarking: Traditional DES [32/32 BS]... DONE
Many salts: 82043 c/s real, 82171 c/s virtual
Only one salt: 78717 c/s real, 78835 c/s virtual
Benchmarking: BSDI DES (x725) [32/32 BS]... DONE
Many salts: 2575 c/s real, 2575 c/s virtual
Only one salt: 2551 c/s real, 2551 c/s virtual
Benchmarking: FreeBSD MD5 [32/32]... DONE
Raw: 1511 c/s real, 1511 c/s virtual
Benchmarking: OpenBSD Blowfish (x32) [32/32]... DONE
Raw: 80.5 c/s real, 80.5 c/s virtual
Benchmarking: Kerberos AFS DES [24/32 4K]... DONE
Short: 37305 c/s real, 37305 c/s virtual
Long: 100006 c/s real, 100006 c/s virtual
Benchmarking: LM DES [32/32 BS]... DONE
Raw: 1161K c/s real, 1161K c/s virtual
Benchmarking: dummy [N/A]... DONE
Raw: 11211K c/s real, 11211K c/s virtual </code><br />
<br />
<code></code>由於筆者對john沒有研究, 分析的工作只好無限延期XDDDDShiunhttp://www.blogger.com/profile/00430694685762085062noreply@blogger.com0tag:blogger.com,1999:blog-694306540104860106.post-61902525491867732602015-06-18T10:50:00.005+08:002015-06-18T10:50:57.542+08:00Android 內建字型<h4>
Android 3.0</h4>
<br />
<ul>
<li>字型: Droid fonts</li>
<li>wiki介紹: <a href="https://en.wikipedia.org/wiki/Droid_fonts">https://en.wikipedia.org/wiki/Droid_fonts</a></li>
</ul>
<br />
<br />
<h4>
Android 4.0新增</h4>
<br />
<ul>
<li>字型: Roboto</li>
<li>wiki介紹: <a href="https://zh.wikipedia.org/wiki/Roboto">https://zh.wikipedia.org/wiki/Roboto</a></li>
</ul>
Shiunhttp://www.blogger.com/profile/00430694685762085062noreply@blogger.com0tag:blogger.com,1999:blog-694306540104860106.post-87574892282139653022015-04-22T22:12:00.001+08:002015-04-23T14:31:10.070+08:00Windows禁用檔案名稱前幾個月前寫程式遇到Windows 7寫入檔案錯誤, 原因很單純的就是Windows 7禁止使用某些檔名,但是今天<b><span style="color: red;">某G開頭</span></b>的朋友(請看本網誌右下的咩田認證)也犯了一樣的錯, 還是筆記一下好了..........<br />
<br />
禁止使用的檔名如下<br />
<br />
1. CON<br />
2. PRN<br />
3. AUX<br />
4. NUL<br />
5. COM系列, 從COM1~COM9<br />
6. LPT系列, 從LPT1~LPT9<br />
<br />
在Windows 7上出現的畫面如下<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgGaplOT7yT94eYT1MeV1cKnzraINs5K3VmsfM5SEDG1jB6IovdWEsDTORGY5NMpbgruMv2UIUlBI5XUKJJwXW33SCMJE_7Vh1paFLo_ZMf4MaSu0GR7Mn56zh5D-LvPWoRNMMrZlnHDCpX/s1600/%E8%9E%A2%E5%B9%95%E5%BF%AB%E7%85%A7+2015-04-22+%E4%B8%8B%E5%8D%889.38.37.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgGaplOT7yT94eYT1MeV1cKnzraINs5K3VmsfM5SEDG1jB6IovdWEsDTORGY5NMpbgruMv2UIUlBI5XUKJJwXW33SCMJE_7Vh1paFLo_ZMf4MaSu0GR7Mn56zh5D-LvPWoRNMMrZlnHDCpX/s1600/%E8%9E%A2%E5%B9%95%E5%BF%AB%E7%85%A7+2015-04-22+%E4%B8%8B%E5%8D%889.38.37.png" height="180" width="640" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
將一個檔案改名為COM1(不管副檔名)</div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjCYOLw409VwCU6yIKenCV99YD-VghZZqNa4UMugerEWjrPaPIN3-1MK0U9x-wiunk59gmOk01cvR9hGddLCRm892rj2ZPaT7R5vKIbXYpOD5185ySCSHr9C0APjSjoTvTFXZjij0QcoToc/s1600/%E8%9E%A2%E5%B9%95%E5%BF%AB%E7%85%A7+2015-04-22+%E4%B8%8B%E5%8D%889.38.45.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjCYOLw409VwCU6yIKenCV99YD-VghZZqNa4UMugerEWjrPaPIN3-1MK0U9x-wiunk59gmOk01cvR9hGddLCRm892rj2ZPaT7R5vKIbXYpOD5185ySCSHr9C0APjSjoTvTFXZjij0QcoToc/s1600/%E8%9E%A2%E5%B9%95%E5%BF%AB%E7%85%A7+2015-04-22+%E4%B8%8B%E5%8D%889.38.45.png" height="376" width="640" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
改名後發生錯誤</div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<br />
詳細資料可參考<a href="https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx" target="_blank">Naming Files, Paths, and Namespaces</a>網站並以<b><span style="color: red;">reserved</span></b>為關鍵字進行搜尋, 可以找到一段話<br />
<code>
Do not use the following reserved names for the name of a file:
CON, PRN, AUX, NUL, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9, LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, and LPT9
</code>
<br />
<br />
參考文章<br />
1. <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx" target="_blank">Naming Files, Paths, and Namespaces</a>Shiunhttp://www.blogger.com/profile/00430694685762085062noreply@blogger.com0tag:blogger.com,1999:blog-694306540104860106.post-60190021745592943742015-04-14T01:00:00.001+08:002015-05-10T15:07:26.937+08:00樹莓派 Raspberry Pi 使用iPhone連線上網 & 橋接至IP分享器當作緊急備用WAN使用<div>
<span style="color: #444444; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"><span style="background-color: white; font-size: 13px; line-height: 18.2000007629395px;">先來說明為啥筆者會想把手機的4G網路橋接到AP上的WAN吧</span></span></div>
<div>
<span style="color: #444444; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"><span style="background-color: white; font-size: 13px; line-height: 18.2000007629395px;"><br /></span></span></div>
<div>
<h4>
<span style="font-size: large;">動機</span></h4>
</div>
<div>
<span style="color: #444444; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"><span style="background-color: white; font-size: 13px; line-height: 18.2000007629395px;"> 1. 筆者租屋處的網路gg.........QAQ</span></span></div>
<div>
<span style="color: #444444; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"><span style="background-color: white; font-size: 13px; line-height: 18.2000007629395px;"><br /></span></span></div>
<div>
<span style="color: #444444; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"><span style="background-color: white; font-size: 13px; line-height: 18.2000007629395px;"> 2. 筆者的網路設備(NAS Pi、AirPlay Pi、...之類的)沒辦法用無線網路去連手機的熱點.......(至於為啥設備名字後面都有個Pi呢.... 就是沒錢只好自己DIY..........)</span></span></div>
<div>
<span style="color: #444444; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"><span style="background-color: white; font-size: 13px; line-height: 18.2000007629395px;"><br /></span></span></div>
<div>
<span style="color: #444444; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"><span style="background-color: white; font-size: 13px; line-height: 18.2000007629395px;"> 3. 手機的熱點有連線數上限,印象中連5個裝置就極限了</span></span></div>
<div>
<span style="color: #444444; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"><span style="background-color: white; font-size: 13px; line-height: 18.2000007629395px;"><br /></span></span></div>
<div>
<span style="color: #444444; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"><span style="background-color: white; line-height: 18.2000007629395px;"><b>租屋處原始架構圖:</b></span></span></div>
<div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjhnwBK31EHDOZ5Eddg3qrfubFTYNdTu1aZ_g6dOoe13Zq1xfvbEC6a__oeAZuTlASQ0h3Cs83BesZdCLkA6Nse5WIS5zZBIo4izUy3TN7ASPmJQTQEsbmfeCP7d5kbLYgpLF87r6C6feCe/s1600/bridge01.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="233" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjhnwBK31EHDOZ5Eddg3qrfubFTYNdTu1aZ_g6dOoe13Zq1xfvbEC6a__oeAZuTlASQ0h3Cs83BesZdCLkA6Nse5WIS5zZBIo4izUy3TN7ASPmJQTQEsbmfeCP7d5kbLYgpLF87r6C6feCe/s1600/bridge01.png" width="400" /></a></div>
<div style="text-align: center;">
<span style="background-color: white; color: #444444; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13px; line-height: 18.2000007629395px;">所有網路設備透過AP連上modem</span></div>
<span style="color: #444444; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"><span style="background-color: white; font-size: 13px; line-height: 18.2000007629395px;"><br /></span></span></div>
<div>
<span style="color: #444444; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"><span style="background-color: white; line-height: 18.2000007629395px;"><b>目標架構圖:</b></span></span><br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgDV4rrbJPelQTU5oAOEDoL7HHLLh1U9yXE0YtuYkvUoNAWDug4VuhAoQm2NQSaabbqH4MpSyo2GrmxWlorepuru-FvQkRNoqaSViLrI64WEVjhEPqckxhJfCMAWuPdXkdp65hWLMOuJu0V/s1600/111.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="252" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgDV4rrbJPelQTU5oAOEDoL7HHLLh1U9yXE0YtuYkvUoNAWDug4VuhAoQm2NQSaabbqH4MpSyo2GrmxWlorepuru-FvQkRNoqaSViLrI64WEVjhEPqckxhJfCMAWuPdXkdp65hWLMOuJu0V/s1600/111.png" width="400" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
刪除右邊的modem</div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjJGMRSDZwv_dNMyTdHCQZ2jGI1lXJ9F8ot99guYYnNb40w8JnQiLCTNlSVJoJDH_3iVGKlgT-dy4KAMikkT8GesyjTfxkbkeiLYRR2okZCnayFYk3Mhh9fhs9dfwVcHEgKEYZXS427dyq5/s1600/444.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="281" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjJGMRSDZwv_dNMyTdHCQZ2jGI1lXJ9F8ot99guYYnNb40w8JnQiLCTNlSVJoJDH_3iVGKlgT-dy4KAMikkT8GesyjTfxkbkeiLYRR2okZCnayFYk3Mhh9fhs9dfwVcHEgKEYZXS427dyq5/s1600/444.png" width="400" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
改由Pi橋接AP和手機以3G/LTE連接上網</div>
<span style="color: #444444; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"><span style="background-color: white; font-size: 13px; line-height: 18.2000007629395px;"><br /></span></span>
<span style="color: #444444; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"><span style="background-color: white; font-size: 13px; line-height: 18.2000007629395px;"><br /></span></span></div>
<div>
<span style="color: #444444; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"><span style="background-color: white; font-size: 13px; line-height: 18.2000007629395px;"><br /></span></span></div>
<div>
<h4>
<span style="color: #444444; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"><span style="background-color: white; font-size: large; line-height: 18.2000007629395px;">關鍵字說明:</span></span></h4>
</div>
<div>
<span style="color: #444444; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"><span style="background-color: white; font-size: 13px; line-height: 18.2000007629395px;"> Tethering: 意指通過智慧型手機或平板電腦等行動裝置,將它的上網功能分享給其他裝置使用的功能。(節錄自wikipedia: Tethering),在iPhone統一使用個人熱點控制</span></span></div>
<div>
<span style="color: #444444; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"><span style="background-color: white; font-size: 13px; line-height: 18.2000007629395px;"><br /></span></span></div>
<div>
<span style="color: #444444; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"><span style="background-color: white; font-size: 13px; line-height: 18.2000007629395px;"> AP: Access Point 也就是一般家用的Wi-Fi無線基地台</span></span></div>
<div>
<span style="color: #444444; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"><span style="background-color: white; font-size: 13px; line-height: 18.2000007629395px;"><br /></span></span></div>
<div>
<h4>
<span style="color: #444444; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"><span style="background-color: white; font-size: large; line-height: 18.2000007629395px;">建立Pi與iPhone個人熱點連線:</span></span></h4>
</div>
<div>
<span style="color: #444444; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"><span style="background-color: white; line-height: 18.2000007629395px;"><b> 1. 安裝套件讓Pi可以使用USB連結iPhone的個人熱點</b></span></span></div>
<div>
<br /></div>
<code><com>#以下步驟全部要使用root權限, 直接切換權限為root</com>
<pro>pi@ShiunPi ~ $</pro> sudo su -
<com>#安裝以下的套件讓Pi可以使用iPhone的Tethering(網路共享功能)</com>
<pro>root@ShiunPi ~ #</pro> apt-get install ipheth-utils
<pro>root@ShiunPi ~ #</pro> apt-get install libimobiledevice-utils
<pro>root@ShiunPi ~ #</pro> aptitude install ifuse
<com>#ifuse是使用掛載的方式讓iPhone的Tethering能正常工作</com>
<com>#因此先建立等會兒要用來連線的掛載點</com>
<pro>root@ShiunPi ~ #</pro> mkdir /media/iphone
</code>
<br />
<div>
<span style="color: #444444; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"><span style="background-color: white; font-size: 13px; line-height: 18.2000007629395px;"><br /></span></span>
<span style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"><span style="color: #444444; font-size: 13px; line-height: 18.2000007629395px;"> </span><span style="line-height: 18.2000007629395px;"><b><span style="color: #444444;">2. 連接iPhone到Pi上並開啟iPhone的個人熱點確定Pi有偵測到iPhone,iPhone若顯示是否信任此裝置請點選</span><span style="color: red;">信任</span><span style="color: #444444;">(參考網站說不用點選信任但筆者這邊選不信任會一直出現錯誤)</span></b></span></span><br />
<code><com>#使用ifconfig -s檢查是否出現新的網路介面(通常是eth1)</com>
<pro>root@ShiunPi ~ #</pro> ifconfig -s</code><br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgcIzvbOJZftiwR8yDjbl_Genxe2liCmWAlt_avYimKR9NKxfpuImQ6C1iNKhElrhaa9ENdGzAYRRZhd5OSJUVHRhK4OnTCNjXRcTA7CX5V-nLjTBQCREcVG-WNt93X4DCAOdWWUgcKNCYf/s1600/1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="107" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgcIzvbOJZftiwR8yDjbl_Genxe2liCmWAlt_avYimKR9NKxfpuImQ6C1iNKhElrhaa9ENdGzAYRRZhd5OSJUVHRhK4OnTCNjXRcTA7CX5V-nLjTBQCREcVG-WNt93X4DCAOdWWUgcKNCYf/s1600/1.png" width="640" /></a></div>
<br />
<span style="color: #444444; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"><span style="line-height: 18.2000007629395px;"><b> 3. 編輯/etc/network/interface讓NetworkManager會自動由eth1取得IP</b></span></span><br />
<code><pro>root@ShiunPi ~ #</pro> vi /etc/network/interface
<com>#加入以下設定</com>
allow-hotplug eth1
iface eth1 inet dhcp </code><span style="color: #444444; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"><span style="font-size: 13px; line-height: 18.2000007629395px;"><br /></span></span><br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj-3OLSwegZVugtG-8SohABTGTt43jAhcYH5Vb4lNBSpve0mSIlf0R0GC6HoXp5pt5X3Ny9rdKCGEgqqK7Oyju8lx13nMRm0cGWxWa-W2peeDyGhuGhd_n54U9eErG87HiVw5miv0BlxwBu/s1600/2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="140" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj-3OLSwegZVugtG-8SohABTGTt43jAhcYH5Vb4lNBSpve0mSIlf0R0GC6HoXp5pt5X3Ny9rdKCGEgqqK7Oyju8lx13nMRm0cGWxWa-W2peeDyGhuGhd_n54U9eErG87HiVw5miv0BlxwBu/s1600/2.png" width="640" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<span style="color: #444444; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"><span style="line-height: 18.2000007629395px;"><b> 4. 重開機</b></span></span><br />
<span style="color: #444444; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"><span style="font-size: 13px; line-height: 18.2000007629395px;"><br /></span></span>
<span style="color: #444444; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"><span style="line-height: 18.2000007629395px;"><b> 5. 使用ifuse指令建立Pi與個人熱點的連線</b></span></span><br />
<code><com>#以下步驟全部要使用root權限, 直接切換權限為root</com>
<pro>pi@ShiunPi ~ $</pro> sudo su -
<com>#使用ifuse掛載/media/iPhone目錄</com>
<pro>root@ShiunPi ~ #</pro> ifuse /media/iPhone </code><br />
<span style="color: #444444; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"><span style="line-height: 18.2000007629395px;"><b> 6. 編輯自動建立連線的script iphoneconnect方便下次iPhone接上Pi後自動建立連線</b></span></span><br />
<code><pro>root@ShiunPi ~ #</pro> vi /lib/udev/iphoneconnect
<com>#!/bin/bash</com>
<com>#避免上一次ifuse掛載後沒有解除掛載,掛載錢先嘗試umount</com>
umount /media/iPhone
ifuse /media/iPhone
ipheth_pair
</code><br />
<code><com>#編輯結束後記得將script權限設定為755</com>
<pro>root@ShiunPi ~ #</pro> chmod 755 /lib/udev/iphoneconnect</code><br />
<span style="color: #444444; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"><span style="font-size: 13px; line-height: 18.2000007629395px;"><br /></span></span>
<code></code><span style="color: #444444; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"><span style="line-height: 18.2000007629395px;"><b> 7. 設定udev偵測到iPhone個人熱點插上Pi時自動執行上一步的script</b></span></span><br />
<code><pro>root@ShiunPi ~ #</pro> vi /lib/udev/rules.d/90-iphone-tether.rules
<com>#修改原本的第五行的 "RUN+=" 為以下內容</com>
ACTION=="add", SUBSYSTEM=="net", ENV{ID_USB_DRIVER}=="ipheth", SYMLINK+="iphone", RUN+="iphoneconnect"
</code><span style="color: #444444; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"><span style="font-size: 13px; line-height: 18.2000007629395px;"> </span></span><br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgxNQV-CUmdGm621UyhcDB68RoO2zdGypTPZG1Ms95Q3w3l5qNYqgjD7OwbInvyRSUSINimqT88U6xiZEVeG_o3h_mxHCceP1-UHH7Of1jEnz-LXoqIOZEE6hwOvCzpUU4jw2pNbAVlLtNH/s1600/3.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="172" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgxNQV-CUmdGm621UyhcDB68RoO2zdGypTPZG1Ms95Q3w3l5qNYqgjD7OwbInvyRSUSINimqT88U6xiZEVeG_o3h_mxHCceP1-UHH7Of1jEnz-LXoqIOZEE6hwOvCzpUU4jw2pNbAVlLtNH/s1600/3.png" width="640" /></a></div>
<span style="color: #444444; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"><span style="font-size: 13px; line-height: 18.2000007629395px;"><br /></span></span>
<span style="color: #444444; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"><span style="line-height: 18.2000007629395px;"><b> 8. 重開機驗證是否可正常執行,到這一步就已經完成iPhone與Pi的連線了</b></span></span><br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi_vArvlua7S_IB0oX8aRaUm6fFogEnlZpFgi6uFb28jWxiwqpGj0UodeMjtb7nj3qM44m4pT2zZFodJkcvtiDQ7EHzjZLRRq-Sq4EgcdsmeZSMMUKFkd8epWtGYrLi4ylFMeJ9-zohRQas/s1600/4.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="188" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi_vArvlua7S_IB0oX8aRaUm6fFogEnlZpFgi6uFb28jWxiwqpGj0UodeMjtb7nj3qM44m4pT2zZFodJkcvtiDQ7EHzjZLRRq-Sq4EgcdsmeZSMMUKFkd8epWtGYrLi4ylFMeJ9-zohRQas/s1600/4.png" width="640" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhQZdiN5PQDMrQSsh83i3yIMmN2PKP1Wt4B7Yn1IsGHsla_Cyik-_whzeMRxhW3wSl2g9m9VSOKBrDc1UTKPP5tJPZ85l-Uvwi9Bfla2pI_kJ0x7Xa19m-2Z4BpCR20TdefSwANQUCIsQwg/s1600/6.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="400" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhQZdiN5PQDMrQSsh83i3yIMmN2PKP1Wt4B7Yn1IsGHsla_Cyik-_whzeMRxhW3wSl2g9m9VSOKBrDc1UTKPP5tJPZ85l-Uvwi9Bfla2pI_kJ0x7Xa19m-2Z4BpCR20TdefSwANQUCIsQwg/s1600/6.JPG" width="300" /></a></div>
<span style="color: #444444; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"><span style="font-size: 13px; line-height: 18.2000007629395px;"><br /></span></span>
<br />
<h4>
<span style="color: #444444; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"><span style="font-size: large; line-height: 18.2000007629395px;"><b>橋接Pi的eth0和iPhone的eth1</b></span></span></h4>
<span style="color: #444444; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"><span style="line-height: 18.2000007629395px;"><b> 1. 安裝橋接用套件</b></span></span><br />
<code><com>#安裝bridge-utils即可</com>
<pro>root@ShiunPi ~ #</pro> apt-get install bridge-utils
</code>
<span style="color: #444444; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"><span style="font-size: 13px; line-height: 18.2000007629395px;"><br /></span></span>
<span style="color: #444444; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"><span style="line-height: 18.2000007629395px;"><b> 2. 設定eth0與eth1橋接在一起</b></span></span><br />
<code><com>#編輯/etc/network/interface檔案</com>
<pro>root@ShiunPi ~ #</pro> vi /etc/network/interface
<com>記得一定要註解掉這兩行,否則會發生同一個IP會出現在兩個interface的奇怪問題</com>
<com>#allow-hotplug eth1</com>
<com>#iface eth1 inet dhcp</com>
auto br0
iface br0 inet dhcp
bridge_ports eth0 eth1
</code>
<span style="color: #444444; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"><span style="font-size: 13px; line-height: 18.2000007629395px;"> </span></span><br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiN2ERALiCzwpVt3bqC20cSF8pyHkrgUcSKP_XpRohwOU9jiYnPeYgKH9GW6hh2EzguI-HhN5XESBTC3z4Rg7D8ByCkf8vS9wdzu8YgLVuIbbWvOH5xtyh22mDdcPaMzjqFq-bNZNECc-aE/s1600/5.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="198" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiN2ERALiCzwpVt3bqC20cSF8pyHkrgUcSKP_XpRohwOU9jiYnPeYgKH9GW6hh2EzguI-HhN5XESBTC3z4Rg7D8ByCkf8vS9wdzu8YgLVuIbbWvOH5xtyh22mDdcPaMzjqFq-bNZNECc-aE/s1600/5.png" width="640" /></a></div>
<span style="color: #444444; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"><span style="font-size: 13px; line-height: 18.2000007629395px;"><br /></span></span>
<span style="color: #444444; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"><span style="line-height: 18.2000007629395px;"><b> 3. 重開機並將筆電 or PC接到Pi的網路孔上(eth0)</b></span></span><br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEje500ETfwYRj2zXNnDgBtf8W7xIBkvyLKzV7K5AxG6oZTnWBUf-JELoVWDziL749ftOpN5dIkiEbEn-6uPymVl-tGICKO85ZXoXwzH22Yh5-k55v6DWfcJnOfE5SPaf70Ol2UldG0pYYjz/s1600/7.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEje500ETfwYRj2zXNnDgBtf8W7xIBkvyLKzV7K5AxG6oZTnWBUf-JELoVWDziL749ftOpN5dIkiEbEn-6uPymVl-tGICKO85ZXoXwzH22Yh5-k55v6DWfcJnOfE5SPaf70Ol2UldG0pYYjz/s1600/7.JPG" width="240" /></a></div>
<span style="color: #444444; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"><span style="font-size: 13px; line-height: 18.2000007629395px;"><br /></span></span>
<span style="color: #444444; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"><span style="font-size: 13px; line-height: 18.2000007629395px;"> 到此步驟後橋接就完成了,此時即可從筆電直接透過iPhone連上internet,接著就直接從筆電上ping外網看看吧</span></span><br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi7CIhBDeQHa8ZOsr2sItXRHS3FdmdTSZ0BSwufaKSrkSXeEftqFyQOy6CzFVy9cMOx9B7Z_tAMVGqDvjvGaw52e6cOltKdqf9Qg4L4MFNrqeR-z6b-VCprM2aSsym_6Utpz-u7wg1PEht0/s1600/8.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="188" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi7CIhBDeQHa8ZOsr2sItXRHS3FdmdTSZ0BSwufaKSrkSXeEftqFyQOy6CzFVy9cMOx9B7Z_tAMVGqDvjvGaw52e6cOltKdqf9Qg4L4MFNrqeR-z6b-VCprM2aSsym_6Utpz-u7wg1PEht0/s1600/8.png" width="640" /></a></div>
<span style="color: #444444; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"><span style="font-size: 13px; line-height: 18.2000007629395px;"><br /></span></span>
<br />
<h4>
<span style="color: #444444; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"><span style="font-size: large; line-height: 18.2000007629395px;"><b>橋接iPhone與AP</b></span></span></h4>
<br />
<span style="background-color: white; color: #444444; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13px; line-height: 18.2000007629395px;">接著直接將Pi的網路孔(eth0)接上AP,iPhone開啟個人熱點並接上Pi的USB孔即完成所有工作,現在就不用再害怕租屋處網路又gg了!!!</span><br />
<span style="background-color: white; color: #444444; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13px; line-height: 18.2000007629395px;"><br /></span>
<br />
<h4>
<span style="color: #444444; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"><span style="font-size: large; line-height: 18.2000007629395px;"><b>參考資料</b></span></span></h4>
<br />
<span style="color: #444444; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"><span style="background-color: white; font-size: 13px; line-height: 18.2000007629395px;">1. </span><span style="font-size: 13px; line-height: 18.2000007629395px;"><a href="http://www.vk3erw.com/index.php/16-software/23-raspberry-pi-tethered-to-iphone-5" target="_blank">Raspberry Pi How To: Tether to iPhone 5</a> (之前更新網誌的時候不小心弄掉參考資料惹QQ)</span></span></div>
Shiunhttp://www.blogger.com/profile/00430694685762085062noreply@blogger.com5tag:blogger.com,1999:blog-694306540104860106.post-79457955648894039692015-04-03T13:19:00.001+08:002015-04-03T13:31:22.388+08:00Java interface中定義變數是合法的?!最近被朋友聊天的時候聊到一個問題: "<b><span style="color: red;">Interface中定義變數(field)是合法的?!</span></b>"<br />
<h4>
描述</h4>
<div>
為啥要討論這問題呢? 請各位看官看看以下的描述 from Wikipedia: <a href="http://zh.wikipedia.org/wiki/%E6%8E%A5%E5%8F%A3_(Java)" target="_blank">Interface (Java)</a></div>
<div>
==================================================</div>
<div>
<span style="background-color: white; color: #252525; font-family: sans-serif; font-size: 14px; line-height: 22.3999996185303px;">介面通常以</span><b style="background-color: white; color: #252525; font-family: sans-serif; font-size: 14px; line-height: 22.3999996185303px;"><code style="background-color: #f9f9f9; border-radius: 2px; border: 1px solid rgb(221, 221, 221); color: black; font-family: monospace, Courier; padding: 1px 4px;">interface</code></b><span style="background-color: white; color: #252525; font-family: sans-serif; font-size: 14px; line-height: 22.3999996185303px;">來宣告,它僅能包含</span><span class="ilh-all" style="background-color: white; color: #252525; font-family: sans-serif; font-size: 14px; line-height: 22.3999996185303px;"><span class="ilh-page"><a class="new" href="http://zh.wikipedia.org/w/index.php?title=%E6%96%B9%E6%B3%95%E7%B0%BD%E5%90%8D&action=edit&redlink=1" original-title="方法簽名 (頁面不存在)" style="background: none; color: #009999; text-decoration: none;">方法簽名</a></span></span><span style="background-color: white; color: #252525; font-family: sans-serif; font-size: 14px; line-height: 22.3999996185303px;">(Method Signature)以及</span><a class="mw-redirect" href="http://zh.wikipedia.org/wiki/%E5%B8%B8%E6%95%B8" style="background: none rgb(255, 255, 255); color: #0b0080; font-family: sans-serif; font-size: 14px; line-height: 22.3999996185303px; text-decoration: none;" title="常數">常數</a><span style="background-color: white; color: #252525; font-family: sans-serif; font-size: 14px; line-height: 22.3999996185303px;">宣告(變數宣告包含了 </span><code style="background-color: #f9f9f9; border-radius: 2px; border: 1px solid rgb(221, 221, 221); font-family: monospace, Courier; font-size: 14px; line-height: 22.3999996185303px; padding: 1px 4px;"><a class="new" href="http://zh.wikipedia.org/w/index.php?title=Static_variable&action=edit&redlink=1" style="background: none; color: #a55858; text-decoration: none;" title="Static variable (頁面不存在)">static</a></code><span style="background-color: white; color: #252525; font-family: sans-serif; font-size: 14px; line-height: 22.3999996185303px;"> 及 </span><code style="background-color: #f9f9f9; border-radius: 2px; border: 1px solid rgb(221, 221, 221); font-family: monospace, Courier; font-size: 14px; line-height: 22.3999996185303px; padding: 1px 4px;"><a class="new" href="http://zh.wikipedia.org/w/index.php?title=Final_(Java)&action=edit&redlink=1" style="background: none; color: #a55858; text-decoration: none;" title="Final (Java) (頁面不存在)">final</a></code><span style="background-color: white; color: #252525; font-family: sans-serif; font-size: 14px; line-height: 22.3999996185303px;">),一個介面不會包含</span><a class="mw-disambig" href="http://zh.wikipedia.org/wiki/%E6%96%B9%E6%B3%95" style="background: none rgb(255, 255, 255); color: #0b0080; font-family: sans-serif; font-size: 14px; line-height: 22.3999996185303px; text-decoration: none;" title="方法">方法</a><span style="background-color: white; color: #252525; font-family: sans-serif; font-size: 14px; line-height: 22.3999996185303px;">的實作(僅有定義)。</span></div>
<div>
==================================================</div>
<div>
<br /></div>
接著來看個範例吧<br />
<pre class="prettyprint">interface ControllStatus {
String QAQ = "qaq";
}
public class Test {
public static void main (String[] args){
System.out.println(ControllStatus.QAQ);
}
}
</pre>
<br />
執行結果如下:<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgLLfqeZHBmQGnthWP48DquroilMFQDvjrejqAhodUwVNBkwRICTkJdsdFJYWC-ASzyg3Gey_FsjyS6DA4H39Vb1F3NamUV2wn7K-UO4mm59HJ84YxOO4Xy7qZmLHX4zh94MMC0PMUElPVI/s1600/%E8%9E%A2%E5%B9%95%E5%BF%AB%E7%85%A7+2015-04-03+%E4%B8%8B%E5%8D%8812.37.04.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgLLfqeZHBmQGnthWP48DquroilMFQDvjrejqAhodUwVNBkwRICTkJdsdFJYWC-ASzyg3Gey_FsjyS6DA4H39Vb1F3NamUV2wn7K-UO4mm59HJ84YxOO4Xy7qZmLHX4zh94MMC0PMUElPVI/s1600/%E8%9E%A2%E5%B9%95%E5%BF%AB%E7%85%A7+2015-04-03+%E4%B8%8B%E5%8D%8812.37.04.png" height="80" width="320" /></a></div>
<br />
<br />
<h4>
討論</h4>
在上述的範例中interface定義了QAQ字串竟然能正常執行!!!<br />
但是Wikipedia的描述中卻說介面<b><span style="color: red;">只宣告不包含定義,除非用static及final來宣告</span></b><br />
<b><span style="color: red;"><br /></span></b>
根據其他OO的語言,直覺上Java應該是將上述的程式碼偷偷加入<b><span style="color: red;">public static final</span></b>的修飾<br />
1. public: 公開<br />
2. static: 程式一開始時就被配置記憶體且擁有"唯一"的特性<br />
3. final: 不可修改<br />
<br />
由此可以猜測Java是不是偷偷地把interface中定義的變數弄成public, static and final,接著來看以下兩個Stack Overflow的討論吧<br />
<a href="http://stackoverflow.com/questions/2430756/why-are-interface-variables-static-and-final-by-default" target="_blank">Why are interface variables static and final by default?</a><br />
<a href="http://stackoverflow.com/questions/17592872/is-public-static-final-redundant-for-a-constant-in-a-java-interface" target="_blank">Is “public static final” redundant for a constant in a Java interface?</a><br />
<br />
這兩個討論都提到Java隱含式的轉換變數為public, static and final<br />
但是Stack Overflow終究是個討論區,接下來我們直接看Java的技術文件吧:<br />
<a href="http://docs.oracle.com/javase/specs/jls/se7/html/jls-9.html" target="_blank">Chapter 9. Interfaces</a><br />
<a href="http://docs.oracle.com/javase/specs/jls/se7/html/jls-9.html#jls-9.3" target="_blank"> -- 9.3. Field (Constant) Declarations</a><br />
<span style="background-color: white; font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: 16.7999992370605px;"><span style="color: red;">Every field declaration in the body of an interface is implicitly public, static, and final</span><span style="background-color: white; font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: 16.7999992370605px;"><span style="color: red;">.</span> It is permitted to redundantly specify any or all of these modifiers for such fields.</span><br />
<br />
顯然的Java真的做了隱含式轉換,所有interface內的filed會被轉換成public static final,因此上述的程式碼不僅正確,該filed還是唯一且不可變的物件!<br />
</span><br />
<h4>
<span style="background-color: white; font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: 16.7999992370605px;">
參考資料</span></h4>
<span style="background-color: white; font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: 16.7999992370605px;">
1. <a href="http://zh.wikipedia.org/wiki/%E6%8E%A5%E5%8F%A3_(Java)" target="_blank">Wikipedia: 介面 (Java)</a><br />
2. <a href="http://stackoverflow.com/questions/2430756/why-are-interface-variables-static-and-final-by-default" target="_blank">Stack Overflow: Why are interface variables static and final by default?</a><br />
3. <a href="http://stackoverflow.com/questions/17592872/is-public-static-final-redundant-for-a-constant-in-a-java-interface" target="_blank">Stack Overflow: Is “public static final” redundant for a constant in a Java interface?</a><br />
4. <a href="http://docs.oracle.com/javase/specs/jls/se7/html/jls-9.html#jls-9.3" target="_blank">Oracle Java Language Specification Chapter 9. Interfaces: 9.3. Field (Constant) Declarations</a><br />
<br />
</span>Shiunhttp://www.blogger.com/profile/00430694685762085062noreply@blogger.com0tag:blogger.com,1999:blog-694306540104860106.post-46948510498180092442015-04-03T12:02:00.002+08:002015-04-03T13:40:14.438+08:00樹莓派Raspberry Pi B+ and Pi2 USB供電不足問題處理<h4>
狀況</h4>
<div>
<div>
前一段時間測試在Pi B+和Pi2上接以下兩種裝置</div>
<div>
4G USB dongle</div>
<div>
USB外接硬碟 </div>
<div>
結果發現Pi B+和Pi2 USB供電不足推不動USB dongle和外接硬碟........<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg6CbHVc8ptCWgvkeY6e26zeV3LqQo04W7uLN7Dm_ig0kMlAGjIMaTnOZWWtpGa-_uwO4RjTsFFuEXYiQFv6uHq6Yt7ULIj3VQidDf9v9GdzvEd4JW5lgY_UbJWqz5vkhp2cIf8d1WG9heS/s1600/IMG_2479.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg6CbHVc8ptCWgvkeY6e26zeV3LqQo04W7uLN7Dm_ig0kMlAGjIMaTnOZWWtpGa-_uwO4RjTsFFuEXYiQFv6uHq6Yt7ULIj3VQidDf9v9GdzvEd4JW5lgY_UbJWqz5vkhp2cIf8d1WG9heS/s1600/IMG_2479.JPG" height="319" width="320" /></a></div>
<br /></div>
</div>
<div>
<div style="text-align: center;">
推不動時上圖的紅燈會熄滅或不規則閃爍</div>
</div>
<h4>
解決方案</h4>
<div>
<div>
經過一番Google後發現有兩個問題要解決<br />
1. adapter的供電要到2A<br />
2. 取消Pi B+和Pi2預設的供電限制,只要在config.txt(註)中加入以下的參數即可解除USB供電限制</div>
<div style="padding-left: 10px;">
<code>max_usb_current=1</code></div>
<div>
註: 將micro SD card插入讀卡機並接上電腦後即可在根目錄下看到config.txt</div>
</div>
<div>
<br /></div>
<div>
<h4>
其他問題</h4>
</div>
<div>
雖然加入以上參數可以解除USB供電限制,但還是可能不夠電去推USB外接硬碟,建議讀者還是買個有供電的USB hub會比較有效</div>
<div>
<br /></div>
<h4>
參考資料</h4>
<div>
<div>
1. http://dumbpcs.blogspot.tw/2014/07/getting-external-hdd-to-work-with.html</div>
<div>
2. https://projects.drogon.net/testing-setting-the-usb-current-limiter-on-the-raspberry-pi-b/</div>
</div>
Shiunhttp://www.blogger.com/profile/00430694685762085062noreply@blogger.com2tag:blogger.com,1999:blog-694306540104860106.post-65544943020458853552015-02-25T23:16:00.004+08:002015-03-03T10:24:47.135+08:00Raspberry Pi 各版本耗電量測試目前Raspberry Pi在市面上的版本就有Model A, A+, B+, Generation 2 Model B(Pi2),筆者根據手上有的以及能借到的測試一下各版本的耗電狀況<br />
<br />
筆者使用下圖的電流電壓測試器,由於此電流電壓測試器的準確度有限,就要各位看官不要太計較了XD<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjUuBTsTQa6Yv6KeM2TcFnzxFHHzDSd6zZm5ytMLwjMIx7E5lAJwQOsHRt5cKKVnesEmyKs0aqEtRQzM0Y-_3tKh8FIHWRhuTIqksfvTZU06Ne9RJceDUPQ2HW6KPAjMosgzbTIdDjyRSQe/s1600/IMG_2410.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjUuBTsTQa6Yv6KeM2TcFnzxFHHzDSd6zZm5ytMLwjMIx7E5lAJwQOsHRt5cKKVnesEmyKs0aqEtRQzM0Y-_3tKh8FIHWRhuTIqksfvTZU06Ne9RJceDUPQ2HW6KPAjMosgzbTIdDjyRSQe/s1600/IMG_2410.jpg" height="240" width="320" /></a></div>
<br />
<br />
檢測結果如下<br />
<div align="center">
<table bgcolor="#000000" border="1" cellpadding="0" cellspacing="1" style="text-align: center;">
<tbody>
<tr bgcolor="#ffffff" height="40"> <!-- 固定欄高、背景顏色-->
<td><div align="center">
Model</div>
</td>
<td><div align="center">
Busy</div>
</td>
<td><div align="center">
Idle</div>
</td>
<td><div align="center">
Power Off</div>
</td>
</tr>
<tr bgcolor="#ffffff" height="40">
<td><div align="center">
A+</div>
</td>
<td><div align="center">
0.11</div>
</td>
<td><div align="center">
0.08</div>
</td>
<td><div align="center">
0.02</div>
</td>
</tr>
<tr bgcolor="#ffffff" height="40">
<td><div align="center">
B</div>
</td>
<td><div align="center">
0.44</div>
</td>
<td><div align="center">
0.39</div>
</td>
<td><div align="center">
0.1</div>
</td>
</tr>
<tr bgcolor="#ffffff" height="40">
<td width="100px"><div align="center">
B+</div>
</td>
<td width="100px"><div align="center">
0.25</div>
</td>
<td width="100px"><div align="center">
0.21</div>
</td>
<td width="100px"><div align="center">
0.04</div>
</td>
</tr>
<tr bgcolor="#ffffff" height="40">
<td width="100px"><div align="center">
<span style="text-align: start;">Pi2</span></div>
</td>
<td width="100px"><div align="center">
0.31</div>
</td>
<td width="100px"><div align="center">
0.24</div>
</td>
<td width="100px"><div align="center">
0.06</div>
</td></tr>
</tbody></table>
</div>
<div style="text-align: center;">
表上的Busy, Idle及Power Off的單位為安培(ampere)</div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
Busy: 故意跑無窮迴圈程式讓CPU維持在100%左右的使用率,由於Pi2有4顆CPU所以故意跑多個無窮迴圈程式使其總CPU loading同樣維持在100%左右</div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
Idel: 開機後不額外執行任何指令或程式只維持開機後靜止不動的狀態</div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
Power Off: 下sudo poweroff並等到所有燈號熄滅只剩電源指示燈有亮的狀況下測量</div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
結論</div>
<div style="text-align: left;">
由上表的結果未來筆者若要再購買會以Pi2為主(畢竟4顆心臟太強大了),且耗電只有0.31A拿來掛網、FTP server、HTTP server、DLNA server非常方便<br />
<br />
而A+在特殊需求下,例如DLNA server配合一般NAS使用也是非常適合<br />
<br />
B板的話則是會慢慢的退至第二線或是送人吧<br />
<br />
Update 2015/3/3<br />
國外另有大大專業的量測,有興趣的人可以閱讀此篇文章http://raspi.tv/2014/how-much-less-power-does-the-raspberry-pi-b-use-than-the-old-model-b</div>
Shiunhttp://www.blogger.com/profile/00430694685762085062noreply@blogger.com0tag:blogger.com,1999:blog-694306540104860106.post-23250247291836967932015-02-25T22:46:00.001+08:002015-02-25T22:46:32.117+08:00Raspberry Pi 2 入手過年前訂的Raspberry Pi 2(簡稱Pi2)終於到貨了~<br />
<br />
安裝前請各位看官重新燒最新的Raspbian到另一張micro SD card,目前的Raspbian是無法在B+和Pi2上替換著用的<br />
<br />
以下為Pi2的簡單測試<br />
<br />
1. 外觀: 除了畫面正上方的小字能看出這是Pi2外,其他長的跟B+幾乎一模一樣 不過各pin腳的對應還需要再確認是不是和B+一模一樣<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjFsnVkJChn3SqPMMbozsvrGL04au87bAkJdWH_S8K9g5gYUX2R3ekymbB1deVDx6JTEE1cwmzBqgsYqUVhKMDFVzlWJJIw7U5_Q0zKY-ho9c9yVfE4SkS1Cd0BL4VwpgyKPRsB9UwQVXnl/s1600/IMG_2439_3.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjFsnVkJChn3SqPMMbozsvrGL04au87bAkJdWH_S8K9g5gYUX2R3ekymbB1deVDx6JTEE1cwmzBqgsYqUVhKMDFVzlWJJIw7U5_Q0zKY-ho9c9yVfE4SkS1Cd0BL4VwpgyKPRsB9UwQVXnl/s1600/IMG_2439_3.jpg" height="240" width="320" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
2. 效能: 從下圖左上方可以看到Pi2從<span style="color: red;">一顆樹梅</span>成長為<span style="color: red;">四顆樹梅</span>當然變強的不像話(被毆</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhxLasClqUdcQWUmMRV8OCmDlRQoNOxxHgxHreB71e31RERCpV1N8qRkVd10uIo-AhdxlKdjZKNcq2FiE4E7mF1-zlM7l3qjaapHcnDFOKLayS7UM8ji2P3Grwrq4Jm2MyXFYQfarq1Vd4x/s1600/AVerMedia_Capture_snapshot-2015-02-24-14-27-0.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhxLasClqUdcQWUmMRV8OCmDlRQoNOxxHgxHreB71e31RERCpV1N8qRkVd10uIo-AhdxlKdjZKNcq2FiE4E7mF1-zlM7l3qjaapHcnDFOKLayS7UM8ji2P3Grwrq4Jm2MyXFYQfarq1Vd4x/s1600/AVerMedia_Capture_snapshot-2015-02-24-14-27-0.png" height="180" width="320" /></a></div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
簡單測試以下項目</div>
<div class="separator" style="clear: both; text-align: left;">
i. 安裝iceweasel瀏覽網頁: Google, Yahoo等網頁跟普通桌機感覺差不多(感動到痛哭流涕!比起過去的Pi光開瀏覽器就lag到昏倒好太多了!!!</div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
ii. 播放YouTube影片: 開啟YouTube網頁速度悲劇,播放時也有lag的狀況大概是繪圖和解碼能力仍舊不足</div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
iii. 開啟vim編輯不會lag!</div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
iv. ssh連線穩定且不會lag!</div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
v. JDownloader安裝速度快許多,安裝完畢後開啟JDownloader幾乎不用等待</div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
vi. 耗電量: 經由測試發現把B+多0.06A</div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
剩下的等有空再測試吧~Shiunhttp://www.blogger.com/profile/00430694685762085062noreply@blogger.com0tag:blogger.com,1999:blog-694306540104860106.post-21182925655003514522015-02-25T22:11:00.002+08:002015-02-25T22:11:21.914+08:00[機械式鍵盤] Cherry 無刻鍵盤清潔與鍵帽清洗最近由於手滑把nano SIM卡掉在鍵盤中,拔起鍵帽後發現卡超多灰塵,連鍵盤外部也髒掉不少.............來花點時間來清潔吧<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgj2XPZEwJFZ0azy5YATtHv4uhf1li7myndCNaUt6W5lTUWlOh42_YtGYLX-v5OILc064sp6AN4DxyuVWTZbylEkv0bY96O0dmCHgdUS4J4kW7Z4b9wqQr_argtVaE3CHp0HSRfJ0-it4Kc/s1600/QAQQAQ.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgj2XPZEwJFZ0azy5YATtHv4uhf1li7myndCNaUt6W5lTUWlOh42_YtGYLX-v5OILc064sp6AN4DxyuVWTZbylEkv0bY96O0dmCHgdUS4J4kW7Z4b9wqQr_argtVaE3CHp0HSRfJ0-it4Kc/s1600/QAQQAQ.jpg" height="131" width="320" /></a></div>
<br />
<br />
1. 拆掉全部鍵帽,由於筆者的鍵盤是Cherry的<span style="color: red;"><b>無刻</b></span>鍵盤,所以在拆之前就要有心理準備裝回去有機會裝錯位置XD<br />
拆完以後如下圖所示<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhAChdwfI1s75IJ7RDogo2O2UY7OYOXSESuWwZJep2vLNt-15AZ7BU2JB8C8Jl3L72VSHTwcS-eLL9G-r3cyGatYUcLAtZ_xM7JWzyxmUv3emUzoVNB-O2WMYTvN8M0e9Pb_DFBacuGjnDE/s1600/k2.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhAChdwfI1s75IJ7RDogo2O2UY7OYOXSESuWwZJep2vLNt-15AZ7BU2JB8C8Jl3L72VSHTwcS-eLL9G-r3cyGatYUcLAtZ_xM7JWzyxmUv3emUzoVNB-O2WMYTvN8M0e9Pb_DFBacuGjnDE/s1600/k2.JPG" height="131" width="320" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgGx-UhSEzCSglF1Ps4WFNkaGcr7F_IyXym2LZ_0smyXfUD2hDvS8YVkB6mHm_PlNPMeQ8vI-hp7wkbPjcTVgZQXKIu_oIkTs1_8g1f8pXSObWsUCN-J4Mu7TWLbP6Vsrw8WZ1l7FA5hzhB/s1600/IMG_2168.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgGx-UhSEzCSglF1Ps4WFNkaGcr7F_IyXym2LZ_0smyXfUD2hDvS8YVkB6mHm_PlNPMeQ8vI-hp7wkbPjcTVgZQXKIu_oIkTs1_8g1f8pXSObWsUCN-J4Mu7TWLbP6Vsrw8WZ1l7FA5hzhB/s1600/IMG_2168.JPG" height="240" width="320" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
2. 接著拿吹球將卡在縫隙裡的灰塵和髒東西清掉,如果能借到輪胎打氣的機器也是個方法,不然像筆者用吹球就吹到手痠個半死</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjyLnJTpInKluuhRPSAJPLXwxl8UtMic_MTdNlWIlK9iDZjWEj5sjV3EjORVi7ThSZIeHI_AYd-h08ZYde6v05hw3U_fUA9Jd7kphec0kUT-3ZEZ-XYJpTIpEqIhc3JKZ2coko-M139LJ_3/s1600/IMG_2192.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjyLnJTpInKluuhRPSAJPLXwxl8UtMic_MTdNlWIlK9iDZjWEj5sjV3EjORVi7ThSZIeHI_AYd-h08ZYde6v05hw3U_fUA9Jd7kphec0kUT-3ZEZ-XYJpTIpEqIhc3JKZ2coko-M139LJ_3/s1600/IMG_2192.JPG" height="240" width="320" /></a></div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
3. 清洗鍵帽,傳說中鍵帽洗衣袋 + 洗衣機就能把鍵帽洗得亮晶晶,但為了環保省水筆者還是決定乖乖用手一個一個拿起來搓洗然後再曬乾</div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
4. 接著就是重點<b><span style="color: red;">放回鍵帽</span></b>,把鍵帽反過來看可以發現鍵帽後面會有I, II, III, IV等字樣,從上到下就是I, II, III, IV,至於長得比較奇怪的鍵帽當然就不用特別去記了~</div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
不過根據筆者的經驗,拆鍵帽的時候還是要順手記一下鍵帽和他所在的排列的號碼對不對,不然像筆者裝到一半就沒把握會很糗XD....</div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjpkXrZMfTUsOl6zp-rI1atJtCSnQrjkY6gD16_NLLX-ps97G-K_mxqSM1vHrRX86r_TJvSlLoH-Jr02_lP_ypxuwDLc22nA0-EnmU8kpoYX3XFJOq8BxoUelXkV3SSImUiBC4pY3Hb7pP9/s1600/IMG_2181.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjpkXrZMfTUsOl6zp-rI1atJtCSnQrjkY6gD16_NLLX-ps97G-K_mxqSM1vHrRX86r_TJvSlLoH-Jr02_lP_ypxuwDLc22nA0-EnmU8kpoYX3XFJOq8BxoUelXkV3SSImUiBC4pY3Hb7pP9/s1600/IMG_2181.jpg" height="320" width="240" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
從上到下為I, II, III和IV</div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiHH3uDcPbosJomH330sovtCcpmovL3A9oUb9YT9E8RYzfKT6j4TmKmEvobW7SkxRpmDzVdd2_pZnvPFSx0ZDy5TbkaJN4SSE9EMgnUFKcHhhXCZi5DCfgnYEhkX7aakbCmWqDSOCf31Z7b/s1600/IMG_2183_3.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiHH3uDcPbosJomH330sovtCcpmovL3A9oUb9YT9E8RYzfKT6j4TmKmEvobW7SkxRpmDzVdd2_pZnvPFSx0ZDy5TbkaJN4SSE9EMgnUFKcHhhXCZi5DCfgnYEhkX7aakbCmWqDSOCf31Z7b/s1600/IMG_2183_3.jpg" height="240" width="320" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
對應到上圖的從左到右的I, II, III, IV</div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
Shiunhttp://www.blogger.com/profile/00430694685762085062noreply@blogger.com0tag:blogger.com,1999:blog-694306540104860106.post-5240120395481978542014-11-13T00:08:00.001+08:002014-11-13T10:11:21.222+08:00Mac OSX 10.10 Yosemite Eclipse 上機測試無法選擇device問題最近跟瘋升級到<span style="background-color: white; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: 14px; line-height: 17.8048000335693px;">Yosemite</span><br />
<br />
<span style="background-color: white; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: 14px; line-height: 17.8048000335693px;">但是今天要測試Android app上機跑的時候卻遇到抓不到裝置的問題(10.9的時候不會這樣啊QAQ)</span><br />
<span style="background-color: white; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: 14px; line-height: 17.8048000335693px;"><br /></span>
<br />
<h2>
<span style="background-color: white; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: 14px; line-height: 17.8048000335693px;">詳細症狀如下</span></h2>
<span style="background-color: white; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: 14px; line-height: 17.8048000335693px;"> 1. 接上Android裝置後按下Run as Android Application卻找不到裝置</span><br />
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhCQ5M_dpV5XE5LcXwzDmCeEsTX8hWlw1tOy4Wco-fDJgugXNSF4xp73creBAotsRi0defg3LvnieNJvMvkWWXzVu7YnKroZbB49BewAz4C89jlNaYUCHg07kgIq5CKERqqlNzLtJp_N-il/s1600/1.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="243" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhCQ5M_dpV5XE5LcXwzDmCeEsTX8hWlw1tOy4Wco-fDJgugXNSF4xp73creBAotsRi0defg3LvnieNJvMvkWWXzVu7YnKroZbB49BewAz4C89jlNaYUCHg07kgIq5CKERqqlNzLtJp_N-il/s320/1.jpg" width="320" /></a></div>
<span style="background-color: white; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: 14px; line-height: 17.8048000335693px;"><br /></span>
<span style="background-color: white; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: 14px; line-height: 17.8048000335693px;"> 2. 在上圖的畫面顯示的情況下拔掉Android裝置再插上去就會出現</span><br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi5ADGRuPoA109Ulf7RVHzJHgdV8fQeDVxj9n48e2jOv2XbY1vz7IPiFlIGKINcnzd_fMNs2wN8W5UHLAs4t9Ko-_KH429Z66i_zLGc36MIhrAIao3brdc_-Ud9zla0yHo90qBjRCmdbiXp/s1600/%E8%9E%A2%E5%B9%95%E5%BF%AB%E7%85%A7+2014-11-12+%E4%B8%8B%E5%8D%8811.54.54.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="245" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi5ADGRuPoA109Ulf7RVHzJHgdV8fQeDVxj9n48e2jOv2XbY1vz7IPiFlIGKINcnzd_fMNs2wN8W5UHLAs4t9Ko-_KH429Z66i_zLGc36MIhrAIao3brdc_-Ud9zla0yHo90qBjRCmdbiXp/s320/%E8%9E%A2%E5%B9%95%E5%BF%AB%E7%85%A7+2014-11-12+%E4%B8%8B%E5%8D%8811.54.54.jpg" width="320" /></a></div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
3. 選擇device後按下OK可以正常上機執行</div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
4. 稍微修改code後再按一下Run as Android Application會發現裝置又消失啦 => 回到第一步loop</div>
<div class="separator" style="clear: both; text-align: left;">
頓時腦中響起「我跳出來啦 我又回去啦 打我啊笨蛋」...... (°Д°#)</div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<h2 style="clear: both; text-align: left;">
解決方式(from Stack Overflow)</h2>
<span style="background-color: white; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: 14px; line-height: 17.8048000335693px;"> 1. 使用adb檢查Android裝置是不是真的有接上Mac</span><br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiesFALMh23pmuMeeBdNf4UlVGsJAirxZixkuqv0Xc7mjX1Mrqg254Yc64o56MTHwBCkZPM8MSVXSOuMFUy96R-p3zSADiPPJhAJF3EhG2Ezl84so9CVD0IutztIa1Ok4l6byCZcQAsJZZs/s1600/%E8%9E%A2%E5%B9%95%E5%BF%AB%E7%85%A7+2014-11-12+%E4%B8%8B%E5%8D%8811.05.08.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="52" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiesFALMh23pmuMeeBdNf4UlVGsJAirxZixkuqv0Xc7mjX1Mrqg254Yc64o56MTHwBCkZPM8MSVXSOuMFUy96R-p3zSADiPPJhAJF3EhG2Ezl84so9CVD0IutztIa1Ok4l6byCZcQAsJZZs/s400/%E8%9E%A2%E5%B9%95%E5%BF%AB%E7%85%A7+2014-11-12+%E4%B8%8B%E5%8D%8811.05.08.png" width="400" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
2. 接著再跑一次Run as Android Application,但是這時候要拖移一下欄位的寬度一切就正常了!!!</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhMmf5X4-fP2yghltQcz8MnQRfuHER3Kraw8aMAO6fgzUoEKZ61yG4FOGDoiDlNxmgrORkOfYFnkQX5HlAtFiZIXI2Sc9h-kc26nkgz_WhTtSSFXRUQ3Db7UToWt3f3ioFutJJoag7PBeI0/s1600/QQ2+2.gif" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="246" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhMmf5X4-fP2yghltQcz8MnQRfuHER3Kraw8aMAO6fgzUoEKZ61yG4FOGDoiDlNxmgrORkOfYFnkQX5HlAtFiZIXI2Sc9h-kc26nkgz_WhTtSSFXRUQ3Db7UToWt3f3ioFutJJoag7PBeI0/s320/QQ2+2.gif" width="320" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
3. 收工(╯°Д°#)╯︵ ┻━┻ </div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
雖然不知道是Yosemite, Android Plugin還是Eclipse luna的layout問題造成問題,但這問題太扯了!!!</div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
參考資料</div>
<div class="separator" style="clear: both; text-align: left;">
1. <a href="http://stackoverflow.com/questions/26416532/just-upgraded-my-os-x-to-yosemite-android-device-chooser-not-showing-device-w" target="_blank">Just upgraded my OS X to Yosemite, Android Device Chooser not showing device - what to do?</a></div>
Shiunhttp://www.blogger.com/profile/00430694685762085062noreply@blogger.com1tag:blogger.com,1999:blog-694306540104860106.post-19839244419045456712014-10-01T23:14:00.002+08:002014-10-02T16:04:27.489+08:00Raspberry Pi 樹莓派 無線AP to 有線網路橋接筆者最近在研究Wi-Fi時發現Pi也可以模擬成AP,因此筆者用閒暇時間整理出如何將Pi變成一台無線基地台<br />
<br />
<h4>
簡介</h4>
首先先說明一下筆者所處的網路架構(圖1),圖片從右到左的介紹如下<br />
i. 首先是modem: 俗稱小烏龜的玩意兒,常見的就是ISP(中華電信, 寬頻業者)給的機器<br />
ii. 無線基地台(AP): 手機或筆記型電腦能透過Wi-Fi連接上AP後就能上網,很重要的一點是<b><span style="color: red;">預設要開啟DHCP功能</span></b><br />
iii. 平板、手機、筆電: 透過Wi-Fi上網的機器<br />
iv. PC: 透過有線網路上網的機器<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi6ir8zXTy9ytiqDpvQaY0sSSDBTXWqQBAnkgkVUVY2ajeNRSXbTwrIn3qpXWhDRgitgW9gUYBBIYGxhIYrsJwHSb30QNMFu48aJ9MS28lPoNEe7-VhjOYH4kzdN_0I4FUVGvw32ByXzr3Q/s1600/bridge01.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi6ir8zXTy9ytiqDpvQaY0sSSDBTXWqQBAnkgkVUVY2ajeNRSXbTwrIn3qpXWhDRgitgW9gUYBBIYGxhIYrsJwHSb30QNMFu48aJ9MS28lPoNEe7-VhjOYH4kzdN_0I4FUVGvw32ByXzr3Q/s1600/bridge01.png" height="187" width="320" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
圖1. 典型網路架構</div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
圖2為筆者目標的網路架構,說明如下</div>
<div class="separator" style="clear: both; text-align: left;">
目標: </div>
<div class="separator" style="clear: both; text-align: left;">
i. 將有線網路延伸出去,像是AP架在客廳可是房間訊號很差,但卻有有線網路能接的情況</div>
<div class="separator" style="clear: both; text-align: left;">
ii. 另一種玩法就是做封包分析<strike>和網路攻擊</strike>的時候中間多個橋接器方便很多的</div>
<div class="separator" style="clear: both; text-align: left;">
iii. 由於是橋接的方式,在Pi無線網域下的所有裝置與有線網域下的所有裝置都還是在同一個網段,簡單的說就是網芳還是通的~</div>
<div class="separator" style="clear: both; text-align: left;">
架構:</div>
<div class="separator" style="clear: both; text-align: left;">
將有線網路接到Pi上,再由Pi發出Wi-Fi的訊號給手機、平板等裝置連線</div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjMyaHq4MY_S4vu8YVTFCmDy5A2Kk-f3oE8n37SahZZDM82edHbZYtvHRSorDxh78Ja40gZWd9GjKTokN85yh2vyA1WrJFdBj1mdOJE1oRsD-3feEdwT77nnS7Pnb9_KeMhxO908EmQm28U/s1600/bridge_2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjMyaHq4MY_S4vu8YVTFCmDy5A2Kk-f3oE8n37SahZZDM82edHbZYtvHRSorDxh78Ja40gZWd9GjKTokN85yh2vyA1WrJFdBj1mdOJE1oRsD-3feEdwT77nnS7Pnb9_KeMhxO908EmQm28U/s1600/bridge_2.png" height="205" width="320" /></a></div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<h4 style="clear: both; text-align: left;">
網卡晶片</h4>
<div class="separator" style="clear: both; text-align: left;">
筆者使用RTL8188CUS配合Realtek的軟體後可正常使用,但RTL8188SU和RTL8192SU試了好幾次都跑不起來.......</div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<h4 style="clear: both; text-align: left;">
架設前注意事項</h4>
<div class="separator" style="clear: both; text-align: left;">
以下的操作<span style="color: red;"><b>建議在Pi上接著螢幕和鍵盤執行</b></span>,橋接完後AP分給Pi的IP可能會改變,此外有線網卡(eth0)基本上不會再取得IP,即便取得也不穩定,因此完成所有步驟前請直接在Pi上操作</div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<h4 style="clear: both; text-align: left;">
架設步驟</h4>
<h3 style="clear: both; text-align: left;">
1. 安裝bridge工具</h3>
<div class="separator" style="clear: both; text-align: left;">
<code><pro>pi@ ~ $ </pro>sudo apt-get install bridge-utils</code></div>
<div class="separator" style="clear: both;">
<br /></div>
<h3 style="clear: both;">
2. 檢查網卡晶片組</h3>
<div style="clear: both;">
若跟筆者一樣是<b><span style="color: red;">Realtek網卡晶片組</span></b>請先跳到下方的"<b><span style="color: red;">修正Realtek網卡在Pi上無法使用hostapd問題</span></b>"章節安裝Realtek的hostapd,完成後再回到第4步</div>
<code><pro><span style="font-family: Heiti TC;"></span>pi@ ~ $ </pro>sudo lsusb</code><br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhznxp-J5NNQQMwBMG0L-TG4DXRe4X11600Eyo6B10Q27b7lLoMeiapvudwooASwGOYCntbHgdiO2ujhA3hUwwyErLhS98vY-dvQL_z5cIVgXP2h7Pg53PY8XFy81lvzwmCpmYECWFwUKSS/s1600/0+%E6%8B%B7%E8%B2%9D.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhznxp-J5NNQQMwBMG0L-TG4DXRe4X11600Eyo6B10Q27b7lLoMeiapvudwooASwGOYCntbHgdiO2ujhA3hUwwyErLhS98vY-dvQL_z5cIVgXP2h7Pg53PY8XFy81lvzwmCpmYECWFwUKSS/s1600/0+%E6%8B%B7%E8%B2%9D.png" height="82" width="320" /></a></div>
<div>
<h3>
3. 安裝官方的hostapd</h3>
(Realtek網卡晶片的使用者請勿使用Pi官方版本的hostapd)<br />
<code><pro>pi@ ~ $ </pro>sudo apt-get install hostapd</code><br />
<br /></div>
<div>
<h3>
4. 檢查網卡代號</h3>
此處筆者的有線網卡為eth0,無線網卡為wlan0<br />
<code><pro>pi@ ~ $ </pro>ifconfig</code><br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjKj9dcLTeierDsZytLAnHppZgXYN9GGo8UD5wWX-OosjVnZ-EcczXqVlAwowVTi9tCIBbHpC5FWk-nZcJv2wMvKS5lneVmL2WdfmGepQ6rwNGEUwuaz1HyOIJzKG0ivow044GVHJN00eGB/s1600/1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjKj9dcLTeierDsZytLAnHppZgXYN9GGo8UD5wWX-OosjVnZ-EcczXqVlAwowVTi9tCIBbHpC5FWk-nZcJv2wMvKS5lneVmL2WdfmGepQ6rwNGEUwuaz1HyOIJzKG0ivow044GVHJN00eGB/s1600/1.png" height="211" width="320" /></a></div>
</div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<h3 style="clear: both;">
5. 修改無線網卡設定使其eth0和wlan0橋接起來</h3>
<code><pro>pi@ ~ $ </pro>sudo vi /etc/network/interfaces</code>
<br />
<pre class="prettyprint">auto lo
iface lo inet loopback
#iface eth0 inet dhcp
#allow-hotplug wlan0
#iface wlan0 inet manual
#wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
#iface default inet dhcp
auto br0
iface br0 inet dhcp
bridge_ports eth0 wlan0
</pre>
<br />
<h3 style="clear: both; text-align: left;">
6. 建立hostapd的設定檔(預設可能不會有此設定檔)</h3>
<code><pro>pi@ ~ $ </pro>sudo vi /etc/hostapd/hostapd.conf</code>
<br />
<pre class="prettyprint">interface=wlan0
driver=rtl871xdrv
bridge=br0
ssid=RPI
channel=6
hw_mode=g
ieee80211n=1
wmm_enabled=1
wpa=2
wpa_passphrase=Raspberry
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP
auth_algs=1
macaddr_acl=0
</pre>
<h3 style="clear: both; text-align: left;">
7. 重開機</h3>
<code><pro>pi@ ~ $ </pro>sudo reboot</code><br />
<h3 style="clear: both; text-align: left;">
8. 測試hostapd是否能正常執行</h3>
<code><pro>pi@ ~ $ </pro>sudo hostapd -dd /etc/hostapd/hostapd.conf</code>
<br />
<h3 style="clear: both; text-align: left;">
9. 編輯hostapd的設定檔</h3>
<div class="separator" style="clear: both; text-align: left;">
使其在啟動daemon時載入/etc/hostapd/hostapd.conf的設定</div>
<code><pro>pi@ ~ $ </pro>sudo vi /etc/default/hostapd</code>
<br />
<pre class="prettyprint">DAEMON_CONF="/etc/hostapd/hostapd.conf"
</pre>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<h3 style="clear: both; text-align: left;">
10. 啟動hostapd,接著就能RPi當做AP來使用了~</h3>
<code><pro>pi@ ~ $ </pro>sudo service hostapd restart</code>
<br />
<h4 style="clear: both; text-align: left;">
</h4>
<h4 style="clear: both; text-align: left;">
</h4>
<h4 style="clear: both; text-align: left;">
修正Realtek網卡在Pi上無法使用hostapd問題</h4>
<div class="separator" style="clear: both; text-align: left;">
1. 安裝Pi官方提供的hostapd,目標只是取得hostapd的daemon而已XD</div>
<code><pro>pi@ ~ $ </pro>sudo apt-get install hostapd</code>
<br />
<div class="separator" style="clear: both; text-align: left;">
2. 刪除hostapd(不要懷疑,上一步就只是為了daemon檔案而已)</div>
<code><pro>pi@ ~ $ </pro>sudo apt-get remove hostapd</code>
<br />
<div class="separator" style="clear: both; text-align: left;">
3. 到Realtek的下載頁面下載RTL8188CUS的工具包(在下載頁面搜尋RTL8188CUS即可找到)</div>
<div class="separator" style="clear: both; text-align: left;">
連結: <a href="http://www.realtek.com/downloads/">http://www.realtek.com/downloads/</a></div>
PS: 由於Realtek的下載頁是用js觸發下載事件,為節省麻煩建議直接用其他電腦的瀏覽器下載再使用scp指令(Windows請用WinSCP or FileZilla的sftp)複製到pi<br />
<br />
4. 將RTL8188CUS的工具包從Mac, Linux or Windows傳送到pi的家目錄中(Windows請使用WinSCP或FileZilla)<br />
<code><pro>Shiun@ShiundeMacBook-Pro ~ </pro>scp RTL8188C_8192C_USB_linux_v4.0.2_9000.20130911.zip pi@192.168.0.148:/home/pi</code>
<br />
5. 將RTL8188工具包解壓縮<br />
<code><pro>pi@ ~ $ </pro>unzip RTL8188C_8192C_USB_linux_v4.0.2_9000.20130911.zip</code>
<br />
6. 移動到hostapd的目錄<br />
<code><pro>pi@ ~ $ </pro>cd RTL8188C_8192C_USB_linux_v4.0.2_9000.20130911/wpa_supplicant_hostapd/</code>
<br />
7. 解壓縮wpa_supplicant_hostapd-0.8_rtw_r7475.20130812.tar.gz<br />
<code><pro>pi@ ~/RTL8188C_8192C_USB_linux_v4.0.2_9000.20130911/wpa_supplicant_hostapd $ </pro>tar -zxvf wpa_supplicant_hostapd-0.8_rtw_r7475.20130812.tar.gz</code>
<br />
8. 進入hostpad的目錄<br />
<code><pro>pi@ ~/RTL8188C_8192C_USB_linux_v4.0.2_9000.20130911/wpa_supplicant_hostapd $ </pro>cd wpa_supplicant_hostapd-0.8_rtw_r7475.20130812/hostapd/</code>
<br />
9. 開始建置hostapd<br />
<code><pro>pi@ ~/RTL8188C_8192C_USB_linux_v4.0.2_9000.20130911/wpa_supplicant_hostapd/wpa_supplicant_hostapd-0.8_rtw_r7475.20130812/hostapd $ </pro>make clean; make</code>
<br />
10. 安裝hostapd到正確的位置<br />
<code><pro>pi@ ~/RTL8188C_8192C_USB_linux_v4.0.2_9000.20130911/wpa_supplicant_hostapd/wpa_supplicant_hostapd-0.8_rtw_r7475.20130812/hostapd $ </pro>sudo make install;</code>
<br />
11. 再複製hostapd的執行檔以方便daemon使用,for Realtek的hostapd到此安裝完成<br />
<code><pro>pi@ ~/RTL8188C_8192C_USB_linux_v4.0.2_9000.20130911/wpa_supplicant_hostapd/wpa_supplicant_hostapd-0.8_rtw_r7475.20130812/hostapd $ </pro>sudo cp /usr/local/bin/hostapd* /usr/sbin</code>
<br />
<br />
<br />
這篇寫的好累Orz.......,讀者若有發現任何錯誤請盡量向筆者糾正,謝謝^^<br />
參考資料<br />
1. <a href="http://blog.itist.tw/2014/05/hotspotd.html" target="_blank">Raspberry Pi 的應用 - Wifi 無線基地台</a>Shiunhttp://www.blogger.com/profile/00430694685762085062noreply@blogger.com0tag:blogger.com,1999:blog-694306540104860106.post-15238598068126995852014-08-17T19:10:00.000+08:002014-08-18T10:57:55.200+08:00使用Eclipse Luna + Tomcat 7 + Jersey 2架設RESTful service<h4>
前言</h4>
此篇文章介紹如何使用Jersey架設RESTful web service,使用到的工具如下<br />
1. Eclipse Luna<br />
2. Tomcat 7<br />
3. Jersey 2.11<br />
<br />
為避免對Maven或Gradle不熟悉的人感到疑惑,因此此篇文章只針對如何使用Jersey 2架設RESTful service,不使用Maven或Gradle進行管理<br />
<br />
<h4>
架設步驟</h4>
1. 開啟Eclipse<br />
<br />
2. 建立新的專案File -> New -> Dynamic Web Project<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgk3p8s1ysEBKlUS8nGwinCXG66E6GCib_RIxi5pHSTEKwX_ttK01i1v5dEn8pbTwthZ38rjK21cUIXr3Plk36riRLAdzIcSRpH7FaK0Eb4d7LNR62F7NQ6ydO3E6FJdibk8xX6v70_Yo8A/s1600/1.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgk3p8s1ysEBKlUS8nGwinCXG66E6GCib_RIxi5pHSTEKwX_ttK01i1v5dEn8pbTwthZ38rjK21cUIXr3Plk36riRLAdzIcSRpH7FaK0Eb4d7LNR62F7NQ6ydO3E6FJdibk8xX6v70_Yo8A/s1600/1.jpg" height="231" width="400" /></a></div>
<br />
<br />
3. 輸入專案名稱JerseySample並選擇Apache Tomcat v7.0作為Target runtime,記得要把Generate web.xml deployment descriptor打勾<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgV1eyfx2vGL6qlVxA75qHg9T2OMyiX1S9KttTA7ZDWVaO3nJOGILdxZHXpJvXMBWOs4xCrjq68hQNb8orHvBYO8fHN1qF2aed6LYWvyJwBSgyPM2ecbs7HKUdb6YAuHkvN_5ZgN1kp46Lx/s1600/2.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgV1eyfx2vGL6qlVxA75qHg9T2OMyiX1S9KttTA7ZDWVaO3nJOGILdxZHXpJvXMBWOs4xCrjq68hQNb8orHvBYO8fHN1qF2aed6LYWvyJwBSgyPM2ecbs7HKUdb6YAuHkvN_5ZgN1kp46Lx/s1600/2.jpg" height="400" width="337" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjklSE_MxulgQMELFnThcrgoW-rS7Iihvb2mRYTCCXoVj6f870m4ShJJ1ZNeMcOAlQ4eFv1usqxetJTGiEdWN-SM80u41gxkGFWxSDz9G40b-766i9xJd7y_FVfEPnRO3QgEn3WaPgzLh-x/s1600/3.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjklSE_MxulgQMELFnThcrgoW-rS7Iihvb2mRYTCCXoVj6f870m4ShJJ1ZNeMcOAlQ4eFv1usqxetJTGiEdWN-SM80u41gxkGFWxSDz9G40b-766i9xJd7y_FVfEPnRO3QgEn3WaPgzLh-x/s1600/3.jpg" height="400" width="313" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiqVS2wEdjjI43Cf0qSuPorj-_Lt3GOcSozNkZd7ltOnnuc69ofX8SEvFOPj4va_YiQLEnuczpEsanp2k0ge8sNbd6RyCXOo8D-u-l9yIfkxI2yOeUubfpk8c9996yRlBQJ3cRsH8NdkoHj/s1600/4.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiqVS2wEdjjI43Cf0qSuPorj-_Lt3GOcSozNkZd7ltOnnuc69ofX8SEvFOPj4va_YiQLEnuczpEsanp2k0ge8sNbd6RyCXOo8D-u-l9yIfkxI2yOeUubfpk8c9996yRlBQJ3cRsH8NdkoHj/s1600/4.jpg" height="400" width="315" /></a></div>
<br />
<br />
4. 將jersey官網下載的jaxrs-ri-2.11.zip解壓縮,接著把所有的jar檔複製到jerseySample/WebContent/WEB-INF/lib下<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgzHt2o6UJ_Uz5WnEbexnesPuU-LciVpUFFXL_pJDHdPvXl8Fe-Pr6eJKDsKcTw7Q2GaZVPI7BF1U3K7o9oMXaiCW31JKDn8UtmnlAtBIsekLYDhF0WNK-3JsWGhOX4q6YrN0Qogce85EQ1/s1600/5.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgzHt2o6UJ_Uz5WnEbexnesPuU-LciVpUFFXL_pJDHdPvXl8Fe-Pr6eJKDsKcTw7Q2GaZVPI7BF1U3K7o9oMXaiCW31JKDn8UtmnlAtBIsekLYDhF0WNK-3JsWGhOX4q6YrN0Qogce85EQ1/s1600/5.jpg" height="161" width="400" /></a></div>
<br />
<br />
5. 重新整理JerseySample專案確保lib都有正常載入<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjQ5nv2g6VOBD5BZfCKYXRMteHsnztCUPsK-rT-XL8MRc8aDLSo-NB1dGUQJOcp15SJumFgSF5rUzFFtjF2qiiluhwxTd1eQFRTfRiuHF7SFxT5j4l_AWY-_dUKCA8jCQ9ZD9rQnaLtPxPh/s1600/6.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjQ5nv2g6VOBD5BZfCKYXRMteHsnztCUPsK-rT-XL8MRc8aDLSo-NB1dGUQJOcp15SJumFgSF5rUzFFtjF2qiiluhwxTd1eQFRTfRiuHF7SFxT5j4l_AWY-_dUKCA8jCQ9ZD9rQnaLtPxPh/s1600/6.jpg" height="400" width="265" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj3HM6Woixxb43Imh1fL6jSsz7SxaH2Z_gVCjSkNxPtCBMu85yodPnggCgFqHHcvbbbu7l-6piCC2iiB4uGV5DBrdPmfnmdi33-RRKVYO7JJmP-bZYi5lSWsvMPRnQD9LusvzFqnOFJ1ysn/s1600/7.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj3HM6Woixxb43Imh1fL6jSsz7SxaH2Z_gVCjSkNxPtCBMu85yodPnggCgFqHHcvbbbu7l-6piCC2iiB4uGV5DBrdPmfnmdi33-RRKVYO7JJmP-bZYi5lSWsvMPRnQD9LusvzFqnOFJ1ysn/s1600/7.jpg" /></a></div>
<br />
<br />
6. 修改web.xml讓Tomcat可以正確載入Jersey相關資源<br />
<pre class="prettyprint"><?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>JerseySample</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>org.sample.test</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
</web-app>
</pre>
<br />
參數說明<br />
i. servlet-class<br />
Jersey 1的servlet-class為<br />
com.sun.jersey.spi.container.servlet.ServletContainer<br />
<br />
而Jersey 2的servlet-class必須改成(畢竟Sun已經R.I.P.了QAQ........)<br />
org.glassfish.jersey.servlet.ServletContainer<br />
<br />
ii. jersey.config.server.provider.packages<br />
Jersey掃描的packet路徑,resource的java檔要放在此package下<br />
<br />
iii. url-pattern<br />
Resource對應的URL位置,client端必須要照此參數設定才能呼叫到對應的resource<br />
<br />
7. 建立package: org.sample.test<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj6nHGIzjCG5NzB2SD7qPouD0yFWNxde0dQGbxqcSnZ4zpoKvLv0C9dSCFL5ZMUXqJWloL8U3NeDQHWX16mwuGSegX2bDD5S4hJw6Lr9plmo8WGjULEAdTnqrJyXgjkF6ix0DJX4Aw1O0NY/s1600/8.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj6nHGIzjCG5NzB2SD7qPouD0yFWNxde0dQGbxqcSnZ4zpoKvLv0C9dSCFL5ZMUXqJWloL8U3NeDQHWX16mwuGSegX2bDD5S4hJw6Lr9plmo8WGjULEAdTnqrJyXgjkF6ix0DJX4Aw1O0NY/s1600/8.jpg" height="343" width="400" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhoyppBWS84kaJFdtBuy8WN6QBmt0pF3n39AL8GSv1Vj6Ece_GdCgosjO9lKY_4-OTbfztdlein75lZIM1FWF8YKn77VbQoaWSZzhLkq_eNA7PxyxhO3qwR17hNbkwDysS8ZWDjK54Of6ag/s1600/9.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhoyppBWS84kaJFdtBuy8WN6QBmt0pF3n39AL8GSv1Vj6Ece_GdCgosjO9lKY_4-OTbfztdlein75lZIM1FWF8YKn77VbQoaWSZzhLkq_eNA7PxyxhO3qwR17hNbkwDysS8ZWDjK54Of6ag/s1600/9.jpg" height="317" width="400" /></a></div>
<br />
<br />
8. 建立resource的java檔案: Hello.java<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEigkVvM1CbJGGfbzF0Y4KrtH0HtXUgiM4Ry82wLTM7eEl2JcyXxhz8ZcBb4KRbn7ltyCVL03z8lkgYjMmlt2G_-ZOPsrYZjDodNb6czUE6jkk415SCjtOH2jqIgkXy95-z3G3g-HhzR0iaK/s1600/10.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEigkVvM1CbJGGfbzF0Y4KrtH0HtXUgiM4Ry82wLTM7eEl2JcyXxhz8ZcBb4KRbn7ltyCVL03z8lkgYjMmlt2G_-ZOPsrYZjDodNb6czUE6jkk415SCjtOH2jqIgkXy95-z3G3g-HhzR0iaK/s1600/10.jpg" height="325" width="400" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgM1ecRweq1O81YijJJlJuc98Q5hU8wmfwCF_9L8Qf0jfamXV1vhi6mgpknUTDIpPF89bEHvIGK_FiU4YcMZ2e5Jlqr3npfCthKTqx_ySMHfASJHrmStOr9Zt7XUlL4YKbFVQ-zoUsPwckH/s1600/11.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgM1ecRweq1O81YijJJlJuc98Q5hU8wmfwCF_9L8Qf0jfamXV1vhi6mgpknUTDIpPF89bEHvIGK_FiU4YcMZ2e5Jlqr3npfCthKTqx_ySMHfASJHrmStOr9Zt7XUlL4YKbFVQ-zoUsPwckH/s1600/11.jpg" height="400" width="333" /></a></div>
<br />
<br />
9. 加入以下的程式碼至Hello.java<br />
<pre class="prettyprint">package test;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/hello")
public class Hello {
// This method is called if TEXT_PLAIN is request
@GET
@Produces(MediaType.TEXT_PLAIN)
public String sayPlainTextHello() {
return "Hello Jersey";
}
// This method is called if XML is request
@GET
@Produces(MediaType.TEXT_XML)
public String sayXMLHello() {
return "" + "<hello> Hello Jersey" + "</hello>";
}
// This method is called if HTML is request
@GET
@Produces(MediaType.TEXT_HTML)
public String sayHtmlHello() {
return "<html> " + "<title>" + "Hello Jersey" + "</title>"
+ "<body><h1>"
+ "Hello Jersey"
+ "</h1></body></html></pre>"
+ " ";
}
}
</pre>
<br />
10. 將專案在Tomcat上跑起來,並使用瀏覽器檢查http://localhost:8080/JerseySample/api/hello是否正確<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEir2lXep6AkCJijJ6oAuzg5AQdZ8Z8kKLN5rOjH4iSvCZHSy92WrH-mxNmfYyh85vdHQsJUIJ-E1x8vhsC1AevbvTuL39iwLJ0Aqd4OXM5HSAAdjsq-XztUjb9OxXJR6wDNfeJS5iP01v_5/s1600/12.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEir2lXep6AkCJijJ6oAuzg5AQdZ8Z8kKLN5rOjH4iSvCZHSy92WrH-mxNmfYyh85vdHQsJUIJ-E1x8vhsC1AevbvTuL39iwLJ0Aqd4OXM5HSAAdjsq-XztUjb9OxXJR6wDNfeJS5iP01v_5/s1600/12.jpg" height="365" width="400" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEggvQYIXI9DjlwvGmqV0Ly7gijROY-OhR01eDQuJ9MP3gu38saKMUEGaELbHfhgOia8Pb4i-CUDFJSyrW85XUmB_jW8pG0sLrVWcnSe-EaGUHcBLvcxr5Lg-Cb9IE5-VaMlfeX8060HMsjZ/s1600/13.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEggvQYIXI9DjlwvGmqV0Ly7gijROY-OhR01eDQuJ9MP3gu38saKMUEGaELbHfhgOia8Pb4i-CUDFJSyrW85XUmB_jW8pG0sLrVWcnSe-EaGUHcBLvcxr5Lg-Cb9IE5-VaMlfeX8060HMsjZ/s1600/13.jpg" height="400" width="320" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjMKbv2bsWHlOdN3cqlVSYj_syx0y-SmVVbZ2IRQ_TTO6tw6MxiTp70u5aKbcLu7xz_Pn8x722CsDA-PYePPiliAuoQin0DNkAYVsp9Jzc7i0OazKJThlwHw1m1JjlR4wGARfrgI230Q88N/s1600/14.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjMKbv2bsWHlOdN3cqlVSYj_syx0y-SmVVbZ2IRQ_TTO6tw6MxiTp70u5aKbcLu7xz_Pn8x722CsDA-PYePPiliAuoQin0DNkAYVsp9Jzc7i0OazKJThlwHw1m1JjlR4wGARfrgI230Q88N/s1600/14.jpg" height="174" width="320" /></a></div>
<br />
<br />
11. 使用RESTful client工具檢查xml的request是否能正常使用。只要送出的request包含Accept: text/xml檔頭,Jersey就會自動切換到回傳sayXMLHello函式結果<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi8qdVDiZpqh4WYTDLGUpt05N2XhD2qeSoOZGdCRmHrZgYWPCQtqkx0BJgzTt4UzyY0qUegtwdhdkw1eZ1u_pfa4lXtP7LhGyDpk1vnEvIN11UwA_y4yq8kbLeQUY4AMryeUHY_ZxH7suSa/s1600/15.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi8qdVDiZpqh4WYTDLGUpt05N2XhD2qeSoOZGdCRmHrZgYWPCQtqkx0BJgzTt4UzyY0qUegtwdhdkw1eZ1u_pfa4lXtP7LhGyDpk1vnEvIN11UwA_y4yq8kbLeQUY4AMryeUHY_ZxH7suSa/s1600/15.jpg" height="287" width="400" /></a></div>
<br />
<br />
參考資料<br />
1. <a href="http://www.vogella.com/tutorials/REST/article.html" target="_blank">REST with Java (JAX-RS) using Jersey - Tutorial</a> (適用Jersey 1)<br />
2. <a href="http://poor-developer.blogspot.tw/2014/02/jersey-25-maven-easy-rest-web-services.html" target="_blank">Jersey 2.5 + Maven = easy REST web services</a>Shiunhttp://www.blogger.com/profile/00430694685762085062noreply@blogger.com1tag:blogger.com,1999:blog-694306540104860106.post-33683227816706747932014-08-17T16:16:00.000+08:002014-08-17T16:16:23.598+08:00[手工藝玩具] 1入2出 音源切換器完成以下兩篇的玩具後,筆者的音樂播放器就從PC換成Pi<br />
<a href="http://alenshiun.blogspot.com/2014/05/raspberry-pi-i2s-dac-or-usb-dab.html" target="_blank">Raspberry Pi 樹莓派 啟動I2S DAC or USB DAB 音效卡</a><br />
<a href="http://alenshiun.blogspot.com/2014/05/raspberry-pi-shairportairplay.html" target="_blank">Raspberry Pi 樹莓派 架設ShairPort使用AirPlay播放音樂</a><br />
<br />
但由於最近有不少狀況需要開PC,又PC要輸出聲音的話需要把喇叭從Pi換到PC的耳機座上,<br />
根據以前悲劇的經驗,這樣不是喇叭的耳機頭先壞掉就是PC的耳機座先被插壞.........<br />
因此筆者決定弄一個音源切換器,參考<a href="http://blog.xuite.net/olympus.chen/ruminate/63837914" target="_blank">DIY音源切換器</a>和<a href="http://www.mobile01.com/topicdetail.php?f=116&t=1919467" target="_blank">[DIY]兩種音源切換器(二入一出)</a>後筆者自製了一個音源切換器<br />
<br />
原料如下<br />
1. 6pin 2段式開關x1<br />
2. 3.5mm 耳機座x3 (二入一出)<br />
3. 塑膠盒子,大小為60x40x30mm<br />
4. 比6pin 2段式開關的pin粗一點點就熱縮套<br />
<br />
以上原料大概只有80元可以搞定,接著就是開始對把線焊上2段式開關 -> 塑膠盒鑽4個孔 -> 鎖上耳機座元件 -> 鎖上2段式開關 -> 將耳機座和2段式開關焊上,接著就完成了(詳細內容請直接參考最底下的參考資料)<br />
<br />
最後成品如下圖~<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgIQWC6b4t5B8thvTI9DiJI9kuvL36Ze1JQ227tktmmm6J_00BP5IWdn9-CGcZ0KYb88QYUxbOlIZCP0OaJLhoGlhBUjSvkbMHFpv91OClxEqIhs8AGDAQ6wtRofdi_7orJdbaSEdwOyDah/s1600/2.jpg" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgIQWC6b4t5B8thvTI9DiJI9kuvL36Ze1JQ227tktmmm6J_00BP5IWdn9-CGcZ0KYb88QYUxbOlIZCP0OaJLhoGlhBUjSvkbMHFpv91OClxEqIhs8AGDAQ6wtRofdi_7orJdbaSEdwOyDah/s320/2.jpg" /></a><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEibvSFszL9muVp5UvJ0HwBocSNJwDDpmrYh3BWqzQACna8UXo_343MCM_i0mxoCIjTigSEwEL7l0AIe4-iGM6cfHjEym-MYuIbeQhfmGkRQyEYDb923LURgrK47nKPwO810LG9m0Ym54M6L/s1600/3.jpg" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEibvSFszL9muVp5UvJ0HwBocSNJwDDpmrYh3BWqzQACna8UXo_343MCM_i0mxoCIjTigSEwEL7l0AIe4-iGM6cfHjEym-MYuIbeQhfmGkRQyEYDb923LURgrK47nKPwO810LG9m0Ym54M6L/s320/3.jpg" /></a><br />
<br />
補上小小心得<br />
整天過著寫程式的生活難免會有枯燥乏味的時候,偶而來DIY些小東西來玩也挺有趣的,尤其是對筆者這種拿焊槍次數10根手指頭就能算完的新手,以玩家的角度來做這東西其實挺有趣的<br />
<br />
在焊接的部份以往都有強者學長支援,但這次在沒人支援的情況下在60x40x30mm的盒子裡焊接東西讓筆者苦戰整個下午才搞定,做完其實挺有成就感的<br />
<br />
而實際上這東西在網路上買的到,價格大概3xx到4xx(2014年8月份),不過根據筆者DIY整個東西的時間,其實3xx元已經非常划算了,如果不是以玩家的心情做的話建議還是花錢直接買現成的吧<br />
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<br />
參考資料<br />
1. <a href="http://blog.xuite.net/olympus.chen/ruminate/63837914" target="_blank">DIY音源切換器</a><br />
2. <a href="http://www.mobile01.com/topicdetail.php?f=116&t=1919467" target="_blank">[DIY]兩種音源切換器(二入一出)</a>Shiunhttp://www.blogger.com/profile/00430694685762085062noreply@blogger.com0