#include <LWiFi.h>
#include <WiFiClient.h>
#include "MCS.h"
#include <LBLE.h>
#include <LBLEPeriphral.h>
// Assign AP ssid / password here
#define _SSID "YOUR_AP_NAME"
#define _KEY "YOUR_PASSWORD"
// Assign device id / key of your test device
//MCSDevice mcs("YOUR_DEVICE_ID", "YOUR_DEVICE_KEY");
MCSLiteDevice mcs("YOUR_DEVICE_ID", "YOUR_DEVICE_KEY", "MCS_LITE_SERVER_ADDRESS", MCS_LITE_SERVICE_PORT);
// Assign channel id
// The test device should have 2 channel
// the first channel should be "Controller" - "On/Off"
// the secord channel should be "Display" - "On/Off"
MCSControllerOnOff led("switch_controller");
MCSDisplayOnOff remote("switch_display");
#define LED_PIN 7
void setup() {
// setup Serial output at 9600
Serial.begin(9600);
// setup LED/Button pin
pinMode(LED_PIN, OUTPUT);
// Initialize BLE subsystem
Serial.println("BLE begin");
LBLE.begin();
while (!LBLE.ready()) {
delay(100);
}
Serial.println("BLE ready");
// configure our advertisement data as iBeacon.
LBLEAdvertisementData beaconData;
// make an Eddystone-URL beacon that this board casts
beaconData.configAsEddystoneURL(EDDY_HTTPS, "goo.gl/evYXvt");
Serial.print("Start advertising Eddystone-URL");
// start advertising it
LBLEPeripheral.advertiseAsBeacon(beaconData);
// setup Wifi connection
while(WL_CONNECTED != WiFi.status())
{
Serial.print("WiFi.begin(");
Serial.print(_SSID);
Serial.print(",");
Serial.print(_KEY);
Serial.println(")...");
WiFi.begin(_SSID);
}
Serial.println("WiFi connected !!");
// setup MCS connection
mcs.addChannel(led);
mcs.addChannel(remote);
while(!mcs.connected())
{
Serial.println("MCS.connect()...");
mcs.connect();
}
Serial.println("MCS connected !!");
// read LED value from MCS server
while(!led.valid())
{
Serial.println("read LED value from MCS...");
led.value();
}
Serial.print("done, LED value = ");
Serial.println(led.value());
digitalWrite(LED_PIN, led.value() ? HIGH : LOW);
}
void loop() {
// call process() to allow background processing, add timeout to avoid high cpu usage
mcs.process(100);
// updated flag will be cleared in process(), user must check it after process() call.
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());
}
}
// check if need to re-connect
while(!mcs.connected())
{
Serial.println("re-connect to MCS...");
mcs.connect();
if(mcs.connected())
Serial.println("MCS connected !!");
}
}