53 lines
1.5 KiB
Arduino
53 lines
1.5 KiB
Arduino
|
#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
|
||
|
}
|