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

與 IoT 相關的雲端服務為數眾多,例如:ThingSpeakWoT.CityAWSIBM BluemixMediaTek MCS 等,並在透過這些雲端服務可在對資料進行資料視覺化、資料分析與其它的應用,而本文透過 Linkit Smart 7688 Duo 將 Sensor 資料透過 MQTT 傳送到 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 paho.mqtt.client as mqtt  
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 = "D7fDOASh"  
deviceKey = "eqGDzbxWsKyJqkl7"  
dataChnId1 = "Humidity"  
dataChnId2 = "Temperature"  
MQTT_SERVER = "mqtt.mcs.mediatek.com"  
MQTT_PORT = 1883  
MQTT_ALIVE = 60  
MQTT_TOPIC1 = "mcs/" + deviceId + "/" + deviceKey + "/" + dataChnId1  
MQTT_TOPIC1 = "mcs/" + deviceId + "/" + deviceKey + "/" + dataChnId2

mqtt_client = mqtt.Client()  
mqtt_client.connect(MQTT_SERVER, MQTT_PORT, MQTT_ALIVE)    

while True:  
    h0 = value.get("Humidity")
    t0 = value.get("Temperature")
    payload = {"datapoints":[{"dataChnId":"Humidity","values":{"value":h0}}]}
    mqtt_client.publish(MQTT_TOPIC1, json.dumps(payload), qos=1)
    payload = {"datapoints":[{"dataChnId":"Temperature","values":{"value":t0}}]}
    mqtt_client.publish(MQTT_TOPIC1, json.dumps(payload), qos=1)
    time.sleep(1)

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 paho-mqtt  
reboot  
  • Step 4. 執行剛傳到 Linkit Smart 7688 Duo 中的 Python Code
python 檔名.py  
  • Step 5. Console 執行畫面 Imgur

  • Step 7. 透過 MQTTLens 來執行 Publish 與 Subscribe [Config]

Hostname: mqtt.mcs.mediatek.com  
Port: 1883  

Imgur

[Subscribe]

訂閱特定資料通道的訂閱主題 -> mcs/:deviceId/:deviceKey/:dataChnId
可訂閱特定裝置下全部資料通道 -> mcs/:deviceId/:deviceKey/+

e.g.,
mcs/D7fDOASh/eqGDzbxWsKyJqkl7/+

Imgur

[Publish]

發佈主題的格式 -> mcs/:deviceId/:deviceKey/:dataChnId
Request Body  
{
  "datapoints": 
  [
    {"values": {"value": ""}, "dataChnId": ""}
  ]
}

e.g.,

{ "datapoints": [ {"values": {"value": "27.00"}, "dataChnId": "Temperature"} ] }

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

MQTTLens - Publish
Imgur

  • Step 8. 透過 Python 來執行 Subscribe 更新程式中的 DeviceId 與 DeviceKey
import paho.mqtt.client as mqtt  
import re  
import httplib, urllib  
import socket  
import sys  
import time

deviceId = "D7fDOASh"  
deviceKey = "eqGDzbxWsKyJqkl7"  
MQTT_SERVER = "mqtt.mcs.mediatek.com"  
MQTT_PORT = 1883  
MQTT_ALIVE = 60  
MQTT_TOPIC = "mcs/" + deviceId + "/" + deviceKey + "/+"

def on_connect(client, userdata, flags, rc):  
    print("MQTT Connected with result code "+str(rc))
    client.subscribe(MQTT_TOPIC)

def on_message(client, userdata, msg):  
    print("mqtt payload=%s" %(msg.payload))

mqtt_client = mqtt.Client()  
mqtt_client.on_connect = on_connect  
mqtt_client.on_message = on_message

mqtt_client.connect(MQTT_SERVER, MQTT_PORT, MQTT_ALIVE)  
mqtt_client.loop_forever()  

Imgur Imgur


GitHub
參考資料
List of blogs

Archer

Having being a full stack engineer. Experience with C, Python, Go, Swift, JavaScript, Node.js, LinkIt 7697, LinkIt Smart 7688, Raspberry Pi, ARM mbed, IoT solutions. Contact us : [email protected]

ALL RIGHTS RESERVED. COPYRIGHT © 2016. Designed and Coded by Makee.io