Using Arduino with W5500 module as a server

Hi,

I am using several arduinos each with a W5500 module to collect data and on request from a client program on the PC, send that data back to be logged. The PC knows what the MAC addresses I have assigned to each arduino is but it cant know what IP address the DHCP is going to assign so it uses the ARP table to find out.

I found that when the arduino connects to the network and is given an IP address by the DHCP, it does not show up in the ARP table. If I ping its IP address (which I can find out if I have the serial monitor running on the IDE) from the PC, it does show up on the table. I also found that if I try to connect to the PC as a client, then the arduino’s IP address does get added to the ARP table immediately.

Now, my PC is on IP address 192.168.1.14 so in my sketch I declare

IPAddress ip(192,168,1,14);

and my code for joining the network is as follows

void  StartEther()
{
  int l=0;
  bool inc= false;
  bool conok = false;
  
  memset(confirm, 0, 5);
  digitalWrite(NWK_LED,LOW); 
  if(Ethernet.begin(mac))
  {
    digitalWrite(NWK_LED,HIGH);
    nwkon=true;   
    server.begin();
    Serial.print("Server IP        : ");
    Serial.println(Ethernet.localIP());
    Serial.print("Sending ENQ to Client ... ");
    arp.connect(ip, 1234);
    delay(100);
    if(arp.connected())
    {
      arp.println("ENQ~");
      delay(100);
      inc = true;
      while (arp.connected() && inc == true) 
      {
        if (arp.available()) 
        {
          char c = arp.read();
          lastChr = millis();
          if (c == '~' && l<4)
            inc = false;
          else
          {
            confirm[l]=c;
            l++;
            if(l>3)
            {
              Serial.println("Incorrect reply");
              arp.flush();
              inc = false;
            }
          }
        }
        if(inc && (millis()-lastChr)> CTIMEOUT)
        {
          arp.flush();
          memset(confirm, 0, 5);
          inc = false;
        }
      }
    }
    if(confirm[0]=='A' && confirm[1]=='C' && confirm[2]=='K')  // Enquiry confirmed with "ACK"
    { 
      Serial.println("Client present");
      clientfound = true;
    }
    else
    {
      Serial.println("No Client found"); 
      clientfound = false;
    }
    arp.stop();
  }
  else
  {
    Serial.println("Network FAIL");
    nwkon=false;
  }
}

This work fine but if for any reason my PC started up and was given a different IP address, the whole thing would fail. I have tried setting the IP address to connect to as client to the broadcast address 255.255.255.255 but that doesn’t work.

Any different ideas how I can make the IP address of the arduino appear on the ARP table automatically

Presumably your router has a fixed address. Can you try (likely unsuccessfully) to connect to that from the Arduino or ping it perhaps. Maybe that'll get you an arp entry.

Most routers have the ability to assign fixed IP to known devices. So if you configure your router to always provide the same IP to your PC/Arduinos, your problem is solved.

BTW, 255.255.255.255 is not the broadcast address of your network. That depends on your network parameters. Given you use standard parameters your network is 192.168.1.0 with a network mask of 255.255.255.0. In that case the broadcast address is 192.168.1.255. If you ping that one all devices in the network should answer, so you have the ARP table filled almost instantly.

Hi,

@Wildbill, Ive already tried using the router's IP address for a connection and it fails. Thanks for that though.

@Pylon, Ive just tried using 192.168.1.255 from the arduino and it failed. You'll see in the code that I send an ENQ to the PC on port 1234 and my VB.net app has a listener running in a separate thread which replies with an ACK so the arduino knows the PC app is running. This works great when I know the PC's IP. If I could broadcast an ENQ to all IP's on port 1234 and when the PC replied, I would know what its current IP address is, that would be a perfect solution. That's what I hoped to do with a broadcast but it didn't work and I am a network newbie. Until six months ago, sockets were used to get the spark plugs out of my car!

I suppose the question is how can I send a message to a device which I know is listening on port 1234 but I don't know its IP?

Ideas gratefully received. Thanks guys. Steve.

Configure fixed IPs on your router!

The alternative is to use mDNS but that’s a bit heavy load for an AVR Arduino.