Skip to content

Commit 615cc71

Browse files
committed
feat: Wi-Fi Credentials not supported
1 parent 79532b1 commit 615cc71

File tree

5 files changed

+51
-7
lines changed

5 files changed

+51
-7
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Library for handling and managing network connections by providing keep-alive fu
2020
#if defined(BOARD_HAS_ETHERNET)
2121
EthernetConnectionHandler conMan;
2222
#elif defined(BOARD_HAS_WIFI)
23-
WiFiConnectionHandler conMan("SECRET_SSID", "SECRET_PASS");
23+
WiFiConnectionHandler conMan("SECRET_WIFI_SSID", "SECRET_WIFI_PASS");
2424
#elif defined(BOARD_HAS_GSM)
2525
GSMConnectionHandler conMan("SECRET_PIN", "SECRET_APN", "SECRET_GSM_LOGIN", "SECRET_GSM_PASS");
2626
#elif defined(BOARD_HAS_NB)

examples/ConnectionHandlerDemo/ConnectionHandlerDemo.ino

+9-4
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
*
1313
* If using a WiFi board (Arduino MKR1000, MKR WiFi 1010, Nano 33 IoT, UNO
1414
* WiFi Rev 2 or ESP8266/32), create a WiFiConnectionHandler object by adding
15-
* Network Name (SECRET_SSID) and password (SECRET_PASS) in the arduino_secrets.h
16-
* file (or Secrets tab in Create Web Editor).
15+
* Network Name (SECRET_WIFI_SSID) and password (SECRET_WIFI_PASS) in the
16+
* arduino_secrets.h file (or Secrets tab in Create Web Editor).
1717
*
18-
* WiFiConnectionHandler conMan(SECRET_SSID, SECRET_PASS);
18+
* WiFiConnectionHandler conMan(SECRET_WIFI_SSID, SECRET_WIFI_PASS);
1919
*
2020
* If using a MKR GSM 1400 or other GSM boards supporting the same API you'll
2121
* need a GSMConnectionHandler object as follows
@@ -42,6 +42,11 @@
4242

4343
#include <Arduino_ConnectionHandler.h>
4444

45+
#if !(defined(USE_NOTECARD) || defined(BOARD_HAS_WIFI) || defined(BOARD_HAS_GSM) || defined(BOARD_HAS_LORA) || \
46+
defined(BOARD_HAS_NB) || defined(BOARD_HAS_ETHERNET) || defined(BOARD_HAS_CATM1_NBIOT))
47+
#error "Please check Arduino Connection Handler supported boards list: https://github.com/arduino-libraries/Arduino_ConnectionHandler/blob/master/README.md"
48+
#endif
49+
4550
#if defined(USE_NOTECARD)
4651
/* The Notecard can provide connectivity to almost any board via ESLOV (I2C)
4752
* or UART. An empty string (or the default value provided below) will not
@@ -55,7 +60,7 @@ NotecardConnectionHandler conMan(NOTECARD_PRODUCT_UID);
5560
#elif defined(BOARD_HAS_ETHERNET)
5661
EthernetConnectionHandler conMan(SECRET_IP, SECRET_DNS, SECRET_GATEWAY, SECRET_NETMASK);
5762
#elif defined(BOARD_HAS_WIFI)
58-
WiFiConnectionHandler conMan(SECRET_SSID, SECRET_PASS);
63+
WiFiConnectionHandler conMan(SECRET_WIFI_SSID, SECRET_WIFI_PASS);
5964
#elif defined(BOARD_HAS_GSM)
6065
GSMConnectionHandler conMan(SECRET_PIN, SECRET_APN, SECRET_GSM_USER, SECRET_GSM_PASS);
6166
#elif defined(BOARD_HAS_NB)

examples/ConnectionHandlerDemo/arduino_secrets.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Required for WiFiConnectionHandler
2-
const char SECRET_SSID[] = "NETWORK NAME";
3-
const char SECRET_PASS[] = "NETWORK PASSWORD";
2+
const char SECRET_WIFI_SSID[] = "NETWORK NAME";
3+
const char SECRET_WIFI_PASS[] = "NETWORK PASSWORD";
44

55
// Required for GSMConnectionHandler
66
const char SECRET_APN[] = "MOBILE PROVIDER APN ADDRESS";

src/Arduino_NotecardConnectionHandler.cpp

+38
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,42 @@ NotecardConnectionHandler::NotecardConnectionHandler(
161161
PUBLIC MEMBER FUNCTIONS
162162
******************************************************************************/
163163

164+
int NotecardConnectionHandler::setWiFiCredentials (const String & ssid_, const String & password_)
165+
{
166+
int result;
167+
168+
// Validate the connection state is not in an initialization state
169+
if (check() == NetworkConnectionState::INIT)
170+
{
171+
Debug.print(DBG_ERROR, F("Failed to set WiFi credentials. Connection has not been initialized."));
172+
result = NotecardCommunicationError::NOTECARD_ERROR_GENERIC;
173+
} else if (J *req = _notecard.newRequest("card.wifi")) {
174+
JAddStringToObject(req, "ssid", ssid_.c_str());
175+
JAddStringToObject(req, "password", password_.c_str());
176+
if (J *rsp = _notecard.requestAndResponse(req)) {
177+
// Check the response for errors
178+
if (NoteResponseError(rsp)) {
179+
const char *err = JGetString(rsp, "err");
180+
Debug.print(DBG_ERROR, F("%s"), err);
181+
Debug.print(DBG_ERROR, F("Failed to set WiFi credentials."));
182+
result = NotecardCommunicationError::NOTECARD_ERROR_GENERIC;
183+
} else {
184+
Debug.print(DBG_INFO, F("WiFi credentials updated. ssid: \"%s\" password: \"%s\"."), ssid_.c_str(), password_.length() ? "**********" : "");
185+
result = NotecardCommunicationError::NOTECARD_ERROR_NONE;
186+
}
187+
JDelete(rsp);
188+
} else {
189+
Debug.print(DBG_ERROR, F("Failed to receive response from Notecard."));
190+
result = NotecardCommunicationError::NOTECARD_ERROR_GENERIC;
191+
}
192+
} else {
193+
Debug.print(DBG_ERROR, F("Failed to allocate request: wifi.set"));
194+
result = NotecardCommunicationError::HOST_ERROR_OUT_OF_MEMORY;
195+
}
196+
197+
return result;
198+
}
199+
164200
const String & NotecardConnectionHandler::syncArduinoDeviceId (const String & device_id_)
165201
{
166202
// Validate the connection state is not in an initialization state
@@ -193,6 +229,7 @@ const String & NotecardConnectionHandler::syncArduinoDeviceId (const String & de
193229
if (NoteResponseError(rsp)) {
194230
const char *err = JGetString(rsp, "err");
195231
Debug.print(DBG_ERROR, F("%s"), err);
232+
Debug.print(DBG_ERROR, F("Failed to update cache."));
196233
} else {
197234
Debug.print(DBG_VERBOSE, F("Cache updated successfully."));
198235
}
@@ -234,6 +271,7 @@ int NotecardConnectionHandler::syncSecretDeviceKey (const String & secret_device
234271
if (NoteResponseError(rsp)) {
235272
const char *err = JGetString(rsp, "err");
236273
Debug.print(DBG_ERROR, F("%s"), err);
274+
Debug.print(DBG_ERROR, F("Failed to sync Secret Device Key."));
237275
result = NotecardCommunicationError::NOTECARD_ERROR_GENERIC;
238276
} else {
239277
Debug.print(DBG_VERBOSE, F("Secret key updated successfully."));

src/Arduino_NotecardConnectionHandler.h

+1
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ class NotecardConnectionHandler final : public ConnectionHandler
9898
_topic_type = topic;
9999
}
100100

101+
int setWiFiCredentials (const String & ssid, const String & pass);
101102
const String & syncArduinoDeviceId (const String & device_id);
102103
int syncSecretDeviceKey (const String & secret_device_key);
103104

0 commit comments

Comments
 (0)