Skip to content

Commit 7a3da84

Browse files
authored
Merge pull request #18 from grandeurtech/development
ESP32 compile error is resolved. Examples for ESP32 are added. Documentation is improved.
2 parents 6099cf6 + 2874914 commit 7a3da84

File tree

34 files changed

+5199
-2365
lines changed

34 files changed

+5199
-2365
lines changed

README.md

Lines changed: 151 additions & 81 deletions
Large diffs are not rendered by default.
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
/**
2+
* @file CrossListening-esp32.ino
3+
* @date 24.03.2020
4+
* @author Grandeur Technologies
5+
*
6+
* Copyright (c) 2019 Grandeur Technologies LLP. All rights reserved.
7+
* This file is part of the Arduino SDK for Grandeur Cloud.
8+
*
9+
* Apollo.h is used for device's communication with Grandeur Cloud.
10+
* WiFi.h is used for handling device's WiFi.
11+
*
12+
* Cross listening means when the device listens for updates from the app and the app
13+
* listens for updates from the device.
14+
* This example illustrates pretty much every basic thing you'd need in order to monitor /
15+
* control your device through Grandeur Cloud. 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.
20+
*/
21+
22+
#include <Apollo.h>
23+
#include <WiFi.h>
24+
25+
// Device's connection configurations
26+
String apiKey = "YOUR-PROJECT-APIKEY";
27+
String deviceID = "YOUR-DEVICE-ID";
28+
String token = "YOUR-ACCESS-TOKEN";
29+
const char* ssid = "YOUR-WIFI-SSID";
30+
const char* passphrase = "YOUR-WIFI-PASSWORD";
31+
32+
// Declaring and initializing other variables
33+
unsigned long current = millis();
34+
Project myProject;
35+
Device myDevice;
36+
int statePin = 4;
37+
int voltagePin = 2;
38+
39+
// Function prototypes
40+
void WiFiEventCallback(WiFiEvent_t event);
41+
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+
48+
49+
void setup() {
50+
Serial.begin(9600);
51+
// This sets up the device WiFi.
52+
setupWiFi();
53+
// This initializes the SDK's configurations and returns a new object of Project class.
54+
myProject = apollo.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
58+
// 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 the Cloud.
62+
myDevice.onParms(parmsUpdatedCallback);
63+
}
64+
65+
void loop() {
66+
// In this loop() function, after every five seconds, we send the updated values of our
67+
// device's voltage and state to the Cloud.
68+
if(myProject.isConnected()) {
69+
if(millis() - current >= 5000) {
70+
// This if-condition makes sure that the code inside this block runs only after
71+
// every five seconds.
72+
73+
Serial.println("Setting Summary");
74+
JSONObject summary;
75+
summary["voltage"] = analogRead(voltagePin);
76+
// This updates the summary of our device on the Cloud and schedules summarySetCallback()
77+
// function to be called when the Cloud 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 the Cloud and schedules parmsSetCallback()
84+
// function to be called when the Cloud responds with the PARMS UPDATED message.
85+
myDevice.setParms(parms, parmsSetCallback);
86+
87+
// This updates the millis counter for
88+
// the five seconds scheduler.
89+
current = millis();
90+
}
91+
}
92+
93+
// This runs the SDK only when the WiFi is connected.
94+
myProject.loop(WiFi.status() == WL_CONNECTED);
95+
}
96+
97+
void WiFiEventCallback(WiFiEvent_t event) {
98+
switch(event) {
99+
case SYSTEM_EVENT_STA_GOT_IP:
100+
// This runs when the device connects with WiFi.
101+
Serial.printf("\nDevice has successfully connected to WiFi. Its IP Address is: %s\n",
102+
WiFi.localIP().toString().c_str());
103+
break;
104+
case SYSTEM_EVENT_STA_DISCONNECTED:
105+
// This runs when the device disconnects with WiFi.
106+
Serial.println("Device is disconnected from WiFi.");
107+
break;
108+
default: break;
109+
}
110+
}
111+
112+
void setupWiFi(void) {
113+
// Disconnecting WiFi if it"s already connected
114+
WiFi.disconnect();
115+
// Setting it to Station mode which basically scans for nearby WiFi routers
116+
WiFi.mode(WIFI_STA);
117+
// Setting WiFi event handler
118+
WiFi.onEvent(WiFiEventCallback);
119+
// Begin connecting to WiFi
120+
WiFi.begin(ssid, passphrase);
121+
Serial.printf("\nDevice is connecting to WiFi using SSID %s and Passphrase %s.\n", ssid, passphrase);
122+
}
123+
124+
void connectionCallback(bool state) {
125+
switch(state) {
126+
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...");
133+
// Initializing the millis counter for the five
134+
// seconds timer.
135+
current = millis();
136+
break;
137+
case DISCONNECTED:
138+
Serial.println("Device is disconnected from the cloud.");
139+
break;
140+
}
141+
}
142+
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"];
148+
digitalWrite(statePin, state);
149+
return;
150+
}
151+
// If the parms could not be fetched.
152+
Serial.println("Failed to Fetch Parms");
153+
return;
154+
}
155+
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"]);
161+
}
162+
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"]);
166+
167+
/* You can set some pins or trigger events here which depend on successful
168+
** device summary update.
169+
*/
170+
return;
171+
}
172+
// If the summary could not be updated.
173+
Serial.println("Failed to Update Summary");
174+
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+
}

examples/CrossListening/CrossListening.ino renamed to examples/CrossListening/CrossListening-esp8266/CrossListening-esp8266.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* @file CrossListening.ino
2+
* @file CrossListening-esp8266.ino
33
* @date 24.03.2020
44
* @author Grandeur Technologies
55
*
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
/**
2+
* @file DashListening-App-esp32.ino
3+
* @date 24.03.2020
4+
* @author Grandeur Technologies
5+
*
6+
* Copyright (c) 2019 Grandeur Technologies LLP. All rights reserved.
7+
* This file is part of the Arduino SDK for Grandeur Cloud.
8+
*
9+
* Apollo.h is used for device's communication to Grandeur Cloud.
10+
* WiFi.h is used for handling device's WiFi.
11+
*
12+
* Dash listening is for one-way listening.
13+
* This example illustrates the use case of an app listening for updates from the device.
14+
* It would be useful in building a DEVICE MONITOR which would show how your devices are
15+
* behaving in terms of their energy units consumed for example.
16+
*/
17+
18+
#include <Apollo.h>
19+
#include <WiFi.h>
20+
21+
// Device's connection configurations
22+
String apiKey = "YOUR-PROJECT-APIKEY";
23+
String deviceID = "YOUR-DEVICE-ID";
24+
String token = "YOUR-ACCESS-TOKEN";
25+
const char* ssid = "YOUR-WIFI-SSID";
26+
const char* passphrase = "YOUR-WIFI-PASSWORD";
27+
28+
// Declaring and initializing other variables
29+
unsigned long current = millis();
30+
Project myProject;
31+
Device myDevice;
32+
int statePin = 4;
33+
int voltagePin = 2;
34+
35+
// Function prototypes
36+
void WiFiEventCallback(WiFiEvent_t event);
37+
void setupWiFi(void);
38+
void connectionCallback(bool state);
39+
void initializeState(JSONObject getResult);
40+
void summarySetCallback(JSONObject setResult);
41+
void parmsSetCallback(JSONObject setResult);
42+
43+
void setup() {
44+
Serial.begin(9600);
45+
// This sets up the device WiFi.
46+
setupWiFi();
47+
// This initializes the SDK's configurations and returns a new object of Project class.
48+
myProject = apollo.init(apiKey, token);
49+
// Getting object of Device class.
50+
myDevice = myProject.device(deviceID);
51+
// This schedules the connectionCallback() function to be called when connection with the cloud
52+
// is made/broken.
53+
myProject.onConnection(connectionCallback);
54+
}
55+
56+
void loop() {
57+
// In this loop() function, after every five seconds, we send the updated values of our
58+
// device's voltage and state to the Cloud.
59+
if(myProject.isConnected()) {
60+
if(millis() - current >= 5000) {
61+
// This if-condition makes sure that the code inside this block runs only after
62+
// every five seconds.
63+
64+
Serial.println("Setting Summary");
65+
JSONObject summary;
66+
summary["voltage"] = analogRead(voltagePin);
67+
// This updates the summary of our device on the Cloud and schedules summarySetCallback()
68+
// function to be called when the Cloud responds with the SUMMARY UPDATED message.
69+
myDevice.setSummary(summary, summarySetCallback);
70+
71+
Serial.println("Setting Parms");
72+
JSONObject parms;
73+
parms["state"] = digitalRead(statePin);
74+
// This updates the parms of our device on the Cloud and schedules parmsSetCallback()
75+
// function to be called when the Cloud responds with the PARMS UPDATED message.
76+
myDevice.setParms(parms, parmsSetCallback);
77+
78+
// This updates the millis counter for
79+
// the five seconds scheduler.
80+
current = millis();
81+
}
82+
}
83+
84+
// This runs the SDK only when the WiFi is connected.
85+
myProject.loop(WiFi.status() == WL_CONNECTED);
86+
}
87+
88+
void WiFiEventCallback(WiFiEvent_t event) {
89+
switch(event) {
90+
case SYSTEM_EVENT_STA_GOT_IP:
91+
// This runs when the device connects with WiFi.
92+
Serial.printf("\nDevice has successfully connected to WiFi. Its IP Address is: %s\n",
93+
WiFi.localIP().toString().c_str());
94+
break;
95+
case SYSTEM_EVENT_STA_DISCONNECTED:
96+
// This runs when the device disconnects with WiFi.
97+
Serial.println("Device is disconnected from WiFi.");
98+
break;
99+
default: break;
100+
}
101+
}
102+
103+
void setupWiFi(void) {
104+
// Disconnecting WiFi if it"s already connected
105+
WiFi.disconnect();
106+
// Setting it to Station mode which basically scans for nearby WiFi routers
107+
WiFi.mode(WIFI_STA);
108+
// Setting WiFi event handler
109+
WiFi.onEvent(WiFiEventCallback);
110+
// Begin connecting to WiFi
111+
WiFi.begin(ssid, passphrase);
112+
Serial.printf("\nDevice is connecting to WiFi using SSID %s and Passphrase %s.\n", ssid, passphrase);
113+
}
114+
115+
void connectionCallback(bool state) {
116+
switch(state) {
117+
case CONNECTED:
118+
// On successful connection with the cloud, we initialize the device's *state*.
119+
// To do that, we get device parms from the cloud and set the *state pin* to the value of *state* in those parms.
120+
Serial.println("Device is connected to the cloud.");
121+
myDevice.getParms(initializeState);
122+
123+
// Initializing the millis counter for the five
124+
// seconds timer.
125+
current = millis();
126+
break;
127+
case DISCONNECTED:
128+
Serial.println("Device is disconnected from the cloud.");
129+
break;
130+
}
131+
}
132+
133+
void initializeState(JSONObject getResult) {
134+
// This function sets the *state pin* to the *state value* that we received in parms
135+
// from the cloud.
136+
if(getResult["code"] == "DEVICE-PARMS-FETCHED") {
137+
int state = getResult["deviceParms"]["state"];
138+
digitalWrite(statePin, state);
139+
return;
140+
}
141+
// If the parms could not be fetched.
142+
Serial.println("Failed to Fetch Parms");
143+
return;
144+
}
145+
146+
void summarySetCallback(JSONObject setResult) {
147+
if(setResult["code"] == "DEVICE-SUMMARY-UPDATED") {
148+
Serial.printf("Voltage is updated to: %d\n", (int) setResult["update"]["voltage"]);
149+
150+
/* You can set some pins or trigger events here which depend on successful
151+
** device summary update.
152+
*/
153+
return;
154+
}
155+
// If the summary could not be updated.
156+
Serial.println("Failed to Update Summary");
157+
return;
158+
}
159+
160+
void parmsSetCallback(JSONObject setResult) {
161+
if(setResult["code"] == "DEVICE-PARMS-UPDATED") {
162+
Serial.printf("State is updated to: %d\n", (bool) setResult["update"]["state"]);
163+
164+
/* You can set some pins or trigger events here which depend on successful
165+
** device parms update.
166+
*/
167+
return;
168+
}
169+
// If the parms could not be updated.
170+
Serial.println("Failed to Update Parms");
171+
return;
172+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* @file DashListening-App.ino
2+
* @file DashListening-App-esp8266.ino
33
* @date 24.03.2020
44
* @author Grandeur Technologies
55
*
@@ -25,7 +25,7 @@ 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();
3030
Project myProject;
3131
Device myDevice;

0 commit comments

Comments
 (0)