Skip to content

Dynamic WiFi credentials - Take 2 #63

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/Arduino_ConnectionHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -217,19 +217,21 @@ class ConnectionHandler {
virtual NetworkConnectionState update_handleConnected () = 0;
virtual NetworkConnectionState update_handleDisconnecting() = 0;
virtual NetworkConnectionState update_handleDisconnected () = 0;

unsigned long _lastConnectionTickTime;
NetworkConnectionState _current_net_connection_state;

private:

unsigned long _lastConnectionTickTime;
NetworkConnectionState _current_net_connection_state;


OnNetworkEventCallback _on_connect_event_callback = NULL,
_on_disconnect_event_callback = NULL,
_on_error_event_callback = NULL;
};

#if defined(BOARD_HAS_WIFI)
#include "Arduino_WiFiConnectionHandler.h"
#include "Arduino_WiFiConnectionHandlerDynamic.h"
#elif defined(BOARD_HAS_GSM)
#include "Arduino_GSMConnectionHandler.h"
#elif defined(BOARD_HAS_NB)
Expand Down
10 changes: 5 additions & 5 deletions src/Arduino_WiFiConnectionHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ NetworkConnectionState WiFiConnectionHandler::update_handleInit()
Debug.print(DBG_ERROR, F("WiFi status ESP: %d"), WiFi.status());
WiFi.disconnect();
delay(300);
WiFi.begin(_ssid, _pass);
WiFi.begin(_ssid.c_str(), _pass.c_str());
delay(1000);
#endif /* #if !defined(BOARD_ESP8266) && !defined(ESP32) */

Expand All @@ -97,22 +97,22 @@ NetworkConnectionState WiFiConnectionHandler::update_handleConnecting()
#if !defined(BOARD_ESP8266) && !defined(ESP32)
if (WiFi.status() != WL_CONNECTED)
{
WiFi.begin(_ssid, _pass);
WiFi.begin(_ssid.c_str(), _pass.c_str());
}
#endif /* ifndef BOARD_ESP8266 */

if (WiFi.status() != NETWORK_CONNECTED)
{
#if !defined(__AVR__)
Debug.print(DBG_ERROR, F("Connection to \"%s\" failed"), _ssid);
Debug.print(DBG_ERROR, F("Connection to \"%s\" failed"), _ssid.c_str());
Debug.print(DBG_INFO, F("Retrying in \"%d\" milliseconds"), CHECK_INTERVAL_TABLE[static_cast<unsigned int>(NetworkConnectionState::CONNECTING)]);
#endif
return NetworkConnectionState::CONNECTING;
}
else
{
#if !defined(__AVR__)
Debug.print(DBG_INFO, F("Connected to \"%s\""), _ssid);
Debug.print(DBG_INFO, F("Connected to \"%s\""), _ssid.c_str());
#endif
#if defined(BOARD_ESP8266) || defined(ESP32)
configTime(0, 0, "time.arduino.cc", "pool.ntp.org", "time.nist.gov");
Expand All @@ -127,7 +127,7 @@ NetworkConnectionState WiFiConnectionHandler::update_handleConnected()
{
#if !defined(__AVR__)
Debug.print(DBG_VERBOSE, F("WiFi.status(): %d"), WiFi.status());
Debug.print(DBG_ERROR, F("Connection to \"%s\" lost."), _ssid);
Debug.print(DBG_ERROR, F("Connection to \"%s\" lost."), _ssid.c_str());
#endif
if (_keep_alive)
{
Expand Down
6 changes: 3 additions & 3 deletions src/Arduino_WiFiConnectionHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ class WiFiConnectionHandler : public ConnectionHandler
virtual NetworkConnectionState update_handleDisconnecting() override;
virtual NetworkConnectionState update_handleDisconnected () override;

private:
String _ssid;
String _pass;

char const * _ssid;
char const * _pass;
private:

WiFiUDP _wifi_udp;
WiFiClient _wifi_client;
Expand Down
46 changes: 46 additions & 0 deletions src/Arduino_WiFiConnectionHandlerDynamic.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
This file is part of ArduinoIoTCloud.

Copyright 2019 ARDUINO SA (http://www.arduino.cc/)

This software is released under the GNU General Public License version 3,
which covers the main part of arduino-cli.
The terms of this license can be found at:
https://www.gnu.org/licenses/gpl-3.0.en.html

You can be released from the requirements of the above licenses by purchasing
a commercial license. Buying such a license is mandatory if you want to modify or
otherwise use the software for commercial activities involving the Arduino
software without disclosing the source code of your own applications. To purchase
a commercial license, send an email to [email protected].
*/

/******************************************************************************
INCLUDE
******************************************************************************/

#include "Arduino_WiFiConnectionHandlerDynamic.h"

#ifdef BOARD_HAS_WIFI /* Only compile if the board has WiFi */

/******************************************************************************
CTOR/DTOR
******************************************************************************/

WiFiConnectionHandlerDynamic::WiFiConnectionHandlerDynamic(bool const keep_alive) : WiFiConnectionHandler{nullptr, nullptr, keep_alive}
{

}

/******************************************************************************
PUBLIC MEMBER FUNCTIONS
******************************************************************************/

void WiFiConnectionHandlerDynamic::setWiFiCredentials(String ssid, String pass)
{
_ssid = ssid;
_pass = pass;
_current_net_connection_state = NetworkConnectionState::INIT;
}

#endif /* #ifdef BOARD_HAS_WIFI */
45 changes: 45 additions & 0 deletions src/Arduino_WiFiConnectionHandlerDynamic.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
This file is part of ArduinoIoTCloud.

Copyright 2019 ARDUINO SA (http://www.arduino.cc/)

This software is released under the GNU General Public License version 3,
which covers the main part of arduino-cli.
The terms of this license can be found at:
https://www.gnu.org/licenses/gpl-3.0.en.html

You can be released from the requirements of the above licenses by purchasing
a commercial license. Buying such a license is mandatory if you want to modify or
otherwise use the software for commercial activities involving the Arduino
software without disclosing the source code of your own applications. To purchase
a commercial license, send an email to [email protected].
*/

#ifndef ARDUINO_WIFI_CONNECTION_HANDLER_DYNAMIC_H_
#define ARDUINO_WIFI_CONNECTION_HANDLER_DYNAMIC_H_

/******************************************************************************
INCLUDE
******************************************************************************/

#include "Arduino_WiFiConnectionHandler.h"

#ifdef BOARD_HAS_WIFI /* Only compile if the board has WiFi */

/******************************************************************************
CLASS DECLARATION
******************************************************************************/

class WiFiConnectionHandlerDynamic : public WiFiConnectionHandler
{
public:

WiFiConnectionHandlerDynamic(bool const keep_alive = true);

void setWiFiCredentials(String ssid, String pass);

};

#endif /* #ifdef BOARD_HAS_WIFI */

#endif /* ARDUINO_WIFI_CONNECTION_HANDLER_DYNAMIC_H_ */