Skip to content

Commit

Permalink
Allow network setting or changing in async client
Browse files Browse the repository at this point in the history
  • Loading branch information
mobizt committed Jul 5, 2024
1 parent a42d570 commit e423654
Show file tree
Hide file tree
Showing 12 changed files with 767 additions and 41 deletions.
61 changes: 58 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/mobizt/FirebaseClient/.github%2Fworkflows%2Fcompile_library.yml?logo=github&label=compile) [![Github Stars](https://img.shields.io/github/stars/mobizt/FirebaseClient?logo=github)](https://github.com/mobizt/FirebaseClient/stargazers) ![Github Issues](https://img.shields.io/github/issues/mobizt/FirebaseClient?logo=github)

![GitHub Release](https://img.shields.io/github/v/release/mobizt/FirebaseClient) ![Arduino](https://img.shields.io/badge/Arduino-v1.3.2-57C207?logo=arduino) ![PlatformIO](https://badges.registry.platformio.org/packages/mobizt/library/FirebaseClient.svg) ![GitHub Release Date](https://img.shields.io/github/release-date/mobizt/FirebaseClient)
![GitHub Release](https://img.shields.io/github/v/release/mobizt/FirebaseClient) ![Arduino](https://img.shields.io/badge/Arduino-v1.3.3-57C207?logo=arduino) ![PlatformIO](https://badges.registry.platformio.org/packages/mobizt/library/FirebaseClient.svg) ![GitHub Release Date](https://img.shields.io/github/release-date/mobizt/FirebaseClient)

[![GitHub Sponsors](https://img.shields.io/github/sponsors/mobizt?logo=github)](https://github.com/sponsors/mobizt)

Revision `2024-07-02T05:07:24Z`
Revision `2024-07-05T02:47:30Z`

## Table of Contents

Expand All @@ -25,6 +25,7 @@ Revision `2024-07-02T05:07:24Z`
3. [Async Queue](#async-queue)
1. [Running Many Tasks Concurrency Using Different Async Clients (In Different SSL Clients)](#running-many-tasks-concurrency-using-different-async-clients-in-different-ssl-clients)
4. [Async Client](#async-client)
1. [Change Network Interfaces](#change-network-interfaces)
5. [Tasks Timeout](#tasks-timeout)
6. [Async Result](#async-result)
1. [App Events](#app-events)
Expand Down Expand Up @@ -561,7 +562,55 @@ The `network_config_data` that obtained from network classes will be copied for
For the details of networking class, see [Working with Networks](#working-with-networks) section.
### Tasks Timeout
#### Change Network Interfaces
Since version 1.3.3, the network interfaces that works with async client chan be changed or set.
The network interface class can be changed with the function `AsyncClientClass::setNetwork`.
The function parametes are included the following.
```cpp
AsyncClientClass::setNetwork(<ssl_client>, <network_config_data>);
```

The SSL client set here should work for the type of network set.

The following code shows how to change the network interface in ESP device between WiFi and Ethernet.

```cpp
EthernetClient ethernet_client;

ESP_SSLClient ethernet_ssl_client;

WiFiClientSecure wifi_ssl_client;

EthernetNetwork ethernet_network(...);

DefaultNetwork wifi_network;

AsyncClientClass aClient(wifi_ssl_client, getNetwork(wifi_network));

void setup()
{
// Connect WiFi code here

ethernet_ssl_client.setClient(&ethernet_client);
ethernet_ssl_client.setInsecure();

// Check network status
if (!aClient.networkStatus())
{
// Change to Ethernet network.
aClient.setNetwork(ethernet_ssl_client, getNetwork(ethernet_network));
}

}
```
Please see [NetworkSwitching](/examples/App/NetworkInterfaces/Async/NoCallback/NetworkSwitching) examples for more detail.
- ### Tasks Timeout
In case async task, the send timeout and read timeout are 30 seconds by default and it cannot be changed.
Expand Down Expand Up @@ -1306,6 +1355,9 @@ The `AsyncClientClass` object requires the network config data (`network_config_
- [GenericNetwork](/examples/App/NetworkInterfaces/Async/Callback/GenericNetwork/) is used with the non-core or user defined networking.
To set or change the network for the `AsyncClientClass`, please see [Change Network Interfaces](#change-network-interfaces).
> [!WARNING]
> In ESP32, [ADC2](https://docs.espressif.com/projects/esp-idf/en/v4.2/esp32/api-reference/peripherals/adc.html) (`GPIO 0`, `GPIO 2`, `GPIO 4`, `GPIO 12`, `GPIO 13`, `GPIO 14`, `GPIO 15`, `GPIO 25`, `GPIO 26` and `GPIO 27`) cannot be used while using WiFi.
>
Expand Down Expand Up @@ -1613,6 +1665,7 @@ The following section will provided the basic (bare minimum) code example and th
* [EthernetNetwork](/examples/App/NetworkInterfaces/Async/Callback/EthernetNetwork/)
* [GenericNetwork](/examples/App/NetworkInterfaces/Async/Callback/GenericNetwork/)
* [GSMNetwork](/examples/App/NetworkInterfaces/Async/Callback/GSMNetwork/)
* [NetworkSwitching](/examples/App/NetworkInterfaces/Async/Callback/NetworkSwitching)
* [NoCallback](/examples/App/NetworkInterfaces/Async/NoCallback/)
* [DefaultNetworks](/examples/App/NetworkInterfaces/Async/NoCallback/DefaultNetworks/)
* [DefaultEthernetNetwork](/examples/App/NetworkInterfaces/Async/NoCallback/DefaultNetworks/DefaultEthernetNetwork/)
Expand All @@ -1623,6 +1676,7 @@ The following section will provided the basic (bare minimum) code example and th
* [EthernetNetwork](/examples/App/NetworkInterfaces/Async/NoCallback/EthernetNetwork/)
* [GenericNetwork](/examples/App/NetworkInterfaces/Async/NoCallback/GenericNetwork/)
* [GSMNetwork](/examples/App/NetworkInterfaces/Async/NoCallback/GSMNetwork/)
* [NetworkSwitching](/examples/App/NetworkInterfaces/Async/NoCallback/NetworkSwitching)
* [Sync](/examples/App/NetworkInterfaces/Sync/)
* [DefaultNetworks](/examples/App/NetworkInterfaces/Sync/DefaultNetworks/)
* [DefaultEthernetNetwork](/examples/App/NetworkInterfaces/Sync/DefaultNetworks/DefaultEthernetNetwork/)
Expand All @@ -1633,6 +1687,7 @@ The following section will provided the basic (bare minimum) code example and th
* [EthernetNetwork](/examples/App/NetworkInterfaces/Sync/EthernetNetwork/)
* [GenericNetwork](/examples/App/NetworkInterfaces/Sync/GenericNetwork/)
* [GSMNetwork](/examples/App/NetworkInterfaces/Sync/GSMNetwork/)
* [NetworkSwitching](/examples/App/NetworkInterfaces/Sync/NetworkSwitching)
* [UserManagement](/examples/App/UserManagement/)
* [Async](/examples/App/UserManagement/Async/)
* [Callback](/examples/App/UserManagement/Async/Callback/)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
/**
* This example shows how to switch the network interfaces between WiFi and Ethernet.
* The ESP32 and W5500 Ethernet module are used in this example.
*/

#include <Arduino.h>
#include <WiFi.h>

#include <Ethernet.h>
#include <WiFiClientSecure.h>
#include <FirebaseClient.h>

#define WIZNET_RESET_PIN 26 // Connect W5500 Reset pin to GPIO 26 of ESP32 (-1 for no reset pin assigned)
#define WIZNET_CS_PIN 5 // Connect W5500 CS pin to GPIO 5 of ESP32
#define WIZNET_MISO_PIN 19 // Connect W5500 MISO pin to GPIO 19 of ESP32
#define WIZNET_MOSI_PIN 23 // Connect W5500 MOSI pin to GPIO 23 of ESP32
#define WIZNET_SCLK_PIN 18 // Connect W5500 SCLK pin to GPIO 18 of ESP32

#define WIFI_SSID "WIFI_AP"
#define WIFI_PASSWORD "WIFI_PASSWORD"

#define API_KEY "Web_API_KEY"
#define USER_EMAIL "USER_EMAIL"
#define USER_PASSWORD "USER_PASSWORD"

uint8_t Eth_MAC[] = {0x02, 0xF0, 0x0D, 0xBE, 0xEF, 0x01};

void asyncCB(AsyncResult &aResult);

void printResult(AsyncResult &aResult);

void connectWiFi();

void checkConnection(AsyncClientClass &a_client);

DefaultNetwork wifi_network(true);

EthernetNetwork ethernet_network(Eth_MAC, WIZNET_CS_PIN, WIZNET_RESET_PIN);

WiFiClientSecure wifi_ssl_client;

ESP_SSLClient ethernet_ssl_client;
EthernetClient ethernet_client;

UserAuth user_auth(API_KEY, USER_EMAIL, USER_PASSWORD, 3000);

FirebaseApp app;

using AsyncClient = AsyncClientClass;

AsyncClient aClient(wifi_ssl_client, getNetwork(wifi_network));

unsigned long last_change_ms = 0;
bool wifi_once_connected = false;

void setup()
{

Serial.begin(115200);

connectWiFi();

Serial.println();

Firebase.printf("Firebase Client v%s\n", FIREBASE_CLIENT_VERSION);

Serial.println("Initializing app...");

wifi_ssl_client.setInsecure();

ethernet_ssl_client.setInsecure();
ethernet_ssl_client.setBufferSizes(1024, 512);
ethernet_ssl_client.setClient(&ethernet_client);

initializeApp(aClient, app, getAuth(user_auth), asyncCB, "authTask");
}

void loop()
{
app.loop();

checkConnection(aClient);
}

void asyncCB(AsyncResult &aResult)
{
printResult(aResult);
}

void printResult(AsyncResult &aResult)
{
if (aResult.isEvent())
{
Firebase.printf("Event task: %s, msg: %s, code: %d\n", aResult.uid().c_str(), aResult.appEvent().message().c_str(), aResult.appEvent().code());
}

if (aResult.isDebug())
{
Firebase.printf("Debug task: %s, msg: %s\n", aResult.uid().c_str(), aResult.debug().c_str());
}

if (aResult.isError())
{
Firebase.printf("Error task: %s, msg: %s, code: %d\n", aResult.uid().c_str(), aResult.error().message().c_str(), aResult.error().code());
}

if (aResult.available())
{
Firebase.printf("task: %s, payload: %s\n", aResult.uid().c_str(), aResult.c_str());
}
}

void connectWiFi()
{
if (!wifi_once_connected)
{
unsigned long ms = millis();
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);

Serial.print("Connecting to Wi-Fi");
while (WiFi.status() != WL_CONNECTED && millis() - ms < 10000)
{
Serial.print(".");
delay(300);
}

if (WiFi.status() == WL_CONNECTED)
{
wifi_once_connected = true;
Serial.println();
Serial.print("Connected with IP: ");
Serial.println(WiFi.localIP());
}
else
{
Serial.println(" failed!");
}
}
}

void checkConnection(AsyncClientClass &a_client)
{
// If network was disconnected over 5 min, change the network interfaces every 1 min if it is still disconnected.

unsigned long lastseen_timeout = 5 * 60 * 1000; // The timeout in 5 min since last seen.
unsigned long change_timeout = 60 * 1000; // Change network every 1 min.

// We will check the last seen network connection with functiopn networkLastSeen.
// The function networkStatus() returns the current network connection status.

if (!a_client.networkStatus() && millis() - last_change_ms > change_timeout && millis() - a_client.networkLastSeen() > lastseen_timeout)
{
last_change_ms = millis();
if (a_client.getNetworkType() == firebase_network_data_default_network)
{
Serial.println("Switch to Ethernet network...");
a_client.setNetwork(ethernet_ssl_client, getNetwork(ethernet_network));
}
else if (a_client.getNetworkType() == firebase_network_data_ethernet_network)
{
Serial.println("Switch to WiFi network...");
connectWiFi();
a_client.setNetwork(wifi_ssl_client, getNetwork(wifi_network));
}
}
}
Loading

0 comments on commit e423654

Please sign in to comment.