|
| 1 | +/** |
| 2 | + * @file CrossListening-esp32.ino |
| 3 | + * @date 24.03.2020 |
| 4 | + * @author Grandeur Technologies |
| 5 | + * |
| 6 | + * Copyright (c) 2019 Grandeur Technologies LLP. All rights reserved. |
| 7 | + * This file is part of the Arduino SDK for Grandeur Cloud. |
| 8 | + * |
| 9 | + * Apollo.h is used for device's communication with Grandeur Cloud. |
| 10 | + * WiFi.h is used for handling device's WiFi. |
| 11 | + * |
| 12 | + * Cross listening means when the device listens for updates from the app and the app |
| 13 | + * listens for updates from the device. |
| 14 | + * This example illustrates pretty much every basic thing you'd need in order to monitor / |
| 15 | + * control your device through Grandeur Cloud. Here are some of those: |
| 16 | + * 1. Listen to the cloud for updates in parms variables. |
| 17 | + * 2. Publish updates in summary and parms variables to the cloud every 5 seconds. |
| 18 | + * 3. Controlling SDK's internal valve. This helps if you want to run the SDK only when a |
| 19 | + * certain condition is true; in our case, if the WiFi is connected. |
| 20 | +*/ |
| 21 | + |
| 22 | +#include <Apollo.h> |
| 23 | +#include <WiFi.h> |
| 24 | + |
| 25 | +// Device's connection configurations |
| 26 | +String apiKey = "YOUR-PROJECT-APIKEY"; |
| 27 | +String deviceID = "YOUR-DEVICE-ID"; |
| 28 | +String token = "YOUR-ACCESS-TOKEN"; |
| 29 | +const char* ssid = "YOUR-WIFI-SSID"; |
| 30 | +const char* passphrase = "YOUR-WIFI-PASSWORD"; |
| 31 | + |
| 32 | +// Declaring and initializing other variables |
| 33 | +unsigned long current = millis(); |
| 34 | +Project myProject; |
| 35 | +Device myDevice; |
| 36 | +int statePin = 4; |
| 37 | +int voltagePin = 2; |
| 38 | + |
| 39 | +// Function prototypes |
| 40 | +void WiFiEventCallback(WiFiEvent_t event); |
| 41 | +void 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 | + |
| 48 | + |
| 49 | +void setup() { |
| 50 | + Serial.begin(9600); |
| 51 | + // This sets up the device WiFi. |
| 52 | + setupWiFi(); |
| 53 | + // This initializes the SDK's configurations and returns a new object of Project class. |
| 54 | + myProject = apollo.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 |
| 58 | + // 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 the Cloud. |
| 62 | + myDevice.onParms(parmsUpdatedCallback); |
| 63 | +} |
| 64 | + |
| 65 | +void loop() { |
| 66 | + // In this loop() function, after every five seconds, we send the updated values of our |
| 67 | + // device's voltage and state to the Cloud. |
| 68 | + if(myProject.isConnected()) { |
| 69 | + if(millis() - current >= 5000) { |
| 70 | + // This if-condition makes sure that the code inside this block runs only after |
| 71 | + // every five seconds. |
| 72 | + |
| 73 | + Serial.println("Setting Summary"); |
| 74 | + JSONObject summary; |
| 75 | + summary["voltage"] = analogRead(voltagePin); |
| 76 | + // This updates the summary of our device on the Cloud and schedules summarySetCallback() |
| 77 | + // function to be called when the Cloud 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 the Cloud and schedules parmsSetCallback() |
| 84 | + // function to be called when the Cloud responds with the PARMS UPDATED message. |
| 85 | + myDevice.setParms(parms, parmsSetCallback); |
| 86 | + |
| 87 | + // This updates the millis counter for |
| 88 | + // the five seconds scheduler. |
| 89 | + current = millis(); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + // This runs the SDK only when the WiFi is connected. |
| 94 | + myProject.loop(WiFi.status() == WL_CONNECTED); |
| 95 | +} |
| 96 | + |
| 97 | +void WiFiEventCallback(WiFiEvent_t event) { |
| 98 | + switch(event) { |
| 99 | + case SYSTEM_EVENT_STA_GOT_IP: |
| 100 | + // This runs when the device connects with WiFi. |
| 101 | + Serial.printf("\nDevice has successfully connected to WiFi. Its IP Address is: %s\n", |
| 102 | + WiFi.localIP().toString().c_str()); |
| 103 | + break; |
| 104 | + case SYSTEM_EVENT_STA_DISCONNECTED: |
| 105 | + // This runs when the device disconnects with WiFi. |
| 106 | + Serial.println("Device is disconnected from WiFi."); |
| 107 | + break; |
| 108 | + default: break; |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +void setupWiFi(void) { |
| 113 | + // Disconnecting WiFi if it"s already connected |
| 114 | + WiFi.disconnect(); |
| 115 | + // Setting it to Station mode which basically scans for nearby WiFi routers |
| 116 | + WiFi.mode(WIFI_STA); |
| 117 | + // Setting WiFi event handler |
| 118 | + WiFi.onEvent(WiFiEventCallback); |
| 119 | + // Begin connecting to WiFi |
| 120 | + WiFi.begin(ssid, passphrase); |
| 121 | + Serial.printf("\nDevice is connecting to WiFi using SSID %s and Passphrase %s.\n", ssid, passphrase); |
| 122 | +} |
| 123 | + |
| 124 | +void connectionCallback(bool state) { |
| 125 | + switch(state) { |
| 126 | + 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..."); |
| 133 | + // Initializing the millis counter for the five |
| 134 | + // seconds timer. |
| 135 | + current = millis(); |
| 136 | + break; |
| 137 | + case DISCONNECTED: |
| 138 | + Serial.println("Device is disconnected from the cloud."); |
| 139 | + break; |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 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"]; |
| 148 | + digitalWrite(statePin, state); |
| 149 | + return; |
| 150 | + } |
| 151 | + // If the parms could not be fetched. |
| 152 | + Serial.println("Failed to Fetch Parms"); |
| 153 | + return; |
| 154 | +} |
| 155 | + |
| 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"]); |
| 161 | +} |
| 162 | + |
| 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"]); |
| 166 | + |
| 167 | + /* You can set some pins or trigger events here which depend on successful |
| 168 | + ** device summary update. |
| 169 | + */ |
| 170 | + return; |
| 171 | + } |
| 172 | + // If the summary could not be updated. |
| 173 | + Serial.println("Failed to Update Summary"); |
| 174 | + 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 | +} |
0 commit comments