|
| 1 | +/* |
| 2 | + Copyright (c) 2025 Arduino SA |
| 3 | +
|
| 4 | + This Source Code Form is subject to the terms of the Mozilla Public |
| 5 | + License, v. 2.0. If a copy of the MPL was not distributed with this |
| 6 | + file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 7 | +*/ |
| 8 | + |
| 9 | +/* |
| 10 | + * This sketch shows how to use the NetworkConfigurator library to configure the |
| 11 | + * network connectivity of the board. The library provides a way to configure and update |
| 12 | + * the network settings using different interfaces like BLE, Serial, etc. |
| 13 | + * The NetworkConfigurator library stores the provided network settings in a persistent |
| 14 | + * key-value storage (KVStore) and uses the ConnectionHandler library to connect to the network. |
| 15 | + * At startup the NetworkConfigurator library reads the stored network settings from the storage |
| 16 | + * and loads them into the ConnectionHandler object. |
| 17 | + * |
| 18 | + * The NetworkConfigurator library provides a way for wiping out the stored network settings, |
| 19 | + * please read the documentation for more information. |
| 20 | + * |
| 21 | + * In this sketch the BLE and Serial interfaces are always enabled and ready for accepting |
| 22 | + * a new set of configuration. |
| 23 | + * |
| 24 | + * Sketch originally added 21 March 2025 |
| 25 | + * by Fabio Centonze |
| 26 | + */ |
| 27 | + |
| 28 | + #include <Arduino_ConnectionHandler.h> |
| 29 | + #include <GenericConnectionHandler.h> |
| 30 | + #include <Arduino_KVStore.h> |
| 31 | + #include <NetworkConfigurator.h> |
| 32 | + #include <ConfiguratorAgents/agents/BLE/BLEAgent.h> |
| 33 | + #include <ConfiguratorAgents/agents/Serial/SerialAgent.h> |
| 34 | + |
| 35 | + KVStore kvstore; |
| 36 | + BLEAgentClass BLEAgent; |
| 37 | + SerialAgentClass SerialAgent; |
| 38 | + GenericConnectionHandler conMan; |
| 39 | + NetworkConfiguratorClass NetworkConfigurator(conMan); |
| 40 | + |
| 41 | +void setup() { |
| 42 | + /* Initialize serial debug port and wait up to 5 seconds for port to open */ |
| 43 | + Serial.begin(9600); |
| 44 | + for(unsigned long const serialBeginTime = millis(); !Serial && (millis() - serialBeginTime <= 5000); ) { } |
| 45 | + |
| 46 | + /* Set the debug message level: |
| 47 | + * - DBG_ERROR: Only show error messages |
| 48 | + * - DBG_WARNING: Show warning and error messages |
| 49 | + * - DBG_INFO: Show info, warning, and error messages |
| 50 | + * - DBG_DEBUG: Show debug, info, warning, and error messages |
| 51 | + * - DBG_VERBOSE: Show all messages |
| 52 | + */ |
| 53 | + setDebugMessageLevel(DBG_INFO); |
| 54 | + |
| 55 | + /* Add callbacks to the ConnectionHandler object to get notified of network |
| 56 | + * connection events. */ |
| 57 | + conMan.addCallback(NetworkConnectionEvent::CONNECTED, onNetworkConnect); |
| 58 | + conMan.addCallback(NetworkConnectionEvent::DISCONNECTED, onNetworkDisconnect); |
| 59 | + conMan.addCallback(NetworkConnectionEvent::ERROR, onNetworkError); |
| 60 | + |
| 61 | + /* Set the KVStore */ |
| 62 | + NetworkConfigurator.setStorage(kvstore); |
| 63 | + /* Add the interfaces that are enabled for configuring the network*/ |
| 64 | + NetworkConfigurator.addAgent(BLEAgent); |
| 65 | + NetworkConfigurator.addAgent(SerialAgent); |
| 66 | + /* Start the network configurator */ |
| 67 | + NetworkConfigurator.begin(); |
| 68 | + |
| 69 | +} |
| 70 | + |
| 71 | +void loop() { |
| 72 | + /* |
| 73 | + * Poll the networkConfigurator and check if connectionHandler is configured. |
| 74 | + * If the ConnectionHandler has been configured, the following code keeps on |
| 75 | + * running connection workflows on our allowing reconnection in case of failure |
| 76 | + * and notification of connect/disconnect event if enabled. |
| 77 | + * |
| 78 | + * NOTE: any use of delay() within the loop or methods called from it will delay |
| 79 | + * the execution of .poll() and .check() methods of the NetworkConfigurator and |
| 80 | + * ConnectionHandler objects which might not guarantee the correct functioning |
| 81 | + * of the code. |
| 82 | + */ |
| 83 | + if(NetworkConfigurator.poll() == NetworkConfiguratorStates::CONFIGURED) { |
| 84 | + conMan.check(); |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +void onNetworkConnect() { |
| 89 | + Serial.println(">>>> CONNECTED to network"); |
| 90 | +} |
| 91 | + |
| 92 | +void onNetworkDisconnect() { |
| 93 | + Serial.println(">>>> DISCONNECTED from network"); |
| 94 | +} |
| 95 | + |
| 96 | +void onNetworkError() { |
| 97 | + Serial.println(">>>> ERROR"); |
| 98 | +} |
0 commit comments