Skip to content

Commit 6d77d32

Browse files
authored
Merge pull request #23 from grandeurtech/feature-trex
This feature introduces the following changes: Added a dynamic type Var. Async functions now accept callback of any type not only void(*)(JSONObject). Message queuing when the device is not connected with Grandeur is improved.
2 parents cd155cd + b61beea commit 6d77d32

File tree

29 files changed

+1632
-950
lines changed

29 files changed

+1632
-950
lines changed

README.md

Lines changed: 149 additions & 317 deletions
Large diffs are not rendered by default.

examples/CrossListening/CrossListening-esp32/CrossListening-esp32.ino

Lines changed: 52 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -29,60 +29,50 @@ String token = "YOUR-ACCESS-TOKEN";
2929
const char* ssid = "YOUR-WIFI-SSID";
3030
const char* passphrase = "YOUR-WIFI-PASSWORD";
3131

32-
// Declaring and initializing other variables
32+
// Declaring and initializing other Variables
3333
unsigned long current = millis();
34-
Project myProject;
35-
Device myDevice;
34+
Project project;
35+
Data data;
3636
int statePin = 4;
3737
int voltagePin = 2;
3838

3939
// Function prototypes
4040
void WiFiEventCallback(WiFiEvent_t event);
4141
void setupWiFi(void);
42-
void connectionCallback(JSONObject updateObject);
43-
void initializeState(JSONObject getResult);
44-
void parmsUpdatedCallback(JSONObject updatedParms);
45-
void summarySetCallback(JSONObject setResult);
46-
void parmsSetCallback(JSONObject setResult);
47-
42+
void connectionCallback(bool state);
43+
void initializeState(Var getResult);
44+
void stateUpdatedCallback(bool state, const char* path);
45+
void voltageSetCallback(Var setResult);
4846

4947
void setup() {
5048
Serial.begin(9600);
5149
// This sets up the device WiFi.
5250
setupWiFi();
53-
// This initializes the SDK's configurations and returns a new object of Project class.
54-
myProject = grandeur.init(apiKey, token);
55-
// Getting object of Device class.
56-
myDevice = myProject.device(deviceID);
57-
// This schedules the connectionCallback() function to be called when connection with the cloud
51+
// This initializes the SDK's configurations and returns reference to your project.
52+
project = grandeur.init(apiKey, token);
53+
// Getting reference to your device.
54+
data = project.device(deviceID).data();
55+
// This schedules the connectionCallback() function to be called when connection with Grandeur
5856
// is made/broken.
59-
myProject.onConnection(connectionCallback);
60-
// This schedules parmsUpdatedCallback() function to be called when variable stored
61-
// in device's parms are changed on Grandeur.
62-
myDevice.onParms(parmsUpdatedCallback);
57+
project.onConnection(connectionCallback);
58+
// This schedules stateUpdatedCallback() function to be called when the device state is
59+
// changed on Grandeur.
60+
data.on("state", stateUpdatedCallback);
6361
}
6462

6563
void loop() {
6664
// In this loop() function, after every five seconds, we send the updated values of our
6765
// device's voltage and state to Grandeur.
68-
if(myProject.isConnected()) {
66+
if(project.isConnected()) {
6967
if(millis() - current >= 5000) {
7068
// This if-condition makes sure that the code inside this block runs only after
7169
// every five seconds.
7270

73-
Serial.println("Setting Summary");
74-
JSONObject summary;
75-
summary["voltage"] = analogRead(voltagePin);
76-
// This updates the summary of our device on Grandeur and schedules summarySetCallback()
77-
// function to be called when Grandeur responds with the SUMMARY UPDATED message.
78-
myDevice.setSummary(summary, summarySetCallback);
79-
80-
Serial.println("Setting Parms");
81-
JSONObject parms;
82-
parms["state"] = digitalRead(statePin);
83-
// This updates the parms of our device on Grandeur and schedules parmsSetCallback()
84-
// function to be called when Grandeur responds with the PARMS UPDATED message.
85-
myDevice.setParms(parms, parmsSetCallback);
71+
Serial.println("Setting Voltage");
72+
int voltage = analogRead(voltagePin);
73+
// This updates the voltage of our device on Grandeur and schedules voltageSetCallback()
74+
// function to be called when Grandeur responds with the DATA UPDATED message.
75+
data.set("voltage", voltage, voltageSetCallback);
8676

8777
// This updates the millis counter for
8878
// the five seconds scheduler.
@@ -91,7 +81,7 @@ void loop() {
9181
}
9282

9383
// This runs the SDK only when the WiFi is connected.
94-
myProject.loop(WiFi.status() == WL_CONNECTED);
84+
project.loop(WiFi.status() == WL_CONNECTED);
9585
}
9686

9787
void WiFiEventCallback(WiFiEvent_t event) {
@@ -121,69 +111,56 @@ void setupWiFi(void) {
121111
Serial.printf("\nDevice is connecting to WiFi using SSID %s and Passphrase %s.\n", ssid, passphrase);
122112
}
123113

124-
void connectionCallback(bool state) {
125-
switch(state) {
114+
void connectionCallback(bool status) {
115+
switch(status) {
126116
case CONNECTED:
127-
// On successful connection with the cloud, we initialize the device's *state*.
128-
// To do that, we get device parms from the cloud and set the *state pin* to the
129-
// value of *state* in those parms.
130-
Serial.println("Device is connected to the cloud.");
131-
myDevice.getParms(initializeState);
132-
Serial.println("Listening for parms update from the cloud...");
117+
// On successful connection with Grandeur, we initialize the device's *state*.
118+
// To do that, we get device state from Grandeur and set the *state pin* to its
119+
// value.
120+
Serial.println("Device is connected with Grandeur.");
121+
data.get("state", initializeState);
122+
Serial.println("Listening for state update from Grandeur...");
133123
// Initializing the millis counter for the five
134124
// seconds timer.
135125
current = millis();
136126
break;
137127
case DISCONNECTED:
138-
Serial.println("Device is disconnected from the cloud.");
128+
Serial.println("Device's connection with Grandeur is broken.");
139129
break;
140130
}
141131
}
142132

143-
void initializeState(JSONObject getResult) {
144-
// This function sets the *state pin* to the *state value* that we received in parms
145-
// from the cloud.
146-
if(getResult["code"] == "DEVICE-PARMS-FETCHED") {
147-
int state = getResult["deviceParms"]["state"];
133+
void initializeState(Var getResult) {
134+
// This function sets the *state pin* to the *state value* that we received in data
135+
// from Grandeur.
136+
if(getResult["code"] == "DEVICE-DATA-FETCHED") {
137+
int state = getResult["data"];
138+
Serial.printf("State is: %d\n", state);
148139
digitalWrite(statePin, state);
149140
return;
150141
}
151-
// If the parms could not be fetched.
152-
Serial.println("Failed to Fetch Parms");
142+
// If the data could not be fetched.
143+
Serial.println("Failed to Fetch State");
153144
return;
154145
}
155146

156-
void parmsUpdatedCallback(JSONObject updatedParms) {
157-
// This function gets the *updated state* from the device parms and set the *state pin*
158-
// with *state value*.
159-
Serial.printf("Updated State is: %d\n", (bool) updatedParms["state"]);
160-
digitalWrite(statePin, (bool) updatedParms["state"]);
147+
void stateUpdatedCallback(bool state, const char* path) {
148+
// This function gets the *updated state* from Grandeur and set the *state pin*
149+
// with its value.
150+
Serial.printf("Updated State is: %d\n", state);
151+
digitalWrite(statePin, state);
161152
}
162153

163-
void summarySetCallback(JSONObject setResult) {
164-
if(setResult["code"] == "DEVICE-SUMMARY-UPDATED") {
165-
Serial.printf("Voltage is updated to: %d\n", (int) setResult["update"]["voltage"]);
154+
void voltageSetCallback(Var setResult) {
155+
if(setResult["code"] == "DEVICE-DATA-UPDATED") {
156+
Serial.printf("Voltage is updated to: %d\n", (int) setResult["update"]);
166157

167158
/* You can set some pins or trigger events here which depend on successful
168-
** device summary update.
159+
** voltage update.
169160
*/
170161
return;
171162
}
172-
// If the summary could not be updated.
173-
Serial.println("Failed to Update Summary");
163+
// If the voltage could not be updated.
164+
Serial.println("Failed to Update Voltage");
174165
return;
175-
}
176-
177-
void parmsSetCallback(JSONObject setResult) {
178-
if(setResult["code"] == "DEVICE-PARMS-UPDATED") {
179-
Serial.printf("State is updated to: %d\n", (bool) setResult["update"]["state"]);
180-
181-
/* You can set some pins or trigger events here which depend on successful
182-
** device parms update.
183-
*/
184-
return;
185-
}
186-
// If the parms could not be updated.
187-
Serial.println("Failed to Update Parms");
188-
return;
189-
}
166+
}

examples/CrossListening/CrossListening-esp8266/CrossListening-esp8266.ino

Lines changed: 48 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -40,50 +40,40 @@ int voltagePin = A0;
4040

4141
// Function prototypes
4242
void setupWiFi(void);
43-
void connectionCallback(JSONObject updateObject);
44-
void initializeState(JSONObject getResult);
45-
void parmsUpdatedCallback(JSONObject updatedParms);
46-
void summarySetCallback(JSONObject setResult);
47-
void parmsSetCallback(JSONObject setResult);
48-
43+
void connectionCallback(bool state);
44+
void initializeState(Var getResult);
45+
void stateUpdatedCallback(bool state, const char* path);
46+
void voltageSetCallback(Var setResult);
4947

5048
void setup() {
5149
Serial.begin(9600);
5250
// This sets up the device WiFi.
5351
setupWiFi();
54-
// This initializes the SDK's configurations and returns a new object of Project class.
55-
myProject = grandeur.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
52+
// This initializes the SDK's configurations and returns reference to your project.
53+
project = grandeur.init(apiKey, token);
54+
// Getting reference to your device.
55+
data = project.device(deviceID).data();
56+
// This schedules the connectionCallback() function to be called when connection with Grandeur
5957
// 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 Grandeur.
63-
myDevice.onParms(parmsUpdatedCallback);
58+
project.onConnection(connectionCallback);
59+
// This schedules stateUpdatedCallback() function to be called when the device state is
60+
// changed on Grandeur.
61+
data.on("state", stateUpdatedCallback);
6462
}
6563

6664
void loop() {
6765
// In this loop() function, after every five seconds, we send the updated values of our
6866
// device's voltage and state to Grandeur.
69-
if(myProject.isConnected()) {
67+
if(project.isConnected()) {
7068
if(millis() - current >= 5000) {
7169
// This if-condition makes sure that the code inside this block runs only after
7270
// every five seconds.
7371

74-
Serial.println("Setting Summary");
75-
JSONObject summary;
76-
summary["voltage"] = analogRead(voltagePin);
77-
// This updates the summary of our device on Grandeur and schedules summarySetCallback()
78-
// function to be called when Grandeur responds with the SUMMARY UPDATED message.
79-
myDevice.setSummary(summary, summarySetCallback);
80-
81-
Serial.println("Setting Parms");
82-
JSONObject parms;
83-
parms["state"] = digitalRead(statePin);
84-
// This updates the parms of our device on Grandeur and schedules parmsSetCallback()
85-
// function to be called when Grandeur responds with the PARMS UPDATED message.
86-
myDevice.setParms(parms, parmsSetCallback);
72+
Serial.println("Setting Voltage");
73+
int voltage = analogRead(voltagePin);
74+
// This updates the voltage of our device on Grandeur and schedules voltageSetCallback()
75+
// function to be called when Grandeur responds with the DATA UPDATED message.
76+
data.set("voltage", voltage, voltageSetCallback);
8777

8878
// This updates the millis counter for
8979
// the five seconds scheduler.
@@ -92,7 +82,7 @@ void loop() {
9282
}
9383

9484
// This runs the SDK only when the WiFi is connected.
95-
myProject.loop(WiFi.status() == WL_CONNECTED);
85+
project.loop(WiFi.status() == WL_CONNECTED);
9686
}
9787

9888
void setupWiFi(void) {
@@ -115,69 +105,56 @@ void setupWiFi(void) {
115105
Serial.printf("\nDevice is connecting to WiFi using SSID %s and Passphrase %s.\n", ssid.c_str(), passphrase.c_str());
116106
}
117107

118-
void connectionCallback(bool state) {
119-
switch(state) {
108+
void connectionCallback(bool status) {
109+
switch(status) {
120110
case CONNECTED:
121-
// On successful connection with the cloud, we initialize the device's *state*.
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.
124-
Serial.println("Device is connected to the cloud.");
125-
myDevice.getParms(initializeState);
126-
Serial.println("Listening for parms update from the cloud...");
111+
// On successful connection with Grandeur, we initialize the device's *state*.
112+
// To do that, we get device state from Grandeur and set the *state pin* to its
113+
// value.
114+
Serial.println("Device is connected with Grandeur.");
115+
data.get("state", initializeState);
116+
Serial.println("Listening for state update from Grandeur...");
127117
// Initializing the millis counter for the five
128118
// seconds timer.
129119
current = millis();
130120
break;
131121
case DISCONNECTED:
132-
Serial.println("Device is disconnected from the cloud.");
122+
Serial.println("Device's connection with Grandeur is broken.");
133123
break;
134124
}
135125
}
136126

137-
void initializeState(JSONObject getResult) {
138-
// This function sets the *state pin* to the *state value* that we received in parms
139-
// from the cloud.
140-
if(getResult["code"] == "DEVICE-PARMS-FETCHED") {
141-
int state = getResult["deviceParms"]["state"];
127+
void initializeState(Var getResult) {
128+
// This function sets the *state pin* to the *state value* that we received in data
129+
// from Grandeur.
130+
if(getResult["code"] == "DEVICE-DATA-FETCHED") {
131+
int state = getResult["data"];
132+
Serial.printf("State is: %d\n", state);
142133
digitalWrite(statePin, state);
143134
return;
144135
}
145-
// If the parms could not be fetched.
146-
Serial.println("Failed to Fetch Parms");
136+
// If the data could not be fetched.
137+
Serial.println("Failed to Fetch State");
147138
return;
148139
}
149140

150-
void parmsUpdatedCallback(JSONObject updatedParms) {
151-
// This function gets the *updated state* from the device parms and set the *state pin*
152-
// with *state value*.
153-
Serial.printf("Updated State is: %d\n", (bool) updatedParms["state"]);
154-
digitalWrite(statePin, (bool) updatedParms["state"]);
141+
void stateUpdatedCallback(bool state, const char* path) {
142+
// This function gets the *updated state* from Grandeur and set the *state pin*
143+
// with its value.
144+
Serial.printf("Updated State is: %d\n", state);
145+
digitalWrite(statePin, state);
155146
}
156147

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"]);
148+
void voltageSetCallback(Var setResult) {
149+
if(setResult["code"] == "DEVICE-DATA-UPDATED") {
150+
Serial.printf("Voltage is updated to: %d\n", (int) setResult["update"]);
160151

161152
/* You can set some pins or trigger events here which depend on successful
162-
** device summary update.
163-
*/
164-
return;
165-
}
166-
// If the summary could not be updated.
167-
Serial.println("Failed to Update Summary");
168-
return;
169-
}
170-
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"]);
174-
175-
/* You can set some pins or trigger events here which depend on successful
176-
** device parms update.
153+
** voltage update.
177154
*/
178155
return;
179156
}
180-
// If the parms could not be updated.
181-
Serial.println("Failed to Update Parms");
157+
// If the voltage could not be updated.
158+
Serial.println("Failed to Update Voltage");
182159
return;
183160
}

0 commit comments

Comments
 (0)