Ethernet 2 Library UDP Example :Need code how to disconnect the UDP

Hi all,
I am using the Hardware: Arduino Mega + Arduino Ethernet shield 2.
I have downloaded the Ethernet 2 Library from the library manager and used the UDP Example code as attached. Kindly refer the attachment. I am able to send and receive the Data from Arduino Mega + Ethernet shield 2. How to write code for disconnection of UDP communication. whenever i disconnect the ethernet cable from board or PC, I should be shown as Cable disconnected. What is the syntax to do it. Kindly let us know and help on this.

Regards,
V. Prakash
UDP_Example_Code.txt (2.2 KB)

Isn't UDP connectionless ?
:thinking:

1 Like

use the Ethernet library

1 Like

Hi Sir, Thanks for the reply. this linkstatus will be for ethernet wire connect or disconnect. In case of data not send properly through the wire, can be detected in the UDP? if yes,How to do it. Kindly let us know.

No - UDP is, by definition, unreliable.

1 Like

Hi Sir. I tried using the linkstatus code with the code attached earlier. I had added this code inside the void loop as follows:

void loop () {
  if (Ethernet.linkStatus() == Unknown) {
    Serial.println("Link status unknown. Link status detection is only available with W5200 and W5500.");
  }
  else if (Ethernet.linkStatus() == LinkON) {
    Serial.println("Link status: On");
  }
  else if (Ethernet.linkStatus() == LinkOFF) {
    Serial.println("Link status: Off");
  }
}'

It is showing the error as 'class EthernetClass' has no member named 'linkStatus'. How to resolve this. Kindly help.

I have added the Ethernet.linkStatus() library in the already attached code.
Here is the full code used:

/*
  UDPSendReceive.pde:
 This sketch receives UDP message strings, prints them to the serial port
 and sends an "acknowledge" string back to the sender

 A Processing sketch is included at the end of file that can be used to send
 and received messages for testing with a computer.

 created 21 Aug 2010
 by Michael Margolis

 This code is in the public domain.
 */

#include <SPI.h>         // needed for Arduino versions later than 0018
#include <Ethernet2.h>
#include <EthernetUdp2.h>         // UDP library from: bjoern@cs.stanford.edu 12/30/2008

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
  0xA8, 0x61, 0x0A, 0xAE, 0xE0, 0x81
};
IPAddress ip(192, 168, 100, 48);

unsigned int localPort = 8888;      // local port to listen on

// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
char  ReplyBuffer[] = "acknowledged";       // a string to send back

// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;

void setup() {
  // start the Ethernet and UDP:
  Ethernet.begin(mac, ip);
  Udp.begin(localPort);

  Serial.begin(9600);
}

void loop() {

 if (Ethernet.linkStatus() == Unknown) {
    Serial.println("Link status unknown. Link status detection is only available with W5200 and W5500.");
  }
  else if (Ethernet.linkStatus() == LinkON) {
    Serial.println("Link status: On");
  }
  else if (Ethernet.linkStatus() == LinkOFF) {
    Serial.println("Link status: Off");
  }

  // if there's data available, read a packet
  int packetSize = Udp.parsePacket();
  if (packetSize)
  {
    Serial.print("Received packet of size ");
    Serial.println(packetSize);
    Serial.print("From ");
    IPAddress remote = Udp.remoteIP();
    for (int i = 0; i < 4; i++)
    {
      Serial.print(remote[i], DEC);
      if (i < 3)
      {
        Serial.print(".");
      }
    }
    Serial.print(", port ");
    Serial.println(Udp.remotePort());

    // read the packet into packetBufffer
    Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
    Serial.println("Contents:");
    Serial.println(packetBuffer);

    // send a reply, to the IP address and port that sent us the packet we received
    Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
    Udp.write(ReplyBuffer);
    Udp.endPacket();
  }
  delay(10);
}

It is showing the error as 'class EthernetClass' has no member named 'linkStatus'. How to resolve this. Kindly help.

use the Ethernet library, not Ethernet2

2 Likes

So you saw this in its documentation:

:thinking:

1 Like

Ok. Thanks for the answer. Will download Ethernet library 2.0.2 and check the same.

Thanks for the update.

Hi sir, After updating the Ethernet library 2.0.2, the error as 'class EthernetClass' has no member named 'linkStatus'. is cleared and it is working. Ethernetlinkstatus is working. In the above example code, after reading the Data packets from the PC UDP terminal Software (Hercules), it is sending the Data to the PC Software(Hercules). My application is to read the data from the Serial Port (Serial Monitor) and the same data have to be sent Via Ethernet Port using UDP to the PC(Hercules). Kindly let us know. How to do this? Kindly help on this.

Regards,
V.Prakash

What have you tried? Where are you stuck?

As always, if you can't solve the Big Problem all at once, break it down into smaller ones, and keep breaking them down until you reach a manageable level.

For a start, you can break down your task into:

  1. read data from the Serial Port;
  2. send data Via Ethernet Port using UDP.

Start by working on each of those separately...

1 Like

Ok Sir. Thank you for the guidance. I will try separately and update on this.

Hi sir, I had broke down the task and worked on it.

  1. Program to read data from the Serial Port and this code is working perfectly:
void setup() {
  // initialize both serial ports:
  Serial.begin(9600);
}

void loop() {

  // read from port 0, send to port 0:
  if (Serial.available()) {
	int inByte = Serial.read();
	Serial.write(inByte);
  }
}

  1. Send data Via Ethernet Port using UDP. I tried this below code to send the UDP data to the Hercules Software(PC). Below code is not working:
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>

byte mac[] = {
  0xA8, 0x61, 0x0A, 0xAE, 0xE0, 0x81
};
IPAddress ip(192, 168, 100, 48);

unsigned int localPort = 8888;      // local port to listen on

// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;

void setup() {
  // You can use Ethernet.init(pin) to configure the CS pin
  //Ethernet.init(10);  // Most Arduino shields
  //Ethernet.init(5);   // MKR ETH Shield
  //Ethernet.init(0);   // Teensy 2.0
  //Ethernet.init(20);  // Teensy++ 2.0
  //Ethernet.init(15);  // ESP8266 with Adafruit FeatherWing Ethernet
  //Ethernet.init(33);  // ESP32 with Adafruit FeatherWing Ethernet

  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);

  // start the Ethernet
  Ethernet.begin(mac, ip);

  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

// start UDP
  Udp.begin(localPort);
}
void loop()
{
byte inChar = 0x55;
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
Udp.write(inChar);
Udp.endPacket();
}

In the above code, i am sending the Data =0x55 to the PC(Hercules) from arduino mega + ethernet shield 2 board. Kindly let us know is there any mistake i made in the code.

To cross check the Hardwares and the PC software is working. I had tested the read and write sample code. this code is working

/*
  UDPSendReceive.pde:
 This sketch receives UDP message strings, prints them to the serial port
 and sends an "acknowledge" string back to the sender

 A Processing sketch is included at the end of file that can be used to send
 and received messages for testing with a computer.

 created 21 Aug 2010
 by Michael Margolis

 This code is in the public domain.
 */

#include <SPI.h>         // needed for Arduino versions later than 0018
#include <Ethernet2.h>
#include <EthernetUdp2.h>         // UDP library from: bjoern@cs.stanford.edu 12/30/2008

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
  0xA8, 0x61, 0x0A, 0xAE, 0xE0, 0x81
};
IPAddress ip(192, 168, 100, 48);

unsigned int localPort = 8888;      // local port to listen on

// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
char  ReplyBuffer[] = "acknowledged";       // a string to send back

// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;

void setup() {
  // start the Ethernet and UDP:
  Ethernet.begin(mac, ip);
  Udp.begin(localPort);

  Serial.begin(9600);
}

void loop() {

 if (Ethernet.linkStatus() == Unknown) {
    Serial.println("Link status unknown. Link status detection is only available with W5200 and W5500.");
  }
  else if (Ethernet.linkStatus() == LinkON) {
    Serial.println("Link status: On");
  }
  else if (Ethernet.linkStatus() == LinkOFF) {
    Serial.println("Link status: Off");
  }

  // if there's data available, read a packet
  int packetSize = Udp.parsePacket();
  if (packetSize)
  {
    Serial.print("Received packet of size ");
    Serial.println(packetSize);
    Serial.print("From ");
    IPAddress remote = Udp.remoteIP();
    for (int i = 0; i < 4; i++)
    {
      Serial.print(remote[i], DEC);
      if (i < 3)
      {
        Serial.print(".");
      }
    }
    Serial.print(", port ");
    Serial.println(Udp.remotePort());

    // read the packet into packetBufffer
    Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
    Serial.println("Contents:");
    Serial.println(packetBuffer);

    // send a reply, to the IP address and port that sent us the packet we received
    Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
    Udp.write(ReplyBuffer);
    Udp.endPacket();
  }
  delay(10);
}

Kindly let us know what is the mistake i made in the Send data Via Ethernet Port using UDP code.

Hi sir, I tried Send data Via Ethernet Port using UDP. I tried this below code to send the UDP data to the Hercules Software(PC). Below code is not working. Kindly help on this to send the UDP data to PC

#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>

byte mac[] = {
  0xA8, 0x61, 0x0A, 0xAE, 0xE0, 0x81
};
IPAddress ip(192, 168, 100, 48);

unsigned int localPort = 8888;      // local port to listen on

// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;

void setup() {
  // You can use Ethernet.init(pin) to configure the CS pin
  //Ethernet.init(10);  // Most Arduino shields
  //Ethernet.init(5);   // MKR ETH Shield
  //Ethernet.init(0);   // Teensy 2.0
  //Ethernet.init(20);  // Teensy++ 2.0
  //Ethernet.init(15);  // ESP8266 with Adafruit FeatherWing Ethernet
  //Ethernet.init(33);  // ESP32 with Adafruit FeatherWing Ethernet

  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);

  // start the Ethernet
  Ethernet.begin(mac, ip);

  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

// start UDP
  Udp.begin(localPort);
}
void loop()
{
byte inChar = 0x55;
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
Udp.write(inChar);
Udp.endPacket();
}

Hi sir, Did anyone can guide me please. I was strucked in it?

Hi sir, My Concern is :

 if(button.isPressed())
  {
    byte Data = 0x55;
    Serial.println("The button is pressed");
        Udp.beginPacket("192.168.100.46", 23);
     //   Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
       Udp.write("released");
     Udp.write(Data);
       Udp.endPacket();
  }

Only "released" text is receiving in the PC. But the Byte Data= 0x55 is not receiving in the PC. Only my string data is receiving in the PC. my byte Data is not receiving in the PC. How to sort it out sir.. Kindly help

Sir . In the above code attached, Only String "released" is writing into my PC. But the Data = 0x55 is not written into the PC. Why the Data is not written into the PC? Kindly guide me on this.

Presumably, Udp.write is expecting a string? What does the documentation say?

1 Like

Yes Sir. Thanks for the reply. Yes your are right. Udp.write("String") is expecting a string. I have changed the udp.write syntax to Udp.Write(buffer, size). In this also, Data byte is not written into the PC. Here is the code. I had filled the buffer with some datas and sending the buffer to PC. In the below code, only string "released" is written into the PC. but the reply buffer data is not written into the PC. what is the Mistake i made.?

Here is the code for your reference:

if(button.isPressed())
  {

ReplyBuffer[0] =1;
ReplyBuffer[1] =2;
ReplyBuffer[2] =3;
ReplyBuffer[3] =4;
ReplyBuffer[4] =5;

Serial.println("The button is pressed");
Udp.beginPacket("192.168.100.46", 23);
 Udp.write("released");
 Udp.write(ReplyBuffer,5);
 Udp.endPacket();
  }