Udp.endpacket keeps returning 0

Hello there,

I’ve been struggling with connecting 2 Uno’s plus ethernet shields directly with Udp, no router, just a direct ethernetcable in between.

This should work as I’ve read in examples in this forum, but Udp.endpacket of the sending Arduino keeps returning 0. I tried a normal ethernet cable, I tried a crossover cable, I purchased 2 new Uno Rev 3’s and 2 new Ethernet shields V2. To no avail.

I understand the 0 indicates that the sender thinks the receiver is not there.

I borrowed OPS’s (other people’s sketches) from the forum but get the same status of 0 on Udp.endpacket. I must make a silly mistake. The ethernet cable does not touch the rest of my network it should be totally isolated network of two nodes.

Since the sender cannot send, of course the receiver doesn’t receive and prints “No data” (at least that makes sense).

This is the sender’s sketch:

#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

byte mac[] = {0x90, 0xA2, 0xDA, 0x11, 0x13, 0xFA};

IPAddress Send_ip (192, 168, 2, 177);
int Send_port = 8888;      // remote port to send to

IPAddress Rec_ip(192, 168, 2, 172);
int Rec_port = 8887;      // local port to listen on

char packetBuffer[UDP_TX_PACKET_MAX_SIZE];
char  SendBuffer[] = "S1 Moves";

int stat;

EthernetUDP Udp;

void setup() {
  Serial.begin(9600);
  Ethernet.begin(mac, Send_ip);
  Serial.println(Ethernet.localIP());
  stat = Udp.begin(Send_port);
  Serial.println ("success udp.begin? ");
  Serial.println (stat);
}

void loop() {
  stat = Udp.beginPacket(Rec_ip, Rec_port);
  Serial.println ("success udp.beginpacket? ");
  Serial.println (stat);
  stat = Udp.write(SendBuffer,UDP_TX_PACKET_MAX_SIZE);
  Serial.println ("success udp.write? ");
  Serial.println (stat);
  stat = Udp.endPacket();
  Serial.println ("success udp.endpacket? ");
  Serial.println (stat);
  Serial.print ("package sent with content: ");
  Serial.println (SendBuffer);
  delay (5000);
}

And the receiver’s sketch:

#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[] = {
  0x90, 0xA2, 0xDA, 0x11, 0x12, 0xF9
};
IPAddress Send_ip (192, 168, 2, 177);
unsigned int Send_port = 8888;      // local port to listen on

IPAddress Rec_ip(192, 168, 2, 172);
unsigned int Rec_port = 8887;      // local port to listen on

// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE];  //buffer to hold incoming packet,

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

void setup() {
  Serial.begin(9600);
  Ethernet.begin(mac, Rec_ip); /* my credentials  */
  Serial.println(Ethernet.localIP());
  Udp.begin(Rec_port);
}

void loop() {
  int packetSize = 0;

  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);

  }
  else
    Serial.println ("No data ");
  delay(1000);
}

The sender’s serial output is:

192.168.2.177
success udp.begin?
1
success udp.beginpacket?
1
success udp.write?
24
success udp.endpacket?
0
package sent with content: S1 Moves

Any suggestion is highly appreciated.

Ton

stat = Udp.write(SendBuffer,UDP_TX_PACKET_MAX_SIZE);

with a buffer that is 9 bytes in size?

The result code you receive from the udp.endpacket means a timeout, so I guess a network problem.

Did you check that network with a TCP connection? Did you use a cross-over cable for the connection between the two Arduinos? Or did you install a switch between the two?

Pylon, thanks for your reply. I examined your questions, started with the cross-over cable to find out it was broken....

How simple this was - spent days looking for SW issues and there it was: a cable.

I made a new crossover cable and all works well.

Wrt your buffer question: I can and will replace that size by a smaller one as soon as I finish the protocol.

Thanks again for your questions - on to the next step!
Ton

Adding to the conversation: I needed the above to work because the deployment of the system will have movement sensors at one end of a building and the main Arduino logic at the other end. Too distant for Wifi and I do not want to use cables.

I now use ethernet over powerlines (purchased Renkforce Ethernet-over-power adapters). All works well.

So the config is : one Uno with multiple movement sensors + Ethernetshield connected to Renkforce adapter in 220V powersocket at one end of the building; and another Uno + Ethernetshield connected to Renkforce adapter in 220V powersocket at other end of the building. The second Uno talks serial to a Mega with the main logic.

Since the Renkforce adapters resolve the crosswire thing, I can now use standard Ethernet cables between the Ethernet shields and the Renkforce adapter.

The adapters work across groups in the power system, so I can do this anywhere as long as both Uno’s are behind the same main power supply, of which I have only 1 in the building.

It may not be the cheapest solution but it definitely works and saves me a whole lot of work and 100 meter of cables.

Ton