ESP32-POE-ISO ethernet static ip allocation not working. Always dhcp

Started by kovbal, June 27, 2024, 05:01:26 PM

Previous topic - Next topic

kovbal

Dear Forum members,

I'm facing an issue, where ethernet static addressing is working in micropython, but not working with arduino-ide. (Arduino ide 2.3.2, esp32 by espressif 3.0.1)

Tried with variables, tried with fixed ip settings, none works. Please advise!

Thanks,
Balazs

the code:


#define ETH_ADDR 0
#define ETH_TYPE ETH_PHY_LAN8720
#define ETH_POWER 12
#define ETH_MDC 23
#define ETH_MDIO 18
#define ETH_CLK ETH_CLOCK_GPIO17_OUT

#include <ETH.h>
#include <SD_MMC.h>
#include <SPI.h>
#include <Ethernet.h>

// Function to read configuration file
bool readConfig(String &ethIP, String &netmask, String &gateway, String &dns, String &atemIP, int &pin1, int &pin2, int &pin3, int &pin4) {
    if (!SD_MMC.begin("/sdcard", true)) {
        Serial.println("SD card initialization failed!");
        return false;
    }

    File configFile = SD_MMC.open("/config.txt");
   
    if (!configFile) {
        Serial.println("Failed to open config file");
        return false;
    }

    while (configFile.available()) {
        String line = configFile.readStringUntil('\n');
        line.trim();
        if (line.startsWith("[ethernet]")) {
            while (configFile.available()) {
                line = configFile.readStringUntil('\n');
                line.trim();
                if (line.startsWith("[")) break;
                if (line.startsWith("ip:")) ethIP = line.substring(3).c_str();
                else if (line.startsWith("netmask:")) netmask = line.substring(8).c_str();
                else if (line.startsWith("gateway:")) gateway = line.substring(8).c_str();
                else if (line.startsWith("dns:")) dns = line.substring(4).c_str();
            }
        } else if (line.startsWith("[atem]")) {
            while (configFile.available()) {
                line = configFile.readStringUntil('\n');
                line.trim();
                if (line.startsWith("[")) break;
                if (line.startsWith("mixer:")) atemIP = line.substring(3).c_str();
                else if (line.startsWith("pin1:")) pin1 = line.substring(5).toInt();
                else if (line.startsWith("pin2:")) pin2 = line.substring(5).toInt();
                else if (line.startsWith("pin3:")) pin3 = line.substring(5).toInt();
                else if (line.startsWith("pin4:")) pin4 = line.substring(5).toInt();
            }
        }
    }
    Serial.print("ETH IP:");
    Serial.println(ethIP);
    Serial.print("Netmask:");
    Serial.println(netmask);
    Serial.print("GW:");
    Serial.println(gateway);
    Serial.print("ATEM IP:");
    Serial.println(atemIP);
    configFile.close();
    return true;
}

// Ethernet and ATEM configuration variables
String ethIP, netmask, gateway, dns, atemIP;
int pin1, pin2, pin3, pin4;
void setup() {
    Serial.begin(115200);

    // Read configuration from file
    if (!readConfig(ethIP, netmask, gateway, dns, atemIP, pin1, pin2, pin3, pin4)) {
        Serial.println("Failed to read configuration");
        return;
    }

    // Convert String IPs to IPAddress
    IPAddress localIP, subnet, gw, dnsServer, atemIPAddress;
    localIP.fromString(ethIP.c_str());
    subnet.fromString(netmask.c_str());
    gw.fromString(gateway.c_str());
    dnsServer.fromString(dns.c_str());
    atemIPAddress.fromString(atemIP.c_str());
    //Serial.println(ethIP);
    // Start Ethernet connection
    ETH.setHostname("kovbaltally");
    //ETH.config(localIP, gw, subnet, dnsServer);
   
    ETH.begin(ETH_TYPE, ETH_ADDR,ETH_MDC,ETH_MDIO,ETH_POWER,ETH_CLK);
    while (!ETH.linkUp()) {
        delay(1000);
        Serial.println("Waiting for Ethernet connection...");
    }
    if (!ETH.config((192,168,80,10),(192,168,80,1),(255,255,255,0),(8,8,8,8),(8,8,4,4),(1,1,1,1)))
   {
       Serial.println("Configuration Failed!");
   }

    Serial.print("ETH MAC: ");
    Serial.println(ETH.macAddress());
    Serial.print("Ethernet connected with IP: ");
    Serial.println(ETH.localIP());

}

void loop() {
   
    delay(2000);
    Serial.println("Running loop");

}

LubOlimex

It is probably something related to the network equipment settings and subnet basics. Maybe check this thread:

https://www.olimex.com/forum/index.php?topic=7158.0
Technical support and documentation manager at Olimex

LubOlimex

Technical support and documentation manager at Olimex