adruino sheild Sent data to another adruino sheild

Hi, I have problem with this code.
Basicly i need from one arduino with Ethernet sheild Read digital signal , and sent character to serial and after send Over ethernet sheild to another arduino with Ethernet sheild and Turn on or off led or something else.

Some details: Tx led flashing on both arduinos but RX led not. Really i dont know how this not works its too complicated for me.

Thank you for every help.

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


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


byte ip[]  = {192, 168, 0, 113};


const unsigned int localPort = 9631;

char packetBuffer[UDP_TX_PACKET_MAX_SIZE+1];


byte remoteIp[4] = {192, 168, 0, 111};


unsigned int remotePort = 1369;


byte         recvdIp[4];
unsigned int recvdPort;

const int ledPin = 4;
const int ledPin2 = 5;
const int levy = 2;
const int pravy = 3;
int buttonState = 0;

EthernetUDP udp;

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

  
   udp.begin(localPort);

   Serial.begin(9600);
   pinMode(ledPin, OUTPUT);
   pinMode(ledPin2, OUTPUT);
   pinMode(levy, INPUT);
   pinMode(pravy, INPUT);

}



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("Odesílám 'H'");
            strcpy(UDPMessageBuffer, "H");
            udp.beginPacket(remoteIp, remotePort);
            udp.write(UDPMessageBuffer);
            break;

       case 'L':
       case 'l':
           Serial.println("Odesínám 'L'");
           strcpy(UDPMessageBuffer, "L");
           udp.beginPacket(remoteIp, remotePort);
           udp.write(UDPMessageBuffer);
           break;
           
       case 'Y':
       case 'y':
           Serial.println("Odesílám 'Y'");
           strcpy(UDPMessageBuffer, "Y");
           udp.beginPacket(remoteIp, remotePort);
           udp.write(UDPMessageBuffer);
           break;

       case 'X':
       case 'x':
           Serial.println("Odesílám 'X'");
           strcpy(UDPMessageBuffer, "X");
           udp.beginPacket(remoteIp, remotePort);
           udp.write(UDPMessageBuffer);
           break;
       }
   }
}
receiver:
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>


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

byte ip[]  = {192, 168, 0, 111};

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 = 8;
const int ledPin2 = 9;

EthernetUDP udp;

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

 
   udp.begin(localPort);

   Serial.begin(9600);
   pinMode(ledPin, OUTPUT);
   pinMode(ledPin2, 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("Levy terč Aktivní");
           digitalWrite(ledPin, HIGH);
           strncpy(replyBuffer, "H: LED is On",UDP_TX_PACKET_MAX_SIZE);
           break;
         case 'L':
           Serial.println("Levy terč Neaktivní");
           strncpy(replyBuffer, "L: LED is Off", UDP_TX_PACKET_MAX_SIZE);
           digitalWrite(ledPin, LOW);
           break;
         case 'Y':
           Serial.println("Pravý terč aktivní");
           digitalWrite(ledPin2, HIGH);
           strncpy(replyBuffer, "Y: LED is On",UDP_TX_PACKET_MAX_SIZE);
           break;
         case 'X':
           Serial.println("Pravý terč nekativní");
           strncpy(replyBuffer, "X: LED is Off", UDP_TX_PACKET_MAX_SIZE);
           digitalWrite(ledPin2, LOW);
           break;
         default:
           Serial.println("No action");
           strncpy(replyBuffer, "Unknown command", UDP_TX_PACKET_MAX_SIZE);
           break;
       }
       udp.beginPacket(remoteIp, remotePort);
       udp.write(replyBuffer);
   }
}

How are the 2 Arduinos link, is it a crossover cable?
We have found that the autonegotiation on some Arduino shields does not appear to work correctly, if you connect both to a switch they work, but using a crossover cable doesn't. Cabling an arduino to a pc with an ethernet cable works so it gives a false sense of assuming arduino to arduino will work when is doesn't.

Hi, I use only straight-through cables . Crossover cables I think I can’t use. Because communication of shield and AP?

Sorry I can't work out what you mean. AP normaly refers to Access Point used for WiFi, but you say you are using 2 arduinos with ethernet sheids. How are the arduinos linked together, using a single cable, both connected to a switch or hub, or some other way?

Hi, Now its trying with switch (Arduino with sheild <-> switch <-> Arduino with sheild)
But for my project i need transfer data for 1/2 KM Distance and final situlation is (Arduino with sheild <-> AP < Wireless > AP <-> Arduino with sheild).
But i still not understand why this not working with switch and switch is connected to local network where is DHCP server but i thing i didnt need DHCP because they have static adresse.

Now i need only read digital input Transmmit data and receive and output data.

Hi,

“Now its trying with switch (Arduino with sheild <-> switch <-> Arduino with sheild)” Does mean you now have it working using one switch?

“But for my project i need transfer data for 1/2 KM Distance and final situlation is (Arduino with
sheild <-> AP < Wireless > AP <-> Arduino with sheild).” 1/2kM is further than a normal Wifi link is likel to operate - wil you be using special aeriels? To use AP <-> you wil need the APs setup so that one is normally in client more rather than AP more - have you have you check your Aps will work lie that?

“But i still not understand why this not working with switch and switch is connected to local network where is DHCP server but i thing i didnt need DHCP because they have static adresse.” I think we may have a misunderstanding in terminology here. Transmitter refers to a client that is sending data, Receiver refers to a client (server) that is getting data sent to it. Your Transmitter seems to start off trying to Read data but does this using BeginPacket which is sued for sending data - In addition Beginpacket needs the remote IP and remotePort, but you appear to be using (recvdIp, recvdPort) which have not yet been obtained as no message from the remote machine has been received. I think you should instead be simply using the udp.begin (remoteIP).

  1. Yes

  2. Therefore, what will communicate through the switch, I would like to try it after two access points. I think it should work when they are connected.

  3. If I understand correctly, use udp.beginPacket (remote Ip, remotePort) instead; change to udp.begin (remoteIP)? or am I so incomprehensible?

or could you fix it?

I think you have two different problems to tackle, which you can tckle seperately.

A) Replace the switch with a different solution that will work 1/2 KM apart. You should be able to set this up using some other hardware than can communicate such as 2 PCs just to ensure that the ethernet connections can use whatever soluton is needed and is independant of getting the software on the auduinos to work. No knowing the full details of what you are trying it is difficult to advise, but using 2 APs with YAGI aeriels (Pringle can antennae) might work.

A) Getting the software on the 2 arduinos to work properly can be done using an ethernet switch to start with and once working you can eplace the switch with your APs, or with fibre optic cable or WiMax or ... The software on the Arduinos will not need to be changed when the ethernet harddware is changed.

For your point 3, yes I believe you should change the udp.beginPacket(recvdIp, recvdPort) to udp.begin (remoteIP).

Still nothing.
Already i have connection with APs.

If i change udp.begin (remoteIP) i must change also byte recvdIp[4]; to byte remoteIP[4];

but does it send anything at all?

In serial Monitor looks fine sending But not receiving. You can see in Attachment.

Its possible communicate with you trough Facebook skype or something else ?

But thank you for everything.

You are awaiting packet UDP_TX_PACKET_MAX_SIZE+1 but you receive only UDP_TX_PACKET_MAX_SIZE. Problem should be there.

You have edited original sketch from example https://www.arduino.cc/en/Tutorial/UDPSendReceiveString
Also after receiving packet, you can add delay there. At ESP32 i need to use delay 500 ms or UDP message (datagram) was not readed by ESP completely. It was called "mode" for slow devices. I was using UDP Windows Client called Packet Sender.
Article about it from me: https://forum.arduino.cc/index.php?topic=634137.0

Hi, Please check direct messages on forum. Im from CZ.

koudysik:
Still nothing.
Already i have connection with APs.

If i change udp.begin (remoteIP) i must change also byte recvdIp[4]; to byte remoteIP[4];

but does it send anything at all?

In serial Monitor looks fine sending But not receiving. You can see in Attachment.

Its possible communicate with you trough Facebook skype or something else ?

But thank you for everything.

I will look into communicating through - I don't currently use it so will have to check things out.

In your Transmitter code the first thing it does after entering the Loop function is to check for a received packet, if there is one it then calls udp.beginPacket(recvdIp, recvdPort); There are 2 problems, the first is that beginpacket is used for transmitting not receiving and the second is that because nothing has been received yet both recvdIp and recvdPort will be zero so the call will fail anyway. Changing to udp.begin (remoteIP) will fix both problems. You do not need to change anything else because remoteIp is already defined. If you look a few lines further down in the code you wil see that the ip address that is printed out on your serial line is from remoteIp.

I think you need to put a some print startments in after:
int packetSize = udp.available();

such as:
serial.print("packetsize = ");
serial.println (packetsize);

If this then shows in your transmitter log, as zero (my suspicion) it would explain why you see nothing else.

It may be that you should be using udp.parsepacket() rather than udp.available - worth trying if packetsize always shows as 0.

I'm based in the UK, but my availablity varies, so it can be a day or two befoe I can repsond in some cases.

Hi
Thanks, For everything. But Don't have time for now, to try this edit.