Pages: [1]   Go Down
Author Topic: Ethernet - Modify Client to allow IP address to be changed  (Read 961 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 13
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have a box that connects to a server but I need to be able to set the server and client addresses during setup.
The current library seems to require predefined addresses.  I found a thread from last year on modifying the library:
http://arduino.cc/forum/index.php/topic,46691.msg337318.html#msg337318

From that thread:
I have therefore added a method to the Client class, as follows:
uint8_t Client::address(uint8_t *ip) {
  if (_sock != 255)
    return 0;   
  _ip = ip;
  return 1;
}
This allows the IP at which the Client is targeted to be changed, as long as it isn't currently connected.

I'm good with C but not so much with C++.  I tried modifying client.h and client.cpp
I'm thinking the code snippet shown gets added to client.cpp in the ethernet library directory and  uint8_t address(); gets added to the public section of client.h

Then, any time I want to change the address of the client (while not connected) I can say:
client.address (SetupInfo.CameraIP);
Where uint8_t SetupInfo.CameraIP[4] is defined dynamically in my code.
The call looks like:
client.address(SetupInfo.CameraIP);

Unfortunately I get these errors when compiling:
ButtonBox_0_4.cpp: In function 'void setup()':
ButtonBox_0_4:384: error: no matching function for call to 'Client::address(uint8_t [4])'
C:\Program Files\arduino-0022\libraries\Ethernet/Client.h:27: note: candidates are: uint8_t Client::address()

Any thoughts?  Different approach?
Thanks,
Steve
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 549
Posts: 46147
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I'm good with C but not so much with C++.
That the function you are trying to add is a method in a class is pretty much irrelevant.

Quote
I'm thinking the code snippet shown gets added to client.cpp in the ethernet library directory
Yes, it does.

Quote
and  uint8_t address(); gets added to the public section of client.h
Close. The where is right. The function declaration is wrong. It does not match the function implementation in the code snippet.

Quote
Unfortunately I get these errors when compiling:
ButtonBox_0_4.cpp: In function 'void setup()':
ButtonBox_0_4:384: error: no matching function for call to 'Client::address(uint8_t [4])'
That's because you didn't correctly define such a function. You defined Client::address(), which isn't the same signature.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 13
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

OK,
So in the .h file I used   uint8_t address(uint8_t *);
Now I can compile.  I haven't started the testing (other issues to get past).
I'm surprised this isn't  common thing.  Dynamically setting IP addresses seems like it should be pretty common as you configure embedded hardware for a network.
Logged

Georgina Ontario
Offline Offline
Sr. Member
****
Karma: 5
Posts: 437
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I don't understand why not use...

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

From the Ethernet library.

I am using the version 22 Arduino software posted here and the "official" Ethernet SD shield.

There is no built-in DHCP protocol if that is what you mean.
Logged

Just another Hacker

0
Offline Offline
Tesla Member
***
Karma: 116
Posts: 8944
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The below link might be of interest.

http://gkaindl.com/software/arduino-ethernet
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

0
Offline Offline
Newbie
*
Karma: 0
Posts: 13
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@WillR:
That works to change the server IP address.  I wanted to be able to program the client IP address also.  The boxes I'm designing have to be reconfigurable in the field.  You can't create an instance of Client within the setup code.

@zoomkat:
Thanks.  That looks interesting.  I didn't find it with a day of googling.

As a final note on this, it works.  I can now have a setup routine that changes all the ethernet parameters in my setup structure, save it to EEPROM and always load those values on power up.

I still have to figure out how to read the hex characters for the MAC address from the console and convert them, but that is fairly straightforward.
Logged

'round the world...
Offline Offline
Faraday Member
**
Karma: 41
Posts: 3112
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

This may sound stupid... but a serial EEPROM probably wouldn't add a lot of space on your board and it would be configurable... sort of.

The other way I see is, if you connect one of the pins on the Arduino at the reset pin and create a function that receives the new IP and MAC address via TCP from a known port with a command sent from a telnet client, writes that data in the built in EEPROM on the Arduino and then resets the chip.

At startup, the Arduino would look at the EEPROM to get the IP and MAC address. The way I see it, you'd probably want to reset the Arduino after an address change. If not, then you need to close all the current sockets you have open, change addresses, and restart your software.
Logged

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).

0
Offline Offline
Newbie
*
Karma: 0
Posts: 13
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hmmm, not working.
I've made the changes to the client.h
 uint8_t address(uint8_t *);

and client.cpp
uint8_t Client::address(uint8_t *ip) {
  if (_sock != 255)
    return 0;
   
  _ip = ip;
 
  return 1;
}

Prior to setup() I create an instance of Client using a dummy IP address as a placeholder:
byte dummyip[] = {
  100, 100, 69,109 };
Client client(dummyip, 7115);

Within setup I try to change the address of the server I'm connecting to from a variable:
   SetupInfo.CameraIP[0] = 100;
   SetupInfo.CameraIP[1] = 100;
   SetupInfo.CameraIP[2] = 69;
   SetupInfo.CameraIP[3] = 108;
   client.address(SetupInfo.CameraIP);

Then I start up the client:
   Ethernet.begin(FixedMac, SetupInfo.BoxIP, SetupInfo.Gateway, SetupInfo.Subnet);
   // give the Ethernet shield a second to initialize:
   delay(1000);
and then test for a connection:
   if (client.connect()) {
      Serial.println("camera connected");
   }

It compiles OK but doesn't connect to a known good server (I can put the address in dummyIP and it will work right.
Again, the point is to have my Arduino be a client connecting to a server that I can select within my software, even if I have to reboot and read it from the internal EEPROM.
Thanks.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 13
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

OK, working.  I can modify the server address that I talk to.  It was simpler than I thought. 
In header the server IP address is a variable that is initialized:
byte dummyip[] = {
  100, 100, 69,109 };
// start with a dummy IP address
byte FixedMac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x19, 0x32 };
Client client(dummyip, 7115);

in setup, you can read eeprom or anything else and change the values of dummyip:
   dummyip[0] = SetupInfo.CameraIP[0];
   dummyip[1] = SetupInfo.CameraIP[1];
   dummyip[2] = SetupInfo.CameraIP[2];
   dummyip[3] = SetupInfo.CameraIP[3];

   Ethernet.begin(FixedMac, SetupInfo.BoxIP, SetupInfo.Gateway, SetupInfo.Subnet);

   delay(1000);

The next time it starts, it starts with a new server address.  Next step is to stop and restart within the code.
Logged

Pages: [1]   Go Up
Jump to: