Want to use two IP addresses on the ethernet coding, but somehow they conflict

Dear Arduino experts,

This is my first post on this forum, but I used a lot of information already from the forum. Last year I started with an automatic irrigation system, based on a Arduino Mega 2560 controller. Step-by-step I figured out the installation, coding and perspectives to automatically irrigate my garden plants.

I also bought an ethernet module, which I coupled with a nano-router, to cheapy get a WIFI functionality without the WIFI board. I'm almost finished with the project but I have somehow a problem. I wanted to interact my Arduino with my iPhone, by an app called iArd. So far so good, after some coding I figured it out. However I also want to get the unix time from the UDP NTP server. When I use the coding (iArd & the tutorial UdpNtpClient from the ethernet examples) individually it works really fine. But when I want to combine these two "ethernet functionalities", somehow only one is functioning correctly.

I can show you parts of my coding (since it's a very long code):

See also this example code from the iArd app.

// 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, 0x00, 0x58, 0x27 };
byte ip[] = {
  192,168,2,100};
byte gateway[] = { 
  192,168,2,254 };
byte subnet[] = { 
  255,255,255,0 };
  
unsigned int localPort = 8888;      // local port to listen on
EthernetServer server(8888);

//UDP NTP server part
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac1[] = {  
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

IPAddress timeServer(132, 163, 4, 101); // time-a.timefreq.bldrdoc.gov NTP server
// IPAddress timeServer(132, 163, 4, 102); // time-b.timefreq.bldrdoc.gov NTP server
// IPAddress timeServer(132, 163, 4, 103); // time-c.timefreq.bldrdoc.gov NTP server

const int NTP_PACKET_SIZE= 48; // NTP time stamp is in the first 48 bytes of the message

byte packetBuffer[ NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets 

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

void setup() {

  Serial.begin(9600);
  Serial.println("Starting iArd...");
  // start the Ethernet
  Ethernet.begin(mac, ip,gateway, subnet);
  server.begin();

  //port setting:
  pinMode (9, OUTPUT); 
  pinMode (6, OUTPUT);
  pinMode (8, OUTPUT);
  pinMode(voltageFlipPin1, OUTPUT);
  pinMode(voltageFlipPin2, OUTPUT);
  pinMode(sensorPin, INPUT);
  delay(5000);
  Serial.println("iArd ready!");
  
  pinMode(Relay_1, OUTPUT);   
  digitalWrite(Relay_1, RELAY_OFF);
  
  timer.every(60000,sensorON);  

  // start Ethernet and UDP
  if (Ethernet.begin(mac1) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    for(;;)
      ;
  }
  Udp.begin(localPort);

}

I already tried some things: only using one MAC adress, or renaming the MAC adres of the UDP to MAC1, assigning another local port, trying to arrange everything from one IP-adress (however then the unix time is not displayed...) etc. etc.

Quite honestly, I'm not an expert about "internet protocols", so I do not know how to configure the right coding. Should I still use two IP-adresses (one for the upload to iArd and one to get the unix time from the ethernet shield), which MAC addresses to use, and how to combine both "ethernet loops" in de void loop?

I want to upload and interact through my iPhone with the Arduino (so that's updated in the loop) and I need to extract the unix time from another IP-address, because I need the time for some internal calculation steps.

Maybe a simple solution for you, but quite honestly I completely lost in all "internet protocols". It seems they somehow conflict with each other.

Since this is my first post, please excuse me for the things I forgot to tell!

Hopefully this is the last hurdle to this Arduino challenge!!! Thanks, Pieter

  Ethernet.begin(mac, ip,gateway, subnet);

You are missing an argument. You need to provide: mac, ip, DNS_SERVER, gateway, subnet.

If you leave out the DNS Server address the code will use 'gateway' for your DNS Server and 'subnet' for your gateway.

Thanks for your addition John, this was directly copied from the example sketch of the iArd app. I'll use it in my coding.

Unfortunately the problem remains, when I try to combine 2 "IP-addresses" for my Arduino functionality. When I try to contact the UDP server, it always gives the same unix date (somewhere in 2035), because the data needs to be send to the app (2 seconds interval)...

I'll try to give a screenshot of the "error"....

You don't need two ip addresses. You don't need two mac addresses. One of each will do. Pick one. Usually for a server you should use the static ip. I avoid using the same port for both tcp and udp. That will cause confusion.

I run udp and tcp on the same ip with a Mega 2560 and ethernet shield.

Somehow when I use the IP-address of the iArd app via the local network, the UDP server isn't working. Also when I have one IP-address and one MAC-address.

Should the IP-address of the time-server always be the one from the "time-a.timefreq.bldrdoc.gov NTP server". When I use a random IP-address from the network, no (correct) time is displayed...

As I stated my "internet protocol" knowledge is very limited... sorry for that!

I get a NTP server ip by resolving pool.ntp.org.

Are you trying to run both a server and s client?

zoomkat: Are you trying to run both a server and s client?

Probably??!! :drooling_face:

I use the ethernet server for data upload to the iArd app and communication via this app. It's via the local network from my "home router", a random IP-address.

The default timeServer IP-address from the Arduino Ethernet example code (UdpNtpClient) I want to use to extract the time from a NTP server. It's another type of address: IPAddress timeServer(132, 163, 4, 101);

SurferTim: I get a NTP server ip by resolving pool.ntp.org.

What's the benefit from that? Will that resolve the problem?

I don't know about resolving all your problems, but it will provide a current NTP server ip. It works for me. I use the ethernet library dns to resolve that domain server to an ip.