Go Down

Topic: Get IP Address of Anyone Connected to my Arduino set up as server (Read 4848 times) previous topic - next topic

bubulindo


I don't quite know what that does, I didn't write it. It was given a few posts back and at one time, I had it working, must have forgot to save my work after the last burn, and lost it. If there is a simpler idea, I'm open to it.


And what were you planning to achieve when that code was given to you? If you don't know what it does... and it is given you trouble. Delete it, and run the program. See if something's missing... if not, you just fixed it. ;)
Eu não sou o teu criado. Se respondo no fórum é para ajudar todos mediante a minha disponibilidade e disposição. Responder por mensagem pessoal iria contra o propósito do fórum e por isso evito-o.
Se realmente pretendes que eu te ajude por mensagem pessoal, então podemos chegar a um acordo e contrato onde me pagas pela ajuda que eu fornecer e poderás então definir os termos de confidencialidade do meu serviço. De forma contrária toda e qualquer ajuda que eu der tem de ser visível a todos os participantes do fórum (será boa ideia, veres o significado da palavra fórum).
Nota também que eu não me responsabilizo por parvoíces escritas neste espaço pelo que se vais seguir algo dito por mim, entende que o farás por tua conta e risco.

Dito isto, mensagens pessoais só se forem pessoais, ou seja, se já interagimos de alguma forma no passado ou se me pretendes convidar para uma churrascada com cerveja (paga por ti, obviamente).

Jeremy-arduino

Well, when robtillaart gave that code bit, I wanted the clients IP written to a string that could be printed to the LCD. I'm not sure if the question was supposed to be sarcastic or not, but when I answered the question with "I don't quit know...", of course I know what I want it to do, BUT, I don't necessarily know what each line is actually doing.

So, I changed this...
Code: [Select]

           for (int i=0; i<3; i++)
            {
              client_ip[i] = () temp & 0xFF; // I added the index thing here...
              temp /= 256;
            }



So, I'm not sure what this is going to do when I upload it to my board, so is there a way to download the working previous version from my board, even if it's not pretty and upload it later if this breaks my program?

draythomp

I'm running IDE 21 and I have the ip of the incoming printing just fine using code I took from this thread.

to Client.cpp I added:

void Client::getRemoteIP_address(uint8_t * addr){ // added by me
   W5100.readSnDIPR(_sock, addr);    // replaces the getSn_DIPR(_sock, addr); V18 and below
}

and to Client.h:

  void getRemoteIP_address(uint8_t * addr);//added by me

Then in my code to get the address:

  uint8_t clientIp[4];

  incoming.getRemoteIP_address(&clientIp[0]);

to format the ip address so you can display it you can call the routine below:

// format an IP address. (I stole this)
const char* ip_to_str(const uint8_t* ipAddr)
{
  strcpy_P(Dbuf2,PSTR("%d.%d.%d.%d\0"));
  sprintf(Dbuf, Dbuf2, ipAddr[0], ipAddr[1], ipAddr[2], ipAddr[3]);  //see how its a 4 byte array?
  return (Dbuf);
}

Dbuf and Dbuf2 are string arrays declared at the beginning of the program and used as scratch buffers throughout.  Call the routine like this to see what you get:

Serial.println(clientIp);  // where clientIp is the result of the incoming.getRemoteIP_address above

I do not recommend converting the ip address to a long or anything else except a string for display.  Use it as a 4 byte array since that is the way it is handed to the various ethernet routines.  The format routine above will create a nnn.nnn.nnn.nnn type of string so you'll see 192.168.0.25 or some such for addresses inside your router.  You can even compare it in the array form.  For example:

#define maskcmp(addr1, addr2, mask) \
(((((uint16_t *)addr1)[0] & ((uint16_t *)mask)[0]) == \
             (((uint16_t *)addr2)[0] & ((uint16_t *)mask)[0])) && \
            ((((uint16_t *)addr1)[1] & ((uint16_t *)mask)[1]) == \
             (((uint16_t *)addr2)[1] & ((uint16_t *)mask)[1])))

This mess of a macro (just copy and paste it, do not try to type that crap in) will compare the ip address (as an array) using the netmask to tell you if the machine that is coming in is inside your router or outside.  If you call it like this:

maskcmp(incomingIp,thisBoardsIp,subnet_mask)

will return true if it is a local address.  Nice for protecting something from the outside.

DON'T GIVE UP !  You can do this and it will make a cool display that you can brag about for months.

Trying to keep my house under control http://www.desert-home.com/

andmiguel

i tried this code but the only ip it show's is allways : 254.237.192.1

where is the problem in the code?


byte client_ip[4];

..


    client.IP_address(&client_ip[0]);
    int c1=client_ip[1];
    int c2=client_ip[2];   
    int c3=client_ip[3];   
    int c4=client_ip[4];
    Serial.println("");
    Serial.print(c1);
    Serial.print(".");
    Serial.print(c2);
    Serial.print(".");
    Serial.print(c3);
    Serial.print(".");   
    Serial.print(c4);

SurferTim

#34
Nov 27, 2011, 11:58 am Last Edit: Nov 27, 2011, 12:06 pm by SurferTim Reason: 1
Post the code from client.h and client.cpp for this function.
Code: [Select]
client.IP_address(&client_ip[0]);

And this will only be valid while a client is connected.

Code: [Select]
Client client = server.available();

if(client)
{
   client.IP_address(client_ip);

   // rest of your code
}

Philgaskin

Has anyone managed to get this working with v1.0 of the IDE?
I tried changing the Client references to EthernetClient but cannot manage to get it to compile without errors.
I had hoped that remote IP would have been part of the library by now.
Any help would be appreciated.
Phil.

Go Up