9 years, 7 months ago.

eth.init() issue

Hi all, I want to change the ip of the microcontroller statically, I know we can change it via eth.init(ip,subnet,gw) command, but I need to change it over and over again. let say after every 5 mins. When in a while loop I change ip for the second time eth.init() gives me an exception. Can any one tell me how I can change the ip for more than one time ??

while (1){ printf("enter ip \n");

gets(buffer); setup ethernet interface

eth.init(buffer,"255.255.255.0",defaultgw);

eth.connect(); printf("IP Address is %s\n\r", eth.getIPAddress());

} thanks in advance

1 Answer

9 years, 7 months ago.

A bit of a work around but if you can't call init twice you could try destroying and creating the interface again each time. e.g.

ethernet *eth = NULL; // create a blank pointer to an ethernet interface

char buffer[20];
char defaultgw[20];

main () {

  while (1) {
    if (eth)  // if an interface exists
      delete eth;  // delete it.

    eth = new ethernet();  // create a new interface
    if (!eth) {
      printf("Failed to create ethernet object. Aborting\n\r");
      break;
    }

    gets(buffer); 
    gets(defaultgw); 
    
   // should probably add some sanity checking here to make sure we got valid IP addresses.

    eth->init(buffer,"255.255.255.0",defaultgw); // eth is now a pointer so use -> instead of .

    eth->connect();
    printf("IP Address is %s\n\r", eth->getIPAddress());    
  }

  // fairly redundant here since 1) the program only gets here if the eth object wasn't created and 2) we are about to exit
  // but because it's good practice delete any dynamic objects that are no longer needed.
  if (eth)
    delete eth;
}

Oh and in the future please use <<code>> and <</code>> on their own lines before and after any c code in order to make it readable.

thank you @andy for posting the solution, I tried it, but unfortunately I am still getting the same issue. It gives error when I initialize it for the second time, though I delete eth as well, but it gave me an error :(

posted by zain aftab 12 Sep 2014