I'm trying to connect ESP32 to the internet either via Ethernet connection or Wi-Fi, but priority to Wi-FI.
Below is the code for ethernet running alone, I can work with Wifi in the same way.
/*
Web client with enc28j60 and EthernetENC
This sketch connects to a test website (httpbin.org)
and try to do a GET request, the output is printed
on Serial
by Renzo Mischianti <www.mischianti.org>
https://www.mischianti.org
*/
#include <SPI.h>
#include <EthernetENC.h>
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(74,125,232,128); // numeric IP for Google (no DNS)
//char server[] = "www.google.com"; // name address for Google (using DNS)
char server[] = "httpbin.org"; // name address for Google (using DNS)
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDa, 0xAf, 0xfE, 0xEa, 0xFE, 0xED };
// Set the static IP address to use if the DHCP fails to assign
#define MYIPADDR 192,168,1,30
#define MYIPMASK 255,255,255,0
#define MYDNS 192,168,1,1
#define MYGW 192,168,1,1
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;
// Variables to measure the speed
unsigned long beginMicros, endMicros;
unsigned long byteCount = 0;
bool printWebData = true; // set to false for better speed measurement
void setup() {
Serial.begin(115200);
delay(1000);
Serial.println("Begin Ethernet");
// You can use Ethernet.init(pin) to configure the CS pin
//Ethernet.init(10); // Most Arduino shields
Ethernet.init(5); // MKR ETH Shield
//Ethernet.init(0); // Teensy 2.0
//Ethernet.init(20); // Teensy++ 2.0
//Ethernet.init(15); // ESP8266 with Adafruit FeatherWing Ethernet
//Ethernet.init(33); // ESP32 with Adafruit FeatherWing Ethernet
if (Ethernet.begin(mac, 20000)) { // Dynamic IP setup
Serial.println("DHCP OK!");
}
else
{
Serial.println("Failed to configure Ethernet using DHCP");
// Check for Ethernet hardware present
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
while (true) {
delay(1); // do nothing, no point running without Ethernet hardware
}
}
if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Ethernet cable is not connected.");
}
IPAddress ip(MYIPADDR);
IPAddress dns(MYDNS);
IPAddress gw(MYGW);
IPAddress sn(MYIPMASK);
Ethernet.begin(mac, ip, dns, gw, sn);
Serial.println("STATIC OK!");
}
delay(5000);
Serial.print("Local IP : ");
Serial.println(Ethernet.localIP());
Serial.print("Subnet Mask : ");
Serial.println(Ethernet.subnetMask());
Serial.print("Gateway IP : ");
Serial.println(Ethernet.gatewayIP());
Serial.print("DNS Server : ");
Serial.println(Ethernet.dnsServerIP());
Serial.println("Ethernet Successfully Initialized");
// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
Serial.println("Connected!");
// Make a HTTP request:
client.println("GET /get HTTP/1.1");
client.println("Host: httpbin.org");
client.println("Connection: close");
client.println();
} else {
// if you didn't get a connection to the server:
Serial.println("connection failed");
}
beginMicros = micros();
}
void loop() {
// if there are incoming bytes available
// from the server, read them and print them:
int len = client.available();
if (len > 0) {
byte buffer[80];
if (len > 80) len = 80;
client.read(buffer, len);
if (printWebData) {
Serial.write(buffer, len); // show in the serial monitor (slows some boards)
}
byteCount = byteCount + len;
}
// if the server's disconnected, stop the client:
if (!client.connected()) {
endMicros = micros();
Serial.println();
Serial.println("disconnecting.");
client.stop();
Serial.print("Received ");
Serial.print(byteCount);
Serial.print(" bytes in ");
float seconds = (float)(endMicros - beginMicros) / 1000000.0;
Serial.print(seconds, 4);
float rate = (float)byteCount / seconds / 1000.0;
Serial.print(", rate = ");
Serial.print(rate);
Serial.print(" kbytes/second");
Serial.println();
// do nothing forevermore:
while (true) {
if(client.connected())
Serial.println("COnnected");
delay(100);
}
}
}
When I tried to merge the code, Its not working successfully
Below code, once it disconnects with WiFi. It doesn't re connect to ethernet. Even when Lan Connected and there is internet.
#include <EthernetENC.h>
#include <SPI.h>
#include <WiFi.h>
#define SS_ETH 5
byte mac[] = { 0xDA, 0xAF, 0xfE, 0xEA, 0xFE, 0xED };
char server[] = "httpbin.org";
EthernetClient ethclient;
WiFiClient wifiClient;
// Variables to measure the speed
unsigned long beginMicros, endMicros;
unsigned long byteCount = 0;
bool printWebData = true;
void setup() {
Serial.begin(115200);
// Initialize Ethernet
Ethernet.init(SS_ETH);
Ethernet.begin(mac, 20000);
Serial.print("Local IP (Ethernet): ");
Serial.println(Ethernet.localIP());
Serial.print("Subnet Mask: ");
Serial.println(Ethernet.subnetMask());
Serial.print("Gateway IP: ");
Serial.println(Ethernet.gatewayIP());
Serial.print("DNS Server: ");
Serial.println(Ethernet.dnsServerIP());
// Initialize Wi-Fi
WiFi.begin("ALMIGHT1", "1234567890");
}
int checkConnection() {
// Check if Ethernet is available
if (Ethernet.linkStatus() == LinkON) {
// Use Ethernet connection
if (!ethclient.connected()) {
Serial.println("Connecting via Ethernet...");
if (ethclient.connect(server, 80)) {
Serial.println("Connected!");
// Make an HTTP request:
ethclient.println("GET /get HTTP/1.1");
ethclient.println("Host: httpbin.org");
ethclient.println("Connection: close");
ethclient.println();
delay(5000);
return 1;
} else {
// If you didn't get a connection to the server:
Serial.println("Connection failed");
}
beginMicros = micros();
}
} else {
// Use Wi-Fi connection
if (WiFi.status() != WL_CONNECTED) {
Serial.println("Connecting via Wi-Fi...");
WiFi.begin();
}
if (WiFi.status() == WL_CONNECTED) {
Serial.print("Connected to Wi-Fi. Local IP: ");
Serial.println(WiFi.localIP());
// Additional Wi-Fi initialization if needed
}
}
return 0;
}
void loop() {
int i = checkConnection();
if (i) {
int len = ethclient.available();
if (len > 0) {
byte buffer[80];
if (len > 80) len = 80;
ethclient.read(buffer, len);
if (printWebData) {
Serial.write(buffer, len); // Show in the serial monitor (slows some boards)
}
byteCount += len;
}
if (!ethclient.connected()) {
endMicros = micros();
Serial.println();
Serial.println("Ethernet disconnected.");
ethclient.stop();
Serial.print("Received ");
Serial.print(byteCount);
Serial.print(" bytes in ");
float seconds = (float)(endMicros - beginMicros) / 1000000.0;
Serial.print(seconds, 4);
float rate = (float)byteCount / seconds / 1000.0;
Serial.print(", rate = ");
Serial.print(rate);
Serial.print(" kbytes/second");
Serial.println();
}
} else {
if (WiFi.status() != WL_CONNECTED) {
Serial.println("Wi-Fi disconnected.");
}
}
// Other code logic
delay(1000);
}
Code stuck at
Wi-Fi disconnected. Wi-Fi disconnected. Wi-Fi disconnected. Wi-Fi disconnected. Wi-Fi disconnected. Wi-Fi disconnected. Wi-Fi disconnected. Wi-Fi disconnected.