-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetworkConfiguratorDemo.ino
121 lines (104 loc) · 4.79 KB
/
NetworkConfiguratorDemo.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
Copyright (c) 2025 Arduino SA
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
/*
* This sketch shows how to use the NetworkConfigurator library to configure the
* network connectivity of the board. The library provides a way to configure and update
* the network settings using different interfaces like BLE, Serial, etc.
* The NetworkConfigurator library stores the provided network settings in a persistent
* key-value storage (KVStore) and uses the ConnectionHandler library to connect to the network.
* At startup the NetworkConfigurator library reads the stored network settings from the storage
* and loads them into the ConnectionHandler object.
*
* The NetworkConfigurator library provides a way for wiping out the stored network settings.
* Reconfiguration procedure:
* - Arduino Opta: press and hold the user button (BTN_USER) until the led (LED_USER) turns off
* - Arduino MKR WiFi 1010: short the pin 7 to GND until the led turns off
* - Arduino GIGA R1 WiFi: short the pin 7 to GND until the led turns off
* - Arduino Nano RP2040 Connect: short the pin 2 to 3.3V until the led turns off
* - Other boards: short the pin 2 to GND until the led turns off
*
* In this sketch the BLE and Serial interfaces are always enabled and ready for accepting
* a new set of configuration.
*
* Sketch originally added 21 March 2025
* by Fabio Centonze
*/
#include <Arduino_ConnectionHandler.h>
#include <GenericConnectionHandler.h>
#include <Arduino_KVStore.h>
#include <Arduino_NetworkConfigurator.h>
#include <ConfiguratorAgents/agents/BLE/BLEAgent.h>
#include <ConfiguratorAgents/agents/Serial/SerialAgent.h>
KVStore kvstore;
BLEAgentClass BLEAgent;
SerialAgentClass SerialAgent;
GenericConnectionHandler conMan;
NetworkConfiguratorClass NetworkConfigurator(conMan);
void setup() {
/* Initialize serial debug port and wait up to 5 seconds for port to open */
Serial.begin(9600);
for(unsigned long const serialBeginTime = millis(); !Serial && (millis() - serialBeginTime <= 5000); ) { }
/* Set the debug message level:
* - DBG_ERROR: Only show error messages
* - DBG_WARNING: Show warning and error messages
* - DBG_INFO: Show info, warning, and error messages
* - DBG_DEBUG: Show debug, info, warning, and error messages
* - DBG_VERBOSE: Show all messages
*/
setDebugMessageLevel(DBG_INFO);
/* Add callbacks to the ConnectionHandler object to get notified of network
* connection events. */
conMan.addCallback(NetworkConnectionEvent::CONNECTED, onNetworkConnect);
conMan.addCallback(NetworkConnectionEvent::DISCONNECTED, onNetworkDisconnect);
conMan.addCallback(NetworkConnectionEvent::ERROR, onNetworkError);
/* Set the KVStore */
NetworkConfigurator.setStorage(kvstore);
/* Add the interfaces that are enabled for configuring the network*/
NetworkConfigurator.addAgent(BLEAgent);
NetworkConfigurator.addAgent(SerialAgent);
//Uncomment this for disabling the reconfiguration pin and the reconfiguration procedure
//NetworkConfigurator.setReconfigurePin(DISABLE_PIN);
/* Uncomment and specify your preferred pin for changing the default reconfiguration pin
* The pin must be in the list of digital pins usable for interrupts.
* Please refer to the Arduino documentation for more details:
* https://docs.arduino.cc/language-reference/en/functions/external-interrupts/attachInterrupt/
*/
//NetworkConfigurator.setReconfigurePin(your_pin);
/* Add a custom callback function to be invoked every time the interrupt on reconfiguration pin is fired*/
NetworkConfigurator.addReconfigurePinCallback(onResetPinInterrupt);
/* Start the network configurator */
NetworkConfigurator.begin();
}
void loop() {
/*
* Poll the networkConfigurator and check if connectionHandler is configured.
* If the ConnectionHandler has been configured, the following code keeps on
* running connection workflows on our allowing reconnection in case of failure
* and notification of connect/disconnect event if enabled.
*
* NOTE: any use of delay() within the loop or methods called from it will delay
* the execution of .update() and .check() methods of the NetworkConfigurator and
* ConnectionHandler objects which might not guarantee the correct functioning
* of the code.
*/
if(NetworkConfigurator.update() == NetworkConfiguratorStates::CONFIGURED) {
conMan.check();
}
}
void onResetPinInterrupt() {
//Add your custom code here
//This function is called when the reconfiguration pin interrupt is fired
}
void onNetworkConnect() {
Serial.println(">>>> CONNECTED to network");
}
void onNetworkDisconnect() {
Serial.println(">>>> DISCONNECTED from network");
}
void onNetworkError() {
Serial.println(">>>> ERROR");
}