Change the IP of the Arduino via WEB. Is it possible?

Hello,
I wonder if you can change the IP of the Arduino through PHP via socket.
Or even directly into it via html.

Thanks

I wonder if you can change the IP of the Arduino through PHP via socket.
Or even directly into it via html.

If you write the right code to parse the client's GET command, and that get command contains a new IP address, and you call Ethernet.begin() again, yes, you can.

Why would you want to, though? If the Arduino is a server, it's IP address should remain fixed. If it is a client, it's IP address really isn't important.

Actually I want to change the field

byte pingAddr[] = {192,168,0,1};

Can you give me an example of how it would look?

And how do I write this field received in the bootloader

thanks

Can you give me an example of how it would look?

No. That would all depend on whether the Arduino is a client or a server. You didn't bother answering that.

And how do I write this field received in the bootloader

You don't.

I'll ask again. What are you trying to do?

I put an arduino with 10 relays.
These relays are connected in a patchpanel poe ports.
IN each port patch panel is connected to a router that is fed by these gates poe.

I put a rule ping to verify that the equipment is connected, if not is it shuts down and turns.

Turning on and off each port via the web is up and running perfectly. Now I want to edit these ip address that will receive the pings.

What code is running on the Arduino? Is the Arduino a client or a server?

The Arduino receives via client.read ();
and sends to the php via client.write ();

The Arduino receives via client.read ();
and sends to the php via client.write ();

regardless of whether it is a client or a server. Is there something about the question that is too hard for you?

Yes, I would like an example of how "pingAddr byte []" can receive the "client.read ()" if it receives a specific character within a "case"

All together now: "Post your code!".

#include <SPI.h>
#include <Ethernet.h>
#include <ICMPPing.h>
#include <Timer.h>

Timer t; 
//Configurações do Ethernet Shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 187,19,27, 1 }; // ip que o arduino assumirá
byte gateway[] = { 187,19,24, 1 };  // ip do roteador
byte subnet[] = { 255, 255,252, 0 };
byte pingAddr[] = {187,19,16,1}; // define enderecço a ser monitorado 
byte pingAddr2[] = {187,19,16,210}; 
byte pingAddr3[] = {187,19,16,235}; 
byte pingAddr4[] = {187,19,26,226}; 
byte pingAddr5[] = {187,19,26,221};
byte pingAddr6[] = {187,19,26,222};
byte pingAddr7[] = {187,19,26,223};
byte pingAddr8[] = {187,19,26,224}; 
byte pingAddr9[] = {187,19,26,220}; 
byte pingAddr10[] = {187,19,26,219}; 
SOCKET pingSocket = 3;
char buffer [256];
char buffer2 [256];
char buffer3 [256];
char buffer4 [256];
char buffer5 [256];
char buffer6 [256];
char buffer7 [256];
char buffer8 [256];
char buffer9 [256];
char buffer10 [256];
String x = "1#";
// String que representa o estado dos dispositivos
char Luz[13] = "0000000011L#";

EthernetServer server(8081); // Cria o servidor na porta 8081
 
// String onde é guardada as msgs recebidas
char msg[13] = "0000000011L#"; 
int p1 = 0;
int p2 = 0;
int p3 = 0;
int p4 = 0;
int p5 = 0;
int p6 = 0;
int p7 = 0;
int p8 = 0;
int p9 = 0;
int p10 = 0;
  ICMPPing ping(pingSocket);
void setup() {
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();
  Serial.begin(9600);
  pinMode(A0,OUTPUT);
  pinMode(A1,OUTPUT);
  pinMode(2,OUTPUT);
  pinMode(3,OUTPUT);
  pinMode(A2,OUTPUT);
  pinMode(5,OUTPUT);
  pinMode(6,OUTPUT);
  pinMode(7,OUTPUT);
  pinMode(8,OUTPUT);
  pinMode(9,OUTPUT);
  //digitalWrite(A0,HIGH);
  //digitalWrite(A1,HIGH);
  digitalWrite(2,HIGH);
  digitalWrite(3,HIGH);
  digitalWrite(A2,HIGH);
  digitalWrite(5,HIGH);
  digitalWrite(6,HIGH);
  digitalWrite(7,HIGH);
  digitalWrite(8,HIGH);
  digitalWrite(9,HIGH);
  //digitalWrite(10,HIGH);
  t.every(10, pagina);
  t.every(60000, pingp1);
  t.every(60001, pingp2);
  t.every(60002, pingp3);
  t.every(60003, pingp4);
  //t.every(30004, pingp5);
  //t.every(30005, pingp6);
  //t.every(30006, pingp7);
  //t.every(30007, pingp8);
  //t.every(30008, pingp9);
  //t.every(30009, pingp10);
}
void loop()  {
  t.update();
}
void pingp1() {
   ping(10, pingAddr, buffer); 
     if( strcmp (buffer, "Request Timed Out") == 0) { 
       digitalWrite(2, LOW);
       p1 = p1++;
       Serial.println(p1);
        delay (5000); 
     digitalWrite(2, HIGH);
    }
}
void pingp2() {
    ping(10, pingAddr2, buffer2); 
     if( strcmp (buffer2, "Request Timed Out") == 0) { 
       digitalWrite(3, LOW);
       p2 = p2++;
       Serial.println(p2);
        delay (5000); 
     digitalWrite(3, HIGH);
    }
}
void pingp3() {
    ping(10, pingAddr3, buffer3); 
    Serial.println(buffer3);
     if( strcmp (buffer3, "Request Timed Out") == 0) { 
       digitalWrite(A2, LOW);
       p3 = p3++;
       Serial.println(p3);
        delay (5000); 
     digitalWrite(A2, HIGH);
    }
}
void pingp4() {
    ping(10, pingAddr4, buffer4); 
     if( strcmp (buffer4, "Request Timed Out") == 0) { 
       digitalWrite(5, LOW);
       p4 = p4++;
       Serial.println(p4);
        delay (5000); 
     digitalWrite(5, HIGH);
    }
}
void pingp5() {
    ping(10, pingAddr5, buffer5); 
     if( strcmp (buffer5, "Request Timed Out") == 0) { 
       digitalWrite(6, LOW);
       p5 = p5++;
       Serial.println(p5);
        delay (5000); 
     digitalWrite(6, HIGH);
    }
}
void pingp6() {
    ping(10, pingAddr6, buffer6); 
     if( strcmp (buffer6, "Request Timed Out") == 0) { 
       digitalWrite(7, LOW);
       p6 = p6++;
       Serial.println(p6);
        delay (5000); 
     digitalWrite(7, HIGH);
    }
}
void pingp7() {
    ping(10, pingAddr7, buffer7); 
     if( strcmp (buffer7, "Request Timed Out") == 0) { 
       digitalWrite(8, LOW);
       p7 = p7++;
       Serial.println(p7);
        delay (5000); 
     digitalWrite(8, HIGH);
    }
}
void pingp8() {
    ping(10, pingAddr8, buffer8); 
     if( strcmp (buffer8, "Request Timed Out") == 0) { 
       digitalWrite(9, LOW);
       p8 = p8++;
       Serial.println(p8);
        delay (5000); 
     digitalWrite(9, HIGH);
    }
}
void pingp9() {
    ping(10, pingAddr9, buffer9); 
     if( strcmp (buffer9, "Request Timed Out") == 0) { 
       digitalWrite(A0, HIGH);
       p9 = p9++;
       Serial.println(p9);
        delay (5000); 
     digitalWrite(A0, LOW);
    }
}
void pingp10() {
    ping(10, pingAddr10, buffer10); 
     if( strcmp (buffer10, "Request Timed Out") == 0) { 
       digitalWrite(A1, HIGH);
       p10 = p10++;
       Serial.println(p10);
        delay (5000); 
     digitalWrite(A1, LOW);
    }
}
void pagina() {
  //Client client = server.available();
  EthernetClient client = server.available();

  // SE receber um caracter...
  if (client) {
    // guarda o caracter na string 'msg'
    msg[1]=msg[2]; msg[2]=msg[3]; msg[3]=msg[4]; msg[4]=msg[5]; msg[5]=msg[6]; msg[6]=msg[7]; msg[7]=msg[8]; msg[8]=msg[9]; msg[9]=msg[10]; msg[10]=msg[11]; msg[11]=msg[12];
    msg[12] = client.read();

    if (msg[12]=='#') {
      switch(msg[11]) {
        case 'R':
          // Se receber o comando 'R#' envia de volta o status dos
          //   dispositivos. (Que é a string 'Luz')
            //Serial.println(msg);
          client.write(Luz);
        break;
        case 'L':
          // Caso L#, ele copia os 10 bytes anteriores p/ a
          //   string 'Luz' e cada byte representa um
          // dispositivo, onde '1'=ON e '0'=OFF
          Luz[0]=msg[1];
          Luz[1]=msg[2];
          Luz[2]=msg[3];
          Luz[3]=msg[4];
          Luz[4]=msg[5];
          Luz[5]=msg[6];
          Luz[6]=msg[7];
          Luz[7]=msg[8];
          Luz[8]=msg[9];
          Luz[9]=msg[10];
          if (Luz[0]=='1') digitalWrite(2,LOW); else digitalWrite(2,HIGH);
          if (Luz[1]=='1') digitalWrite(3,LOW); else digitalWrite(3,HIGH);
          if (Luz[2]=='1') digitalWrite(A2,LOW); else digitalWrite(A2,HIGH);
          if (Luz[3]=='1') digitalWrite(5,LOW); else digitalWrite(5,HIGH);
          if (Luz[4]=='1') digitalWrite(6,LOW); else digitalWrite(6,HIGH);
          if (Luz[5]=='1') digitalWrite(7,LOW); else digitalWrite(7,HIGH);
          if (Luz[6]=='1') digitalWrite(8,LOW); else digitalWrite(8,HIGH);
          if (Luz[7]=='1') digitalWrite(9,LOW); else digitalWrite(9,HIGH);
          if (Luz[8]=='0') digitalWrite(A0,HIGH); else digitalWrite(A0,LOW);
          if (Luz[9]=='0') digitalWrite(A1,HIGH); else digitalWrite(A1,LOW);
        break;
        case '1':
        if (msg[12]=='#') {

          x = "1#";
          String y = x + p1;
          char z[7];
          y.toCharArray(z, 7);
            Serial.println(y);
          client.print(z);
        }
        break;
        case '2':
        if (msg[12]=='#') {
          
          x = "2#";
          String y = x + p2;
          char z[7];
          y.toCharArray(z, 7);
            Serial.println(y);
          client.print(z);
        }
        break;
        case '3':
        if (msg[12]=='#') {
		
          x = "3#";
          String y = x + p3;
          char z[7];
          y.toCharArray(z, 7);
            Serial.println(y);
          client.print(z);
        }
        break;
        case '4':
        if (msg[12]=='#') {
		
          x = "4#";
          String y = x + p4;
          char z[7];
          y.toCharArray(z, 7);
          
            Serial.println(y);
          client.print(z);
        }
        break;
        case '5':
        if (msg[12]=='#') {

          x = "5#";
          String y = x + p5;
          char z[7];
          y.toCharArray(z, 7);
            Serial.println(y);
          client.print(z);
        }
        break;
        case '6':
        if (msg[12]=='#') {

          x = "6#";
          String y = x + p6;
          char z[7];
          y.toCharArray(z, 7);
          
            Serial.println(y);
          client.print(z);
        }
        break;
        case '7':
        if (msg[12]=='#') {
		
          x = "7#";
          String y = x + p7;
          char z[7];
          y.toCharArray(z, 7);
            Serial.println(y);
          client.print(z);
        }
        break;
        case '8':
        if (msg[12]=='#') {
		
          x = "8#";
          String y = x + p8;
          char z[7];
          y.toCharArray(z, 7);
            Serial.println(y);
          client.print(z);
        }
        break;
        case '9':
        if (msg[12]=='#') {
          x = "9#";
          String y = x + p9;
          char z[7];
          y.toCharArray(z, 7);
            Serial.println(y);
          client.print(z);
        }
        break;
        case 'A':
        if (msg[12]=='#') {
          x = "A#";
          String y = x + p10;
          char z[7];
          y.toCharArray(z, 7);
            Serial.println(y);
          client.print(z);
        }
        break;
      }
    }
  }
}

patchpanel_watchdog2(2).ino (8.77 KB)

PaulS:
All together now: "Post your code!".

...in code tags.

If it doesn't fit in one post, then attach it instead.

I want to create another "case" to "pingAddr byte []" receive "client.read ()"

char buffer [256];
char buffer2 [256];
char buffer3 [256];
char buffer4 [256];
char buffer5 [256];
char buffer6 [256];
char buffer7 [256];
char buffer8 [256];
char buffer9 [256];
char buffer10 [256];

Which Arduino do you have? This far exceeds the amount of SRAM you have on a 328-based Arduino.

You have 10 pingpXX functions, when one would suffice. Why? Can't you figure out how to pass arguments to functions?

String x = "1#";

What a meaningful name. I wonder why more people don't use obscure names like this to make the code impossible to follow.

Speaking of following the code, you need to use Tools + Auto Format, and make that code presentable.

    msg[1]=msg[2]; msg[2]=msg[3]; msg[3]=msg[4]; msg[4]=msg[5]; msg[5]=msg[6]; msg[6]=msg[7]; msg[7]=msg[8]; msg[8]=msg[9]; msg[9]=msg[10]; msg[10]=msg[11]; msg[11]=msg[12];

Something wrong with a for loop?

for(byte i=1; i<12; i++)
{
   msg[i] = msg[i+1];
}
          Luz[0]=msg[1];
          Luz[1]=msg[2];
          Luz[2]=msg[3];
          Luz[3]=msg[4];
          Luz[4]=msg[5];
          Luz[5]=msg[6];
          Luz[6]=msg[7];
          Luz[7]=msg[8];
          Luz[8]=msg[9];
          Luz[9]=msg[10];

Another opportunity for a for loop to shorten the code.

    if (msg[12]=='#') {
      switch(msg[11]) {
        case '1':
        if (msg[12]=='#') {

Why is it necessary to check the value in msg[12] in most of the cases? The case will never be a candidate for execution unless msg12 is '#'.

Finally, what GET request are you (planning on) sending to the Arduino that should change it's IP address.

Since the Arduino is a server, and is responding to GET requests, why do you want to change it's IP address? If you do that, you'll need to change the address you send the GET requests to. The reason for doing that escapes me.

Thanks for the tips above.

I put this "if (msg [12] == '#')" because without him I was not able to put the "String y =" other "case" below. Do not know why.

And I do not want to change the "byte ip []" but "pingAddr byte []" and then save the bootloader.

terolando:
I do not want to change the "byte ip []" but "pingAddr byte []" and then save the bootloader.

Um, do you want to have another go at that? As it stands, that makes no sense at all.

I put this "if (msg [12] == '#')" because without him I was not able to put the "String y =" other "case" below. Do not know why.

I do. You don't need the if statement. You DO need the curly braces.

Please answer all the questions asked. It is impossible to help you unless you do. Now, it is quite possible to send the Arduino a GET request that contains 5 values - which ping address to change, and the 4 octets of the address. It is quite possible to parse that request and get the 5 values. It is quite possible to change the values in the correct array. That has nothing to do with the bootloader.

It seems like you really need to store the IP addresses in EEPROM, and read them on start up. At appropriate times (in response to the appropriate GET request, the data in the EEPROM can be overwritten, as can the data in the array(s).

We need to know what you envision the GET request looking like, though.

Since the only thing being changed is the input to a function, there is not even a need to reset the Ethernet connection after the change.

PaulS:

It seems like you really need to store the IP addresses in EEPROM, and read them on start up. At appropriate times (in response to the appropriate GET request, the data in the EEPROM can be overwritten, as can the data in the array(s).

That's exactly what I need.

Something like:

case 'I':

byte pingAddr [] = receive what is sent by php (192,168,1,254), but removing the part that does not matter.

break;



Excuse my difficulty to answer, because I am using the google translator to write.

receive what is sent by php (192,168,1,254), but removing the part that does not matter.

Then, you need to show us the php script that is sending the data, so we can see what it is sending.

My php code attached

Would have to create a field where the values ??put the "pingAddr []"

casa3.php (8.49 KB)