linkit one tutorial #2- communication and cloud service

51
LinkIt ONE 物物物物物物 物物物物物物物

Upload: cavedu-education

Post on 13-Jan-2017

248 views

Category:

Devices & Hardware


9 download

TRANSCRIPT

Page 1: LinkIt ONE tutorial #2- Communication and cloud service

LinkIt ONE 物聯網研習營相關文章請點我

Page 2: LinkIt ONE tutorial #2- Communication and cloud service

Wifi 範例:取得網站字元• ExamplesLWifiWifiWebClient

Page 3: LinkIt ONE tutorial #2- Communication and cloud service

重點說明:網路設定• #define WIFI_AP “ 無線網路名稱” ( 名稱不要有空格 )• #define WIFI_PASSWORD “ 無線網路密碼 "

• #define WIFI_AUTH LWIFI_WPA // 請依照實際狀況修改為 LWIFI_OPEN, LWIFI_WPA, or LWIFI_WEP

• #define SITE_URL www.appinventor.tw• // 要讀取的網站內容

Page 4: LinkIt ONE tutorial #2- Communication and cloud service

重點說明:宣告與連線• LWiFiClient c;

• while (0 == LWiFi.connect(WIFI_AP, LWiFiLoginInfo(WIFI_AUTH, WIFI_PASSWORD)))

• {• delay(1000);• }

Page 5: LinkIt ONE tutorial #2- Communication and cloud service

重點說明:爬字元• while (c)• {• int v = c.read();• if (v != -1)• {• Serial.print((char)v); // 一個個字元印出來• }• else• { …

Page 6: LinkIt ONE tutorial #2- Communication and cloud service

執行結果

Page 7: LinkIt ONE tutorial #2- Communication and cloud service

來取得 Facebook 粉絲頁按讚數• 請上網下載: WifiWebClient_FBGraph.ino

• c.println("GET /caveeducation HTTP/1.1");– 粉絲專頁名稱

Page 9: LinkIt ONE tutorial #2- Communication and cloud service

執行結果• 再取出喜歡的欄位即可

Page 10: LinkIt ONE tutorial #2- Communication and cloud service

GPRS 範例:• ExamplesLWifiWifiWebClient• 架構相同,但引用的函式不同: LGPRS• 當然不需要 Wifi 帳密

Page 11: LinkIt ONE tutorial #2- Communication and cloud service

GPRS 範例:簡訊控制 LED

• 請上網下載: SMS_LED.ino

Page 12: LinkIt ONE tutorial #2- Communication and cloud service

MediaTek Cloud Sandbox (MCS)

Page 13: LinkIt ONE tutorial #2- Communication and cloud service

MCS 主要功能• 雲端平台,執行程式• 例: Google App Engine 、 Amazon Web

Services 、 Microsoft Azure• 提供網頁介面,讓你「點按滑鼠」就能建立雲端程式• 提供 API 程式存取介面• 提供網頁介面,觀看與控制各種資料

Page 14: LinkIt ONE tutorial #2- Communication and cloud service

MCS 其他功能• 通知( Notification ),當某資料符合特定條件時,寄送電子郵件• 使用者權限( User Privilege ),設定開發成員的存取修改權限• FOTA ( Firmware Over The Air ),版本控管,韌體經由無線傳輸自動更新

Page 15: LinkIt ONE tutorial #2- Communication and cloud service

目標• 到 MCS 註冊帳號• 建立網頁應用程式( web application )• 透過 MCS REST API 進行溝通• LinkIt ONE 板子、手機、瀏覽器

Page 16: LinkIt ONE tutorial #2- Communication and cloud service

範例• 從雲端控制板子內建的 LED• LED :想像成家裡的燈• 為什麼從雲端?不能直接連線嗎?

Page 17: LinkIt ONE tutorial #2- Communication and cloud service
Page 18: LinkIt ONE tutorial #2- Communication and cloud service

MCS 重要觀念• 原型 (Prototype) ,產品的原型• 資料頻道 (Data Channel) ,帶有時間資訊的資料串,具有 Data Channel

Id 、 Datapoint• Test Device ,產品的不同版本,具有

DeviceId 、 DeviceKey 讓程式得以指定• 例: LED 控制器、溫濕度感測站

Page 19: LinkIt ONE tutorial #2- Communication and cloud service
Page 21: LinkIt ONE tutorial #2- Communication and cloud service

MCS Dashboard (儀表板)

Page 22: LinkIt ONE tutorial #2- Communication and cloud service

重要索引• Development :建立 Prototype 、新增修改 Data channel 、查詢 Data

channel Id• My Devices :查詢 DeviceId 、 DeviceKey• Resources :教學文件、參考資料• < 帳號 > :代表已登入

Page 23: LinkIt ONE tutorial #2- Communication and cloud service

範例:控制 LED 並檢視狀態程式: mcsled.js / mcsled.py

Page 24: LinkIt ONE tutorial #2- Communication and cloud service

流程1. 建立 Prototype2. 依照所需功能,新增 Data channel ,記下

Data channel Id3. 建立 Test device ,記下 DeviceId 與

DeviceKey

Page 25: LinkIt ONE tutorial #2- Communication and cloud service

建立 Prototype

• Prototype name : LED Manager可自己挑個名字• Prototype version : 1.0• Hardware platform : LinkIt ONE (MT2502)• Industry : Personal Services• Application : Other (Personal Services)

Page 26: LinkIt ONE tutorial #2- Communication and cloud service
Page 27: LinkIt ONE tutorial #2- Communication and cloud service

Data channel• 顯示器 Controller :可從 MCS 控制開發板• 顯示器 Display :讓開發板能回傳資料到 MCS

Page 28: LinkIt ONE tutorial #2- Communication and cloud service

新增 Data channel (1/2)

• 請新增一個 Controller• Data channel name : LED CONTROL• Data channel Id : LED_Control重要!將用於程式碼內• Data type : ON/OFF

Page 29: LinkIt ONE tutorial #2- Communication and cloud service
Page 30: LinkIt ONE tutorial #2- Communication and cloud service

建立 Test device

• Device name : LED Manager可自己挑個名字

Page 31: LinkIt ONE tutorial #2- Communication and cloud service

到 My Devices

• 找出 DeviceId 與DeviceKey

Page 32: LinkIt ONE tutorial #2- Communication and cloud service

您應該得到• DeviceId例:「 D0pxIEKU 」• DeviceKey例:「 Pw0Cl0i0f6oN1Zk1 」• 兩個 Data channel Id例:「 LED_CONTROL 」與「 LED 」• 然後修改程式碼裡的設定

Page 33: LinkIt ONE tutorial #2- Communication and cloud service

MCS 新增 channel 完成設定 nodejs 環境

Page 34: LinkIt ONE tutorial #2- Communication and cloud service

程式 mcsled.js 修改處var myApp = mcs.register({ deviceId: 'ABC123', deviceKey: 'XYZ123', });

以上紅字換成您 test device 的資訊

Page 35: LinkIt ONE tutorial #2- Communication and cloud service

程式 mcsled.py 修改處DEVICE_INFO = { 'device_id' : 'YOUR_DEVICE_ID',

'device_key' : 'YOUR_DEVICE_KEY' }

以上紅字換成您 test device 的資訊

Page 36: LinkIt ONE tutorial #2- Communication and cloud service

抓取 Datapoint 的 HTTP 請求• 動作: GET• 網址: /mcs/v2/devices/:deviceId/

datachannels/:datachannelId/datapoints• 標頭:

Content-Type:application/jsondeviceKey:DeviceKey

Page 37: LinkIt ONE tutorial #2- Communication and cloud service

抓取 Datapoint 的 HTTP 回應內容{ "apiVersion":"0.0.1", "code":200, "message":"Request has succeeded", "deviceId":"D0pxIEKU", "dataChannels":[{ "dataChnId":"LED_CONTROL", "isOverflow":false, "dataPoints" :[{ "recordedAt":1430903644863, "values":{"value":0} }] }]}

Page 38: LinkIt ONE tutorial #2- Communication and cloud service

上傳 Datapoint 的 HTTP 請求• 動作: POST• 網址: /mcs/v2/devices/:deviceId/datapoints• 標頭:

Content-Type:application/jsondeviceKey:DeviceKey

Page 39: LinkIt ONE tutorial #2- Communication and cloud service

上傳 Datapoint 的 HTTP 請求內容{ "datapoints":[{ "dataChnId":"LED", "values":{ "value":0 } }]}

Page 40: LinkIt ONE tutorial #2- Communication and cloud service

上傳 Datapoint 的 HTTP 回應內容{ "apiVersion":"0.0.1", "code":200, "message":"Request has succeeded", "results":"Success."}

Page 41: LinkIt ONE tutorial #2- Communication and cloud service

範例程式: lo_pwm_analog.ino

• 自行建立新 Prototype• 兩個 Data channel :一個 Controller 型的

PWM 、一個 Display 型的 Integer• 電路圖跟「以可變電阻控制 LED亮度」相同

Page 42: LinkIt ONE tutorial #2- Communication and cloud service

程式 lo_pwm_analog.ino

• b :開啟WiFi模組, e :關閉WiFi模組• c :與 WiFi AP 連線, d :切斷• m :建立與 MCS之間的永久連線• 從 MCS 送出 PWM 的值,控制 LED 的亮度• 若有永久連線,板子就會讀取可變電阻的值,上傳到 MCS• 注意:記得修改程式碼裡相關設定值

Page 43: LinkIt ONE tutorial #2- Communication and cloud service

MediaTek Cloud Sandbox

請用先前下載 SDK 的帳號登入

Page 44: LinkIt ONE tutorial #2- Communication and cloud service

DeviceID/ Key DeviceID/ Key

Page 45: LinkIt ONE tutorial #2- Communication and cloud service

流程1. 建立 prototype2. 根據所需要的來新增對應的 Data channel

(Controller / Display)3. 建立 test device (DeviceId / DeviceKey)

Page 46: LinkIt ONE tutorial #2- Communication and cloud service

GPS(Global Positioning System)全球定位系統

Page 47: LinkIt ONE tutorial #2- Communication and cloud service

GPS天線

Page 48: LinkIt ONE tutorial #2- Communication and cloud service

GPGGA全球定位系統資料• $GPGGA,170834.254,4124.8963,N,08151.683

8,W,1,05,1.5,280.2,M,-34.0,M,0.0,0000*74• 170834.254 :時間, 17:08:34.254• 4124.8963 : 41 度 24.8963分• N :北緯• 08151.6838 : 81 度 51.6838分• W :西經

Page 49: LinkIt ONE tutorial #2- Communication and cloud service

GPGGA全球定位系統資料• 1 :定位有效程度, 0是無效、 1是 GPS fix• 05 :衛星數量• 1.5 :水平精確度• 280.2,M :離海平面的高度,單位公尺• -34.0,M :大地水準面離WGS84 的高度,單位公尺• 0.0 :距離上次 DGPS 更新的時間,單位秒• 0000 : DGPS 站台識別號碼• *74 ,校驗碼

Page 50: LinkIt ONE tutorial #2- Communication and cloud service

顯示 GPS 於 MCS

Page 51: LinkIt ONE tutorial #2- Communication and cloud service

建立新 Data Channel :GPS x1, float x 2