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

Visit MCS web console 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.

If you are not sure how to create prototype, test device and channels, refer to the MCS Getting Started page.

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

Use MCS Library to connect to MCS

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

  1. 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"
  2. 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");
  3. 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:

Run the Sketch

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

  2. Click Serial Monitor in the toolbar and adjust the Baud rate setting to 9600.

Code

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.

Last updated