/
IotKitActuationExample.ino
executable file
·92 lines (81 loc) · 3.51 KB
/
IotKitActuationExample.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
Copyright (c) 2015, Intel Corporation
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
//This example reacts for default actuator component if registered on device
//Prequisites:
// 1. Device must be activated
// 2. Agent must be started and configured to use MQTT protocol
// 3. A component named "power" of the default type "powerswitch.v1.0" must be
// registered on the device. The default powerswitch.v1.0 component type has
// a command named "LED.v1.0"
//When executed from Control section on dashboard with value 0, LED light is turned off
//When executed from Control section on dashboard with value 1, LED light is turned on
//iotkit-agent must use MQTT connection (not REST) for actuation to work.
//It also sends 1 as power when agent is started.
#include <IoTkit.h> // include IoTkit.h to use the Intel IoT Kit
#include <Ethernet.h> // must be included to use IoTkit
#include <aJSON.h>
#include <stdio.h>
// create an object of the IoTkit class
IoTkit iotkit;
int temp;
char buf[112];
void setup() {
Serial.begin(115200);
// call begin on the IoTkit object before calling any other methods
iotkit.begin();
delay(10500);
iotkit.send("power", 1);
}
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");
return;
}
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!");
pinMode(13, OUTPUT);
digitalWrite(13, false);
}
if (strcmp(command->valuestring, "LED.v1.0") == 0 && strcmp(value->valuestring, "1") == 0) {
Serial.println("Light on!");
pinMode(13, OUTPUT);
digitalWrite(13, true);
}
}
}
}
}