Skip to content

Commit

Permalink
feat: Wi-Fi Credentials not supported
Browse files Browse the repository at this point in the history
  • Loading branch information
zfields committed Jul 25, 2024
1 parent 79532b1 commit 615cc71
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Library for handling and managing network connections by providing keep-alive fu
#if defined(BOARD_HAS_ETHERNET)
EthernetConnectionHandler conMan;
#elif defined(BOARD_HAS_WIFI)
WiFiConnectionHandler conMan("SECRET_SSID", "SECRET_PASS");
WiFiConnectionHandler conMan("SECRET_WIFI_SSID", "SECRET_WIFI_PASS");
#elif defined(BOARD_HAS_GSM)
GSMConnectionHandler conMan("SECRET_PIN", "SECRET_APN", "SECRET_GSM_LOGIN", "SECRET_GSM_PASS");
#elif defined(BOARD_HAS_NB)
Expand Down
13 changes: 9 additions & 4 deletions examples/ConnectionHandlerDemo/ConnectionHandlerDemo.ino
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
*
* If using a WiFi board (Arduino MKR1000, MKR WiFi 1010, Nano 33 IoT, UNO
* WiFi Rev 2 or ESP8266/32), create a WiFiConnectionHandler object by adding
* Network Name (SECRET_SSID) and password (SECRET_PASS) in the arduino_secrets.h
* file (or Secrets tab in Create Web Editor).
* Network Name (SECRET_WIFI_SSID) and password (SECRET_WIFI_PASS) in the
* arduino_secrets.h file (or Secrets tab in Create Web Editor).
*
* WiFiConnectionHandler conMan(SECRET_SSID, SECRET_PASS);
* WiFiConnectionHandler conMan(SECRET_WIFI_SSID, SECRET_WIFI_PASS);
*
* If using a MKR GSM 1400 or other GSM boards supporting the same API you'll
* need a GSMConnectionHandler object as follows
Expand All @@ -42,6 +42,11 @@

#include <Arduino_ConnectionHandler.h>

#if !(defined(USE_NOTECARD) || defined(BOARD_HAS_WIFI) || defined(BOARD_HAS_GSM) || defined(BOARD_HAS_LORA) || \
defined(BOARD_HAS_NB) || defined(BOARD_HAS_ETHERNET) || defined(BOARD_HAS_CATM1_NBIOT))
#error "Please check Arduino Connection Handler supported boards list: https://github.com/arduino-libraries/Arduino_ConnectionHandler/blob/master/README.md"
#endif

#if defined(USE_NOTECARD)
/* The Notecard can provide connectivity to almost any board via ESLOV (I2C)
* or UART. An empty string (or the default value provided below) will not
Expand All @@ -55,7 +60,7 @@ NotecardConnectionHandler conMan(NOTECARD_PRODUCT_UID);
#elif defined(BOARD_HAS_ETHERNET)
EthernetConnectionHandler conMan(SECRET_IP, SECRET_DNS, SECRET_GATEWAY, SECRET_NETMASK);
#elif defined(BOARD_HAS_WIFI)
WiFiConnectionHandler conMan(SECRET_SSID, SECRET_PASS);
WiFiConnectionHandler conMan(SECRET_WIFI_SSID, SECRET_WIFI_PASS);
#elif defined(BOARD_HAS_GSM)
GSMConnectionHandler conMan(SECRET_PIN, SECRET_APN, SECRET_GSM_USER, SECRET_GSM_PASS);
#elif defined(BOARD_HAS_NB)
Expand Down
4 changes: 2 additions & 2 deletions examples/ConnectionHandlerDemo/arduino_secrets.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Required for WiFiConnectionHandler
const char SECRET_SSID[] = "NETWORK NAME";
const char SECRET_PASS[] = "NETWORK PASSWORD";
const char SECRET_WIFI_SSID[] = "NETWORK NAME";
const char SECRET_WIFI_PASS[] = "NETWORK PASSWORD";

// Required for GSMConnectionHandler
const char SECRET_APN[] = "MOBILE PROVIDER APN ADDRESS";
Expand Down
38 changes: 38 additions & 0 deletions src/Arduino_NotecardConnectionHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,42 @@ NotecardConnectionHandler::NotecardConnectionHandler(
PUBLIC MEMBER FUNCTIONS
******************************************************************************/

int NotecardConnectionHandler::setWiFiCredentials (const String & ssid_, const String & password_)
{
int result;

// Validate the connection state is not in an initialization state
if (check() == NetworkConnectionState::INIT)
{
Debug.print(DBG_ERROR, F("Failed to set WiFi credentials. Connection has not been initialized."));
result = NotecardCommunicationError::NOTECARD_ERROR_GENERIC;
} else if (J *req = _notecard.newRequest("card.wifi")) {
JAddStringToObject(req, "ssid", ssid_.c_str());
JAddStringToObject(req, "password", password_.c_str());
if (J *rsp = _notecard.requestAndResponse(req)) {
// Check the response for errors
if (NoteResponseError(rsp)) {
const char *err = JGetString(rsp, "err");
Debug.print(DBG_ERROR, F("%s"), err);
Debug.print(DBG_ERROR, F("Failed to set WiFi credentials."));
result = NotecardCommunicationError::NOTECARD_ERROR_GENERIC;
} else {
Debug.print(DBG_INFO, F("WiFi credentials updated. ssid: \"%s\" password: \"%s\"."), ssid_.c_str(), password_.length() ? "**********" : "");
result = NotecardCommunicationError::NOTECARD_ERROR_NONE;
}
JDelete(rsp);
} else {
Debug.print(DBG_ERROR, F("Failed to receive response from Notecard."));
result = NotecardCommunicationError::NOTECARD_ERROR_GENERIC;
}
} else {
Debug.print(DBG_ERROR, F("Failed to allocate request: wifi.set"));
result = NotecardCommunicationError::HOST_ERROR_OUT_OF_MEMORY;
}

return result;
}

const String & NotecardConnectionHandler::syncArduinoDeviceId (const String & device_id_)
{
// Validate the connection state is not in an initialization state
Expand Down Expand Up @@ -193,6 +229,7 @@ const String & NotecardConnectionHandler::syncArduinoDeviceId (const String & de
if (NoteResponseError(rsp)) {
const char *err = JGetString(rsp, "err");
Debug.print(DBG_ERROR, F("%s"), err);
Debug.print(DBG_ERROR, F("Failed to update cache."));
} else {
Debug.print(DBG_VERBOSE, F("Cache updated successfully."));
}
Expand Down Expand Up @@ -234,6 +271,7 @@ int NotecardConnectionHandler::syncSecretDeviceKey (const String & secret_device
if (NoteResponseError(rsp)) {
const char *err = JGetString(rsp, "err");
Debug.print(DBG_ERROR, F("%s"), err);
Debug.print(DBG_ERROR, F("Failed to sync Secret Device Key."));
result = NotecardCommunicationError::NOTECARD_ERROR_GENERIC;
} else {
Debug.print(DBG_VERBOSE, F("Secret key updated successfully."));
Expand Down
1 change: 1 addition & 0 deletions src/Arduino_NotecardConnectionHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ class NotecardConnectionHandler final : public ConnectionHandler
_topic_type = topic;
}

int setWiFiCredentials (const String & ssid, const String & pass);
const String & syncArduinoDeviceId (const String & device_id);
int syncSecretDeviceKey (const String & secret_device_key);

Expand Down

0 comments on commit 615cc71

Please sign in to comment.