Compare remoteIP to byte address

Hi All

First time poster, long time viewer. Hoping you could give me a hand.

I’m trying to use an if to compare the Udp.remoteIP() to a pre-determined byte address. See script below.

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

EthernetUDP Udp;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
byte ardIP[] = {172, 16, 0, 172};
byte QAIP[] = {172, 16, 0, 173};
const unsigned int ardPort = 24600;
EthernetServer server(80);

void setup(){
  Setup_EthernetServers();
}

void Setup_EthernetServers(){
  Serial.begin(9600);
  Ethernet.begin(mac, ardIP);
  Udp.begin(ardPort);
  server.begin();
}

void loop(){
  IPAddress incomingIP = Udp.remoteIP();
if (incomingIP == QAIP)
{//some code here
}
else
{
}

}

Any help would be greatly appreciated.

QAIP is an array of bytes
Where is the IPAddress data type defined and is it an array if bytes ?

If both of them are arrays of bytes then you can compare them using the memcmp() function

UKHeliBob:
Where is the IPAddress data type defined and is it an array if bytes ?

If both of them are arrays of bytes then you can compare them using the memcmp() function

The IPAddress 'incomingIP' is only defined by the Udp.remoteIP() as far as I know

Just declare QAIP as a variable of type "IPAddress". Then you can just compare them using the == operator:

const IPAddress ardIP = {172, 16, 0, 172};
const IPAddress QAIP = {172, 16, 0, 173};

Of course, an IP address is just an array of 4 bytes, but it doesn't make sense to use memcmp if you have an equality operator available.

Pieter

cameronaffleck:
The IPAddress 'incomingIP' is only defined by the Udp.remoteIP() as far as I know

C++ is a statically typed language, that means that you always know all types at compile type.
The EthernetUDP::remoteIP() method is declared to return an object of type IPAddress:

PieterP:
Just declare QAIP as a variable of type “IPAddress”. Then you can just compare them using the == operator:

Pieter

Sadly I need to keep QAIP as a byte type due to also changing these IP addresses via a web form and EEPROM - sorry should have mentioned that

Of course, an IP address is just an array of 4 bytes, but it doesn't make sense to use memcmp if you have an equality operator available.

Can you compare array like that ?
For example

byte arrayA[] = {1, 2, 3, 4};
byte arrayB[] = {1, 2, 3, 4};

void setup()
{
  Serial.begin(115200);
  Serial.println("Using == ");
  if (arrayA == arrayB)
  {
    Serial.println("match");
  }
  else
  {
    Serial.println("no match");
  }
  Serial.println("Using memcmp()");
  if (memcmp(arrayA, arrayB, 4) == 0)
  {
    Serial.println("match");
  }
  else
  {
    Serial.println("no match");
  }
}
void loop()
{
}

cameronaffleck:
Sadly I need to keep QAIP as a byte type due to also changing these IP addresses via a web form and EEPROM - sorry should have mentioned that

Why would changing the IP address via the web or saving them in EEPROM require them to be an array of bytes? You should always try to use types that express your intent. QAIP is an IP address, so its type should be IPAddress.

That being said, IP addresses can be converted to bytes and vice versa, if you really need it:

UKHeliBob:
Can you compare array like that ?

Not if you declare them as arrays. I'm talking about the built-in IPAddress class, please see the link in my first reply.

PieterP:
Why would changing the IP address via the web or saving them in EEPROM require them to be an array of bytes? You should always try to use types that express your intent. QAIP is an IP address, so its type should be IPAddress.

That being said, IP addresses can be converted to bytes and vice versa, if you really need it:
ArduinoCore-avr/IPAddress.h at 7a2e1cd815266fef3012a5c9b48f88d78551f838 · arduino/ArduinoCore-avr · GitHub
Not if you declare them as arrays. I’m talking about the built-in IPAddress class, please see the link in my first reply.

I don’t follow how to convert IPAddresses into bytes, but I’ll do some homework and try to work it out. Alternatively I’ll see if using the IPAddress type for my web form will allow the web server to run…

  [b][color=#d35400]IPAddress[/color][/b] [color=#000000]ip[/color] [color=#434f54]=[/color] [color=#000000]{[/color][color=#000000]192[/color][color=#434f54],[/color][color=#000000]168[/color][color=#434f54],[/color][color=#000000]1[/color][color=#434f54],[/color][color=#000000]1[/color][color=#000000]}[/color][color=#000000];[/color]   [color=#434f54]// Create an IP address[/color]
  [color=#00979c]uint8_t[/color] [color=#000000]firstByte[/color] [color=#434f54]=[/color] [color=#000000]ip[/color][color=#000000][[/color][color=#000000]0[/color][color=#000000]][/color][color=#000000];[/color]      [color=#434f54]// Get the first byte of the address[/color]
  [color=#000000]ip[/color][color=#000000][[/color][color=#000000]3[/color][color=#000000]][/color] [color=#434f54]=[/color] [color=#000000]100[/color][color=#000000];[/color]                    [color=#434f54]// Change the last byte of the address[/color]
  [color=#00979c]uint8_t[/color] [color=#434f54]*[/color][color=#000000]asBytes[/color] [color=#434f54]=[/color] [color=#434f54]&[/color][color=#000000]ip[/color][color=#000000][[/color][color=#000000]0[/color][color=#000000]][/color][color=#000000];[/color]      [color=#434f54]// Get a pointer to the internal array of bytes[/color]
  [b][color=#d35400]IPAddress[/color][/b] [color=#000000]fromBytes[/color] [color=#434f54]=[/color] [color=#000000]asBytes[/color][color=#000000];[/color]  [color=#434f54]// Create a new IP address from this array of bytes  [/color]
  [color=#00979c]uint8_t[/color] [color=#000000]bytesArray[/color][color=#000000][[/color][color=#000000]][/color] [color=#434f54]=[/color] [color=#000000]{[/color][color=#000000]10[/color][color=#434f54],[/color][color=#000000]42[/color][color=#434f54],[/color][color=#000000]0[/color][color=#434f54],[/color][color=#000000]127[/color][color=#000000]}[/color][color=#000000];[/color]
  [b][color=#d35400]IPAddress[/color][/b] [color=#000000]anotherOneFromBytes[/color] [color=#434f54]=[/color] [color=#000000]bytesArray[/color][color=#000000];[/color]