Skip to content

Commit c6852b3

Browse files
committed
Examples are updated.
1 parent 1aa3cd6 commit c6852b3

File tree

12 files changed

+275
-203
lines changed

12 files changed

+275
-203
lines changed

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

Lines changed: 61 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
String apiKey = "YOUR-PROJECT-APIKEY";
2929
String deviceID = "YOUR-DEVICE-ID";
3030
String token = "YOUR-ACCESS-TOKEN";
31-
const char* ssid = "YOUR-WIFI-SSID";
32-
const char* passphrase = "YOUR-WIFI-PASSWORD";
31+
const char *ssid = "YOUR-WIFI-SSID";
32+
const char *passphrase = "YOUR-WIFI-PASSWORD";
3333

3434
// Handles our 5 second timer in loop().
3535
unsigned long currentTime = millis();
@@ -49,11 +49,12 @@ void startWiFi(void);
4949
// Handles Grandeur connection/disconnection events.
5050
void GrandeurConnectionCallback(bool state);
5151
// 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);
52+
void initializeStatePin(const char *code, bool state);
53+
void setStatePinToNewValue(const char *path, bool state);
54+
void afterVoltageIsUpdated(const char *code, int voltage);
5555

56-
void setup() {
56+
void setup()
57+
{
5758
.begin(9600);
5859
startWiFi();
5960
// This initializes the SDK's configurations and returns reference to your project.
@@ -69,11 +70,14 @@ void setup() {
6970
Serial.println("Listening for state update from Grandeur...");
7071
}
7172

72-
void loop() {
73+
void loop()
74+
{
7375
// In this loop() function, after every five seconds, we send the updated values of our
7476
// device's voltage to Grandeur.
75-
if(project.isConnected()) {
76-
if(millis() - currentTime >= 5000) {
77+
if (project.isConnected())
78+
{
79+
if (millis() - currentTime >= 5000)
80+
{
7781
// This if-condition makes sure that the code inside this block runs only after
7882
// every five seconds.
7983

@@ -89,25 +93,30 @@ void loop() {
8993
}
9094

9195
// This runs the SDK only when the WiFi is connected.
92-
project.loop(WiFi.status() == WL_CONNECTED);
96+
if (WiFi.status() == WL_CONNECTED)
97+
project.loop();
9398
}
9499

95-
void WiFiEventCallback(WiFiEvent_t event) {
96-
switch(event) {
97-
case SYSTEM_EVENT_STA_GOT_IP:
98-
// This runs when the device connects with WiFi.
99-
Serial.printf("\nDevice has successfully connected to WiFi. Its IP Address is: %s\n",
100-
WiFi.localIP().toString().c_str());
101-
break;
102-
case SYSTEM_EVENT_STA_DISCONNECTED:
103-
// This runs when the device disconnects with WiFi.
104-
Serial.println("Device is disconnected from WiFi.");
105-
break;
106-
default: break;
100+
void WiFiEventCallback(WiFiEvent_t event)
101+
{
102+
switch (event)
103+
{
104+
case SYSTEM_EVENT_STA_GOT_IP:
105+
// This runs when the device connects with WiFi.
106+
Serial.printf("\nDevice has successfully connected to WiFi. Its IP Address is: %s\n",
107+
WiFi.localIP().toString().c_str());
108+
break;
109+
case SYSTEM_EVENT_STA_DISCONNECTED:
110+
// This runs when the device disconnects with WiFi.
111+
Serial.println("Device is disconnected from WiFi.");
112+
break;
113+
default:
114+
break;
107115
}
108116
}
109117

110-
void startWiFi(void) {
118+
void startWiFi(void)
119+
{
111120
// Disconnecting WiFi if it"s already connected
112121
WiFi.disconnect();
113122
// Setting it to Station mode which basically scans for nearby WiFi routers
@@ -119,29 +128,33 @@ void startWiFi(void) {
119128
Serial.printf("\nDevice is connecting to WiFi using SSID %s and Passphrase %s.\n", ssid, passphrase);
120129
}
121130

122-
void GrandeurConnectionCallback(bool status) {
123-
switch(status) {
124-
case CONNECTED: // Expands to true.
125-
Serial.println("Device is connected with Grandeur.");
126-
// On successful connection with Grandeur, we initialize the device's *state*.
127-
// To do that, we get device state from Grandeur and set the *state pin* to its
128-
// value.
129-
data.get("state", initializeStatePin);
130-
131-
// Initializing the millis counter for the five
132-
// seconds timer.
133-
currentTime = millis();
134-
break;
135-
case DISCONNECTED: // Expands to false.
136-
Serial.println("Device's connection with Grandeur is broken.");
137-
break;
131+
void GrandeurConnectionCallback(bool status)
132+
{
133+
switch (status)
134+
{
135+
case CONNECTED: // Expands to true.
136+
Serial.println("Device is connected with Grandeur.");
137+
// On successful connection with Grandeur, we initialize the device's *state*.
138+
// To do that, we get device state from Grandeur and set the *state pin* to its
139+
// value.
140+
data.get("state", initializeStatePin);
141+
142+
// Initializing the millis counter for the five
143+
// seconds timer.
144+
currentTime = millis();
145+
break;
146+
case DISCONNECTED: // Expands to false.
147+
Serial.println("Device's connection with Grandeur is broken.");
148+
break;
138149
}
139150
}
140151

141-
void initializeStatePin(const char* code, bool state) {
152+
void initializeStatePin(const char *code, bool state)
153+
{
142154
// This function sets the *state pin* to the *state value* that we received in data
143155
// from Grandeur.
144-
if(strcmp(code, "DEVICE-DATA-FETCHED") == 0) {
156+
if (strcmp(code, "DEVICE-DATA-FETCHED") == 0)
157+
{
145158
Serial.printf("State is: %d\n", state);
146159
digitalWrite(statePin, state);
147160
return;
@@ -151,16 +164,19 @@ void initializeStatePin(const char* code, bool state) {
151164
return;
152165
}
153166

154-
void setStatePinToNewValue(const char* path, bool state) {
167+
void setStatePinToNewValue(const char *path, bool state)
168+
{
155169
// This function sets the *state pin* to state value.
156170
Serial.printf("Updated State is: %d\n", state);
157171
digitalWrite(statePin, state);
158172
}
159173

160-
void afterVoltageIsUpdated(const char* code, int voltage) {
161-
if(strcmp(code, "DEVICE-DATA-UPDATED") == 0) {
174+
void afterVoltageIsUpdated(const char *code, int voltage)
175+
{
176+
if (strcmp(code, "DEVICE-DATA-UPDATED") == 0)
177+
{
162178
Serial.printf("Voltage is updated to: %d\n", voltage);
163-
179+
164180
/* You can set some pins or trigger events here which depend on successful
165181
** voltage update.
166182
*/

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

Lines changed: 57 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
String apiKey = "YOUR-PROJECT-APIKEY";
2929
String deviceID = "YOUR-DEVICE-ID";
3030
String token = "YOUR-ACCESS-TOKEN";
31-
const char* ssid = "YOUR-WIFI-SSID";
32-
const char* passphrase = "YOUR-WIFI-PASSWORD";
31+
const char *ssid = "YOUR-WIFI-SSID";
32+
const char *passphrase = "YOUR-WIFI-PASSWORD";
3333

3434
// Handles our 5 second timer in loop().
3535
unsigned long currentTime = millis();
@@ -50,11 +50,12 @@ void startWiFi(void);
5050
// Handles Grandeur connection/disconnection events.
5151
void GrandeurConnectionCallback(bool state);
5252
// 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);
53+
void initializeStatePin(const char *code, bool state);
54+
void setStatePinToNewValue(const char *path, bool state);
55+
void afterVoltageIsUpdated(const char *code, int voltage);
5656

57-
void setup() {
57+
void setup()
58+
{
5859
Serial.begin(9600);
5960
startWiFi();
6061
// This initializes the SDK's configurations and returns reference to your project.
@@ -70,11 +71,14 @@ void setup() {
7071
Serial.println("Listening for state update from Grandeur...");
7172
}
7273

73-
void loop() {
74+
void loop()
75+
{
7476
// In this loop() function, after every five seconds, we send the updated values of our
7577
// device's voltage to Grandeur.
76-
if(project.isConnected()) {
77-
if(millis() - currentTime >= 5000) {
78+
if (project.isConnected())
79+
{
80+
if (millis() - currentTime >= 5000)
81+
{
7882
// This if-condition makes sure that the code inside this block runs only after
7983
// every five seconds.
8084

@@ -90,52 +94,60 @@ void loop() {
9094
}
9195

9296
// This runs the SDK only when the WiFi is connected.
93-
project.loop(WiFi.status() == WL_CONNECTED);
97+
if (WiFi.status() == WL_CONNECTED)
98+
project.loop();
9499
}
95100

96-
void startWiFi(void) {
101+
void startWiFi(void)
102+
{
97103
// Disconnecting WiFi if it"s already connected
98104
WiFi.disconnect();
99105
// Setting it to Station mode which basically scans for nearby WiFi routers
100106
WiFi.mode(WIFI_STA);
101107
// Setting WiFi event handlers
102-
onWiFiConnectedHandler = WiFi.onStationModeGotIP([](const WiFiEventStationModeGotIP& event) {
103-
// This runs when the device connects with WiFi.
104-
Serial.printf("\nDevice has successfully connected to WiFi. Its IP Address is: %s\n",
105-
WiFi.localIP().toString().c_str());
106-
});
107-
onWiFiDisconnectedHandler = WiFi.onStationModeDisconnected([](const WiFiEventStationModeDisconnected& event) {
108-
// This runs when the device disconnects with WiFi.
109-
Serial.println("Device is disconnected from WiFi.");
110-
});
108+
onWiFiConnectedHandler = WiFi.onStationModeGotIP([](const WiFiEventStationModeGotIP &event)
109+
{
110+
// This runs when the device connects with WiFi.
111+
Serial.printf("\nDevice has successfully connected to WiFi. Its IP Address is: %s\n",
112+
WiFi.localIP().toString().c_str());
113+
});
114+
onWiFiDisconnectedHandler = WiFi.onStationModeDisconnected([](const WiFiEventStationModeDisconnected &event)
115+
{
116+
// This runs when the device disconnects with WiFi.
117+
Serial.println("Device is disconnected from WiFi.");
118+
});
111119
// Begin connecting to WiFi
112120
WiFi.begin(ssid, passphrase);
113121
Serial.printf("\nDevice is connecting to WiFi using SSID %s and Passphrase %s.\n", ssid.c_str(), passphrase.c_str());
114122
}
115123

116-
void GrandeurConnectionCallback(bool status) {
117-
switch(status) {
118-
case CONNECTED: // Expands to true.
119-
Serial.println("Device is connected with Grandeur.");
120-
// On successful connection with Grandeur, we initialize the device's *state*.
121-
// To do that, we get device state from Grandeur and set the *state pin* to its
122-
// value.
123-
data.get("state", initializeStatePin);
124-
125-
// Initializing the millis counter for the five
126-
// seconds timer.
127-
currentTime = millis();
128-
break;
129-
case DISCONNECTED: // Expands to false.
130-
Serial.println("Device's connection with Grandeur is broken.");
131-
break;
124+
void GrandeurConnectionCallback(bool status)
125+
{
126+
switch (status)
127+
{
128+
case CONNECTED: // Expands to true.
129+
Serial.println("Device is connected with Grandeur.");
130+
// On successful connection with Grandeur, we initialize the device's *state*.
131+
// To do that, we get device state from Grandeur and set the *state pin* to its
132+
// value.
133+
data.get("state", initializeStatePin);
134+
135+
// Initializing the millis counter for the five
136+
// seconds timer.
137+
currentTime = millis();
138+
break;
139+
case DISCONNECTED: // Expands to false.
140+
Serial.println("Device's connection with Grandeur is broken.");
141+
break;
132142
}
133143
}
134144

135-
void initializeStatePin(const char* code, bool state) {
145+
void initializeStatePin(const char *code, bool state)
146+
{
136147
// This function sets the *state pin* to the *state value* that we received in data
137148
// from Grandeur.
138-
if(strcmp(code, "DEVICE-DATA-FETCHED") == 0) {
149+
if (strcmp(code, "DEVICE-DATA-FETCHED") == 0)
150+
{
139151
Serial.printf("State is: %d\n", state);
140152
digitalWrite(statePin, state);
141153
return;
@@ -145,16 +157,19 @@ void initializeStatePin(const char* code, bool state) {
145157
return;
146158
}
147159

148-
void setStatePinToNewValue(const char* path, bool state) {
160+
void setStatePinToNewValue(const char *path, bool state)
161+
{
149162
// This function sets the *state pin* to state value.
150163
Serial.printf("Updated State is: %d\n", state);
151164
digitalWrite(statePin, state);
152165
}
153166

154-
void afterVoltageIsUpdated(const char* code, int voltage) {
155-
if(strcmp(code, "DEVICE-DATA-UPDATED") == 0) {
167+
void afterVoltageIsUpdated(const char *code, int voltage)
168+
{
169+
if (strcmp(code, "DEVICE-DATA-UPDATED") == 0)
170+
{
156171
Serial.printf("Voltage is updated to: %d\n", voltage);
157-
172+
158173
/* You can set some pins or trigger events here which depend on successful
159174
** voltage update.
160175
*/

0 commit comments

Comments
 (0)