patch panel POE with ping watchdog

Hello, I’m trying to do a patch panel POE with watchdog. Each pin is connected to a relay.
The part of patchpanel can cut power to each port is working perfectly. (this is done via a php page via socket)

At the end of the code tried to make monitoring with ping, so that if he loses all packets sent, it will turn the unit off, wait 5 seconds, turn it on again and wait 60 seconds before checking again to make time to connect the equipment.
The code works, but when the equipment does not respond to pings and enters this last “if”, all the rest does not work, why not run simultaneously. My page is not connected with arduino while the equipment does not come back to respond positively to Pings.

Would have any way to do what I want to work?

Code:

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

//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,26,226}; // define enderecço a ser monitorado
byte pingAddr2 = {187,19,24,1};
SOCKET pingSocket = 3;
char buffer [256];
char buffer2 [256];
// 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#”;

void setup() {
Ethernet.begin(mac, ip, gateway, subnet);
server.begin();
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);
}

void loop() {
//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’)
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;
}
}
}

//WatchDog Porta 1
ICMPPing ping(pingSocket);
ping(10, pingAddr, buffer);
if( strcmp (buffer, “Request Timed Out”) == 0) {
digitalWrite(2, LOW);
delay(5000);
digitalWrite(2, HIGH);
delay(60000);
}
}

Thanks!!
ps: sorry my english :confused:

The part that I need to running in the background or in parallel is this:

//WatchDog Porta 1 ICMPPing ping(pingSocket); ping(10, pingAddr, buffer); if( strcmp (buffer, "Request Timed Out") == 0) { digitalWrite(2, LOW); delay(5000); digitalWrite(2, HIGH); delay(60000); }

When it falls in the latter "if", the rest all over it does not work until you let go of this "if"

          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];

Time to learn about for loops.

The part that I need to running in the background or in parallel is this:

There is no background on an single processor/single threaded microprocessor, and no parallel threads. So, forget that.

What you can do is forget about the use of delay(). Ever. Forget that it even exists, because you should not be stalling the processor for near that long. Loos at the blink without delay example. Understand how to make things happen using a watch and pad of paper. The millis() function corresponds to the watch. Variables correspond to the pad of paper.

Yes, you have to re-write your whole sketch. You went the wrong way with delay() the first time. Now, go the right way without it.

Thanks for responding.

I had a breakthrough by putting an "else" before ICMPPing.

else {ICMPPing ping(pingSocket); ping(10, pingAddr, buffer); if( strcmp (buffer, "Request Timed Out") == 0) { digitalWrite(2, LOW); delay(5000); digitalWrite(2, HIGH); delay(60000); } } }

So I already can not stop this "if" when I request a space in the "if (client)" it from above. Now the only thing that catches me is the "delay"

I was taking a look at this millis (). But I can exchange these "delays" with "millis" and make the line "If (strcmp (buffer," Request Timed Out ")" run only when this "millis" finish?

Thanks!

I was taking a look at this millis (). But I can exchange these "delays" with "millis" and make the line "If (strcmp (buffer," Request Timed Out ")" run only when this "millis" finish?

You can, but it sounds like you are planning to implement a delay() of your own design using millis(). Forget that. Just use delay(). But, don't expect to be able to do anything while the delay() is happening.

The millis() function is like your watch. If you are cooking dinner, and you are having pasta, you don't set a timer when you put the pasta in, and then stand there staring at the pasta while the sauce pops all over the place and burns, do you?

Of course not. You note the time that the pasta went it, and periodically check how long it has been in, while still stirring the sauce and watching the bread under the broiler.

Set flags and times. Note whether the flags are set, and, if do, whether enough times has elapsed to necessitate doing something else.

Would simply put a kind of interruption when "if (client) {.." receive anything, interrupted the "if (strcmp (buffer," Request Timed Out ") == 0) {..." ?

Or will access the Arduino via the Web is something that has to be done without other functions.

Please do this if I could access it while he executed this task to restart a door, it would be all I need in my business.

thank you