[ Linkit Smart 7688 ] 傳送 Sensor Data 到 MediaTek Cloud Sandbox (MCS) - API

與 IoT 相關的雲端服務為數眾多,例如:ThingSpeakWoT.CityAWSIBM BluemixMediaTek MCS 等,並在透過這些雲端服務可在對資料進行資料視覺化、資料分析與其它的應用,而本文透過 Linkit Smart 7688 Duo 將 Sensor 資料傳送到 MCS 並視覺化顯示出相關資訊 。

Imgur

執行步驟

Imgur

前置準備作業

  1. 準備 Linkit Smart 7688 Duo 開發板

  2. 準備 Arduino Breakout for LinkIt Smart 7688 Duo

  3. 準備 Grove - Temperature & Humidity Sensor

  4. 將 Linkit Smart 7688 Duo 與 Arduino Breakout for LinkIt Smart 7688 Duo 組裝、Grove - Temperature & Humidity Sensor 裝到 A0 的位置並將 Linkit Smart 7688 Duo 連接至電腦

  5. 安裝 Arduino IDE (version: 1.6.6)
    https://www.arduino.cc/en/Main/OldSoftwareReleases

  6. 安裝 IDE ( Sublime Text or Visual Studio Code )

  7. Windows 平台,請安裝 Putty 與 FileZilla
    http://oranwind.org/-linkit/

Linkit Smart 7688 Duo 與 Grove - Temperature & Humidity Sensor 連接圖

Imgur

MediaTek Cloud Sandbox (MCS) 端

  • Step 1. 到 MediaTek Cloud Sandbox (MCS) 網站申請帳號

https://mcs.mediatek.com

  • Step 2. 登入 MediaTek Cloud Sandbox (MCS) 網站 Imgur

  • Step 3. 點擊 開始 Imgur

  • Step 4. 點擊 創建 Imgur

  • Step 5. 輸入 產品原型名稱、產品原型版本、硬體平台、產業、應用程式 再按儲存 Imgur

  • Step 6. 點擊 Last updated prototype 中的 Linkit Smart 7688 Duo Imgur

  • Step 7. 點擊 Add Data channel now! 中的 Add Imgur

  • Step 8. 點擊 Display 中的 Add Imgur

  • Step 9. 輸入 Temperature 的 Data channel name、Data channel Id、Data type、Unit 再按 Save Imgur

  • Step 10. 點擊 Display 中的 Add Imgur

  • Step 11. 輸入 Humidity 的 Data channel name、Data channel Id、Data type、Unit 再按 Save Imgur

  • Step 12. 點擊 Test device -> Linkit Smart 7688 Duo 取得 DeviceId 與 DeviceKey Imgur Imgur Imgur

Linkit Smart 7688 Duo - MCU ( Arduino ) 端

  • Step 1. 透過 MCU 讀取 Sensor Data
// MCU To MPU
#include <Bridge.h>

// DHT
#include "DHT.h"
#define DHTPIN A0
#define DHTTYPE DHT11 
DHT dht(DHTPIN, DHTTYPE);

void setup() {  
  Serial.begin(9600); 
  dht.begin();
  Bridge.begin();
}

void loop() {  
  // Get Temperature and Humidity
  float h = dht.readHumidity();
  float t = dht.readTemperature();

  // Display Temperature and Humidity Value
  Serial.print("DHT-Humidity: "); 
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("DHT-Temperature: "); 
  Serial.print(t);
  Serial.println(" *C\t");

  // MCU To MPU
  Bridge.put("Humidity", String(h));
  Bridge.put("Temperature", String(t));

  delay(1000); //每秒回傳一次資料
}

Imgur

Linkit Smart 7688 Duo - MPU ( Python ) 端

  • Step 1. 透過 Python 讀取 MCU Sensor Data 更新程式中的 DeviceId 與 DeviceKey
import time  
import sys  
import httplib, urllib  
import json

sys.path.insert(0, '/usr/lib/python2.7/bridge/')  
from bridgeclient import BridgeClient as bridgeclient

value = bridgeclient()

deviceId = "DjpJIckU"  
deviceKey = "fFZLPwndLgDTggMU"


def post_to_mcs(payload):  
    headers = {"Content-type": "application/json", "deviceKey": deviceKey}
    not_connected = 1
    while (not_connected):
        try:
            conn = httplib.HTTPConnection("api.mediatek.com:80")
            conn.connect()
            not_connected = 0
        except (httplib.HTTPException, socket.error) as ex:
            print "Error: %s" % ex
            time.sleep(10)  # sleep 10 seconds

    conn.request("POST", "/mcs/v2/devices/" + deviceId + "/datapoints", json.dumps(payload), headers)
    response = conn.getresponse()
    print( response.status, response.reason, json.dumps(payload), time.strftime("%c"))
    data = response.read()
    conn.close()

while True:  
    h0 = value.get("Humidity")
    t0 = value.get("Temperature")
    payload = {"datapoints":[{"dataChnId":"Humidity","values":{"value":h0}},{"dataChnId":"Temperature","values":{"value":t0}}]}
    post_to_mcs(payload)
    time.sleep(10)

Imgur

  • Step 2. 將 Python Code 傳送到 Linkit Smart 7688 Duo

  • Step 3. 安裝相關套件於 Linkit Smart 7688 Duo

opkg update  
uci set yunbridge.config.disabled=0  
uci commit  
pip install httplib2  
reboot  
  • Step 4. 執行剛傳到 Linkit Smart 7688 Duo 中的 Python Code
python 檔名.py  
  • Step 5. Console 執行畫面 Imgur

  • Step 6. MediaTek Cloud Sandbox (MCS) 畫面 Imgur

  • Step 7. 透過 Postman 來執行 POST 與 GET 的 API 呼叫

[GET]

GET https://api.mediatek.com/mcs/v2/devices/:deviceId/datachannels/:datachannelId/datapoints  
Request Header  
deviceKey:  
Content-Type: application/json  

e.g.,
https://api.mediatek.com/mcs/v2/devices/DjpJIckU/datachannels/Humidity/datapoints

Imgur

[POST]

POST https://api.mediatek.com/mcs/v2/devices/:deviceId/datapoints  
Request Header  
deviceKey:  
Content-Type: application/json  
Request Body  
{
  "datapoints": 
  [
    {"values": {"value": ""}, "dataChnId": ""}
  ]
}

e.g.,
http://api.mediatek.com/mcs/v2/devices/DjpJIckU/datapoints
Request Body
{ "datapoints": [ {"values": {"value": "48.00"}, "dataChnId": "Humidity"}, {"values": {"value": "27.00"}, "dataChnId": "Temperature"} ] }

將上面 Request Body 的資料放到 jsoneditoronline 來觀看 Imgur

Postman - POST
Imgur


GitHub
參考資料
List of blogs
ALL RIGHTS RESERVED. COPYRIGHT © 2016. Designed and Coded by Makee.io