2525String apiKey = " YOUR-PROJECT-APIKEY" ;
2626String deviceID = " YOUR-DEVICE-ID" ;
2727String 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().
3232unsigned long currentTime = millis();
@@ -35,37 +35,47 @@ Grandeur::Project project;
3535// Device data object to get/set/subscribe to device variables.
3636Grandeur::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.
4343WiFiEventHandler onWiFiConnectedHandler;
4444WiFiEventHandler onWiFiDisconnectedHandler;
4545// Starts the device WiFi.
4646void startWiFi (void );
4747// Handles Grandeur connection/disconnection events.
4848void 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 (" \n Device 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 (" \n Device 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 (" \n Device is connecting to WiFi using SSID %s and Passphrase %s.\n " , ssid. c_str () , passphrase. c_str () );
117+ Serial.printf (" \n Device 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 */
0 commit comments