arduino yun 物聯網 lesson 3

62
Arduino Yun 物物物物物 Lesson 3 RestfulAPI 物物物物物 Web 物物 物物 Facebook 物物

Upload: cavedu-education

Post on 21-Apr-2017

5.230 views

Category:

Devices & Hardware


0 download

TRANSCRIPT

Page 1: Arduino Yun 物聯網 Lesson 3

Arduino Yun 物聯網應用Lesson 3

RestfulAPI 控制繼電器Web 介面發布 Facebook 動態

Page 2: Arduino Yun 物聯網 Lesson 3

本日進度• Arduino Yún 接上繼電器,並透過 Yún REST

API 來使用網路瀏覽器遙控此繼電器。• 使用類比電流感測器來測接接在繼電器的設備電力消耗量,並計算電力即時消耗量。• 將此資料傳送至 Google Docs 試算表以方便從任何瀏覽器或是手機應用程式來遠端存取。• 製作簡易網路介面以方便使用電腦、智慧型手機或平板來遙控檯燈。

Page 3: Arduino Yun 物聯網 Lesson 3

範例列表• EX_1 : sensor_test

– 透過網路瀏覽器下指令來控制繼電器開關。– 顯示感測器值於 Serial Monitor

• EX_2 : energy_log– 等待網路指令來開關繼電器。– 將資料定期發送到 Google Docs 試算表。

• EX_3 : webinterface– 使用網路介面來遙控 ( 電腦或手機皆可 )

Page 4: Arduino Yun 物聯網 Lesson 3

專題 1 [ 遠距耗能監控裝置 ]

Page 5: Arduino Yun 物聯網 Lesson 3

所需硬體• Arduino Yun• 類比電流感測器 Arduino A0• 繼電器模組 Arduino D8• 公 / 母電線接頭

Page 6: Arduino Yun 物聯網 Lesson 3

繼電器• 繼電器是一個電磁開關,當我們需要用小電壓( Arduino 開發板的

5V )的指令訊號來開關相當大的電壓( 110V 或 230V )時便會派上用場。• 訊號 Arduino

D8 , GNDArduino GND

Page 7: Arduino Yun 物聯網 Lesson 3

類比電流感測器• 是由印刷電路板、晶片本體與其它元件 ( 電阻和電容等 ) 組成。• 這個感測器會輸出與測得電流量成正比的類比訊號。• 訊號 Arduino

A0 , GNDArduino GND

Page 8: Arduino Yun 物聯網 Lesson 3

接線完成繼電器為 HIGH 時,電路接通電流感測器與電線串聯

Page 9: Arduino Yun 物聯網 Lesson 3

REST API

• 請到 Arduino Yun 的設定頁面最下方,把 REST API ACCESS 改為 OPEN 。

• 不然會一直要您輸入密碼,而且沒反應…

• 由 sketch 中的 void process(YunClient client) 來處理 API call 。

Page 10: Arduino Yun 物聯網 Lesson 3

如果發生這種狀況• 請把 Line35 的 server.listenOnLocalhost(); 註解掉

Page 11: Arduino Yun 物聯網 Lesson 3

測試 D13 with LED

• < 網址 >/arduino/digital/ 腳位 /1 or 0

Page 12: Arduino Yun 物聯網 Lesson 3

讀取數位腳位狀態

Page 13: Arduino Yun 物聯網 Lesson 3

也可以類比寫入• < 網址 >/arduino/analog/ 腳位 /0~255

Page 14: Arduino Yun 物聯網 Lesson 3

讀取類比腳位狀態• A0 腳位對應的編號為 14• http://playground.arduino.cc/Learning/Pins

Page 15: Arduino Yun 物聯網 Lesson 3

設定數位腳位狀態• < 網址 >/arduino/mode/input 或 output

Page 16: Arduino Yun 物聯網 Lesson 3

EX_1 網路控制繼電器與抓感測器值/ sensor_test透過網路瀏覽器下指令來控制繼電器開關。還可把感測器值顯示於 Serial Monitor

Page 17: Arduino Yun 物聯網 Lesson 3

修改電壓 230 110 (V)

• float effective_voltage = 230;

Page 18: Arduino Yun 物聯網 Lesson 3

getSensorValue() 函式移動平均 ( 取 100 筆 )

for (int i = 0; i < nb_measurements; i++) { sensorValue = analogRead(CURRENT_SENSOR); avgSensor = avgSensor + float(sensorValue);}

avgSensor = avgSensor/float(nb_measurements);

return avgSensor // 回傳計算結果

Page 19: Arduino Yun 物聯網 Lesson 3

功率量測: Line59

• // Perform power measurement• float sensor_value = getSensorValue();• Serial.print("Sensor value: ");• Serial.println(sensor_value);

Page 20: Arduino Yun 物聯網 Lesson 3

轉換為電流: Line64

• // Convert to current• amplitude_current =(float)(sensor_value-zero_sensor)/1024*5/185*1000000;• effective_value=amplitude_current/1.414; // 電流振幅 ÷ 根號 2 即為有效電流

Page 21: Arduino Yun 物聯網 Lesson 3

轉換為有效功率: Line74

• abs(effective_value*effective_voltage/1000)

• 功率 = 電流 * 電壓 // P = IV

Page 22: Arduino Yun 物聯網 Lesson 3

開始測試• 確認您的 Yun 與電腦都在同一個無線網路• 在瀏覽器輸入 xx.local/arduino/digital/8/1

• 透過 REST API 來呼叫 digitalWrite(8,HIGH) 指令。您應該馬上會聽見繼電器切換的聲音,並看見燈光亮起。• 請用 … /8/0 來關閉檯燈

Page 23: Arduino Yun 物聯網 Lesson 3

檢視電流量測值• 把 Arduino Yun 用 USB接回電腦• 在 Serial Monitor 中檢查以下數值

Page 24: Arduino Yun 物聯網 Lesson 3

EX_2 傳送資料到 Google Doc

/energy_log等待網路指令來開或關上繼電器;將資料依固定時間區間傳送到 Google Docs 試算表,方便追蹤能量消耗值。

Page 25: Arduino Yun 物聯網 Lesson 3

建立 Google Docs 試算表• 試算表名稱任意,本範例為 Power (Line42)• 欄位: Time 、 Interval 、 Power 和 Energy

Page 26: Arduino Yun 物聯網 Lesson 3

公式說明• Energy = Power x Time• 能量 = 功率 x 時間• 在此其實要用到積分,但我們改用梯形公式來近似即可。• Energy= (PowerMeasurement +

NextPowerMeasurement)*TimeInverval/2• D2 = (B2 + B3)*C2/2

Page 27: Arduino Yun 物聯網 Lesson 3

申請 Temboo帳號https://www.temboo.com/

Page 28: Arduino Yun 物聯網 Lesson 3

註冊流程• 在主頁面中,請輸入您的電子郵件地址來註冊並點選 Sign up 。• 建立您的第一個應用程式。請記錄以下資料:應用程式的名稱,還有系統指派給您的金鑰;本書之後都會用到它們。

Page 29: Arduino Yun 物聯網 Lesson 3

ACCOUNT/新增一個 Application

• 建立您的第一個應用程式。請記錄以下資料:應用程式的名稱,還有系統指派給您的金鑰;本書之後都會用到它們。

Page 30: Arduino Yun 物聯網 Lesson 3

ACTIVITY/ 流量監控畫面

Page 31: Arduino Yun 物聯網 Lesson 3

Temboo Choreo 物件• 用來與指定 web service 互動,本範例為

Google Docs• 另外也可用於 Gmail 、 Twitter 等

Page 32: Arduino Yun 物聯網 Lesson 3

在程式中加入 Google account

• Line40• const String GOOGLE_USERNAME =

"yourGoogleUsername";• const String GOOGLE_PASSWORD =

"yourGooglePass";• const String SPREADSHEET_TITLE = "Power";

Page 33: Arduino Yun 物聯網 Lesson 3

在 TembooAccount.h 中修改• #define TEMBOO_ACCOUNT

"temboo_accout_name" //Temboo 帳號名稱• #define TEMBOO_APP_KEY_NAME "

temboo_app_name " //Temboo app 名稱• #define TEMBOO_APP_KEY "

temboo_api_key " //Temboo app 金鑰

Page 34: Arduino Yun 物聯網 Lesson 3

檢查是否該量測電流了• if (power_measurement_cycles >

power_measurement_cycles_max)• float sensor_value = getSensorValue();

Page 35: Arduino Yun 物聯網 Lesson 3

計算• // 轉換為電流• amplitude_current=(float)(sensor_value-

zero_sensor)/1024*5/185*1000000;• effective_value=amplitude_current/1.414;• // 計算功率• float effective_power = abs(effective_value *

effective_voltage/1000);

Page 36: Arduino Yun 物聯網 Lesson 3

傳送資料,計數器歸零• runAppendRow(measurements_interval,effec

tive_power);

• power_measurement_cycles = 0;

Page 37: Arduino Yun 物聯網 Lesson 3

組合資料並送出• // Format data – L149• String data = "";• data = data + timeString + "," +

String(measurements_interval) + "," + String(effectiveValue);

• // Set Choreo inputs• AppendRowChoreo.addInput("RowData", data);

• // Run the Choreo• unsigned int returnCode = AppendRowChoreo.run();

Page 38: Arduino Yun 物聯網 Lesson 3

檢查 Google Docs 有沒有資料進來

Page 39: Arduino Yun 物聯網 Lesson 3

EX_3 使用網路介面來遙控/webinterface

Arduino 程式沿用 EX_2

Page 40: Arduino Yun 物聯網 Lesson 3

網路介面包含了那些東西• Interface.html• jquery-2.0.3.min.js• script.js• style.css• update_state.php

Page 41: Arduino Yun 物聯網 Lesson 3

interface.html

• <script src="jquery-2.0.3.min.js"></script>• <script src="script.js"></script>• <link rel="stylesheet" type="text/css"

href="style.css"> // 匯入 JS 函式庫與 css 樣式檔• <input type="button" id="on"

class="commandButton" value="On" • onClick=“relayOn()”/> // 按下 ON 按鈕呼叫 relayOn() 函式

Page 42: Arduino Yun 物聯網 Lesson 3

style.css 中定義版面配置與顏色等body {

font-family: Helvetica;}

#relay {text-align: center;

}.commandButton {

background-color: orange;border: 1px solid black; font-size: 40px;cursor: pointer;border-radius: 10px;width: 300px;height: 100px;

}

@media screen and (max-device-width: 400px) {

.commandButton {width: 100%;height: 200px;border: 2px solid black; font-size: 80px;margin-bottom: 50px;text-align: center;background-color:

orange;}

}

Page 43: Arduino Yun 物聯網 Lesson 3

JavaScript 用來溝通 html 與 php

• function relayOn(){• $.get( "update_state.php", { command: "1"} );• }

Page 44: Arduino Yun 物聯網 Lesson 3

update_state.php 中則是實際動作<?php // Create cURL call, make sure to change it with your Yun name $service_url = 'http://myarduinoyun.local/arduino/digital/8/' . $_GET["command"]; $curl = curl_init($service_url);

// Send cURL to Yun board curl_setopt($curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 ); $curl_response = curl_exec($curl); // 執行 curl_close($curl); ?>

Page 45: Arduino Yun 物聯網 Lesson 3

自行架設 php server

• 安裝 Appserv 之後,把 webinterface 資料夾中所有檔案都放到 C:\AppServ\www 中• 在 cmd 中使用 ipconfig 指令查找本機

IP ,這樣才可以用手機等外部裝置連入。• 在瀏覽器中輸入 http://localhost/index.php 可看到預設畫面

Page 46: Arduino Yun 物聯網 Lesson 3

AppServ 預設畫面

Page 47: Arduino Yun 物聯網 Lesson 3

操作• 打開瀏覽器,輸入您的電腦 IP 或網路名稱,應該會看見專案中的各檔案顯示出來。• http://localhost/interface.html• http://YourIP/interface.html

Page 48: Arduino Yun 物聯網 Lesson 3

手機端操作• 如果是智慧型手機,則介面會自動調整螢幕大小

Page 49: Arduino Yun 物聯網 Lesson 3

Facebook 發布動態Temboo 連結 Facebook app

Page 50: Arduino Yun 物聯網 Lesson 3

Temboo 網站左側的 Facebook

• Facebook / Publishing/SetStatus

Page 51: Arduino Yun 物聯網 Lesson 3

目標:先取得 OAuth Tokens 再取得 AccessToken

Page 52: Arduino Yun 物聯網 Lesson 3

0. 記得開 IoT Mode / Arduino Yun

• Arduino + 網路擴充板• Arduino Yun• 德儀 LaunchPad

Page 53: Arduino Yun 物聯網 Lesson 3

1. 先在 Facebook Developer 建立一個 app

Page 54: Arduino Yun 物聯網 Lesson 3

2. 設定 FB app 的 Callback URL

Page 55: Arduino Yun 物聯網 Lesson 3

在 Settings 下設定完成

Page 56: Arduino Yun 物聯網 Lesson 3

3. 填入 App ID 與 App Secret

Page 57: Arduino Yun 物聯網 Lesson 3

4. 啟動認證 – 同意外部連結

Page 58: Arduino Yun 物聯網 Lesson 3

5. 取得 AccessToken

Page 59: Arduino Yun 物聯網 Lesson 3

6. 輸入 AccessToken 與 Message

Page 60: Arduino Yun 物聯網 Lesson 3

畫面下方會自動產生 Arduino code

Page 61: Arduino Yun 物聯網 Lesson 3

發布完成!

Page 62: Arduino Yun 物聯網 Lesson 3

參考資料• RESTAPI

– http://android.serverbox.ch/?p=1039– https://

learn.adafruit.com/a-rest-api-for-arduino-and-the-cc3000-wifi-chip/overview

– http://yehnan.blogspot.tw/2014/04/arduino-yunbridgeyunserveryunclient.html

• PHP– http://blog.xuite.net/arcloveangel/lovestore/22930165-Appserv%E6%9E

%B6%E7%AB%99%E6%95%99%E5%AD%B8-%E5%AE%8C%E6%95%B4%E5%AE%89%E8%A3%9D%E8%A8%AD%E5%AE%9A%E6%8C%87%E5%8D%97

– http://mark528.pixnet.net/blog/post/7267604-%E5%9C%A8windows%E5%AE%89%E8%A3%9Dphp%E9%96%8B%E7%99%BC%E7%92%B0%E5%A2%83