t41_to_t41_comm/eth_tcp_sender.ino

53 lines
1.3 KiB
Arduino
Raw Normal View History

2025-02-17 06:24:35 +01:00
#include <NativeEthernet.h>
byte mac[] = { 0x04, 0xE9, 0xE5, 0x10, 0x00, 0x01 };
IPAddress ip(192, 168, 1, 100);
IPAddress serverIP(192, 168, 1, 101);
const int serverPort = 8888;
EthernetClient client;
byte buffer[256];
void setup() {
Serial.begin(115200);
//while (!Serial);
Serial.println("Initializing Ethernet...");
Ethernet.begin(mac, ip);
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet hardware not found!");
while (true);
}
if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Ethernet cable not connected!");
}
Serial.println("Connecting to server...");
if (client.connect(serverIP, serverPort)) {
Serial.println("Connected to server!");
} else {
Serial.println("Connection failed.");
}
for (int i = 0; i < 256; i++) {
buffer[i] = i; // Fill buffer with sample data (0-255)
}
}
void loop() {
if (!client.connected()) {
Serial.println("Connection lost. Reconnecting...");
client.stop();
delay(1000);
client.connect(serverIP, serverPort);
return;
}
Serial.println("Sending 256-byte packet...");
client.write(buffer, 256);
//delay(500); // Small delay to avoid overloading the receiver
}