Arduino to Arduino direct ethernet data exchange

Hello comunity. I have been working with Arduino controllers for some time. But this is the first time I need to use ethernet shields in my progect. I have two

Arduino UNO boards

and two

WIZnet ethernet shields W5100

The task is simple. If i recieve some data on com port at one end i should transfer this data to another board, or if an io is trigered also to send data about this event to other board.,

I have found a code snapshot on arduino forum here

http://forum.arduino.cc/index.php?topic=57396.0

But due to the fact that it is for old library types I have changed it to corespond to new library types

Server board

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


byte mac[] = {0x90, 0xa2, 0xda, 0x00, 0x23, 0x16};


byte ip[]  = {10, 10, 5, 124};

const unsigned int localPort = 1369;


char recvdBuffer[UDP_TX_PACKET_MAX_SIZE+1]; //buffer to hold incoming packet
char replyBuffer[UDP_TX_PACKET_MAX_SIZE+1]; //a string to send back


byte        remoteIp[4]; // Will save the originator's IP address from the received packet
unsigned int remotePort; // Will save the originator's port number from the received packet


const int ledPin = 9;

EthernetUDP udp;

void setup()
{
  
   Ethernet.begin(mac,ip);

 
   udp.begin(localPort);

   Serial.begin(9600);
   pinMode(ledPin, OUTPUT);

}

void loop()
{
   
   int recvdSize = udp.available(); // note that this includes the UDP header
   if(recvdSize) {
       
       udp.beginPacket(remoteIp, remotePort);
       udp.read(recvdBuffer,UDP_TX_PACKET_MAX_SIZE);
      
       recvdBuffer[recvdSize] = '\0';
    
       recvdSize -= 8;
       
       Serial.print("Received packet data size = ");
       Serial.print(recvdSize);
       Serial.print(" from ");
       for (int i = 0; i < 4; i++) { // Print IP address xxx.xxx.xxx.xxx
           Serial.print(remoteIp[i], DEC);
           if (i < 3) {
               Serial.print(".");
           }
       }
       Serial.print(", port number ");
       Serial.println(remotePort);

       Serial.print("Contents: ");
       Serial.println(recvdBuffer);
       
       switch (recvdBuffer[0]) {
         case 'H':
           Serial.println("Turning LED on");
           digitalWrite(ledPin, HIGH);
           strncpy(replyBuffer, "H: LED is On",UDP_TX_PACKET_MAX_SIZE);
           break;
         case 'L':
           Serial.println("Turning LED off");
           strncpy(replyBuffer, "L: LED is Off", UDP_TX_PACKET_MAX_SIZE);
           digitalWrite(ledPin, LOW);
           break;
         default:
           Serial.println("No action");
           strncpy(replyBuffer, "Unknown command", UDP_TX_PACKET_MAX_SIZE);
           break;
       }
       udp.beginPacket(remoteIp, remotePort);
       udp.write(replyBuffer);
   }
}

Client board

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


byte mac[] = {0x90, 0xa2, 0xda, 0x00, 0x11, 0x07};


byte ip[]  = {10, 10, 5, 123};


const unsigned int localPort = 9631;

char packetBuffer[UDP_TX_PACKET_MAX_SIZE+1]; 


byte remoteIp[4] = {10, 10, 5, 124};


unsigned int remotePort = 1369;


byte         recvdIp[4];
unsigned int recvdPort;

const int ledPin = 9;

EthernetUDP udp;

void setup()
{
  
   Ethernet.begin(mac, ip);

  
   udp.begin(localPort);

   Serial.begin(9600);
   pinMode(ledPin, OUTPUT);

}



char UDPMessageBuffer[80];
void loop()
{
  
   int packetSize = udp.available();
   if(packetSize)
   {
       udp.beginPacket(recvdIp, recvdPort);
       udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);
      
       packetBuffer[packetSize] = '\0';
       
      
       packetSize -= 8;
       
       Serial.print("Received packet size = ");
       Serial.print(packetSize);
       Serial.print(" from ");
       for (int i = 0; i < 4; i++) { // Print IP address xxx.xxx.xxx.xxx
           Serial.print(remoteIp[i], DEC);
           if (i < 3) {
               Serial.print(".");
           }
       }
       Serial.print(", port number ");
       Serial.println(remotePort);

       Serial.print("Contents: ");
       Serial.println(packetBuffer);
   }


  
   char inchar;
   if (Serial.available() > 0) {
       inchar = Serial.read();
       switch(inchar) {
       case 'H':
       case 'h':
           Serial.println("Sending 'H'");
           strcpy(UDPMessageBuffer, "H");
           udp.beginPacket(remoteIp, remotePort);
           udp.write(UDPMessageBuffer);
           break;

       case 'L':
       case 'l':
           Serial.println("Sending 'L'");
           strcpy(UDPMessageBuffer, "L");
           udp.beginPacket(remoteIp, remotePort);
           udp.write(UDPMessageBuffer);
           break;
       }
   }
}

But it doesn't work =/

I have downloaded all the updates for the shield 5100 libraries but still no result.
My question is - If there ixists a simple (e.d. very simple) code example to send data from one board via ethernet shield - for example from com console and to triger some i/o on the other board?

But it doesn't work

Of course it does. The code on one Arduino or the other simply does not do what you expect. All that means is that your expectations are wrong.

How are you connecting the two ethernet shields?

Of course, the fact that neither the client or the server ever calls the EthernetUDP::endPacket() function, to actually send the packet, could be partially responsible for the discrepancy between what the code actually does and what your expectations are.

PaulS:
How are you connecting the two ethernet shields?

I am connecting them directly one shiled to another with a custom UTP cable (WO)(O)(WG)(B)(WB)(G)(WBr)(Br)

Also I have found code snapshots on arduino code example Udp.read() and Udp.write().

The code is
Sending part

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

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEE };

IPAddress ip(192, 168, 1, 176);
IPAddress destIp(92, 168, 1, 177);

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

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

EthernetUDP Udp;

//String message;

void setup() {

  // start the Ethernet and UDP:

  Ethernet.begin(mac,ip);
  Udp.begin(localPort);
  Serial.begin(115200);
  Serial.print("Local IP: ");
  Serial.println(Ethernet.localIP());
}

void loop() {
Serial.print("Local IP: ");
Serial.println(Ethernet.localIP());

  Udp.beginPacket(destIp, localPort);

    Udp.write("HELLO!");

    Udp.endPacket();

}

and reading part

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

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {  
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 177);

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

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

char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,

void setup() 
{
  // start the Ethernet and UDP:
  Ethernet.begin(mac,ip);
  Udp.begin(localPort);
  pinMode(5, OUTPUT);
  Serial.begin(115200);
  Serial.print("Local IP: ");
 Serial.println(Ethernet.localIP());
}

void loop() 
{       
  int packetSize = Udp.parsePacket();
  if(packetSize)
  {
      digitalWrite(5, HIGH);
      delay(500); 
      digitalWrite(5, LOW);  
      delay(500);
         
    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);   
}
}

Seems ok. I get the RX led on one shield and TX led on the other flashing. But No data comming. Also If i delete the if statement to simply see the incoming traffic i see garbage simbols and nothing more. It means the code is working but it is crooked. What coulkd be wrong? Please advice some solution or where should i search for a real working example.

UPDATE:

Also i have tried the programm suggested in the example provided here
http://rafaelbaquero.com/blog/embedded-systems/arduino-ethernet-udp-blinky-part-1/

SocketTest v 3.0.0.0

The programm cannot connect to arduino with direct connection from my pc. I change the ip from dhcp to static as provided in client code and try both starit and crossover cables. Nothing works. No data transmited.

But I can ping Arduino from CMD and I gues that it is good. What could be wrong? Please advice something.

One more thing i have noticed. The LINK led is constantly flashig. Does it mean that it t looping trying to establish a lik or is it normal?

PaulS:
Of course, the fact that neither the client or the server ever calls the EthernetUDP::endPacket() function, to actually send the packet, could be partially responsible for the discrepancy between what the code actually does and what your expectations are.

I have changed the code slightly to see what is inside the int packetSize to show it in console and I have an endless string of zeros going

Also I have added Udp.EndPacket(); at the end

 int packetSize = Udp.parsePacket();
  Serial.print(packetSize);
  if(packetSize)
  {
      digitalWrite(5, HIGH);
      delay(500); 
      digitalWrite(5, LOW);  
      delay(500);
         
    Serial.print("Received packet of size ");
    Serial.println(packetSize);
    Serial.print("From ");
    //IPAddress remote = Udp.remoteIP();
    IPAddress remote = remIp;
    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);       
    Udp.endPacket();
  }

It means that something is realy beeing sent but not the hello word.

All ok! I have figured it out on myself. =)

Sketch of SENDER 100% working over simple cat5 UTP cable.

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

// define timig intervals
#define LED_ON   100
#define LED_OFF  900
#define MSG_INTERVAL 1000

// SD slave select pin
#define SD_SS 4

// led pins
#define LED1 5    // pin for led 1
#define LED2 6    // pin for led 2

// led state
boolean ledOn;

// network parameters
byte mac[] = {
  0x90, 0xA2, 0xDA, 0x0E, 0x05, 0x04};  // ethernet interface MAC address
IPAddress localIp(192, 168, 1, 251);    // local ip address
IPAddress destIp(192, 168, 1, 15);      // destination ip address
unsigned int port = 5678;               // destination port

// EthernetUDP to send and receive messages.
EthernetUDP Udp;

// message string
char message[] = "Hello World!";

// timing
unsigned long previousLedMillis;
unsigned long ledInterval;
unsigned long previousSendMillis;
unsigned long sendInterval;

// setup the arduino and shields
void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(115200);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  // disable sd
  pinMode(SD_SS, OUTPUT);
  digitalWrite(SD_SS, HIGH);

  // initialize led pins
  pinMode(LED1, OUTPUT);
  digitalWrite(LED1, LOW);
  pinMode(LED2, OUTPUT);
  digitalWrite(LED2, LOW);

  // start ethernet and udp
  Ethernet.begin(mac,localIp);    // static ip version
  //if(Ethernet.begin(mac) == 0) {     // dhcp version
    // report failure to obtain network parameters
   // Serial.println("DHCP error");

    // no point in carrying on, loop indefinitely:
   // while(true)
     // ;
  //}
  
  // open UDP port
  Udp.begin(port);

  // show the local ip address (useful for dhcp)
  Serial.print("Local IP: ");
  Serial.println(Ethernet.localIP());

  // initialize previous millis variables
  previousLedMillis = 0;
  previousSendMillis = 0;

  // initialize send interval
  sendInterval = MSG_INTERVAL;

  // initialize led1 state and interval
  ledOn = false;
  ledInterval = LED_OFF;
}

// do tasks
void loop() {  
  // check if led state has to be toggled
  if(millis() - previousLedMillis >= ledInterval) {
    toggleLed();
  }

  // check if udp string has to be sent
  if(millis() - previousSendMillis >= sendInterval) {
    sendMessage();
  }
}

// send udp string
void sendMessage() {
  Serial.println("Sending UDP message");

  // store current millis
  previousSendMillis = millis();

  // send udp message
  Udp.beginPacket(destIp, port);
  Udp.write(message);
  Udp.endPacket();
}

// toggle led1 state
void toggleLed() {
  // store current millis
  previousLedMillis = millis();

  // toggle led1 state
  if(ledOn) {
    digitalWrite(LED1, LOW);
    digitalWrite(LED2, LOW);
    ledOn = false;
    ledInterval = LED_OFF;
  } 
  else {
    digitalWrite(LED1, HIGH);
    digitalWrite(LED2, HIGH);
    ledOn = true;
    ledInterval = LED_ON;
  }
}

Sketch of RESIEVER 100% working

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

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {  
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 15);
IPAddress remIp(92, 168, 1, 176);

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

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

char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,

void setup() 
{
  // start the Ethernet and UDP:
  Ethernet.begin(mac,ip);
  Udp.begin(localPort);
  pinMode(5, OUTPUT);
  Serial.begin(115200);
  Serial.print("Local IP: ");
 Serial.println(Ethernet.localIP());
}

void loop() 
{  
  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);
}
 
}

open the terminal window and watch the result "HELLO WORLD!";
Topic closed. Thanks for everybody.

Hi mate. Sorry its been ages since you have sorted this but i am trying to replicate this but can't seem to receive anything from the transmitter. Could you potentially help me out?