2828String apiKey = " YOUR-PROJECT-APIKEY" ;
2929String deviceID = " YOUR-DEVICE-ID" ;
3030String token = " YOUR-ACCESS-TOKEN" ;
31- const char * ssid = " YOUR-WIFI-SSID" ;
32- const char * passphrase = " YOUR-WIFI-PASSWORD" ;
31+ const char * ssid = " YOUR-WIFI-SSID" ;
32+ const char * passphrase = " YOUR-WIFI-PASSWORD" ;
3333
3434// Handles our 5 second timer in loop().
3535unsigned long currentTime = millis();
@@ -50,11 +50,12 @@ void startWiFi(void);
5050// Handles Grandeur connection/disconnection events.
5151void GrandeurConnectionCallback (bool state);
5252// 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);
53+ void initializeStatePin (const char * code, bool state);
54+ void setStatePinToNewValue (const char * path, bool state);
55+ void afterVoltageIsUpdated (const char * code, int voltage);
5656
57- void setup () {
57+ void setup ()
58+ {
5859 Serial.begin (9600 );
5960 startWiFi ();
6061 // This initializes the SDK's configurations and returns reference to your project.
@@ -70,11 +71,14 @@ void setup() {
7071 Serial.println (" Listening for state update from Grandeur..." );
7172}
7273
73- void loop () {
74+ void loop ()
75+ {
7476 // In this loop() function, after every five seconds, we send the updated values of our
7577 // device's voltage to Grandeur.
76- if (project.isConnected ()) {
77- if (millis () - currentTime >= 5000 ) {
78+ if (project.isConnected ())
79+ {
80+ if (millis () - currentTime >= 5000 )
81+ {
7882 // This if-condition makes sure that the code inside this block runs only after
7983 // every five seconds.
8084
@@ -90,52 +94,60 @@ void loop() {
9094 }
9195
9296 // This runs the SDK only when the WiFi is connected.
93- project.loop (WiFi.status () == WL_CONNECTED);
97+ if (WiFi.status () == WL_CONNECTED)
98+ project.loop ();
9499}
95100
96- void startWiFi (void ) {
101+ void startWiFi (void )
102+ {
97103 // Disconnecting WiFi if it"s already connected
98104 WiFi.disconnect ();
99105 // Setting it to Station mode which basically scans for nearby WiFi routers
100106 WiFi.mode (WIFI_STA);
101107 // Setting WiFi event handlers
102- onWiFiConnectedHandler = WiFi.onStationModeGotIP ([](const WiFiEventStationModeGotIP& event) {
103- // This runs when the device connects with WiFi.
104- Serial.printf (" \n Device has successfully connected to WiFi. Its IP Address is: %s\n " ,
105- WiFi.localIP ().toString ().c_str ());
106- });
107- onWiFiDisconnectedHandler = WiFi.onStationModeDisconnected ([](const WiFiEventStationModeDisconnected& event) {
108- // This runs when the device disconnects with WiFi.
109- Serial.println (" Device is disconnected from WiFi." );
110- });
108+ onWiFiConnectedHandler = WiFi.onStationModeGotIP ([](const WiFiEventStationModeGotIP &event)
109+ {
110+ // This runs when the device connects with WiFi.
111+ Serial.printf (" \n Device has successfully connected to WiFi. Its IP Address is: %s\n " ,
112+ WiFi.localIP ().toString ().c_str ());
113+ });
114+ onWiFiDisconnectedHandler = WiFi.onStationModeDisconnected ([](const WiFiEventStationModeDisconnected &event)
115+ {
116+ // This runs when the device disconnects with WiFi.
117+ Serial.println (" Device is disconnected from WiFi." );
118+ });
111119 // Begin connecting to WiFi
112120 WiFi.begin (ssid, passphrase);
113121 Serial.printf (" \n Device is connecting to WiFi using SSID %s and Passphrase %s.\n " , ssid.c_str (), passphrase.c_str ());
114122}
115123
116- void GrandeurConnectionCallback (bool status) {
117- switch (status) {
118- case CONNECTED: // Expands to true.
119- Serial.println (" Device is connected with Grandeur." );
120- // On successful connection with Grandeur, we initialize the device's *state*.
121- // To do that, we get device state from Grandeur and set the *state pin* to its
122- // value.
123- data.get (" state" , initializeStatePin);
124-
125- // Initializing the millis counter for the five
126- // seconds timer.
127- currentTime = millis ();
128- break ;
129- case DISCONNECTED: // Expands to false.
130- Serial.println (" Device's connection with Grandeur is broken." );
131- break ;
124+ void GrandeurConnectionCallback (bool status)
125+ {
126+ switch (status)
127+ {
128+ case CONNECTED: // Expands to true.
129+ Serial.println (" Device is connected with Grandeur." );
130+ // On successful connection with Grandeur, we initialize the device's *state*.
131+ // To do that, we get device state from Grandeur and set the *state pin* to its
132+ // value.
133+ data.get (" state" , initializeStatePin);
134+
135+ // Initializing the millis counter for the five
136+ // seconds timer.
137+ currentTime = millis ();
138+ break ;
139+ case DISCONNECTED: // Expands to false.
140+ Serial.println (" Device's connection with Grandeur is broken." );
141+ break ;
132142 }
133143}
134144
135- void initializeStatePin (const char * code, bool state) {
145+ void initializeStatePin (const char *code, bool state)
146+ {
136147 // This function sets the *state pin* to the *state value* that we received in data
137148 // from Grandeur.
138- if (strcmp (code, " DEVICE-DATA-FETCHED" ) == 0 ) {
149+ if (strcmp (code, " DEVICE-DATA-FETCHED" ) == 0 )
150+ {
139151 Serial.printf (" State is: %d\n " , state);
140152 digitalWrite (statePin, state);
141153 return ;
@@ -145,16 +157,19 @@ void initializeStatePin(const char* code, bool state) {
145157 return ;
146158}
147159
148- void setStatePinToNewValue (const char * path, bool state) {
160+ void setStatePinToNewValue (const char *path, bool state)
161+ {
149162 // This function sets the *state pin* to state value.
150163 Serial.printf (" Updated State is: %d\n " , state);
151164 digitalWrite (statePin, state);
152165}
153166
154- void afterVoltageIsUpdated (const char * code, int voltage) {
155- if (strcmp (code, " DEVICE-DATA-UPDATED" ) == 0 ) {
167+ void afterVoltageIsUpdated (const char *code, int voltage)
168+ {
169+ if (strcmp (code, " DEVICE-DATA-UPDATED" ) == 0 )
170+ {
156171 Serial.printf (" Voltage is updated to: %d\n " , voltage);
157-
172+
158173 /* You can set some pins or trigger events here which depend on successful
159174 ** voltage update.
160175 */
0 commit comments