# C02. 上傳溫溼度到雲端

### 專案說明 <a href="#c02.mcs-shang-chuan-wen-shi-du-dao-yun-duan-zhuan-an-shui-ming" id="c02.mcs-shang-chuan-wen-shi-du-dao-yun-duan-zhuan-an-shui-ming"></a>

## **MediaTek Cloud Sandbox 團隊已於2021.4.16** 停止 MCS 服務 如要繼續使用請使用離線板[**MCS Lite**](https://cavedu.gitbook.io/cavedu/mcs-lite/mcs-lite-introduction-environment)

使用 **LinkIt 7697** + **溫溼度感測器**，收集**溫溼度資訊**並上傳到 [**MCS** ](https://mcs.mediatek.com/)**雲端顯示。**

![](/files/7cCFv5e9gHkgVvmPNWsc)

此範例主要是演示 **顯示器通道** (Display Channel) 如何使用。請先詳讀 [範例 C01](https://docs.labs.mediatek.com/linkit-7697-blocklyduino/c01-led-12879996.html), 了解基本 MCS 的基本概念, 本範例不再重複介紹。

### 硬體連接 <a href="#c02.mcs-shang-chuan-wen-shi-du-dao-yun-duan-ying-ti-lian-jie" id="c02.mcs-shang-chuan-wen-shi-du-dao-yun-duan-ying-ti-lian-jie"></a>

可參考 [B03. Grove 溫溼度感測器](https://docs.labs.mediatek.com/linkit-7697-blocklyduino/b03-grove-12880218.html), 將**溫溼度感測器**連接到**Grove 擴充版**的連接埠 **D2**&#x20;

### 雲端設定 <a href="#c02.mcs-shang-chuan-wen-shi-du-dao-yun-duan-yun-duan-she-ding" id="c02.mcs-shang-chuan-wen-shi-du-dao-yun-duan-yun-duan-she-ding"></a>

在 **MCS** 建立一個**原型** (Prototype)，含有兩個 **浮點數顯示通道** 代表**溫度**和**相對濕度**，並使用這原型來創建一個使操作使用的**測試裝置** (Test Device)

▼ 點選 **創建** 原型

![](/files/kdlkhQgpVRAzonFNamni)

▼ 幫**原型**取個**名稱**和**版本**。 **硬體平台**選擇 **"LinkIt 7697"**， 產業和應用程式隨意選擇。 填完後按下 **儲存**

![](https://docs.labs.mediatek.com/linkit-7697-blocklyduino/files/12880045/12880557/1/1498724376012/2017-06-29_16-22-21.png)

▼ **原型**已創建成功， 點擊 **詳情** 進入

![](/files/oHZq8at3LpnIMLda1Dvb)

▼ 點擊 **新增** 來加入新的通道資料

![](/files/ueiAjuaOaU4GF6xz3RJ1)

▼ **新增 "顯示器型態"** 的資料通道

![](/files/afVfqSYHuAbu7OB8u8NU)

▼ 新增一個**溫度**的資料通道，幫此資料通道取個**名稱**(隨意)， 並取一個 **Id** (此 Id 會用於程式中來控制此通道)， 然後選擇 "**整數"** 的資料型態並選擇單位為 **"攝氏"。** 最後 **儲存** 此通道。

![](/files/nYfFqMT1DWx1SV92j2gh)

▼ 再新增一個**相對溼度**的資料通道, 選擇 "**浮點數"** 的資料型態並選擇單位為 **"百分比"。** 最後 **儲存** 此通道。

![](/files/1ohyRupe49UPm0lcNsD1)

▼紅色圈圈為新增的兩個浮點數的 **顯示通道，** 紅色箭頭為這 **資料通道 Id** (會用在程式內)。 接下來就要用這**原型**創建一個**測試裝置** (橘色箭頭)

![](/files/7IkCCGjHv6D6ZSwl8f8R)

▼幫這測試裝置取個**裝置名稱**， 並按下 **創建**&#x20;

![](/files/R6z911kq1TC5WAESecBK)

▼**測試裝置**新增完成， 畫面上的四個紅色箭頭所指的 "**資料通道 Id**"， "**DeviceId**"， "**DeviceKey**" 為之後開發板上軟體開發時會用到的重要資料

![](/files/igs2mi2qLYvzMnI86yCl)

### &#x20;積木畫布  <a href="#c02.mcs-shang-chuan-wen-shi-du-dao-yun-duan-ji-mu-hua-bu" id="c02.mcs-shang-chuan-wen-shi-du-dao-yun-duan-ji-mu-hua-bu"></a>

**初始化**&#x20;

* 建立 Wi-Fi 連線， 連到 AP (Wi-Fi SSID 和密碼請填寫要連接 AP 的資料)
* 建立 MCS 連線， 填入 DeviceID 和 DevieKey， 和上面步驟建立的雲端 **測試物件** 做連接
* 新增兩個 **"整數" "顯示控制通道"** (Float DisplayChannel) 物件， 填入正確的 **控制通道 Id**

**重複執行**&#x20;

* 第一步要加上 **和 MCS 同步**。 每次執行這一個積木， 程式會去和**雲端做資料同步**&#x20;
* 將溫溼度傳感器的數值更新到雲端對應的**顯示通道**
* 嘗試去握著傳感器改變溫度， 或是吹氣改變濕度，可以觀察 **MCS 控制台** 上的數值在改變

![](/files/xPKQ1NRTfM5ZrFpIgRZ0)

\
產生出的 Arduino 程式如下

```
#include <LWiFi.h>
#include "MCS.h"
#include "DHT.h"

int t;
int h;

char _lwifi_ssid[] = "MediaTek_Labs";
char _lwifi_pass[] = "84149961";

MCSDevice mcs("Drsls7CQ", "kKUGahHDR5Hnp5Nn");
MCSDisplayInteger temp("temp");
MCSDisplayInteger humid("humid");

DHT __dht2(2, DHT22);

void setup()
{
  Serial.begin(9600);

  mcs.addChannel(temp);
  mcs.addChannel(humid);
  Serial.println("Wi-Fi 開始連線");
  while (WiFi.begin(_lwifi_ssid, _lwifi_pass) != WL_CONNECTED) { delay(1000); }
  Serial.println("Wi-Fi 連線成功");
  while(!mcs.connected()) { mcs.connect(); }
  Serial.println("MCS 連線成功");
  Serial.begin(9600);
  __dht2.begin();
}

void loop()
{
  while (!mcs.connected()) {
  	mcs.connect();
  	if (mcs.connected()) { Serial.println("MCS 已重新連線"); }
  }
  mcs.process(100);

  t = __dht2.readTemperature();
  h = __dht2.readHumidity();
  temp.set(t);
  humid.set(h);

  Serial.print("攝氏溫度 : ");
  Serial.println(t);
  Serial.print("相對溼度 :");
  Serial.println(h);

  delay(1000);
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://cavedu.gitbook.io/linkit-7697/linkit-7697-blocklyduino-shi-yong-zhi-nan/shi-zuo-fan-li-jiao-xue/c02.-shang-chuan-wen-shi-du-dao-yun-duan.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
