ESP32-GATEWAY-EA-IND rev. G -> eth to PC

Started by staley, February 03, 2025, 05:48:13 PM

Previous topic - Next topic

staley

Hi.

I want to send data from the Olimex gateway board using ethernet (TCP/IP), using a simple TCP server on the PC (I am using Hercules...).

However, I am currently unable to make it work.

This is the current code (chat gpt):

#include <ETH.h>
#include <WiFi.h>

// --- Network Configuration ---
// Set a static IP for the ESP32 Ethernet interface.
IPAddress local_IP(192, 168, 1, 200);  // ESP32's IP
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress primaryDNS(8, 8, 8, 8);      // Optional
IPAddress secondaryDNS(8, 8, 4, 4);    // Optional

// Set the TCP server (Hercules) IP and port.
IPAddress serverIP(192, 168, 1, 150);  // Change to your PC's IP
const uint16_t serverPort = 23;        // The port on which Hercules listens

// Global flag to track Ethernet status
bool ethConnected = false;
// TCP client object
WiFiClient client;

//
// Ethernet event handler callback.
//
void WiFiEvent(WiFiEvent_t event) {
  switch (event) {
    case ARDUINO_EVENT_ETH_START:
      Serial.println("[ETH] Ethernet Started");
      ETH.setHostname("esp32-client");
      break;

    case ARDUINO_EVENT_ETH_CONNECTED:
      Serial.println("[ETH] Ethernet Link Up");
      break;

    case ARDUINO_EVENT_ETH_GOT_IP:
      Serial.println("[ETH] Ethernet got IP");
      Serial.print("   - MAC: ");
      Serial.println(ETH.macAddress());
      Serial.print("   - IPv4: ");
      Serial.println(ETH.localIP());
      ethConnected = true;
      break;

    case ARDUINO_EVENT_ETH_DISCONNECTED:
      Serial.println("[ETH] Ethernet Link Down");
      ethConnected = false;
      break;

    case ARDUINO_EVENT_ETH_STOP:
      Serial.println("[ETH] Ethernet Stopped");
      ethConnected = false;
      break;

    default:
      break;
  }
}

//
// Setup function: initialize serial, Ethernet, and register event handler.
//
void setup() {
  Serial.begin(115200);
  delay(1000);
  Serial.println("ESP32 Ethernet TCP Client Example");

  // Disable Wi-Fi since we are using Ethernet only.
  WiFi.mode(WIFI_MODE_NULL);
  // Register Ethernet event handler.
  WiFi.onEvent(WiFiEvent);

  // Optionally, configure static IP settings before starting Ethernet.
  ETH.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS);

  // IMPORTANT: Adjust the following parameters to match your board's PHY and pin configuration.
  // Function signature: ETH.begin(eth_phy_type_t type, int32_t phy_addr, int mdc, int mdio, int power, eth_clock_mode_t clk_mode);
  ETH.begin(ETH_PHY_LAN8720,   // PHY type: adjust if you have a different PHY (e.g., ETH_PHY_LAN8710)
            0,                // PHY address (commonly 0 or 1)
            23,               // MDC pin
            18,               // MDIO pin
            5,               // Power pin (-1 if not used or permanently powered) 12
            ETH_CLOCK_GPIO17_OUT); // Clock mode: adjust as needed

  Serial.println("Waiting for Ethernet to obtain IP...");
}

//
// Loop function: once Ethernet is up, attempt to connect to the TCP server and exchange data.
//
void loop() {
  if (ethConnected) {
    // If not already connected to the server, try to connect.
    if (!client.connected()) {
      Serial.println("Attempting to connect to TCP server...");
      if (client.connect(serverIP, serverPort)) {
        Serial.println("Connected to TCP server!");
        // Send an initial greeting message.
        client.println("Hello from ESP32 TCP Client!");
      } else {
        Serial.println("Connection to server failed. Will retry...");
        delay(3000); // Wait before retrying
        return;
      }
    } else {
      // If connected, send periodic messages.
      static unsigned long lastSendTime = 0;
      if (millis() - lastSendTime > 5000) {  // every 5 seconds
        client.println("Ping from ESP32 client");
        Serial.println("Message sent to server.");
        lastSendTime = millis();
      }

      // Check for and print any response from the server.
      while (client.available()) {
        String response = client.readStringUntil('\n');
        response.trim();
        Serial.print("Server response: ");
        Serial.println(response);
      }
    }
  }
  delay(100);
}

staley

Does anyone have a working code/example for WHATEVER communication over the ethernet modul? So that i would have a basis to continue. Thank you! :)

LubOlimex

#2
In the latest package for ESP32 for Arduino IDE, select Olimex ESP32-GATEWAY, remember to select "Revision F or newer" option and the proper COM port from "Tools". And then use demo from Examples -> Ethernet -> ETH_LAN8720 - it works without problems.
Technical support and documentation manager at Olimex