Introduction: Smart Home Part 2: Driving Relay Through the Cloud.

Please get familiar and try to make my first instructable where I show you how to log your house temperature into intel cloud using Edison. Please follow steps 2-3 of the above instructable to set up your edison and your Intel IoT Analytics Dashboard.

This time it took me few evenings to master this... in the meantime I had to overcome many difficulties... ok let's start...

Step 1: Assemble the Edison!

First you have to attach your Edison to Arduino dev board.

Guide it through mounting screws and press gently press it in place where you see white label.

Second - get base board from the Grove Starter Kit and put it in place on dev board.

At last - take one cable and Relay from the Grove Starter Kit and connect it together to DIGITAL I/O, in my case D2.

Step 2: Update Iotkit-admin and Pair Your Edison With IoT Cloud.

It is crucial you update iotkit-admin as current image comes with old version which doesn't support control function correctly. These steps took me lot of forum consulting, trying and failing but finally I got it right :-)

  1. npm update -g iotkit-agent

  2. iotkit-admin -V

    Should read "1.6.5" or higher

  3. iotkit-admin test

    Should return with no errors

  4. iotkit-admin move-data-directory ~/.data

    Saves your config files in ~/.data/

  5. iotkit-admin device-id

    Should get "Device ID"

  6. Mark device id and copy it.

  7. Go to your IoT Analytics to My Devices panel and press "Add new device". Paste device id into fields ID and Gateway, give it some cool name and save. Now go to My Account panel, generate new activation code and make it visible.

  8. Go back to Edison command prompt and type in "iotkit-admin activate YOURACTIVATIONCODE"(substitute your code of course).

  9. iotkit-admin activate

  10. iotkit-admin register power powerswitch.v1.0

  11. iotkit-admin protocol mqtt
  12. systemctl restart iotkit-agent

Step 3: The Hardest Part... the CODE

Please try to analyze it and understand what each line is doing, that is how you can learn the best.

#include  // include IoTkit.h to use the Intel IoT Kit
#include // must be included to use IoTkit #include #include // create an object of the IoTkit class IoTkit iotkit; const int relayPin = 2; void setup() { Serial.begin(115200); // call begin on the IoTkit object before calling any other methods iotkit.begin(); delay(10500); pinMode(relayPin, OUTPUT); } void loop() { iotkit.receive(callback); delay(5000); } void callback(char* json) { Serial.println(json); aJsonObject* parsed = aJson.parse(json); if (&parsed == NULL) { // invalid or empty JSON // Serial.println("recieved invalid JSON"); // for troubleshooting return; //parsing JSON } aJsonObject* component = aJson.getObjectItem(parsed, "component"); aJsonObject* command = aJson.getObjectItem(parsed, "command"); aJsonObject* argv = aJson.getObjectItem(parsed, "argv"); aJsonObject* argvArray = argv->child; aJsonObject* name = argvArray->child; // name : on aJsonObject* value = name->next; // value: 1/0 if ((component != NULL)) { if (strcmp(component->valuestring, "power") == 0) { if ((command != NULL)) { if (strcmp(command->valuestring, "LED.v1.0") == 0 && strcmp(value->valuestring, "0") == 0) { Serial.println("Light Off!"); digitalWrite(relayPin, false); } if (strcmp(command->valuestring, "LED.v1.0") == 0 && strcmp(value->valuestring, "1") == 0) { Serial.println("Light on!"); digitalWrite(relayPin, true); } } } } }

Step 4: The Easy Part, Controlling Relay Through IoT Analytics Dashboard

If you did all the previous steps correctly you should be ready to control the relay through IoT Analytics Dashboard!

  1. Go to Control module in IoT AD.
  2. Select your device and Component.
  3. Set parameter value to "1" and press "add action"
  4. Press "Send" next to your newly created action.

In few seconds you should hear your relay "click" and it's LED will start glowing.

You can connect some device you want to control thorough cloud to this relay.