ethernet shield change ip address while running

Hello there,

I'm new to this forum, so i'm not quite sure this is the best category my topic fits into, but i'll try anyways.

Here's my scenario: start up the arduino with a pre-configured ip and mac address, start a server to which a client connects, and sends the new ip/mac address. Now, the arduino would change it's ip address, and start operating.

What i've done so far: i tied a digital pin to the reset pin on the ethernet shield so that i can reset the ethernet shield from sketch. I also receive the bytes, process them, reset the ethernet shield and do a second Ethernet.begin();

After sending the arduino the new configuration, i can see it respond then restart, but after the restart, the ethernet connection stops working absolutely. It doesn't respond to ping on the old address, but ping does not say it is unreachable, which is the case when trying the second address.

I don't know what I could do, and I've been googling and trying for a few days now, and it's getting pretty annoying.

Thanks for your help in advance!

How do you change the ip address over the ethernet shield and then make the Arduino implement it ? Surely that can only be done inside the sketch ?

This is the exact way i’m now trying – but it doesn’t work, as I said.
Setting the reset pin to low and high again performs a reset action. Or well, it should perform (i looks like a real reset, though, it just doesn’t reset the w5100 like it should)

#include <Ethernet2.h>

Server srv = Server(23); // using default telnet for receiving config
boolean etherset = false; // indicates whether a new config has been set

void setup() { 
      byte ip[] = { 10, 10, 1, 1 };
      byte mac[] = { 0xAB, 0xCD, 0xEF, 0x01, 0x10, 0x11 };

      Serial.begin(2400);
      pinMode(4, OUTPUT);
      
      // initial reset of the ethernet shield (it tends not to work without this)
      digitalWrite(4, LOW);
      delay(500);
      digitalWrite(4, HIGH);
      delay(500);

      Ethernet.begin(mac, ip);
      srv.begin(); // start the server on telnet port

      Serial.println("setup");
}

void loop() {
      // the led at 13 lights if i poll, so not polling means everything went ok
      if(!etherset) {
            Client client = srv.available();
            if(client)
                    setEthernet(client))
      }
}

boolean setEthernet(Client client) {
      byte mac[6];
      byte ip[4];

      for(byte i = 0; i < 6; i++) {
            mac[i] = client.read();
      }
      for(byte i = 0; i < 4; i++) {
            ip[i] = client.read();
      }

      client.write('o');
      client.write('k');
      client.write('\n');
      client.stop();
      srv.close(); // this is something i wrote. closes the server socket.
      
      Ethernet.begin(mac, ip); //once
      
        // here i reset the shield again
      digitalWrite(4, LOW);
      delay(500);
      digitalWrite(4, HIGH);
      delay(500);

      Ethernet.begin(mac, ip); // dunno why, it just felt ok to do it twice
      etherset = true; // turn off polling (and the led at 13)

      return true;
}

I guess your arduino's Resetpin is not connected to the ethernetshield's one. (By stacking them, without leaving the pin out.)

(as a comment: I'm not sure, but i think i read in atmels datasheet, that the resetpin of the arduino can have a higher voltage than 5V, not sure, how it's on the ethernetshield. But "sounds" like a potential of sinking more than 20-30mA through a digitalpin on arduino...)

One alternative might be writing the IP/MAC/whatever to the eeprom of the arduino. and set a flag (stored to eeprom) that this alternative IP should be loaded. (Or maybe a pushbutton at startup on your setup, that indicates NOT to use the stored IP, but the "first-startup-standard-ip") Using it this way let's you do a hardreset. Or a "pin-reset" of both arduino and ethernetshield, maybe secured with an optocoupler.

Thanks, i'll see into how to store stuff on the eeprom.

by the way, i did not want to reset the arduino board itself, and yeah, it was odd, that writing low to the reset pin only resets the ethernet shield, but it would have been enough, anyway.

What is the new IP and MAC addreses you are trying to use ?

This is the series of bytes i'm sending to the arduino; the first six bytes are the mac, and the remaining 4 are the ip address.

0xaa, 0xbb, 0x11, 0xcc, 0xdd, 0xe1, 10, 10, 2, 1
----------------mac---------------- -----ip-----

rsdy,

Depending on your network's netmask, 10.10.1.1 and 10.10.2.1 may be on different networks. If they are on different networks, they can't talk to each other over the same local area network.

If the netmask is 255.0.0.0, then that should work. However, if the netmask is 255.255.255.0, then that's a problem.

Regards,

-Mike

Exactly what i was thinking ;-)

If it isn’t netmask related; you may be reading outside the available data:

boolean setEthernet(Client client) {
      byte mac[6];
      byte ip[4];

      for(byte i = 0; i < 6; i++) {
            [glow]mac[i] = client.read();[/glow]
      }
      for(byte i = 0; i < 4; i++) {
            [glow]ip[i] = client.read();[/glow]
      }

The calls to client.read() may get -1 if the data isn’t there.

Maybe change that to this:

boolean setEthernet(Client client) {

       while(client.available()<10)
       {
              delay(1);
       }

      byte mac[6];
      byte ip[4];

      for(byte i = 0; i < 6; i++) {
            mac[i] = client.read();
      }
      for(byte i = 0; i < 4; i++) {
            ip[i] = client.read();
}

There may be a better way than just while-looping like this, but it is a quick test to see if that is the problem…

Netmask isn't the problem, because my computer has netmask 255.0.0.0, it it can talk with the arduino. The delay stuff, however makes sense, i'll try that later today, and report whether it works!

Thanks!