to set server ip due to externally modification of server address [Arduino Mega]

Hello gli arduini,

I was following the forum for a long while as a guest, but today I signed up because of a couple of big question marks on my mind. I've had a quick search in this forum, however I could not find the exact solution. Anyway, please correct me if I am wrong. Firstly I created MS SQL database and connect it to my Arduino via ethernet shield, by the way Arduino listens port 5555 with TCP/IP over a local area network. The problem is, what does happen if connection loss due to cut off in the electricity? I mean Arduino takes its IP automatically (e.g. 192.168.0.26), so when the electricity has come again IP address may change(Server's as well) to for example 192.168.0.29 . That leads to makes connection impossible by changing it manually. Without collision, how can I connect them together? ( I assume that server ip and arduino's client ip have changed, when electricity has been cut off. Also, at the beginning both ip addresses written in the code. To sum up, I want to change them both, by receiving the data from buffer and parsing to the related address.) Last question, is it possible to assign server name to server ip in order to make connection through local area? Because I thought by writing server's name and connecting, it does not affect connection whether server ip changes or not.)

P.S. : I have a GLCD and keyboard with particular menu. I also would like to change by using this hardwares.

Thanks for your further helps and concerns

If the server is set up correctly, its IP should not change. If it does, it is a bit harder to change in the client than if it is a static IP assignment.

The client should renew its IP on a schedule determined by the DHCP server. If you are using an ethernet shield, this would be done with Ethernet.maintain().

Thank you SurferTim,

Unfortunately, it seems dynamical IP assignment and I will try to find a solution to make it static, but considering the worst case do you have any suggestions for it?

SurferTim:
The client should renew its IP on a schedule determined by the DHCP server. If you are using an ethernet shield, this would be done with Ethernet.maintain().
http://www.arduino.cc/en/Reference/EthernetMaintain

Link seems quite useful, but is there any example source code of the command?

I don't know how network savvy you are, but there are two ways you can set up that server if it is in your control.

  1. Static IP. That is assigned to the shield during the setup function of the Arduino.
  2. Dynamic IP assigned as a static DHCP assignment. Check your router's manual for how to do that.

The client can call Ethernet.maintain as often as it wants. It does nothing if it is not time to renew the DHCP lease. Here is a modified version of what I use. It prints nothing if the maintain function returns 0 (not time to renew yet). You can change it to print something if it returns 0, but it is a pain in the backside because it prints every iteration of the loop.

void loop() {
  switch(Ethernet.maintain()) {
    case 1:   Serial.println(F("\n\rDHCP: Renew failed")); break;
    case 2:   Serial.println(F("\n\rDHCP: Renew success")); break;
    case 3:   Serial.println(F("\n\rDHCP: Rebind fail")); break;
    case 4:   Serial.println(F("\n\rDHCP: Rebind success")); break;
  }

// rest of your loop code.

How can I see the renewed IP by writing below routine?

for (byte thisByte = 0; thisByte < 4; thisByte++) {
    // print the value of each byte of the IP address:
      GLCD.print(Ethernet.localIP()[thisByte], DEC);
      GLCD.print(".");

For this code, is it possible to call Ethernet.maintain() in some other function when it is needed? And I would like to assign renewed IP to the IPAddress ip(), does this idea logical or should I just leave it as I defined at the beginning?

#include <SPI.h>
#include <Ethernet.h>
EthernetClient client;
IPAddress      ip(192, 168, 1, 26); //Pre-defined ip address of shield, can be changed from admin menu later.
IPAddress   myDns(192, 168, 1, 1);
IPAddress gateway(192, 168, 1, 1);
IPAddress  subnet(255, 255, 0, 0);
byte    mac[] = {0x90, 0xA2, 0xDA, 0x0F, 0x5A, 0x21};
byte server[] = {192, 168, 1, 27}; // server ip

void LeaseIP() {
  switch(Ethernet.maintain()) {
    case 1:   Serial.println(F("\n\rDHCP: Renew failed")); break;
    case 2:   Serial.println(F("\n\rDHCP: Renew success")); break;
    case 3:   Serial.println(F("\n\rDHCP: Rebind fail")); break;
    case 4:   Serial.println(F("\n\rDHCP: Rebind success")); break;
  }

// rest of your loop code.

If you use a static IP, you don't need to call the maintain function. It does nothing.

If you want to display the new IP, that code looks correct. Put it in a function and call it if the maintain function returns a non zero number.

Thanks a lot, it works!