Upload files to "/"

This commit is contained in:
Adema 2025-02-17 06:24:35 +01:00
commit 7e5082f72d
4 changed files with 217 additions and 0 deletions

61
eth_tcp_receiv.ino Normal file
View File

@ -0,0 +1,61 @@
#include <NativeEthernet.h>
#include <NativeEthernetServer.h>
byte mac[] = { 0x04, 0xE9, 0xE5, 0x10, 0x00, 0x02 };
IPAddress ip(192, 168, 1, 101);
const int localPort = 8888;
EthernetServer server(localPort);
EthernetClient client;
byte packetBuffer[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!");
}
server.begin();
Serial.println("TCP Server Ready. Waiting for connections...");
}
void loop() {
if (!client || !client.connected()) {
client = server.available();
if (client) {
Serial.println("Client connected!");
}
return;
}
static int bytesReceived = 0;
while (client.available()) { // Keep reading while data is available
int byteRead = client.read(packetBuffer + bytesReceived, 256 - bytesReceived);
if (byteRead > 0) {
bytesReceived += byteRead;
}
if (bytesReceived == 256) { // Full packet received
Serial.print("Received full 256-byte packet: ");
for (int i = 0; i < 256; i++) {
Serial.print(packetBuffer[i], DEC);
Serial.print(" ");
}
Serial.println();
bytesReceived = 0; // Reset for the next packet
}
}
}

52
eth_tcp_sender.ino Normal file
View File

@ -0,0 +1,52 @@
#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
}

52
eth_udp_receiv.ino Normal file
View File

@ -0,0 +1,52 @@
#include <NativeEthernet.h>
#include <NativeEthernetUdp.h>
byte mac[] = { 0x04, 0xE9, 0xE5, 0x10, 0x00, 0x02 }; // Unique MAC address for receiver
IPAddress ip(192, 168, 1, 101); // Receiver IP address
unsigned int localPort = 8888; // Port number to listen on
EthernetUDP Udp;
byte packetBuffer[256]; // Buffer to hold received data
void setup() {
Serial.begin(115200);
//while (!Serial); // Wait for Serial Monitor
Serial.println("Initializing Ethernet...");
Ethernet.begin(mac, ip);
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet hardware not found!");
while (true); // Halt execution
}
if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Ethernet cable not connected!");
}
Udp.begin(localPort); // Start listening on the specified port
Serial.println("UDP Receiver Ready");
}
void loop() {
int packetSize = Udp.parsePacket();
if (packetSize) {
Serial.print("Received packet of size: ");
Serial.println(packetSize);
// Read the 256 bytes into packetBuffer
int len = Udp.read(packetBuffer, sizeof(packetBuffer));
if (len > 0) {
packetBuffer[len] = 0; // Null-terminate buffer for string output (optional)
Serial.print("Message: ");
for (int i = 0; i < len; i++) {
// Print the received data (in decimal for example)
Serial.print(packetBuffer[i], DEC);
Serial.print(" ");
}
Serial.println();
}
}
}

52
eth_udp_sender.ino Normal file
View File

@ -0,0 +1,52 @@
#include <NativeEthernet.h>
#include <NativeEthernetUdp.h>
byte mac[] = { 0x04, 0xE9, 0xE5, 0x10, 0x00, 0x01 }; // Unique MAC address
IPAddress ip(192, 168, 1, 100); // Sender IP address
IPAddress receiverIP(192, 168, 1, 101); // Receiver IP address
unsigned int receiverPort = 8888; // Port number to send data
EthernetUDP Udp;
// Create a 256-byte buffer to send
byte buffer[256];
void setup() {
Serial.begin(115200);
//while (!Serial); // Wait for Serial Monitor
Serial.println("Initializing Ethernet...");
Ethernet.begin(mac, ip);
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield not found!");
while (true); // Halt execution
}
if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Ethernet cable not connected!");
}
Udp.begin(8888); // Initialize UDP
Serial.println("UDP Sender Ready");
// Fill the buffer with some data (e.g., increasing bytes)
for (int i = 0; i < 256; i++) {
buffer[i] = i; // Example: Fill with 0, 1, 2, ..., 255
}
}
void loop() {
Serial.println("Sending packet...");
// Send the 256-byte buffer to the receiver
if (Udp.beginPacket(receiverIP, receiverPort)) {
Udp.write(buffer, 256); // Send the full 256 bytes
Udp.endPacket();
Serial.println("Packet sent successfully");
} else {
Serial.println("Failed to start UDP packet");
}
//delay(1000); // Wait for a second before sending the next packet
}