|
| 1 | +/* SECRET_ fields are in `arduino_secrets.h` (included below) |
| 2 | + * |
| 3 | + * This example is a lightly modified version of ConnectionHandlerDemo to showcase |
| 4 | + * the possibility of changing the timeout values for the different states a connectionhandler have |
| 5 | + */ |
| 6 | + |
| 7 | +#include <Arduino_ConnectionHandler.h> |
| 8 | + |
| 9 | +#include "arduino_secrets.h" |
| 10 | + |
| 11 | +#if !(defined(BOARD_HAS_WIFI) || defined(BOARD_HAS_GSM) || defined(BOARD_HAS_LORA) || \ |
| 12 | + defined(BOARD_HAS_NB) || defined(BOARD_HAS_ETHERNET) || defined(BOARD_HAS_CATM1_NBIOT)) |
| 13 | + #error "Please check Arduino Connection Handler supported boards list: https://github.com/arduino-libraries/Arduino_ConnectionHandler/blob/master/README.md" |
| 14 | +#endif |
| 15 | + |
| 16 | +#if defined(BOARD_HAS_ETHERNET) |
| 17 | +EthernetConnectionHandler conMan(SECRET_IP, SECRET_DNS, SECRET_GATEWAY, SECRET_NETMASK); |
| 18 | +#elif defined(BOARD_HAS_WIFI) |
| 19 | +WiFiConnectionHandler conMan(SECRET_WIFI_SSID, SECRET_WIFI_PASS); |
| 20 | +#elif defined(BOARD_HAS_GSM) |
| 21 | +GSMConnectionHandler conMan(SECRET_PIN, SECRET_APN, SECRET_GSM_USER, SECRET_GSM_PASS); |
| 22 | +#elif defined(BOARD_HAS_NB) |
| 23 | +NBConnectionHandler conMan(SECRET_PIN); |
| 24 | +#elif defined(BOARD_HAS_LORA) |
| 25 | +LoRaConnectionHandler conMan(SECRET_APP_EUI, SECRET_APP_KEY); |
| 26 | +#elif defined(BOARD_HAS_CATM1_NBIOT) |
| 27 | +CatM1ConnectionHandler conMan(SECRET_PIN, SECRET_APN, SECRET_GSM_USER, SECRET_GSM_PASS); |
| 28 | +#elif defined(BOARD_HAS_CELLULAR) |
| 29 | +CellularConnectionHandler conMan(SECRET_PIN, SECRET_APN, SECRET_GSM_USER, SECRET_GSM_PASS); |
| 30 | +#endif |
| 31 | + |
| 32 | +bool attemptConnect = false; |
| 33 | +uint32_t lastConnToggleMs = 0; |
| 34 | + |
| 35 | +void setup() { |
| 36 | + /* Initialize serial debug port and wait up to 5 seconds for port to open */ |
| 37 | + Serial.begin(9600); |
| 38 | + for(unsigned long const serialBeginTime = millis(); !Serial && (millis() - serialBeginTime <= 5000); ) { } |
| 39 | + |
| 40 | +#ifndef __AVR__ |
| 41 | + /* Set the debug message level: |
| 42 | + * - DBG_ERROR: Only show error messages |
| 43 | + * - DBG_WARNING: Show warning and error messages |
| 44 | + * - DBG_INFO: Show info, warning, and error messages |
| 45 | + * - DBG_DEBUG: Show debug, info, warning, and error messages |
| 46 | + * - DBG_VERBOSE: Show all messages |
| 47 | + */ |
| 48 | + setDebugMessageLevel(DBG_INFO); |
| 49 | +#endif |
| 50 | + |
| 51 | + /* Add callbacks to the ConnectionHandler object to get notified of network |
| 52 | + * connection events. */ |
| 53 | + conMan.addCallback(NetworkConnectionEvent::CONNECTED, onNetworkConnect); |
| 54 | + conMan.addCallback(NetworkConnectionEvent::DISCONNECTED, onNetworkDisconnect); |
| 55 | + conMan.addCallback(NetworkConnectionEvent::ERROR, onNetworkError); |
| 56 | + |
| 57 | + /* By using updateTimeoutInterval I can change the timeout value for a specific |
| 58 | + * state of the connection handler |
| 59 | + */ |
| 60 | + conMan.updateTimeoutInterval(NetworkConnectionState::INIT, 8000); |
| 61 | + conMan.updateTimeoutInterval(NetworkConnectionState::CONNECTING, 1000); |
| 62 | + conMan.updateTimeoutInterval(NetworkConnectionState::CONNECTED, 20000); |
| 63 | + conMan.updateTimeoutInterval(NetworkConnectionState::DISCONNECTING, 200); |
| 64 | + conMan.updateTimeoutInterval(NetworkConnectionState::DISCONNECTED, 2000); |
| 65 | + conMan.updateTimeoutInterval(NetworkConnectionState::CLOSED, 2000); |
| 66 | + conMan.updateTimeoutInterval(NetworkConnectionState::ERROR, 2000); |
| 67 | +} |
| 68 | + |
| 69 | +void loop() { |
| 70 | + conMan.check(); |
| 71 | +} |
| 72 | + |
| 73 | +void onNetworkConnect() { |
| 74 | + Serial.println(">>>> CONNECTED to network"); |
| 75 | +} |
| 76 | + |
| 77 | +void onNetworkDisconnect() { |
| 78 | + Serial.println(">>>> DISCONNECTED from network"); |
| 79 | +} |
| 80 | + |
| 81 | +void onNetworkError() { |
| 82 | + Serial.println(">>>> ERROR"); |
| 83 | +} |
0 commit comments