#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);
}
Quote from: LubOlimex on February 03, 2025, 01:35:42 PMhttps://www.olimex.com/Products/Components/Connectors/FEMALE-1X16-STD/CON3 on the Pinguino OTG is a 0.05 spaced connector if I am right. It is not the standard pin header.
Quote from: LubOlimex on February 03, 2025, 01:35:42 PMThe powering priority is as follows: Power jack (9V in your case) > USB > Li-PoIn the PIC32 itsel from SW, I think it is not easy to determine. It was also some years ago when I tried, but as far as I remember, I had problems to read out from the USB module if there is power on the USB. I think I will check this again in the future. I mean USB can be read out theoretically. LiPo can be measured. If not of them gives power then obviusly it is external powered