Connect Blynk via WiFi and switch to Ethernet if WiFi is disconnected

Hello everyone,
Would you please help me solve a coding problem to connect Blynk via my ESP32 by WiFi, and when WiFI is disconnected, use W5500 Ethernet for connection?

I have that ESP32S with few sensors, and I added a W5500 shield for Ethernet LAN; I could connect the ESP32 to the WiFi, and when the WiFi is disconnected, it will connect by Ethernet, and it works very well. I tried to connect the device to Blynk Cloud to upload my data, but I could not manage both connections on one sketch if I use
#include <BlynkSimpleEsp32.h>
The blynk is secured only when WiFi is available

when I use
#include <BlynkSimpleEthernet.h>
The blynk is only connected via Ethernet, and when I tried to use both libraries, I got a verification error.

I appreciate your help
below is a simple sketch as an example of what I am trying to do

/* Fill in information from Blynk Device Info here */
/* Fill in information from Blynk Device Info here */
#define BLYNK_TEMPLATE_ID "Template"
#define BLYNK_TEMPLATE_NAME "Name"
#define BLYNK_AUTH_TOKEN "BLYNK_AUTH_TOKEN"
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <BlynkSimpleEsp32.h>
#include <WiFi.h>
const char* ssid = "1234";
const char* password = "1234567890";

void setup() {
  // Debug console
  Serial.begin(9600);

  Ethernet.init(15);
  if (Ethernet.linkStatus() == LinkON) {
    Serial.println("Connected to Ethernet.");
  } else {

    // Try connecting to WiFi
    WiFi.begin(ssid, password);
    int wifiAttempts = 0;
    while (WiFi.status() != WL_CONNECTED && wifiAttempts < 10) {
      delay(1000);
      wifiAttempts++;
    }

    if (WiFi.status() == WL_CONNECTED ) {
      Serial.println("Failed to connect to Ethernet.");
      Serial.println("WiFi connected");
      Serial.print("IP address: ");
      Serial.println(WiFi.localIP());
    }
  }
   Blynk.begin(BLYNK_AUTH_TOKEN);
}

void loop() {
  Blynk.run();
  if (!Blynk.connected()) {
    Serial.println("Blynk Is disConnected");

    // Blynk.connect()
  } else {
    Serial.println("Blynk Is Connected");
  }
}

@haypackup Please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```

Copy and paste these if you can’t find the correct symbol on your keyboard.

Pete.

I’ve never tried to do what you have, but you aren’t going to achieve your goal using Blynk.begin.
You need to use Blynk.config and Blynk.connect instead.

It might be worth you reading this, and the included link about using Ethernet and WiFi at the same time, or as a fallover…

This isn’t going to give you a ready-made solution for your hardware (or for the LILLYGO board either) but might point you in the right direction.

Pete.

Thank you, Pete,

I tried with Blynk.config and got the same result; only one library works at a time. Either Ethernet or WiFi depends on which library is included. If I tried both of them, there would be a conflict. Excuse my lack of knowledge, and would you please guide me for an example?

I don’t have your hardware, so wouldn’t be able to do any testing myself.
I would imagine it would be better to use an Ethernet board that uses the ESP32 ETH.h library as the LILYGO board I tested does.

Pete.

I do not have any problem connecting the ESP32S board to the Internet Via Ethernet or WiFi, Both connections work well, and If I switch off the WiFi, the board will connect to the Ethernet very fast. The problem is that Blynk only maintains a connection to the cloud over WiFi if #include <BlynkSimpleEsp32.h>; if wifi is disconnected, the board will connect by Ethernet, but Blynk is not. Or to the Ethernet with #include <BlynkSimpleEthernet.h>, so if the Ethernet is disconnected, the board will connect to the WiFi, but Blynk does not. If I use both libraries, I get a conflict error. I just try to maintain a connection on one board with one sketch whenever or whether WiFi or Ethernet is available, the blynk is connecting, too. Thanks

Check out this:

You can do the same for ESP32 + W5500 Ethernet

2 Likes

That’s perfect guide @vshymanskyy
I Edit the sketch as below

  Blynk is a platform with iOS and Android apps to control
  ESP32, Arduino, Raspberry Pi and the likes over the Internet.
  You can easily build mobile and web interfaces for any
  projects by simply dragging and dropping widgets.

    Downloads, docs, tutorials: https://www.blynk.io
    Sketch generator:           https://examples.blynk.cc
    Blynk community:            https://community.blynk.cc
    Follow us:                  https://www.fb.com/blynkapp
                                https://twitter.com/blynk_app

  Blynk library is licensed under MIT license
  This example code is in public domain.
 *************************************************************
  This sketch shows how to use multiple connectivity options
  at the same time:
    - Arduino MKR1400 GSM module provides GPRS
    - Arduino MKR ETH shield provides Ethernet
  Using a standard Client interface is a convenient way to integrate
  any connectivity shield, even if it's not directly supported by Blynk.
 *************************************************************/

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial

/* Uncomment this to see the verbose Blynk protocol log */
//#define BLYNK_DEBUG

/* Fill in information from Blynk Device Info here */
/* Read more: https://bit.ly/BlynkSimpleAuth */
#define BLYNK_TEMPLATE_ID           "TMPxxxxxx"
#define BLYNK_TEMPLATE_NAME         "Device"
#define BLYNK_AUTH_TOKEN            "YourAuthToken"

/* BlynkMultiClient allows attaching Blynk to the standard Arduino Client,
   and also allows multiple (up to 4) connections to be registered.
   NOTE: don't replace it with any of the BlynkSimple*.h variants */
#include <BlynkMultiClient.h>
//#include <BlynkSimpleEsp32.h>

/*
 * Ethernet
 */

// You can specify your board mac adress
byte ETH_MAC[] =        { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

#include <SPI.h>
#include <Ethernet.h>
#include <WiFi.h>
const char* ssid = "MySSID";
const char* password = "MyPassword";
// Ethernet shield and SDcard pins
#define MKRETH_CS  15


static EthernetClient blynkEthernetClient;
static WiFiClient blynkWifiClient;
/*
 * Main
 */

void connectEthernet()
{
  if (Ethernet.begin(ETH_MAC, 5000L, 500L)) {
    Serial.print("Ethernet IP: ");
    Serial.println(Ethernet.localIP());
  } else if (Ethernet.hardwareStatus() == EthernetNoHardware) {
    Serial.println("Ethernet shield was not found.");
  } else if (Ethernet.linkStatus() == LinkOFF) {
    Serial.println("Ethernet cable is not connected.");
  } else {
    Serial.println("Ethernet: DHCP configuration failed.");
  }
}

void connectWiFi()
{


    // Try connecting to WiFi
    WiFi.begin(ssid, password);
    int wifiAttempts = 0;
    while (WiFi.status() != WL_CONNECTED && wifiAttempts < 10) {
      delay(1000);
      wifiAttempts++;
    }

    if (WiFi.status() == WL_CONNECTED ) {
      Serial.println("Failed to connect to Ethernet.");
      Serial.println("WiFi connected");
      Serial.print("IP address: ");
      Serial.println(WiFi.localIP());
    }
  }

void setup()
{
  // Debug console
  Serial.begin(115200);
  Ethernet.init(MKRETH_CS);

  connectEthernet();
  connectWiFi();

  // Setup Blynk
  Blynk.addClient("ETH", blynkEthernetClient, 80);
  Blynk.addClient("WiFi", blynkWifiClient, 80);
  Blynk.config(BLYNK_AUTH_TOKEN);
}

void loop()
{
  Blynk.run();
  Ethernet.maintain();
    if (!Blynk.connected()) {
    Serial.println("Blynk Is disConnected");

    // Blynk.connect()
  } else {
    Serial.println("Blynk Is Connected");
  }
}

it maintains the connection over Wifi or Ethernet; if one is down, switch to the other one.
I appreciate your help.

2 Likes

Hiii Did you check is that works ?