|
| 1 | +/* SECRET_ fields are in `arduino_secrets.h` (included below) |
| 2 | + * |
| 3 | + * If using a Host + Notecard connected over I2C you'll need a |
| 4 | + * NotecardConnectionHandler object as follows: |
| 5 | + * |
| 6 | + * NotecardConnectionHandler conMan(NOTECARD_PRODUCT_UID); |
| 7 | + * |
| 8 | + * If using a Host + Notecard connected over Serial you'll need a |
| 9 | + * NotecardConnectionHandler object as follows: |
| 10 | + * |
| 11 | + * NotecardConnectionHandler conMan(NOTECARD_PRODUCT_UID, UART_INTERFACE); |
| 12 | + */ |
| 13 | + |
| 14 | +#include <Notecard.h> // MUST include this first to enable Notecard support |
| 15 | +#include <Arduino_ConnectionHandler.h> |
| 16 | + |
| 17 | +#include "arduino_secrets.h" |
| 18 | + |
| 19 | +/* Uncomment the following line to use this example in a manner that is more |
| 20 | + * compatible with LoRa. |
| 21 | + */ |
| 22 | +// #define USE_NOTE_LORA |
| 23 | + |
| 24 | +#ifndef USE_NOTE_LORA |
| 25 | +#define CONN_TOGGLE_MS 60000 |
| 26 | +#else |
| 27 | +#define CONN_TOGGLE_MS 300000 |
| 28 | +#endif |
| 29 | + |
| 30 | +/* The Notecard can provide connectivity to almost any board via ESLOV (I2C) |
| 31 | + * or UART. An empty string (or the default value provided below) will not |
| 32 | + * override the Notecard's existing configuration. |
| 33 | + * Learn more at: https://dev.blues.io */ |
| 34 | +#define NOTECARD_PRODUCT_UID "com.domain.you:product" |
| 35 | + |
| 36 | +/* Uncomment the following line to use the Notecard over UART */ |
| 37 | +// #define UART_INTERFACE Serial1 |
| 38 | + |
| 39 | +#ifndef UART_INTERFACE |
| 40 | +NotecardConnectionHandler conMan(NOTECARD_PRODUCT_UID); |
| 41 | +#else |
| 42 | +NotecardConnectionHandler conMan(NOTECARD_PRODUCT_UID, UART_INTERFACE); |
| 43 | +#endif |
| 44 | + |
| 45 | +bool attemptConnect = false; |
| 46 | +uint32_t lastConnToggleMs = 0; |
| 47 | + |
| 48 | +void setup() { |
| 49 | + /* Initialize serial debug port and wait up to 5 seconds for port to open */ |
| 50 | + Serial.begin(9600); |
| 51 | + for(unsigned long const serialBeginTime = millis(); !Serial && (millis() - serialBeginTime <= 5000); ) { } |
| 52 | + |
| 53 | + /* Set the debug message level: |
| 54 | + * - DBG_ERROR: Only show error messages |
| 55 | + * - DBG_WARNING: Show warning and error messages |
| 56 | + * - DBG_INFO: Show info, warning, and error messages |
| 57 | + * - DBG_DEBUG: Show debug, info, warning, and error messages |
| 58 | + * - DBG_VERBOSE: Show all messages |
| 59 | + */ |
| 60 | + setDebugMessageLevel(DBG_INFO); |
| 61 | + |
| 62 | + /* Add callbacks to the ConnectionHandler object to get notified of network |
| 63 | + * connection events. */ |
| 64 | + conMan.addCallback(NetworkConnectionEvent::CONNECTED, onNetworkConnect); |
| 65 | + conMan.addCallback(NetworkConnectionEvent::DISCONNECTED, onNetworkDisconnect); |
| 66 | + conMan.addCallback(NetworkConnectionEvent::ERROR, onNetworkError); |
| 67 | + |
| 68 | + /* First call to `check()` initializes the connection to the Notecard. While |
| 69 | + * not strictly necessary, it cleans up the logging from this application. |
| 70 | + */ |
| 71 | + conMan.check(); |
| 72 | + |
| 73 | +#ifndef USE_NOTE_LORA |
| 74 | + /* Set the Wi-Fi credentials for the Notecard */ |
| 75 | + String ssid = SECRET_WIFI_SSID; |
| 76 | + if (ssid.length() > 0 && ssid != "NETWORK NAME") { |
| 77 | + conMan.setWiFiCredentials(SECRET_WIFI_SSID, SECRET_WIFI_PASS); |
| 78 | + } |
| 79 | +#else |
| 80 | + conMan.setNotehubPollingInterval(720); // poll twice per day |
| 81 | +#endif |
| 82 | + |
| 83 | + /* Confirm Interface */ |
| 84 | + Serial.print("Network Adapter Interface: "); |
| 85 | + if (NetworkAdapter::NOTECARD == conMan.getInterface()) { |
| 86 | + Serial.print("Notecard "); |
| 87 | + Serial.print(conMan.getNotecardUid()); |
| 88 | +#ifndef UART_INTERFACE |
| 89 | + Serial.println(" (via I2C)"); |
| 90 | +#else |
| 91 | + Serial.println(" (via UART)"); |
| 92 | +#endif |
| 93 | + } else { |
| 94 | + Serial.println("ERROR: Unexpected Interface"); |
| 95 | + while(1); |
| 96 | + } |
| 97 | + |
| 98 | + /* Display the Arduino IoT Cloud Device ID */ |
| 99 | + displayCachedDeviceId(); |
| 100 | +} |
| 101 | + |
| 102 | +void loop() { |
| 103 | + /* Toggle the connection every `CONN_TOGGLE_MS` milliseconds */ |
| 104 | + if ((millis() - lastConnToggleMs) > CONN_TOGGLE_MS) { |
| 105 | + Serial.println("Toggling connection..."); |
| 106 | + if (attemptConnect) { |
| 107 | + displayCachedDeviceId(); |
| 108 | + conMan.connect(); |
| 109 | + } else { |
| 110 | + // Flush any queued Notecard requests before disconnecting |
| 111 | + conMan.initiateNotehubSync(NotecardConnectionHandler::SyncType::Outbound); |
| 112 | + conMan.disconnect(); |
| 113 | + } |
| 114 | + attemptConnect = !attemptConnect; |
| 115 | + lastConnToggleMs = millis(); |
| 116 | + } |
| 117 | + |
| 118 | + /* The following code keeps on running connection workflows on our |
| 119 | + * ConnectionHandler object, hence allowing reconnection in case of failure |
| 120 | + * and notification of connect/disconnect event if enabled (see |
| 121 | + * addConnectCallback/addDisconnectCallback) NOTE: any use of delay() within |
| 122 | + * the loop or methods called from it will delay the execution of .update(), |
| 123 | + * which might not guarantee the correct functioning of the ConnectionHandler |
| 124 | + * object. |
| 125 | + */ |
| 126 | + conMan.check(); |
| 127 | +} |
| 128 | + |
| 129 | +void displayCachedDeviceId() { |
| 130 | + Serial.print("Cached Arduino IoT Cloud Device ID: "); |
| 131 | + Serial.println(conMan.getDeviceId()); |
| 132 | +} |
| 133 | + |
| 134 | +void onNetworkConnect() { |
| 135 | + Serial.println(">>>> CONNECTED to network"); |
| 136 | +} |
| 137 | + |
| 138 | +void onNetworkDisconnect() { |
| 139 | + Serial.println(">>>> DISCONNECTED from network"); |
| 140 | +} |
| 141 | + |
| 142 | +void onNetworkError() { |
| 143 | + Serial.println(">>>> ERROR"); |
| 144 | +} |
0 commit comments