Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 61 additions & 45 deletions examples/CrossListening/CrossListening-esp32/CrossListening-esp32.ino
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
String apiKey = "YOUR-PROJECT-APIKEY";
String deviceID = "YOUR-DEVICE-ID";
String token = "YOUR-ACCESS-TOKEN";
const char* ssid = "YOUR-WIFI-SSID";
const char* passphrase = "YOUR-WIFI-PASSWORD";
const char *ssid = "YOUR-WIFI-SSID";
const char *passphrase = "YOUR-WIFI-PASSWORD";

// Handles our 5 second timer in loop().
unsigned long currentTime = millis();
Expand All @@ -49,11 +49,12 @@ void startWiFi(void);
// Handles Grandeur connection/disconnection events.
void GrandeurConnectionCallback(bool state);
// Data get/set/update callback functions:
void initializeStatePin(const char* code, bool state);
void setStatePinToNewValue(const char* path, bool state);
void afterVoltageIsUpdated(const char* code, int voltage);
void initializeStatePin(const char *code, bool state);
void setStatePinToNewValue(const char *path, bool state);
void afterVoltageIsUpdated(const char *code, int voltage);

void setup() {
void setup()
{
.begin(9600);
startWiFi();
// This initializes the SDK's configurations and returns reference to your project.
Expand All @@ -69,11 +70,14 @@ void setup() {
Serial.println("Listening for state update from Grandeur...");
}

void loop() {
void loop()
{
// In this loop() function, after every five seconds, we send the updated values of our
// device's voltage to Grandeur.
if(project.isConnected()) {
if(millis() - currentTime >= 5000) {
if (project.isConnected())
{
if (millis() - currentTime >= 5000)
{
// This if-condition makes sure that the code inside this block runs only after
// every five seconds.

Expand All @@ -89,25 +93,30 @@ void loop() {
}

// This runs the SDK only when the WiFi is connected.
project.loop(WiFi.status() == WL_CONNECTED);
if (WiFi.status() == WL_CONNECTED)
project.loop();
}

void WiFiEventCallback(WiFiEvent_t event) {
switch(event) {
case SYSTEM_EVENT_STA_GOT_IP:
// This runs when the device connects with WiFi.
Serial.printf("\nDevice has successfully connected to WiFi. Its IP Address is: %s\n",
WiFi.localIP().toString().c_str());
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
// This runs when the device disconnects with WiFi.
Serial.println("Device is disconnected from WiFi.");
break;
default: break;
void WiFiEventCallback(WiFiEvent_t event)
{
switch (event)
{
case SYSTEM_EVENT_STA_GOT_IP:
// This runs when the device connects with WiFi.
Serial.printf("\nDevice has successfully connected to WiFi. Its IP Address is: %s\n",
WiFi.localIP().toString().c_str());
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
// This runs when the device disconnects with WiFi.
Serial.println("Device is disconnected from WiFi.");
break;
default:
break;
}
}

void startWiFi(void) {
void startWiFi(void)
{
// Disconnecting WiFi if it"s already connected
WiFi.disconnect();
// Setting it to Station mode which basically scans for nearby WiFi routers
Expand All @@ -119,29 +128,33 @@ void startWiFi(void) {
Serial.printf("\nDevice is connecting to WiFi using SSID %s and Passphrase %s.\n", ssid, passphrase);
}

void GrandeurConnectionCallback(bool status) {
switch(status) {
case CONNECTED: // Expands to true.
Serial.println("Device is connected with Grandeur.");
// On successful connection with Grandeur, we initialize the device's *state*.
// To do that, we get device state from Grandeur and set the *state pin* to its
// value.
data.get("state", initializeStatePin);

// Initializing the millis counter for the five
// seconds timer.
currentTime = millis();
break;
case DISCONNECTED: // Expands to false.
Serial.println("Device's connection with Grandeur is broken.");
break;
void GrandeurConnectionCallback(bool status)
{
switch (status)
{
case CONNECTED: // Expands to true.
Serial.println("Device is connected with Grandeur.");
// On successful connection with Grandeur, we initialize the device's *state*.
// To do that, we get device state from Grandeur and set the *state pin* to its
// value.
data.get("state", initializeStatePin);

// Initializing the millis counter for the five
// seconds timer.
currentTime = millis();
break;
case DISCONNECTED: // Expands to false.
Serial.println("Device's connection with Grandeur is broken.");
break;
}
}

void initializeStatePin(const char* code, bool state) {
void initializeStatePin(const char *code, bool state)
{
// This function sets the *state pin* to the *state value* that we received in data
// from Grandeur.
if(strcmp(code, "DEVICE-DATA-FETCHED") == 0) {
if (strcmp(code, "DEVICE-DATA-FETCHED") == 0)
{
Serial.printf("State is: %d\n", state);
digitalWrite(statePin, state);
return;
Expand All @@ -151,16 +164,19 @@ void initializeStatePin(const char* code, bool state) {
return;
}

void setStatePinToNewValue(const char* path, bool state) {
void setStatePinToNewValue(const char *path, bool state)
{
// This function sets the *state pin* to state value.
Serial.printf("Updated State is: %d\n", state);
digitalWrite(statePin, state);
}

void afterVoltageIsUpdated(const char* code, int voltage) {
if(strcmp(code, "DEVICE-DATA-UPDATED") == 0) {
void afterVoltageIsUpdated(const char *code, int voltage)
{
if (strcmp(code, "DEVICE-DATA-UPDATED") == 0)
{
Serial.printf("Voltage is updated to: %d\n", voltage);

/* You can set some pins or trigger events here which depend on successful
** voltage update.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
String apiKey = "YOUR-PROJECT-APIKEY";
String deviceID = "YOUR-DEVICE-ID";
String token = "YOUR-ACCESS-TOKEN";
const char* ssid = "YOUR-WIFI-SSID";
const char* passphrase = "YOUR-WIFI-PASSWORD";
const char *ssid = "YOUR-WIFI-SSID";
const char *passphrase = "YOUR-WIFI-PASSWORD";

// Handles our 5 second timer in loop().
unsigned long currentTime = millis();
Expand All @@ -50,11 +50,12 @@ void startWiFi(void);
// Handles Grandeur connection/disconnection events.
void GrandeurConnectionCallback(bool state);
// Data get/set/update callback functions:
void initializeStatePin(const char* code, bool state);
void setStatePinToNewValue(const char* path, bool state);
void afterVoltageIsUpdated(const char* code, int voltage);
void initializeStatePin(const char *code, bool state);
void setStatePinToNewValue(const char *path, bool state);
void afterVoltageIsUpdated(const char *code, int voltage);

void setup() {
void setup()
{
Serial.begin(9600);
startWiFi();
// This initializes the SDK's configurations and returns reference to your project.
Expand All @@ -70,11 +71,14 @@ void setup() {
Serial.println("Listening for state update from Grandeur...");
}

void loop() {
void loop()
{
// In this loop() function, after every five seconds, we send the updated values of our
// device's voltage to Grandeur.
if(project.isConnected()) {
if(millis() - currentTime >= 5000) {
if (project.isConnected())
{
if (millis() - currentTime >= 5000)
{
// This if-condition makes sure that the code inside this block runs only after
// every five seconds.

Expand All @@ -90,52 +94,60 @@ void loop() {
}

// This runs the SDK only when the WiFi is connected.
project.loop(WiFi.status() == WL_CONNECTED);
if (WiFi.status() == WL_CONNECTED)
project.loop();
}

void startWiFi(void) {
void startWiFi(void)
{
// Disconnecting WiFi if it"s already connected
WiFi.disconnect();
// Setting it to Station mode which basically scans for nearby WiFi routers
WiFi.mode(WIFI_STA);
// Setting WiFi event handlers
onWiFiConnectedHandler = WiFi.onStationModeGotIP([](const WiFiEventStationModeGotIP& event) {
// This runs when the device connects with WiFi.
Serial.printf("\nDevice has successfully connected to WiFi. Its IP Address is: %s\n",
WiFi.localIP().toString().c_str());
});
onWiFiDisconnectedHandler = WiFi.onStationModeDisconnected([](const WiFiEventStationModeDisconnected& event) {
// This runs when the device disconnects with WiFi.
Serial.println("Device is disconnected from WiFi.");
});
onWiFiConnectedHandler = WiFi.onStationModeGotIP([](const WiFiEventStationModeGotIP &event)
{
// This runs when the device connects with WiFi.
Serial.printf("\nDevice has successfully connected to WiFi. Its IP Address is: %s\n",
WiFi.localIP().toString().c_str());
});
onWiFiDisconnectedHandler = WiFi.onStationModeDisconnected([](const WiFiEventStationModeDisconnected &event)
{
// This runs when the device disconnects with WiFi.
Serial.println("Device is disconnected from WiFi.");
});
// Begin connecting to WiFi
WiFi.begin(ssid, passphrase);
Serial.printf("\nDevice is connecting to WiFi using SSID %s and Passphrase %s.\n", ssid.c_str(), passphrase.c_str());
}

void GrandeurConnectionCallback(bool status) {
switch(status) {
case CONNECTED: // Expands to true.
Serial.println("Device is connected with Grandeur.");
// On successful connection with Grandeur, we initialize the device's *state*.
// To do that, we get device state from Grandeur and set the *state pin* to its
// value.
data.get("state", initializeStatePin);

// Initializing the millis counter for the five
// seconds timer.
currentTime = millis();
break;
case DISCONNECTED: // Expands to false.
Serial.println("Device's connection with Grandeur is broken.");
break;
void GrandeurConnectionCallback(bool status)
{
switch (status)
{
case CONNECTED: // Expands to true.
Serial.println("Device is connected with Grandeur.");
// On successful connection with Grandeur, we initialize the device's *state*.
// To do that, we get device state from Grandeur and set the *state pin* to its
// value.
data.get("state", initializeStatePin);

// Initializing the millis counter for the five
// seconds timer.
currentTime = millis();
break;
case DISCONNECTED: // Expands to false.
Serial.println("Device's connection with Grandeur is broken.");
break;
}
}

void initializeStatePin(const char* code, bool state) {
void initializeStatePin(const char *code, bool state)
{
// This function sets the *state pin* to the *state value* that we received in data
// from Grandeur.
if(strcmp(code, "DEVICE-DATA-FETCHED") == 0) {
if (strcmp(code, "DEVICE-DATA-FETCHED") == 0)
{
Serial.printf("State is: %d\n", state);
digitalWrite(statePin, state);
return;
Expand All @@ -145,16 +157,19 @@ void initializeStatePin(const char* code, bool state) {
return;
}

void setStatePinToNewValue(const char* path, bool state) {
void setStatePinToNewValue(const char *path, bool state)
{
// This function sets the *state pin* to state value.
Serial.printf("Updated State is: %d\n", state);
digitalWrite(statePin, state);
}

void afterVoltageIsUpdated(const char* code, int voltage) {
if(strcmp(code, "DEVICE-DATA-UPDATED") == 0) {
void afterVoltageIsUpdated(const char *code, int voltage)
{
if (strcmp(code, "DEVICE-DATA-UPDATED") == 0)
{
Serial.printf("Voltage is updated to: %d\n", voltage);

/* You can set some pins or trigger events here which depend on successful
** voltage update.
*/
Expand Down
Loading