|
| 1 | +/* SECRET_ fields are in `arduino_secrets.h` (included below) |
| 2 | + * |
| 3 | + * If using a WiFi board (Arduino MKR1000, MKR WiFi 1010, Nano 33 IoT, UNO |
| 4 | + * WiFi Rev 2 or ESP8266/32), create a WiFiConnectionHandler object by adding |
| 5 | + * Network Name (SECRET_WIFI_SSID) and password (SECRET_WIFI_PASS) in the |
| 6 | + * arduino_secrets.h file (or Secrets tab in Create Web Editor). |
| 7 | + * |
| 8 | + * WiFiConnectionHandler conMan(SECRET_WIFI_SSID, SECRET_WIFI_PASS); |
| 9 | + * |
| 10 | + * If using a MKR GSM 1400 or other GSM boards supporting the same API you'll |
| 11 | + * need a GSMConnectionHandler object as follows |
| 12 | + * |
| 13 | + * GSMConnectionHandler conMan(SECRET_PIN, SECRET_APN, SECRET_GSM_USER, SECRET_GSM_PASS); |
| 14 | + * |
| 15 | + * If using a MKR NB1500 you'll need a NBConnectionHandler object as follows |
| 16 | + * |
| 17 | + * NBConnectionHandler conMan(SECRET_PIN); |
| 18 | + * |
| 19 | + * If using a Portenta + Ethernet shield you'll need a EthernetConnectionHandler object as follows: |
| 20 | + * |
| 21 | + * DHCP mode |
| 22 | + * EthernetConnectionHandler conMan; |
| 23 | + * |
| 24 | + * Manual configuration |
| 25 | + * EthernetConnectionHandler conMan(SECRET_IP, SECRET_DNS, SECRET_GATEWAY, SECRET_NETMASK); |
| 26 | + * |
| 27 | + * Manual configuration will fallback on DHCP mode if SECRET_IP is invalid or equal to INADDR_NONE. |
| 28 | + * |
| 29 | + */ |
| 30 | + |
| 31 | +#include <GenericConnectionHandler.h> |
| 32 | + |
| 33 | +#include "arduino_secrets.h" |
| 34 | + |
| 35 | +#define CONN_TOGGLE_MS 60000 |
| 36 | + |
| 37 | +#if !(defined(BOARD_HAS_WIFI) || defined(BOARD_HAS_GSM) || defined(BOARD_HAS_LORA) || \ |
| 38 | + defined(BOARD_HAS_NB) || defined(BOARD_HAS_ETHERNET) || defined(BOARD_HAS_CATM1_NBIOT)) |
| 39 | + #error "Please check Arduino Connection Handler supported boards list: https://github.com/arduino-libraries/Arduino_ConnectionHandler/blob/master/README.md" |
| 40 | +#endif |
| 41 | + |
| 42 | +GenericConnectionHandler conMan; |
| 43 | + |
| 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 | +#ifndef __AVR__ |
| 54 | + /* Set the debug message level: |
| 55 | + * - DBG_ERROR: Only show error messages |
| 56 | + * - DBG_WARNING: Show warning and error messages |
| 57 | + * - DBG_INFO: Show info, warning, and error messages |
| 58 | + * - DBG_DEBUG: Show debug, info, warning, and error messages |
| 59 | + * - DBG_VERBOSE: Show all messages |
| 60 | + */ |
| 61 | + setDebugMessageLevel(DBG_INFO); |
| 62 | +#endif |
| 63 | + |
| 64 | + models::NetworkSetting setting = models::settingsDefault(NetworkAdapter::WIFI); |
| 65 | + |
| 66 | + strcpy(setting.wifi.ssid, SECRET_WIFI_SSID); |
| 67 | + strcpy(setting.wifi.pwd, SECRET_WIFI_PASS); |
| 68 | + |
| 69 | + /* Add callbacks to the ConnectionHandler object to get notified of network |
| 70 | + * connection events. */ |
| 71 | + conMan.addCallback(NetworkConnectionEvent::CONNECTED, onNetworkConnect); |
| 72 | + conMan.addCallback(NetworkConnectionEvent::DISCONNECTED, onNetworkDisconnect); |
| 73 | + conMan.addCallback(NetworkConnectionEvent::ERROR, onNetworkError); |
| 74 | + |
| 75 | + conMan.updateSetting(setting); |
| 76 | + Serial.print("Network Adapter Interface: "); |
| 77 | + switch (conMan.getInterface()) { |
| 78 | + case NetworkAdapter::WIFI: |
| 79 | + Serial.println("Wi-Fi"); |
| 80 | + break; |
| 81 | + case NetworkAdapter::ETHERNET: |
| 82 | + Serial.println("Ethernet"); |
| 83 | + break; |
| 84 | + case NetworkAdapter::NB: |
| 85 | + Serial.println("Narrowband"); |
| 86 | + break; |
| 87 | + case NetworkAdapter::GSM: |
| 88 | + Serial.println("GSM"); |
| 89 | + break; |
| 90 | + case NetworkAdapter::LORA: |
| 91 | + Serial.println("LoRa"); |
| 92 | + break; |
| 93 | + case NetworkAdapter::CATM1: |
| 94 | + Serial.println("Category M1"); |
| 95 | + break; |
| 96 | + case NetworkAdapter::CELL: |
| 97 | + Serial.println("Cellular"); |
| 98 | + break; |
| 99 | + default: |
| 100 | + Serial.println("Unknown"); |
| 101 | + break; |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +void loop() { |
| 106 | + /* Toggle the connection every `CONN_TOGGLE_MS` milliseconds */ |
| 107 | + if ((millis() - lastConnToggleMs) > CONN_TOGGLE_MS) { |
| 108 | + Serial.println("Toggling connection..."); |
| 109 | + if (attemptConnect) { |
| 110 | + conMan.connect(); |
| 111 | + } else { |
| 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 onNetworkConnect() { |
| 130 | + Serial.println(">>>> CONNECTED to network"); |
| 131 | +} |
| 132 | + |
| 133 | +void onNetworkDisconnect() { |
| 134 | + Serial.println(">>>> DISCONNECTED from network"); |
| 135 | +} |
| 136 | + |
| 137 | +void onNetworkError() { |
| 138 | + Serial.println(">>>> ERROR"); |
| 139 | +} |
0 commit comments