> For the complete documentation index, see [llms.txt](https://cavedu.gitbook.io/linkit-7697/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://cavedu.gitbook.io/linkit-7697/linkit-7697-development-guide-for-arduino-ide/developer-guide/using-mcs-library/connecting-to-mcs.md).

# Connecting to MCS

We use an example to show you how to connect to MCS web service. This example connects to the service with **MCSDevice** class, and allows users to remotely turn on and off the on-board LED of the LinkIt 7697 HDK from MCS web console.

### Create your MCS test device <a href="#connectingtomcs-createyourmcstestdevice" id="connectingtomcs-createyourmcstestdevice"></a>

Visit [MCS web console](https://mcs.mediatek.com/) and create your prototype, data channels and a test device. In this example, you need to create 2 data channels:

1. A **Controller** channel with type **ON/OFF**.
2. A **Display** channel with type **ON/OFF**.&#x20;

{% hint style="info" %}
If you are not sure how to create prototype, test device and channels, refer to the [MCS Getting Started](http://mcs.mediatek.com/resources/latest/tutorial/getting_started) page.
{% endhint %}

Your test device should look like the screenshot below. Note the **Data channel Id**, **DeviceId**, and **DeviceKey** fields, which we'll be using later.

![](https://3972650740-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FY4gduUSLWOCI23CXUWej%2Fuploads%2FJm77CM2bxVykk959HAhm%2Fimage2017-5-24%2B11_13_45.png?alt=media\&token=5ada0f26-7778-49e0-8177-ab9174daaca0)

### Use MCS Library to connect to MCS <a href="#connectingtomcs-usemcslibrarytoconnecttomcs" id="connectingtomcs-usemcslibrarytoconnecttomcs"></a>

To connect to MCS, we need to create a **MCSDevice** object with **device key** and **device ID**, and set the data channels accordingly.

1. In your Arduino IDE, select **File**, then **Examples**, click **MCS** then select **LED\_control**\
   ![](https://3972650740-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FY4gduUSLWOCI23CXUWej%2Fuploads%2FTtgH20NZZxPKhMIJ22xD%2Farduino_example.png?alt=media\&token=18b6ce7d-f9bb-432c-ae6d-daf88fffa071)
2. We'll access MCS through the Wi-Fi network. Modify the Wi-Fi **SSID** and **password** of your Wi-Fi network in the example sketch:

   ```
   // Assign AP ssid/password here
   #define _SSID "your_ssid"
   #define _KEY  "your_password"
   ```
3. Set the **device ID** and **device key** to the MCSDevice constructor in the sketch:

   ```
   // Assign device id/key of your test device
   MCSDevice mcs("your_device_id", "your_device_key");
   ```

   The device ID and device key can be found in the MCS console, as shown below:![](https://3972650740-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FY4gduUSLWOCI23CXUWej%2Fuploads%2FbikJBlISdoeT8WVovkQJ%2Fimage2017-5-24%2B11_16_0.png?alt=media\&token=8c0b780e-0736-4be4-95eb-7ca5a9c7c0cb)<br>
4. Assign the channel IDs of your test device:

   ```
   // Assign data channel ID 
   MCSControllerOnOff led("your_channel1_id");
   MCSDisplayOnOff    remote("your_channel2_id");
   ```

   The channel IDs can be found in the bottom of the data channel view, as shown below:&#x20;

![](https://3972650740-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FY4gduUSLWOCI23CXUWej%2Fuploads%2FY4FfnOYyPc1S3Qkwxapt%2Fimage2017-5-24%2B11_17_41.png?alt=media\&token=ddde8b1d-86e9-43c9-9f16-470199dceabe)

### Run the Sketch <a href="#connectingtomcs-runthesketch" id="connectingtomcs-runthesketch"></a>

1. Click **Upload** and wait for the **Done Uploading** message to appear.

   ![](https://3972650740-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FY4gduUSLWOCI23CXUWej%2Fuploads%2FMAZVTEprasKJz3TRsqUl%2Farduino_upload.png?alt=media\&token=fc9ed14a-8317-41f9-89d5-acd601913ddf)
2. Click **Serial Monitor** in the toolbar and adjust the **Baud rate** setting to **9600.**

   ![](https://3972650740-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FY4gduUSLWOCI23CXUWej%2Fuploads%2FMx8QVjjxFU3ozh6nJy8O%2Farduino_serial_console.png?alt=media\&token=968bb8ac-766e-4ee0-a6aa-a4fff77bddcd)<br>
3. You can now operate the **switch controller** on [MCS web console](https://mcs.mediatek.com/v2console/) to turn on or off the LED light on the development board. It also updates the value back to **switch display channel**.\
   ![](https://3972650740-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FY4gduUSLWOCI23CXUWej%2Fuploads%2Fe02GP0JMfJ1jFCNZo617%2Fmcs_test_device.png?alt=media\&token=e2dc83e4-f04c-4e6e-92bf-26436941836d)
4. Please observe the **LED PIN 7** blinking on the development board. The LED light is on and off according to your operation on the [MCS web console](https://mcs.mediatek.com/v2console/).![](https://3972650740-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FY4gduUSLWOCI23CXUWej%2Fuploads%2FtxmnvGv1NI2v8sqluSOI%2F7697%2B-%2BCopy.png?alt=media\&token=1d7f1e91-c998-4cd8-a57a-5ddb8bbce592)<br>

### Code <a href="#connectingtomcs-code" id="connectingtomcs-code"></a>

In this case, use **MCS library** to deal with the interaction between the physical device and MCS web server. First, include the MCS library.

Create instances of **MCSControllerOnOff** and **MCSDisplayOnOff** class which represent the **switch data channels** you created in the prototype on MCS and provide data channel ID as the parameter.

```
MCSControllerOnOff led("your_channel1_id");
MCSDisplayOnOff    remote("your_channel2_id")
```

Associate this physical device to the **test device** on MCS by creating an instance of **MCSDevice** class with device ID and key.

```
MCSDevice mcs("your_device_id", "your_device_key");
```

Attach those data channel instances to the test device and use `connect()` to initialize the TCP connection to MCS server.

```
  // setup MCS connection
  mcs.addChannel(led);
  mcs.addChannel(remote);
  while(!mcs.connected())
  {
    Serial.println("MCS.connect()...");
    mcs.connect();
  }
```

`Call update()` in the loop() to check if there is new data point received on physical device for **switch controller** data channel. If yes, then assign the new value to **LED\_PIN** setting to either turn on or off the LED.

```
if(led.updated())
  {
    Serial.print("LED updated, new value = ");
    Serial.println(led.value());
    digitalWrite(LED_PIN, led.value() ? HIGH : LOW);
    if(!remote.set(led.value()))
    {
      Serial.print("Failed to update remote");
      Serial.println(remote.value());
    }
  }
```

To know more about the MCS library, please refer to MCS Library Usage.
