Skip to content

Commit 6dad3b1

Browse files
authored
Merge pull request #26 from grandeurtech/feature-trex
Updated event emitter, class hierarchies, enhancement to device.data().get() and device.data().set(), and support for Var and Callback in examples.
2 parents 6d77d32 + 1f98e0b commit 6dad3b1

File tree

39 files changed

+1376
-1931
lines changed

39 files changed

+1376
-1931
lines changed

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

Lines changed: 56 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/**
22
* @file CrossListening-esp32.ino
3-
* @date 24.03.2020
3+
* @date 21.02.2021
44
* @author Grandeur Technologies
55
*
6-
* Copyright (c) 2019 Grandeur Technologies LLP. All rights reserved.
6+
* Copyright (c) 2021 Grandeur Technologies Inc. All rights reserved.
77
* This file is part of the Arduino SDK for Grandeur.
88
*
99
* Grandeur.h is used for device's communication with Grandeur.
@@ -13,70 +13,78 @@
1313
* listens for updates from the device.
1414
* This example illustrates pretty much every basic thing you'd need in order to monitor /
1515
* control your device through Grandeur. Here are some of those:
16-
* 1. Listen to the cloud for updates in parms variables.
17-
* 2. Publish updates in summary and parms variables to the cloud every 5 seconds.
18-
* 3. Controlling SDK's internal valve. This helps if you want to run the SDK only when a
19-
* certain condition is true; in our case, if the WiFi is connected.
16+
* 1. Listen to Grandeur for updates in device variables.
17+
* 2. Publish updates in variables to Grandeur every 5 seconds.
18+
* 3. Running the SDK only when a certain condition is true; in our case, if the WiFi is connected.
19+
*
20+
* After uploading this sketch to your ESP, go to https://canvas.grandeur.tech and add a button and
21+
* a display to control state and monitor voltage variable, respectively.
2022
*/
2123

2224
#include <Grandeur.h>
2325
#include <WiFi.h>
2426

25-
// Device's connection configurations
27+
// Device's connection configurations:
2628
String apiKey = "YOUR-PROJECT-APIKEY";
2729
String deviceID = "YOUR-DEVICE-ID";
2830
String token = "YOUR-ACCESS-TOKEN";
2931
const char* ssid = "YOUR-WIFI-SSID";
3032
const char* passphrase = "YOUR-WIFI-PASSWORD";
3133

32-
// Declaring and initializing other Variables
33-
unsigned long current = millis();
34-
Project project;
35-
Data data;
34+
// Handles our 5 second timer in loop().
35+
unsigned long currentTime = millis();
36+
// Object of Grandeur project.
37+
Grandeur::Project project;
38+
// Device data object to get/set/subscribe to device variables.
39+
Grandeur::Project::Device::Data data;
40+
// State and voltage pins to set.
3641
int statePin = 4;
3742
int voltagePin = 2;
3843

39-
// Function prototypes
44+
// FUNCTION PROTOTYPES:
45+
// Handles WiFi connection/disconnection events.
4046
void WiFiEventCallback(WiFiEvent_t event);
41-
void setupWiFi(void);
42-
void connectionCallback(bool state);
43-
void initializeState(Var getResult);
44-
void stateUpdatedCallback(bool state, const char* path);
45-
void voltageSetCallback(Var setResult);
47+
// Starts the device WiFi.
48+
void startWiFi(void);
49+
// Handles Grandeur connection/disconnection events.
50+
void GrandeurConnectionCallback(bool state);
51+
// Data get/set/update callback functions:
52+
void initializeStatePin(const char* code, bool state);
53+
void setStatePinToNewValue(const char* path, bool state);
54+
void afterVoltageIsUpdated(const char* code, int voltage);
4655

4756
void setup() {
4857
Serial.begin(9600);
49-
// This sets up the device WiFi.
50-
setupWiFi();
58+
startWiFi();
5159
// This initializes the SDK's configurations and returns reference to your project.
5260
project = grandeur.init(apiKey, token);
53-
// Getting reference to your device.
61+
// Getting object of your device data.
5462
data = project.device(deviceID).data();
55-
// This schedules the connectionCallback() function to be called when connection with Grandeur
63+
// This schedules the GrandeurConnectionCallback() function to be called when connection with Grandeur
5664
// is made/broken.
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);
65+
project.onConnection(GrandeurConnectionCallback);
66+
// This schedules setStatePinToNewValue() function to be called when a change in device state occurs
67+
// on Grandeur.
68+
data.on("state", setStatePinToNewValue);
69+
Serial.println("Listening for state update from Grandeur...");
6170
}
6271

6372
void loop() {
6473
// In this loop() function, after every five seconds, we send the updated values of our
65-
// device's voltage and state to Grandeur.
74+
// device's voltage to Grandeur.
6675
if(project.isConnected()) {
67-
if(millis() - current >= 5000) {
76+
if(millis() - currentTime >= 5000) {
6877
// This if-condition makes sure that the code inside this block runs only after
6978
// every five seconds.
7079

71-
Serial.println("Setting Voltage");
80+
Serial.println("Setting Voltage");
7281
int voltage = analogRead(voltagePin);
73-
// This updates the voltage of our device on Grandeur and schedules voltageSetCallback()
82+
// This updates the voltage of our device on Grandeur and schedules afterVoltageIsUpdated()
7483
// function to be called when Grandeur responds with the DATA UPDATED message.
75-
data.set("voltage", voltage, voltageSetCallback);
84+
data.set("voltage", voltage, afterVoltageIsUpdated);
7685

77-
// This updates the millis counter for
78-
// the five seconds scheduler.
79-
current = millis();
86+
// This updates the current time for the five seconds timer.
87+
currentTime = millis();
8088
}
8189
}
8290

@@ -99,7 +107,7 @@ void WiFiEventCallback(WiFiEvent_t event) {
99107
}
100108
}
101109

102-
void setupWiFi(void) {
110+
void startWiFi(void) {
103111
// Disconnecting WiFi if it"s already connected
104112
WiFi.disconnect();
105113
// Setting it to Station mode which basically scans for nearby WiFi routers
@@ -111,30 +119,29 @@ void setupWiFi(void) {
111119
Serial.printf("\nDevice is connecting to WiFi using SSID %s and Passphrase %s.\n", ssid, passphrase);
112120
}
113121

114-
void connectionCallback(bool status) {
122+
void GrandeurConnectionCallback(bool status) {
115123
switch(status) {
116-
case CONNECTED:
124+
case CONNECTED: // Expands to true.
125+
Serial.println("Device is connected with Grandeur.");
117126
// On successful connection with Grandeur, we initialize the device's *state*.
118127
// To do that, we get device state from Grandeur and set the *state pin* to its
119128
// value.
120-
Serial.println("Device is connected with Grandeur.");
121-
data.get("state", initializeState);
122-
Serial.println("Listening for state update from Grandeur...");
129+
data.get("state", initializeStatePin);
130+
123131
// Initializing the millis counter for the five
124132
// seconds timer.
125-
current = millis();
133+
currentTime = millis();
126134
break;
127-
case DISCONNECTED:
135+
case DISCONNECTED: // Expands to false.
128136
Serial.println("Device's connection with Grandeur is broken.");
129137
break;
130138
}
131139
}
132140

133-
void initializeState(Var getResult) {
141+
void initializeStatePin(const char* code, bool state) {
134142
// This function sets the *state pin* to the *state value* that we received in data
135143
// from Grandeur.
136-
if(getResult["code"] == "DEVICE-DATA-FETCHED") {
137-
int state = getResult["data"];
144+
if(strcmp(code, "DEVICE-DATA-FETCHED") == 0) {
138145
Serial.printf("State is: %d\n", state);
139146
digitalWrite(statePin, state);
140147
return;
@@ -144,16 +151,15 @@ void initializeState(Var getResult) {
144151
return;
145152
}
146153

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.
154+
void setStatePinToNewValue(const char* path, bool state) {
155+
// This function sets the *state pin* to state value.
150156
Serial.printf("Updated State is: %d\n", state);
151157
digitalWrite(statePin, state);
152158
}
153159

154-
void voltageSetCallback(Var setResult) {
155-
if(setResult["code"] == "DEVICE-DATA-UPDATED") {
156-
Serial.printf("Voltage is updated to: %d\n", (int) setResult["update"]);
160+
void afterVoltageIsUpdated(const char* code, int voltage) {
161+
if(strcmp(code, "DEVICE-DATA-UPDATED") == 0) {
162+
Serial.printf("Voltage is updated to: %d\n", voltage);
157163

158164
/* You can set some pins or trigger events here which depend on successful
159165
** voltage update.

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

Lines changed: 59 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/**
22
* @file CrossListening-esp8266.ino
3-
* @date 24.03.2020
3+
* @date 21.02.2021
44
* @author Grandeur Technologies
55
*
6-
* Copyright (c) 2019 Grandeur Technologies LLP. All rights reserved.
6+
* Copyright (c) 2021 Grandeur Technologies Inc. All rights reserved.
77
* This file is part of the Arduino SDK for Grandeur.
88
*
99
* Grandeur.h is used for device's communication with Grandeur.
@@ -13,79 +13,87 @@
1313
* listens for updates from the device.
1414
* This example illustrates pretty much every basic thing you'd need in order to monitor /
1515
* control your device through Grandeur. Here are some of those:
16-
* 1. Listen to the cloud for updates in parms variables.
17-
* 2. Publish updates in summary and parms variables to the cloud every 5 seconds.
18-
* 3. Controlling SDK's internal valve. This helps if you want to run the SDK only when a
19-
* certain condition is true; in our case, if the WiFi is connected.
16+
* 1. Listen to Grandeur for updates in device variables.
17+
* 2. Publish updates in variables to Grandeur every 5 seconds.
18+
* 3. Running the SDK only when a certain condition is true; in our case, if the WiFi is connected.
19+
*
20+
* After uploading this sketch to your ESP, go to https://canvas.grandeur.tech and add a button and
21+
* a display to control state and monitor voltage variable, respectively.
2022
*/
2123

2224
#include <Grandeur.h>
2325
#include <ESP8266WiFi.h>
2426

25-
// Device's connection configurations
27+
// Device's connection configurations:
2628
String apiKey = "YOUR-PROJECT-APIKEY";
2729
String deviceID = "YOUR-DEVICE-ID";
2830
String token = "YOUR-ACCESS-TOKEN";
29-
String ssid = "YOUR-WIFI-SSID";
30-
String passphrase = "YOUR-WIFI-PASSWORD";
31+
const char* ssid = "YOUR-WIFI-SSID";
32+
const char* passphrase = "YOUR-WIFI-PASSWORD";
3133

32-
// Declaring and initializing other variables
33-
unsigned long current = millis();
34-
Project myProject;
35-
Device myDevice;
36-
WiFiEventHandler onWiFiConnectedHandler;
37-
WiFiEventHandler onWiFiDisconnectedHandler;
34+
// Handles our 5 second timer in loop().
35+
unsigned long currentTime = millis();
36+
// Object of Grandeur project.
37+
Grandeur::Project project;
38+
// Device data object to get/set/subscribe to device variables.
39+
Grandeur::Project::Device::Data data;
40+
// State and voltage pins to set.
3841
int statePin = D0;
3942
int voltagePin = A0;
4043

41-
// Function prototypes
42-
void setupWiFi(void);
43-
void connectionCallback(bool state);
44-
void initializeState(Var getResult);
45-
void stateUpdatedCallback(bool state, const char* path);
46-
void voltageSetCallback(Var setResult);
44+
// FUNCTION PROTOTYPES:
45+
// These handle WiFi connection/disconnection events.
46+
WiFiEventHandler onWiFiConnectedHandler;
47+
WiFiEventHandler onWiFiDisconnectedHandler;
48+
// Starts the device WiFi.
49+
void startWiFi(void);
50+
// Handles Grandeur connection/disconnection events.
51+
void GrandeurConnectionCallback(bool state);
52+
// Data get/set/update callback functions:
53+
void initializeStatePin(const char* code, bool state);
54+
void setStatePinToNewValue(const char* path, bool state);
55+
void afterVoltageIsUpdated(const char* code, int voltage);
4756

4857
void setup() {
4958
Serial.begin(9600);
50-
// This sets up the device WiFi.
51-
setupWiFi();
59+
startWiFi();
5260
// This initializes the SDK's configurations and returns reference to your project.
5361
project = grandeur.init(apiKey, token);
54-
// Getting reference to your device.
62+
// Getting object of your device data.
5563
data = project.device(deviceID).data();
5664
// This schedules the connectionCallback() function to be called when connection with Grandeur
5765
// is made/broken.
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);
66+
project.onConnection(GrandeurConnectionCallback);
67+
// This schedules setStatePinToNewValue() function to be called when a change in device state occurs
68+
// on Grandeur.
69+
data.on("state", setStatePinToNewValue);
70+
Serial.println("Listening for state update from Grandeur...");
6271
}
6372

6473
void loop() {
6574
// In this loop() function, after every five seconds, we send the updated values of our
66-
// device's voltage and state to Grandeur.
75+
// device's voltage to Grandeur.
6776
if(project.isConnected()) {
68-
if(millis() - current >= 5000) {
77+
if(millis() - currentTime >= 5000) {
6978
// This if-condition makes sure that the code inside this block runs only after
7079
// every five seconds.
7180

72-
Serial.println("Setting Voltage");
81+
Serial.println("Setting Voltage");
7382
int voltage = analogRead(voltagePin);
74-
// This updates the voltage of our device on Grandeur and schedules voltageSetCallback()
83+
// This updates the voltage of our device on Grandeur and schedules afterVoltageIsUpdated()
7584
// function to be called when Grandeur responds with the DATA UPDATED message.
76-
data.set("voltage", voltage, voltageSetCallback);
85+
data.set("voltage", voltage, afterVoltageIsUpdated);
7786

78-
// This updates the millis counter for
79-
// the five seconds scheduler.
80-
current = millis();
87+
// This updates the current time for the five seconds timer.
88+
currentTime = millis();
8189
}
8290
}
8391

8492
// This runs the SDK only when the WiFi is connected.
8593
project.loop(WiFi.status() == WL_CONNECTED);
8694
}
8795

88-
void setupWiFi(void) {
96+
void startWiFi(void) {
8997
// Disconnecting WiFi if it"s already connected
9098
WiFi.disconnect();
9199
// Setting it to Station mode which basically scans for nearby WiFi routers
@@ -105,30 +113,29 @@ void setupWiFi(void) {
105113
Serial.printf("\nDevice is connecting to WiFi using SSID %s and Passphrase %s.\n", ssid.c_str(), passphrase.c_str());
106114
}
107115

108-
void connectionCallback(bool status) {
116+
void GrandeurConnectionCallback(bool status) {
109117
switch(status) {
110-
case CONNECTED:
118+
case CONNECTED: // Expands to true.
119+
Serial.println("Device is connected with Grandeur.");
111120
// On successful connection with Grandeur, we initialize the device's *state*.
112121
// To do that, we get device state from Grandeur and set the *state pin* to its
113122
// value.
114-
Serial.println("Device is connected with Grandeur.");
115-
data.get("state", initializeState);
116-
Serial.println("Listening for state update from Grandeur...");
123+
data.get("state", initializeStatePin);
124+
117125
// Initializing the millis counter for the five
118126
// seconds timer.
119-
current = millis();
127+
currentTime = millis();
120128
break;
121-
case DISCONNECTED:
129+
case DISCONNECTED: // Expands to false.
122130
Serial.println("Device's connection with Grandeur is broken.");
123131
break;
124132
}
125133
}
126134

127-
void initializeState(Var getResult) {
135+
void initializeStatePin(const char* code, bool state) {
128136
// This function sets the *state pin* to the *state value* that we received in data
129137
// from Grandeur.
130-
if(getResult["code"] == "DEVICE-DATA-FETCHED") {
131-
int state = getResult["data"];
138+
if(strcmp(code, "DEVICE-DATA-FETCHED") == 0) {
132139
Serial.printf("State is: %d\n", state);
133140
digitalWrite(statePin, state);
134141
return;
@@ -138,16 +145,15 @@ void initializeState(Var getResult) {
138145
return;
139146
}
140147

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.
148+
void setStatePinToNewValue(const char* path, bool state) {
149+
// This function sets the *state pin* to state value.
144150
Serial.printf("Updated State is: %d\n", state);
145151
digitalWrite(statePin, state);
146152
}
147153

148-
void voltageSetCallback(Var setResult) {
149-
if(setResult["code"] == "DEVICE-DATA-UPDATED") {
150-
Serial.printf("Voltage is updated to: %d\n", (int) setResult["update"]);
154+
void afterVoltageIsUpdated(const char* code, int voltage) {
155+
if(strcmp(code, "DEVICE-DATA-UPDATED") == 0) {
156+
Serial.printf("Voltage is updated to: %d\n", voltage);
151157

152158
/* You can set some pins or trigger events here which depend on successful
153159
** voltage update.

0 commit comments

Comments
 (0)