Skip to content

Commit 6099cf6

Browse files
authored
Merge pull request #15 from grandeurtech/development
New architecture + datastore
2 parents 7d64b9f + bb7a364 commit 6099cf6

33 files changed

+1204
-4659
lines changed

README.md

Lines changed: 250 additions & 224 deletions
Large diffs are not rendered by default.

examples/CrossListening/CrossListening.ino

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,16 @@
2323
#include <ESP8266WiFi.h>
2424

2525
// Device's connection configurations
26-
String deviceID = "YOUR-DEVICE-ID";
2726
String apiKey = "YOUR-PROJECT-APIKEY";
27+
String deviceID = "YOUR-DEVICE-ID";
2828
String token = "YOUR-ACCESS-TOKEN";
2929
String ssid = "YOUR-WIFI-SSID";
3030
String passphrase = "YOUR-WIFI-PASSWORD";
3131

3232
// Declaring and initializing other variables
3333
unsigned long current = millis();
34-
ApolloDevice device;
34+
Project myProject;
35+
Device myDevice;
3536
WiFiEventHandler onWiFiConnectedHandler;
3637
WiFiEventHandler onWiFiDisconnectedHandler;
3738
int statePin = D0;
@@ -40,30 +41,32 @@ int voltagePin = A0;
4041
// Function prototypes
4142
void setupWiFi(void);
4243
void connectionCallback(JSONObject updateObject);
43-
void initializeState(JSONObject payload);
44+
void initializeState(JSONObject getResult);
4445
void parmsUpdatedCallback(JSONObject updatedParms);
45-
void summarySetCallback(JSONObject payload);
46-
void parmsSetCallback(JSONObject payload);
46+
void summarySetCallback(JSONObject setResult);
47+
void parmsSetCallback(JSONObject setResult);
4748

4849

4950
void setup() {
5051
Serial.begin(9600);
5152
// This sets up the device WiFi.
5253
setupWiFi();
53-
// This initializes the SDK's configurations and returns a new object of ApolloDevice class.
54-
device = apollo.init(deviceID, apiKey, token);
55-
// This schedules the connectionCallback() function to be called when the device makes/breaks
56-
// connection with the cloud.
57-
device.onConnection(connectionCallback);
58-
// This schedules parmsUpdatedCallback() function to be called when someone changes any
59-
// variable stored in device's parms on the Cloud.
60-
device.onParmsUpdated(parmsUpdatedCallback);
54+
// This initializes the SDK's configurations and returns a new object of Project class.
55+
myProject = apollo.init(apiKey, token);
56+
// Getting object of Device class.
57+
myDevice = myProject.device(deviceID);
58+
// This schedules the connectionCallback() function to be called when connection with the cloud
59+
// is made/broken.
60+
myProject.onConnection(connectionCallback);
61+
// This schedules parmsUpdatedCallback() function to be called when variable stored
62+
// in device's parms are changed on the Cloud.
63+
myDevice.onParms(parmsUpdatedCallback);
6164
}
6265

6366
void loop() {
6467
// In this loop() function, after every five seconds, we send the updated values of our
6568
// device's voltage and state to the Cloud.
66-
if(device.getState() == CONNECTED) {
69+
if(myProject.isConnected()) {
6770
if(millis() - current >= 5000) {
6871
// This if-condition makes sure that the code inside this block runs only after
6972
// every five seconds.
@@ -73,14 +76,14 @@ void loop() {
7376
summary["voltage"] = analogRead(voltagePin);
7477
// This updates the summary of our device on the Cloud and schedules summarySetCallback()
7578
// function to be called when the Cloud responds with the SUMMARY UPDATED message.
76-
device.setSummary(summary, summarySetCallback);
79+
myDevice.setSummary(summary, summarySetCallback);
7780

7881
Serial.println("Setting Parms");
7982
JSONObject parms;
8083
parms["state"] = digitalRead(statePin);
8184
// This updates the parms of our device on the Cloud and schedules parmsSetCallback()
8285
// function to be called when the Cloud responds with the PARMS UPDATED message.
83-
device.setParms(parms, parmsSetCallback);
86+
myDevice.setParms(parms, parmsSetCallback);
8487

8588
// This updates the millis counter for
8689
// the five seconds scheduler.
@@ -89,7 +92,7 @@ void loop() {
8992
}
9093

9194
// This runs the SDK only when the WiFi is connected.
92-
device.loop(WiFi.status() == WL_CONNECTED);
95+
myProject.loop(WiFi.status() == WL_CONNECTED);
9396
}
9497

9598
void setupWiFi(void) {
@@ -112,14 +115,15 @@ void setupWiFi(void) {
112115
Serial.printf("\nDevice is connecting to WiFi using SSID %s and Passphrase %s.\n", ssid.c_str(), passphrase.c_str());
113116
}
114117

115-
void connectionCallback(JSONObject updateObject) {
116-
switch((int) updateObject["event"]) {
118+
void connectionCallback(bool state) {
119+
switch(state) {
117120
case CONNECTED:
118121
// On successful connection with the cloud, we initialize the device's *state*.
119-
// We set the *state pin* to the value of *state* that we receive from the cloud in parms.
122+
// To do that, we get device parms from the cloud and set the *state pin* to the
123+
// value of *state* in those parms.
120124
Serial.println("Device is connected to the cloud.");
121-
device.getParms(initializeState);
122-
125+
myDevice.getParms(initializeState);
126+
Serial.println("Listening for parms update from the cloud...");
123127
// Initializing the millis counter for the five
124128
// seconds timer.
125129
current = millis();
@@ -130,11 +134,11 @@ void connectionCallback(JSONObject updateObject) {
130134
}
131135
}
132136

133-
void initializeState(JSONObject payload) {
137+
void initializeState(JSONObject getResult) {
134138
// This function sets the *state pin* to the *state value* that we received in parms
135139
// from the cloud.
136-
if(payload["code"] == "DEVICE-PARMS-FETCHED") {
137-
int state = payload["deviceParms"]["state"];
140+
if(getResult["code"] == "DEVICE-PARMS-FETCHED") {
141+
int state = getResult["deviceParms"]["state"];
138142
digitalWrite(statePin, state);
139143
return;
140144
}
@@ -150,9 +154,9 @@ void parmsUpdatedCallback(JSONObject updatedParms) {
150154
digitalWrite(statePin, (bool) updatedParms["state"]);
151155
}
152156

153-
void summarySetCallback(JSONObject payload) {
154-
if(payload["code"] == "DEVICE-SUMMARY-UPDATED") {
155-
Serial.printf("Voltage is updated to: %d\n", (int) payload["update"]["voltage"]);
157+
void summarySetCallback(JSONObject setResult) {
158+
if(setResult["code"] == "DEVICE-SUMMARY-UPDATED") {
159+
Serial.printf("Voltage is updated to: %d\n", (int) setResult["update"]["voltage"]);
156160

157161
/* You can set some pins or trigger events here which depend on successful
158162
** device summary update.
@@ -164,9 +168,9 @@ void summarySetCallback(JSONObject payload) {
164168
return;
165169
}
166170

167-
void parmsSetCallback(JSONObject payload) {
168-
if(payload["code"] == "DEVICE-PARMS-UPDATED") {
169-
Serial.printf("State is updated to: %d\n", (bool) payload["update"]["state"]);
171+
void parmsSetCallback(JSONObject setResult) {
172+
if(setResult["code"] == "DEVICE-PARMS-UPDATED") {
173+
Serial.printf("State is updated to: %d\n", (bool) setResult["update"]["state"]);
170174

171175
/* You can set some pins or trigger events here which depend on successful
172176
** device parms update.

examples/DashListening-App/DashListening-App.ino

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,42 +19,45 @@
1919
#include <ESP8266WiFi.h>
2020

2121
// Device's connection configurations
22-
String deviceID = "YOUR-DEVICE-ID";
2322
String apiKey = "YOUR-PROJECT-APIKEY";
23+
String deviceID = "YOUR-DEVICE-ID";
2424
String token = "YOUR-ACCESS-TOKEN";
2525
String ssid = "YOUR-WIFI-SSID";
2626
String passphrase = "YOUR-WIFI-PASSWORD";
2727

28-
// Declaring and initializing other variables
28+
/// Declaring and initializing other variables
2929
unsigned long current = millis();
30-
ApolloDevice device;
30+
Project myProject;
31+
Device myDevice;
3132
WiFiEventHandler onWiFiConnectedHandler;
3233
WiFiEventHandler onWiFiDisconnectedHandler;
3334
int statePin = D0;
3435
int voltagePin = A0;
3536

3637
// Function prototypes
3738
void setupWiFi(void);
38-
void connectionCallback(JSONObject updateObject);
39-
void initializeState(JSONObject payload);
40-
void summarySetCallback(JSONObject payload);
41-
void parmsSetCallback(JSONObject payload);
39+
void connectionCallback(bool state);
40+
void initializeState(JSONObject getResult);
41+
void summarySetCallback(JSONObject setResult);
42+
void parmsSetCallback(JSONObject setResult);
4243

4344
void setup() {
4445
Serial.begin(9600);
4546
// This sets up the device WiFi.
4647
setupWiFi();
47-
// This initializes the SDK's configurations and returns a new object of ApolloDevice class.
48-
device = apollo.init(deviceID, apiKey, token);
49-
// This schedules the connectionCallback() function to be called when the device makes/breaks
50-
// connection with the cloud.
51-
device.onConnection(connectionCallback);
48+
// This initializes the SDK's configurations and returns a new object of Project class.
49+
myProject = apollo.init(apiKey, token);
50+
// Getting object of Device class.
51+
myDevice = myProject.device(deviceID);
52+
// This schedules the connectionCallback() function to be called when connection with the cloud
53+
// is made/broken.
54+
myProject.onConnection(connectionCallback);
5255
}
5356

5457
void loop() {
5558
// In this loop() function, after every five seconds, we send the updated values of our
5659
// device's voltage and state to the Cloud.
57-
if(device.getState() == CONNECTED) {
60+
if(myProject.isConnected()) {
5861
if(millis() - current >= 5000) {
5962
// This if-condition makes sure that the code inside this block runs only after
6063
// every five seconds.
@@ -64,14 +67,14 @@ void loop() {
6467
summary["voltage"] = analogRead(voltagePin);
6568
// This updates the summary of our device on the Cloud and schedules summarySetCallback()
6669
// function to be called when the Cloud responds with the SUMMARY UPDATED message.
67-
device.setSummary(summary, summarySetCallback);
70+
myDevice.setSummary(summary, summarySetCallback);
6871

6972
Serial.println("Setting Parms");
7073
JSONObject parms;
7174
parms["state"] = digitalRead(statePin);
7275
// This updates the parms of our device on the Cloud and schedules parmsSetCallback()
7376
// function to be called when the Cloud responds with the PARMS UPDATED message.
74-
device.setParms(parms, parmsSetCallback);
77+
myDevice.setParms(parms, parmsSetCallback);
7578

7679
// This updates the millis counter for
7780
// the five seconds scheduler.
@@ -80,7 +83,7 @@ void loop() {
8083
}
8184

8285
// This runs the SDK only when the WiFi is connected.
83-
device.loop(WiFi.status() == WL_CONNECTED);
86+
myProject.loop(WiFi.status() == WL_CONNECTED);
8487
}
8588

8689
void setupWiFi(void) {
@@ -103,13 +106,13 @@ void setupWiFi(void) {
103106
Serial.printf("\nDevice is connecting to WiFi using SSID %s and Passphrase %s.\n", ssid.c_str(), passphrase.c_str());
104107
}
105108

106-
void connectionCallback(JSONObject updateObject) {
107-
switch((int) updateObject["event"]) {
109+
void connectionCallback(bool state) {
110+
switch(state) {
108111
case CONNECTED:
109112
// On successful connection with the cloud, we initialize the device's *state*.
110-
// We set the *state pin* to the value of *state* that we receive from the cloud in parms.
113+
// To do that, we get device parms from the cloud and set the *state pin* to the value of *state* in those parms.
111114
Serial.println("Device is connected to the cloud.");
112-
device.getParms(initializeState);
115+
myDevice.getParms(initializeState);
113116

114117
// Initializing the millis counter for the five
115118
// seconds timer.
@@ -121,11 +124,11 @@ void connectionCallback(JSONObject updateObject) {
121124
}
122125
}
123126

124-
void initializeState(JSONObject payload) {
127+
void initializeState(JSONObject getResult) {
125128
// This function sets the *state pin* to the *state value* that we received in parms
126129
// from the cloud.
127-
if(payload["code"] == "DEVICE-PARMS-FETCHED") {
128-
int state = payload["deviceParms"]["state"];
130+
if(getResult["code"] == "DEVICE-PARMS-FETCHED") {
131+
int state = getResult["deviceParms"]["state"];
129132
digitalWrite(statePin, state);
130133
return;
131134
}
@@ -134,9 +137,9 @@ void initializeState(JSONObject payload) {
134137
return;
135138
}
136139

137-
void summarySetCallback(JSONObject payload) {
138-
if(payload["code"] == "DEVICE-SUMMARY-UPDATED") {
139-
Serial.printf("Voltage is updated to: %d\n", (int) payload["update"]["voltage"]);
140+
void summarySetCallback(JSONObject setResult) {
141+
if(setResult["code"] == "DEVICE-SUMMARY-UPDATED") {
142+
Serial.printf("Voltage is updated to: %d\n", (int) setResult["update"]["voltage"]);
140143

141144
/* You can set some pins or trigger events here which depend on successful
142145
** device summary update.
@@ -148,9 +151,9 @@ void summarySetCallback(JSONObject payload) {
148151
return;
149152
}
150153

151-
void parmsSetCallback(JSONObject payload) {
152-
if(payload["code"] == "DEVICE-PARMS-UPDATED") {
153-
Serial.printf("State is updated to: %d\n", (bool) payload["update"]["state"]);
154+
void parmsSetCallback(JSONObject setResult) {
155+
if(setResult["code"] == "DEVICE-PARMS-UPDATED") {
156+
Serial.printf("State is updated to: %d\n", (bool) setResult["update"]["state"]);
154157

155158
/* You can set some pins or trigger events here which depend on successful
156159
** device parms update.

examples/DashListening-Device/DashListening-Device.ino

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,41 +19,44 @@
1919
#include <ESP8266WiFi.h>
2020

2121
// Device's connection configurations
22-
String deviceID = "YOUR-DEVICE-ID";
2322
String apiKey = "YOUR-PROJECT-APIKEY";
23+
String deviceID = "YOUR-DEVICE-ID";
2424
String token = "YOUR-ACCESS-TOKEN";
2525
String ssid = "YOUR-WIFI-SSID";
2626
String passphrase = "YOUR-WIFI-PASSWORD";
2727

2828
// Declaring and initializing other variables
29-
ApolloDevice device;
29+
Project myProject;
30+
Device myDevice;
3031
WiFiEventHandler onWiFiConnectedHandler;
3132
WiFiEventHandler onWiFiDisconnectedHandler;
3233
int statePin = D0;
3334

3435
// Function prototypes
3536
void setupWiFi(void);
36-
void connectionCallback(JSONObject updateObject);
37-
void initializeState(JSONObject payload);
37+
void connectionCallback(bool state);
38+
void initializeState(JSONObject getResult);
3839
void parmsUpdatedCallback(JSONObject updatedParms);
3940

4041
void setup() {
4142
Serial.begin(9600);
4243
// This sets up the device WiFi.
4344
setupWiFi();
4445
// This initializes the SDK's configurations and returns a new object of ApolloDevice class.
45-
device = apollo.init(deviceID, apiKey, token);
46-
// This schedules the connectionCallback() function to be called when the device makes/breaks
47-
// connection with the cloud.
48-
device.onConnection(connectionCallback);
49-
// This schedules parmsUpdatedCallback() function to be called when someone changes any
50-
// variable stored in device's parms on the Cloud.
51-
device.onParmsUpdated(parmsUpdatedCallback);
46+
myProject = apollo.init(apiKey, token);
47+
// Getting object of Device class.
48+
myDevice = myProject.device(deviceID);
49+
// This schedules the connectionCallback() function to be called when connection with the cloud
50+
// is made/broken.
51+
myProject.onConnection(connectionCallback);
52+
// This schedules parmsUpdatedCallback() function to be called when variable stored
53+
// in device's parms are changed on the Cloud.
54+
myDevice.onParms(parmsUpdatedCallback);
5255
}
5356

5457
void loop() {
5558
// The SDK only runs when the WiFi is connected.
56-
device.loop(WiFi.status() == WL_CONNECTED);
59+
myProject.loop(WiFi.status() == WL_CONNECTED);
5760
}
5861

5962
void setupWiFi(void) {
@@ -76,25 +79,27 @@ void setupWiFi(void) {
7679
Serial.printf("\nDevice is connecting to WiFi using SSID %s and Passphrase %s.\n", ssid.c_str(), passphrase.c_str());
7780
}
7881

79-
void connectionCallback(JSONObject updateObject) {
80-
switch((int) updateObject["event"]) {
82+
void connectionCallback(bool status) {
83+
switch(status) {
8184
case CONNECTED:
8285
// On successful connection with the cloud, we initialize the device's *state*.
83-
// We set the *state pin* to the value of *state* that we receive from the cloud in parms.
86+
// To do that, we get device parms from the cloud and set the *state pin* to the
87+
// value of *state* in those parms.
8488
Serial.println("Device is connected to the cloud.");
85-
device.getParms(initializeState);
89+
myDevice.getParms(initializeState);
90+
Serial.println("Listening for parms update from the cloud...");
8691
break;
8792
case DISCONNECTED:
8893
Serial.println("Device is disconnected from the cloud.");
8994
break;
9095
}
9196
}
9297

93-
void initializeState(JSONObject payload) {
98+
void initializeState(JSONObject getResult) {
9499
// This function sets the *state pin* to the *state value* that we received in parms
95100
// from the cloud.
96-
if(payload["code"] == "DEVICE-PARMS-FETCHED") {
97-
int state = payload["deviceParms"]["state"];
101+
if(getResult["code"] == "DEVICE-PARMS-FETCHED") {
102+
int state = getResult["deviceParms"]["state"];
98103
digitalWrite(statePin, state);
99104
return;
100105
}

0 commit comments

Comments
 (0)