@@ -29,60 +29,50 @@ String token = "YOUR-ACCESS-TOKEN";
2929const char * ssid = " YOUR-WIFI-SSID" ;
3030const char * passphrase = " YOUR-WIFI-PASSWORD" ;
3131
32- // Declaring and initializing other variables
32+ // Declaring and initializing other Variables
3333unsigned long current = millis();
34- Project myProject ;
35- Device myDevice ;
34+ Project project ;
35+ Data data ;
3636int statePin = 4 ;
3737int voltagePin = 2 ;
3838
3939// Function prototypes
4040void WiFiEventCallback (WiFiEvent_t event);
4141void 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-
42+ void connectionCallback (bool state);
43+ void initializeState (Var getResult);
44+ void stateUpdatedCallback (bool state, const char * path);
45+ void voltageSetCallback (Var setResult);
4846
4947void setup () {
5048 Serial.begin (9600 );
5149 // This sets up the device WiFi.
5250 setupWiFi ();
53- // This initializes the SDK's configurations and returns a new object of Project class .
54- myProject = grandeur.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
51+ // This initializes the SDK's configurations and returns reference to your project .
52+ project = grandeur.init (apiKey, token);
53+ // Getting reference to your device .
54+ data = project .device (deviceID). data ( );
55+ // This schedules the connectionCallback() function to be called when connection with Grandeur
5856 // 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 Grandeur.
62- myDevice. onParms (parmsUpdatedCallback );
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 );
6361}
6462
6563void loop () {
6664 // In this loop() function, after every five seconds, we send the updated values of our
6765 // device's voltage and state to Grandeur.
68- if (myProject .isConnected ()) {
66+ if (project .isConnected ()) {
6967 if (millis () - current >= 5000 ) {
7068 // This if-condition makes sure that the code inside this block runs only after
7169 // every five seconds.
7270
73- Serial.println (" Setting Summary" );
74- JSONObject summary;
75- summary[" voltage" ] = analogRead (voltagePin);
76- // This updates the summary of our device on Grandeur and schedules summarySetCallback()
77- // function to be called when Grandeur 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 Grandeur and schedules parmsSetCallback()
84- // function to be called when Grandeur responds with the PARMS UPDATED message.
85- myDevice.setParms (parms, parmsSetCallback);
71+ Serial.println (" Setting Voltage" );
72+ int voltage = analogRead (voltagePin);
73+ // This updates the voltage of our device on Grandeur and schedules voltageSetCallback()
74+ // function to be called when Grandeur responds with the DATA UPDATED message.
75+ data.set (" voltage" , voltage, voltageSetCallback);
8676
8777 // This updates the millis counter for
8878 // the five seconds scheduler.
@@ -91,7 +81,7 @@ void loop() {
9181 }
9282
9383 // This runs the SDK only when the WiFi is connected.
94- myProject .loop (WiFi.status () == WL_CONNECTED);
84+ project .loop (WiFi.status () == WL_CONNECTED);
9585}
9686
9787void WiFiEventCallback (WiFiEvent_t event) {
@@ -121,69 +111,56 @@ void setupWiFi(void) {
121111 Serial.printf (" \n Device is connecting to WiFi using SSID %s and Passphrase %s.\n " , ssid, passphrase);
122112}
123113
124- void connectionCallback (bool state ) {
125- switch (state ) {
114+ void connectionCallback (bool status ) {
115+ switch (status ) {
126116 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 ..." );
117+ // On successful connection with Grandeur , we initialize the device's *state*.
118+ // To do that, we get device state from Grandeur and set the *state pin* to its
119+ // value.
120+ Serial.println (" Device is connected with Grandeur ." );
121+ data. get ( " state " , initializeState);
122+ Serial.println (" Listening for state update from Grandeur ..." );
133123 // Initializing the millis counter for the five
134124 // seconds timer.
135125 current = millis ();
136126 break ;
137127 case DISCONNECTED:
138- Serial.println (" Device is disconnected from the cloud ." );
128+ Serial.println (" Device's connection with Grandeur is broken ." );
139129 break ;
140130 }
141131}
142132
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" ];
133+ void initializeState (Var getResult) {
134+ // This function sets the *state pin* to the *state value* that we received in data
135+ // from Grandeur.
136+ if (getResult[" code" ] == " DEVICE-DATA-FETCHED" ) {
137+ int state = getResult[" data" ];
138+ Serial.printf (" State is: %d\n " , state);
148139 digitalWrite (statePin, state);
149140 return ;
150141 }
151- // If the parms could not be fetched.
152- Serial.println (" Failed to Fetch Parms " );
142+ // If the data could not be fetched.
143+ Serial.println (" Failed to Fetch State " );
153144 return ;
154145}
155146
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" ]);
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.
150+ Serial.printf (" Updated State is: %d\n " , state);
151+ digitalWrite (statePin, state);
161152}
162153
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 " ] );
154+ void voltageSetCallback (Var setResult) {
155+ if (setResult[" code" ] == " DEVICE-DATA -UPDATED" ) {
156+ Serial.printf (" Voltage is updated to: %d\n " , (int ) setResult[" update" ]);
166157
167158 /* You can set some pins or trigger events here which depend on successful
168- ** device summary update.
159+ ** voltage update.
169160 */
170161 return ;
171162 }
172- // If the summary could not be updated.
173- Serial.println (" Failed to Update Summary " );
163+ // If the voltage could not be updated.
164+ Serial.println (" Failed to Update Voltage " );
174165 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- }
166+ }
0 commit comments