Connecting to MCS
Last updated
Last updated
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.
Visit MCS web console and create your prototype, data channels and a test device. In this example, you need to create 2 data channels:
A Controller channel with type ON/OFF.
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.
To connect to MCS, we need to create a MCSDevice object with device key and device ID, and set the data channels accordingly.
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:
Set the device ID and device key to the MCSDevice constructor in the sketch:
Assign the channel IDs of your test device:
The channel IDs can be found in the bottom of the data channel view, as shown below:
Click Upload and wait for the Done Uploading message to appear.
Click Serial Monitor in the toolbar and adjust the Baud rate setting to 9600.
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.
Associate this physical device to the test device on MCS by creating an instance of MCSDevice class with device ID and key.
Attach those data channel instances to the test device and use connect()
to initialize the TCP connection to MCS server.
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.
To know more about the MCS library, please refer to MCS Library Usage.
In your Arduino IDE, select File, then Examples, click MCS then select LED_control
The device ID and device key can be found in the MCS console, as shown below:
You can now operate the switch controller on MCS web console to turn on or off the LED light on the development board. It also updates the value back to switch display channel.
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.