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.
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:
2628String apiKey = " YOUR-PROJECT-APIKEY" ;
2729String deviceID = " YOUR-DEVICE-ID" ;
2830String 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.
3841int statePin = D0;
3942int 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
4857void 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
6473void 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 (" \n Device 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