2323#include < ESP8266WiFi.h>
2424
2525// Device's connection configurations
26- String deviceID = " YOUR-DEVICE-ID" ;
2726String apiKey = " YOUR-PROJECT-APIKEY" ;
27+ String deviceID = " YOUR-DEVICE-ID" ;
2828String token = " YOUR-ACCESS-TOKEN" ;
2929String ssid = " YOUR-WIFI-SSID" ;
3030String passphrase = " YOUR-WIFI-PASSWORD" ;
3131
3232// Declaring and initializing other variables
3333unsigned long current = millis();
34- ApolloDevice device;
34+ Project myProject;
35+ Device myDevice;
3536WiFiEventHandler onWiFiConnectedHandler;
3637WiFiEventHandler onWiFiDisconnectedHandler;
3738int statePin = D0;
@@ -40,30 +41,32 @@ int voltagePin = A0;
4041// Function prototypes
4142void setupWiFi (void );
4243void connectionCallback (JSONObject updateObject);
43- void initializeState (JSONObject payload );
44+ void initializeState (JSONObject getResult );
4445void parmsUpdatedCallback (JSONObject updatedParms);
45- void summarySetCallback (JSONObject payload );
46- void parmsSetCallback (JSONObject payload );
46+ void summarySetCallback (JSONObject setResult );
47+ void parmsSetCallback (JSONObject setResult );
4748
4849
4950void setup () {
5051 Serial.begin (9600 );
5152 // This sets up the device WiFi.
5253 setupWiFi ();
53- // This initializes the SDK's configurations and returns a new object of ApolloDevice class.
54- device = apollo.init (deviceID, apiKey, token);
55- // This schedules the connectionCallback() function to be called when the device makes/breaks
56- // connection with the cloud.
57- device.onConnection (connectionCallback);
58- // This schedules parmsUpdatedCallback() function to be called when someone changes any
59- // variable stored in device's parms on the Cloud.
60- device.onParmsUpdated (parmsUpdatedCallback);
54+ // This initializes the SDK's configurations and returns a new object of Project class.
55+ myProject = apollo.init (apiKey, token);
56+ // Getting object of Device class.
57+ myDevice = myProject.device (deviceID);
58+ // This schedules the connectionCallback() function to be called when connection with the cloud
59+ // is made/broken.
60+ myProject.onConnection (connectionCallback);
61+ // This schedules parmsUpdatedCallback() function to be called when variable stored
62+ // in device's parms are changed on the Cloud.
63+ myDevice.onParms (parmsUpdatedCallback);
6164}
6265
6366void loop () {
6467 // In this loop() function, after every five seconds, we send the updated values of our
6568 // device's voltage and state to the Cloud.
66- if (device. getState () == CONNECTED ) {
69+ if (myProject. isConnected () ) {
6770 if (millis () - current >= 5000 ) {
6871 // This if-condition makes sure that the code inside this block runs only after
6972 // every five seconds.
@@ -73,14 +76,14 @@ void loop() {
7376 summary[" voltage" ] = analogRead (voltagePin);
7477 // This updates the summary of our device on the Cloud and schedules summarySetCallback()
7578 // function to be called when the Cloud responds with the SUMMARY UPDATED message.
76- device .setSummary (summary, summarySetCallback);
79+ myDevice .setSummary (summary, summarySetCallback);
7780
7881 Serial.println (" Setting Parms" );
7982 JSONObject parms;
8083 parms[" state" ] = digitalRead (statePin);
8184 // This updates the parms of our device on the Cloud and schedules parmsSetCallback()
8285 // function to be called when the Cloud responds with the PARMS UPDATED message.
83- device .setParms (parms, parmsSetCallback);
86+ myDevice .setParms (parms, parmsSetCallback);
8487
8588 // This updates the millis counter for
8689 // the five seconds scheduler.
@@ -89,7 +92,7 @@ void loop() {
8992 }
9093
9194 // This runs the SDK only when the WiFi is connected.
92- device .loop (WiFi.status () == WL_CONNECTED);
95+ myProject .loop (WiFi.status () == WL_CONNECTED);
9396}
9497
9598void setupWiFi (void ) {
@@ -112,14 +115,15 @@ void setupWiFi(void) {
112115 Serial.printf (" \n Device is connecting to WiFi using SSID %s and Passphrase %s.\n " , ssid.c_str (), passphrase.c_str ());
113116}
114117
115- void connectionCallback (JSONObject updateObject ) {
116- switch (( int ) updateObject[ " event " ] ) {
118+ void connectionCallback (bool state ) {
119+ switch (state ) {
117120 case CONNECTED:
118121 // On successful connection with the cloud, we initialize the device's *state*.
119- // We set the *state pin* to the value of *state* that we receive from the cloud in parms.
122+ // To do that, we get device parms from the cloud and set the *state pin* to the
123+ // value of *state* in those parms.
120124 Serial.println (" Device is connected to the cloud." );
121- device .getParms (initializeState);
122-
125+ myDevice .getParms (initializeState);
126+ Serial. println ( " Listening for parms update from the cloud... " );
123127 // Initializing the millis counter for the five
124128 // seconds timer.
125129 current = millis ();
@@ -130,11 +134,11 @@ void connectionCallback(JSONObject updateObject) {
130134 }
131135}
132136
133- void initializeState (JSONObject payload ) {
137+ void initializeState (JSONObject getResult ) {
134138 // This function sets the *state pin* to the *state value* that we received in parms
135139 // from the cloud.
136- if (payload [" code" ] == " DEVICE-PARMS-FETCHED" ) {
137- int state = payload [" deviceParms" ][" state" ];
140+ if (getResult [" code" ] == " DEVICE-PARMS-FETCHED" ) {
141+ int state = getResult [" deviceParms" ][" state" ];
138142 digitalWrite (statePin, state);
139143 return ;
140144 }
@@ -150,9 +154,9 @@ void parmsUpdatedCallback(JSONObject updatedParms) {
150154 digitalWrite (statePin, (bool ) updatedParms[" state" ]);
151155}
152156
153- void summarySetCallback (JSONObject payload ) {
154- if (payload [" code" ] == " DEVICE-SUMMARY-UPDATED" ) {
155- Serial.printf (" Voltage is updated to: %d\n " , (int ) payload [" update" ][" voltage" ]);
157+ void summarySetCallback (JSONObject setResult ) {
158+ if (setResult [" code" ] == " DEVICE-SUMMARY-UPDATED" ) {
159+ Serial.printf (" Voltage is updated to: %d\n " , (int ) setResult [" update" ][" voltage" ]);
156160
157161 /* You can set some pins or trigger events here which depend on successful
158162 ** device summary update.
@@ -164,9 +168,9 @@ void summarySetCallback(JSONObject payload) {
164168 return ;
165169}
166170
167- void parmsSetCallback (JSONObject payload ) {
168- if (payload [" code" ] == " DEVICE-PARMS-UPDATED" ) {
169- Serial.printf (" State is updated to: %d\n " , (bool ) payload [" update" ][" state" ]);
171+ void parmsSetCallback (JSONObject setResult ) {
172+ if (setResult [" code" ] == " DEVICE-PARMS-UPDATED" ) {
173+ Serial.printf (" State is updated to: %d\n " , (bool ) setResult [" update" ][" state" ]);
170174
171175 /* You can set some pins or trigger events here which depend on successful
172176 ** device parms update.
0 commit comments