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.
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:
// Assign AP ssid/password here
#define _SSID "your_ssid"
#define _KEY "your_password"
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");
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:
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.
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.
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.