Set IPAddress using INT variables

Hello! :slight_smile:

Im having trouble using a Webform to set an IP address of a device my project is talking to via ethernet.

What I am doing is taking the 4 parts of the NEW IP address and saving them as INTs. The problem is when I try to use them to set the device IP address it only partially works. Here is basic example.

//The IP address of the Push box is one of the initial declarations I do with a hard coded IP address
IPAddress push( 192,168,1,120 );

void loop()
{
//Pretend these values came from a webform, I cant be bothered including all that code…
int pushIP1 = 1;
int pushIP2 = 2;
int pushIP3 = 3;
int pushIP4 = 4;

if(awaitingPushIP == true){
push = (pushIP1, pushIP2, pushIP3, pushIP4);
Serial.println(“Push IP Set”);
awaitingPushIP = false;
}
}

The outcome of the new push IP address becomes 4.0.0.0 !! not 1.2.3.4

It is only using the last variable (pushIP4) to set the new IP address, and its becoming the FIRST section of the new IP address. Then part 2 3 and 4 of the IP address are becoming “0”. The other variables, pushIP1, pushIP2, pushIP3 are being disregarded or something.

TO SUM THIS UP
How do I set a variable of type IPAddress using variables instead of hard coding IPAddress push(192,168,1,120) ??

Thanks for you help !

 push = (pushIP1, pushIP2, pushIP3, pushIP4);

You probably realise that this is the cause of your problem.

One solution

char buffer[16];
int pushIP1 = 1;
int pushIP2 = 2;
int pushIP3 = 3;
int pushIP4 = 4;

void setup() 
{
  Serial.begin(115200);
  sprintf(buffer,"%d.%d.%d.%d",pushIP1,pushIP2,pushIP3,pushIP4);
  Serial.println(buffer);
}

void loop() 
{
}

Thanks for you fast reply!

So iv made a little test section to try and get an IP address to print to the serial window using you buffer suggestion, which has worked.

if(awaitingPushIP == true){
sprintf(buffer,"%d.%d.%d.%d",pushIP1,pushIP2,pushIP3,pushIP4);
serial.println(buffer);
Serial.println(“Push IP Set”);
awaitingPushIP = false;
}

The result is the correct IP address I entered into the webform being shown in the serial window from the variable buffer

When I however try to set the push IP address variable using “buffer” I get the error “call of overloaded ‘IPAddress(char[16])’ is ambuguous”

This is the call im making
IPAddress push(buffer);

Iv also tried changing this to
push=(buffer);
IPAddress push = push(buffer);

All of them return errors :astonished:

How do I use “buffer” to set the IP address correctly?

Use the old method of ip assignment. I would use byte types for the pushIP variables though.

byte push[] = {pushIP1, pushIP2, pushIP3, pushIP4};

I'm puzzled by the operation of IPAddress. Most examples I see, including the Reference (https://www.arduino.cc/en/Reference/EthernetIPAddress) use constants and are placed outside the setup() function, as if they are fully bound at compile time. This discussion shows that the values can be variables and can be set at run time.

SO, does the compiler do constant propagation or does the IPAddress function need to be INSIDE the setup() function to insure things are reset not just after the sketch is downloaded, but every time the sketch restarts subsequently, including after a commercial power failure?

Most examples I see, including the Reference (https://www.arduino.cc/en/Reference/EthernetIPAddress) use constants and are placed outside the setup() function, as if they are fully bound at compile time.

If your router, like mine, is configured to assign the Arduino the same IP address every time it connects, then using constants and placing the instance initialization in global space makes sense.

SO, does the compiler do constant propagation

I have no idea what you mean by "constant propagation".

or does the IPAddress function need to be INSIDE the setup() function to insure things are reset not just after the sketch is downloaded, but every time the sketch restarts subsequently, including after a commercial power failure?

IPAddress is a class. You create instance of the class. There is no IPAddress function, so talking about where to call the function is pointless.