Skip to content

Commit 1aa3cd6

Browse files
committed
Crashing on connecting bug is resolved.
1 parent 74baf3c commit 1aa3cd6

File tree

9 files changed

+493
-336
lines changed

9 files changed

+493
-336
lines changed

examples/DashListening-App/DashListening-App-esp8266/DashListening-App-esp8266.ino

Lines changed: 80 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
String apiKey = "YOUR-PROJECT-APIKEY";
2626
String deviceID = "YOUR-DEVICE-ID";
2727
String token = "YOUR-ACCESS-TOKEN";
28-
const char* ssid = "YOUR-WIFI-SSID";
29-
const char* passphrase = "YOUR-WIFI-PASSWORD";
28+
const char *ssid = "YOUR-WIFI-SSID";
29+
const char *passphrase = "YOUR-WIFI-PASSWORD";
3030

3131
// Handles our 5 second timer in loop().
3232
unsigned long currentTime = millis();
@@ -35,37 +35,47 @@ Grandeur::Project project;
3535
// Device data object to get/set/subscribe to device variables.
3636
Grandeur::Project::Device::Data data;
3737
// State and voltage pins to set.
38-
int statePin = 4;
39-
int voltagePin = 2;
38+
int statePin = D0;
39+
int voltagePin = A0;
4040

4141
// FUNCTION PROTOTYPES:
42-
// Handles WiFi connection/disconnection events.
42+
// These handle WiFi connection/disconnection events.
4343
WiFiEventHandler onWiFiConnectedHandler;
4444
WiFiEventHandler onWiFiDisconnectedHandler;
4545
// Starts the device WiFi.
4646
void startWiFi(void);
4747
// Handles Grandeur connection/disconnection events.
4848
void GrandeurConnectionCallback(bool state);
49-
// Function to call when acknowledgement for voltage update arrives from Grandeur.
50-
void afterVoltageIsUpdated(const char* code, int voltage);
49+
// Data get/set/update callback functions:
50+
void initializeStatePin(const char *code, bool state);
51+
void setStatePinToNewValue(const char *path, bool state);
52+
void afterVoltageIsUpdated(const char *code, int voltage);
5153

52-
void setup() {
54+
void setup()
55+
{
5356
Serial.begin(9600);
5457
startWiFi();
5558
// This initializes the SDK's configurations and returns reference to your project.
5659
project = grandeur.init(apiKey, token);
5760
// Getting object of your device data.
5861
data = project.device(deviceID).data();
59-
// This schedules the GrandeurConnectionCallback() function to be called when connection with Grandeur
62+
// This schedules the connectionCallback() function to be called when connection with Grandeur
6063
// is made/broken.
6164
project.onConnection(GrandeurConnectionCallback);
65+
// This schedules setStatePinToNewValue() function to be called when a change in device state occurs
66+
// on Grandeur.
67+
data.on("state", setStatePinToNewValue);
68+
Serial.println("Listening for state update from Grandeur...");
6269
}
6370

64-
void loop() {
71+
void loop()
72+
{
6573
// In this loop() function, after every five seconds, we send the updated values of our
6674
// device's voltage to Grandeur.
67-
if(project.isConnected()) {
68-
if(millis() - currentTime >= 5000) {
75+
if (project.isConnected())
76+
{
77+
if (millis() - currentTime >= 5000)
78+
{
6979
// This if-condition makes sure that the code inside this block runs only after
7080
// every five seconds.
7181

@@ -84,45 +94,78 @@ void loop() {
8494
project.loop(WiFi.status() == WL_CONNECTED);
8595
}
8696

87-
void startWiFi(void) {
97+
void startWiFi(void)
98+
{
8899
// Disconnecting WiFi if it"s already connected
89100
WiFi.disconnect();
90101
// Setting it to Station mode which basically scans for nearby WiFi routers
91102
WiFi.mode(WIFI_STA);
92103
// Setting WiFi event handlers
93-
onWiFiConnectedHandler = WiFi.onStationModeGotIP([](const WiFiEventStationModeGotIP& event) {
94-
// This runs when the device connects with WiFi.
95-
Serial.printf("\nDevice has successfully connected to WiFi. Its IP Address is: %s\n",
96-
WiFi.localIP().toString().c_str());
97-
});
98-
onWiFiDisconnectedHandler = WiFi.onStationModeDisconnected([](const WiFiEventStationModeDisconnected& event) {
99-
// This runs when the device disconnects with WiFi.
100-
Serial.println("Device is disconnected from WiFi.");
101-
});
104+
onWiFiConnectedHandler = WiFi.onStationModeGotIP([](const WiFiEventStationModeGotIP &event)
105+
{
106+
// This runs when the device connects with WiFi.
107+
Serial.printf("\nDevice has successfully connected to WiFi. Its IP Address is: %s\n",
108+
WiFi.localIP().toString().c_str());
109+
});
110+
onWiFiDisconnectedHandler = WiFi.onStationModeDisconnected([](const WiFiEventStationModeDisconnected &event)
111+
{
112+
// This runs when the device disconnects with WiFi.
113+
Serial.println("Device is disconnected from WiFi.");
114+
});
102115
// Begin connecting to WiFi
103116
WiFi.begin(ssid, passphrase);
104-
Serial.printf("\nDevice is connecting to WiFi using SSID %s and Passphrase %s.\n", ssid.c_str(), passphrase.c_str());
117+
Serial.printf("\nDevice is connecting to WiFi using SSID %s and Passphrase %s.\n", ssid, passphrase);
105118
}
106119

107-
void GrandeurConnectionCallback(bool status) {
108-
switch(status) {
109-
case CONNECTED: // Expands to true.
110-
Serial.println("Device is connected with Grandeur.");
120+
void GrandeurConnectionCallback(bool status)
121+
{
122+
switch (status)
123+
{
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);
111130

112-
// Initializing the millis counter for the five
113-
// seconds timer.
114-
currentTime = millis();
115-
break;
116-
case DISCONNECTED: // Expands to false.
117-
Serial.println("Device's connection with Grandeur is broken.");
118-
break;
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;
119138
}
120139
}
121140

122-
void afterVoltageIsUpdated(const char* code, int voltage) {
123-
if(strcmp(code, "DEVICE-DATA-UPDATED") == 0) {
141+
void initializeStatePin(const char *code, bool state)
142+
{
143+
// This function sets the *state pin* to the *state value* that we received in data
144+
// from Grandeur.
145+
if (strcmp(code, "DEVICE-DATA-FETCHED") == 0)
146+
{
147+
Serial.printf("State is: %d\n", state);
148+
digitalWrite(statePin, state);
149+
return;
150+
}
151+
// If the data could not be fetched.
152+
Serial.println("Failed to Fetch State");
153+
return;
154+
}
155+
156+
void setStatePinToNewValue(const char *path, bool state)
157+
{
158+
// This function sets the *state pin* to state value.
159+
Serial.printf("Updated State is: %d\n", state);
160+
digitalWrite(statePin, state);
161+
}
162+
163+
void afterVoltageIsUpdated(const char *code, int voltage)
164+
{
165+
if (strcmp(code, "DEVICE-DATA-UPDATED") == 0)
166+
{
124167
Serial.printf("Voltage is updated to: %d\n", voltage);
125-
168+
126169
/* You can set some pins or trigger events here which depend on successful
127170
** voltage update.
128171
*/

examples/DashListening-Device/DashListening-Device-esp8266/DashListening-Device-esp8266.ino

Lines changed: 46 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
String apiKey = "YOUR-PROJECT-APIKEY";
2525
String deviceID = "YOUR-DEVICE-ID";
2626
String token = "YOUR-ACCESS-TOKEN";
27-
const char* ssid = "YOUR-WIFI-SSID";
28-
const char* passphrase = "YOUR-WIFI-PASSWORD";
27+
const char *ssid = "YOUR-WIFI-SSID";
28+
const char *passphrase = "YOUR-WIFI-PASSWORD";
2929

3030
// Handles our 5 second timer in loop().
3131
unsigned long currentTime = millis();
@@ -45,10 +45,11 @@ void startWiFi(void);
4545
// Handles Grandeur connection/disconnection events.
4646
void GrandeurConnectionCallback(bool state);
4747
// Data get/set/update callback functions:
48-
void initializeStatePin(const char* code, bool state);
49-
void setStatePinToNewValue(const char* path, bool state);
48+
void initializeStatePin(const char *code, bool state);
49+
void setStatePinToNewValue(const char *path, bool state);
5050

51-
void setup() {
51+
void setup()
52+
{
5253
Serial.begin(9600);
5354
startWiFi();
5455
// This initializes the SDK's configurations and returns reference to your project.
@@ -63,55 +64,63 @@ void setup() {
6364
data.on("state", setStatePinToNewValue);
6465
}
6566

66-
void loop() {
67+
void loop()
68+
{
6769
// The SDK only runs when the WiFi is connected.
6870
project.loop(WiFi.status() == WL_CONNECTED);
6971
}
7072

71-
void setupWiFi(void) {
73+
void startWiFi(void)
74+
{
7275
// Disconnecting WiFi if it"s already connected
7376
WiFi.disconnect();
7477
// Setting it to Station mode which basically scans for nearby WiFi routers
7578
WiFi.mode(WIFI_STA);
7679
// Setting WiFi event handlers
77-
onWiFiConnectedHandler = WiFi.onStationModeGotIP([](const WiFiEventStationModeGotIP& event) {
78-
// This runs when the device connects with WiFi.
79-
Serial.printf("\nDevice has successfully connected to WiFi. Its IP Address is: %s\n",
80-
WiFi.localIP().toString().c_str());
81-
});
82-
onWiFiDisconnectedHandler = WiFi.onStationModeDisconnected([](const WiFiEventStationModeDisconnected& event) {
83-
// This runs when the device disconnects with WiFi.
84-
Serial.println("Device is disconnected from WiFi.");
85-
});
80+
onWiFiConnectedHandler = WiFi.onStationModeGotIP([](const WiFiEventStationModeGotIP &event)
81+
{
82+
// This runs when the device connects with WiFi.
83+
Serial.printf("\nDevice has successfully connected to WiFi. Its IP Address is: %s\n",
84+
WiFi.localIP().toString().c_str());
85+
});
86+
onWiFiDisconnectedHandler = WiFi.onStationModeDisconnected([](const WiFiEventStationModeDisconnected &event)
87+
{
88+
// This runs when the device disconnects with WiFi.
89+
Serial.println("Device is disconnected from WiFi.");
90+
});
8691
// Begin connecting to WiFi
8792
WiFi.begin(ssid, passphrase);
88-
Serial.printf("\nDevice is connecting to WiFi using SSID %s and Passphrase %s.\n", ssid.c_str(), passphrase.c_str());
93+
Serial.printf("\nDevice is connecting to WiFi using SSID %s and Passphrase %s.\n", ssid, passphrase);
8994
}
9095

91-
void GrandeurConnectionCallback(bool status) {
92-
switch(status) {
93-
case CONNECTED: // Expands to true.
94-
Serial.println("Device is connected with Grandeur.");
95-
// On successful connection with Grandeur, we initialize the device's *state*.
96-
// To do that, we get device state from Grandeur and set the *state pin* to its
97-
// value.
98-
data.get("state", initializeStatePin);
99-
Serial.println("Listening for state update from Grandeur...");
96+
void GrandeurConnectionCallback(bool status)
97+
{
98+
switch (status)
99+
{
100+
case CONNECTED: // Expands to true.
101+
Serial.println("Device is connected with Grandeur.");
102+
// On successful connection with Grandeur, we initialize the device's *state*.
103+
// To do that, we get device state from Grandeur and set the *state pin* to its
104+
// value.
105+
data.get("state", initializeStatePin);
106+
Serial.println("Listening for state update from Grandeur...");
100107

101-
// Initializing the millis counter for the five
102-
// seconds timer.
103-
currentTime = millis();
104-
break;
105-
case DISCONNECTED: // Expands to false.
106-
Serial.println("Device's connection with Grandeur is broken.");
107-
break;
108+
// Initializing the millis counter for the five
109+
// seconds timer.
110+
currentTime = millis();
111+
break;
112+
case DISCONNECTED: // Expands to false.
113+
Serial.println("Device's connection with Grandeur is broken.");
114+
break;
108115
}
109116
}
110117

111-
void initializeStatePin(const char* code, bool state) {
118+
void initializeStatePin(const char *code, bool state)
119+
{
112120
// This function sets the *state pin* to the *state value* that we received in data
113121
// from Grandeur.
114-
if(code == "DEVICE-DATA-FETCHED") {
122+
if (code == "DEVICE-DATA-FETCHED")
123+
{
115124
Serial.printf("State is: %d\n", state);
116125
digitalWrite(statePin, state);
117126
return;
@@ -121,7 +130,8 @@ void initializeStatePin(const char* code, bool state) {
121130
return;
122131
}
123132

124-
void setStatePinToNewValue(const char* path, bool state) {
133+
void setStatePinToNewValue(const char *path, bool state)
134+
{
125135
// This function sets the *state pin* to state value.
126136
Serial.printf("Updated State is: %d\n", state);
127137
digitalWrite(statePin, state);

0 commit comments

Comments
 (0)