53 lines
1.6 KiB
Arduino
53 lines
1.6 KiB
Arduino
|
#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();
|
||
|
}
|
||
|
}
|
||
|
}
|