-->
Page 1 of 1

how to extract de IP from function"Udp.remoteIP()"

PostPosted: Sat Aug 08, 2015 4:32 pm
by pacojarcia
Hello,
At firts, I explain my proyect:
I have a server and several clients with diferent IPs.
Each client has a temperatura sensor and send every second.
The server lisen and receive this information from all clients.
My problem is: I want to now witch send.
With de function "Udp.remoteIP()" I can do "Serial.print(Udp.remoteIP())" (for explample 192.168.4.10). But, how to extract the number 10?
I try:
IPAddress IP_Cliente_10 = (192, 168, 4, 10);
IP_Cliente = Udp.remoteIP();
if (IP_Cliente_10 == IP_Cliente) ........

But don't work.
Any idea?

Re: how to extract de IP from function"Udp.remoteIP()"

PostPosted: Sat Aug 08, 2015 5:14 pm
by kolban
Howdy Paco,
The UDP.remoteIP method returns an instance of an IPAddress class. This class has an operator override for array indexing.

For example ...

IPAddress myIP = udp.remoteIP();

then myIP[0] will be the first byte of the IP address and myIP[3] will be the last byte of the IP address. So if all you want is the last byte of the address, myIP[3] should do the trick.

Neil

Re: how to extract de IP from function"Udp.remoteIP()"

PostPosted: Sat Aug 08, 2015 7:58 pm
by martinayotte
There is an "equal" operator in IPAddress.
so, you can do

IPAddress IP_Cliente_10 = IPAddress(192, 168, 4, 10);

IP_Cliente = Udp.remoteIP();
if (IP_Cliente_10 == IP_Cliente) { ... }

Note the bold part missing in your code ...

Re: how to extract de IP from function"Udp.remoteIP()"

PostPosted: Mon Aug 10, 2015 3:55 pm
by pacojarcia
Thanks a lot! I've lost hours trying.