Arduino Ethernet freez always after about 1 hour

Hy Folks!

I have an arduino and my home alarm system.
It's a simple zone monitoring project from the net.

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

byte mac[] = {0x90,0xA2,0xDA,0x0D,0xDE,0xBF};
char server[] = "www.turak-kft.hu";
IPAddress ip(192,168,2,170);

int an;
boolean _intArray[6];
int _timeArray[6];
String radars;
boolean emailIt=true;
int anLow=200;
int resp;
int isOnlineTime=0;
boolean isConnected;

void setup() {
  SPI.begin();
  Ethernet.begin(mac, ip);
  Serial.begin(9600);
  for (an = 1; an < 6; an++) {
    _timeArray[an]=0;
    _intArray[an]=true;
  }
  pinMode(A0, INPUT);
  pinMode(A1, INPUT);
  pinMode(A2, INPUT);
  pinMode(A3, OUTPUT);
  pinMode(A4, OUTPUT);
  pinMode(A5, OUTPUT);

  pinMode(3, INPUT);
  pinMode(5, INPUT);
  pinMode(6, INPUT);
}

void loop() {
  if (analogRead(0)>anLow && emailIt==true){
    for (an = 1; an < 7; an++){
      switch (an){
        case 1:
          if (analogRead(an)>anLow){
            if (_intArray[an]==true){
              String myString = String(an);
              int z=alarm(1,myString);
            }
          }
        break;
        case 2:
          if (analogRead(an)>anLow){
            if (_intArray[an]==true){
              String myString = String(an);
              int z=alarm(1,myString);
            }
          }
        break;
        case 3:
        if (digitalRead(an)==HIGH){
          if (_intArray[an]==true){
              String myString = String(an);
              int z=alarm(1,myString);
            }
          }
        break;
        case 5:
        if (digitalRead(an)==HIGH){
          if (_intArray[an]==true){
              String myString = String(an);
              int z=alarm(1,myString);
            }
          }
        break;
        case 6:
        if (digitalRead(an)==HIGH){
          if (_intArray[an]==true){
              String myString = String(an);
              int z=alarm(1,myString);
            }
          }
        break;
      }
      if (10<_timeArray[an]){
        _intArray[an]=true;
        _timeArray[an]=0;
      }else{
        _intArray[an]=false;
        _timeArray[an]++;
      }
    }
  }
  
  radars="";
  for (an = 0; an < 7; an++){
    switch (an){
      case 0:
        if (analogRead(an)>anLow){
          radars+="1|";
        }else{
          radars+="0|";
        }
      break;
      case 1:
        if (analogRead(an)>anLow){
          radars+="1|";
        }else{
          radars+="0|";
        }
      break;
      case 2:
        if (analogRead(an)>anLow){
          radars+="1|";
        }else{
          radars+="0|";
        }
      break;
      case 3:
        if (digitalRead(an)==HIGH){
          radars+="1|";
        }else{
          radars+="0|";
        }
      break;
      case 5:
        if (digitalRead(an)==HIGH){
          radars+="1|";
        }else{
          radars+="0|";
        }
      break;
      case 6:
        if (digitalRead(an)==HIGH){
          radars+="1|";
        }else{
          radars+="0|";
        }
      break;
    }
  }

  int x=alarm(2,radars);
  
//BOF gets
  //BOF Sentinel
  int p=gets(1);
  if (p==1){
    analogWrite(A3, 255);
    int q=gets(7);
  }else if (p==3){
    analogWrite(A3, 0);
    int f=gets(6);
  }
  //EOF Sentinel
  
  //BOF switchReq
  int v=gets(2);
  if (v==1){
    analogWrite(A4, 255);
    delay(700);
    analogWrite(A4, 0);
    int b=gets(8);
  }else if (v==3){
    analogWrite(A5, 255);
    delay(700);
    analogWrite(A5, 0);
    int s=gets(8);
  }
  //EOF switchReq
  
  //BOF isOnline
  int u=gets(3);
  if (u==1){
    isOnlineTime=0;
    int t=gets(4);
  }else if (u==0){
    isOnlineTime+=1;
  }
  //EOF isOnline
  
  //BOF emailIt
  int y=gets(5);
  if (y==0){
    if (isOnlineTime>5){
      emailIt=true;
      int h=gets(9);
    }
  }else if (y==1){
    emailIt=true;
  }else if (y==3){
    emailIt=false;
  }
  //BOF emailIt
//EOF gets
}

int alarm(int typ1,String zone){
  EthernetClient client;
  String data;
  data="";
  switch (typ1){
    case 1:
      data+="zone=";
      data+=zone;
    break;
    case 2:
      data+="radar=";
      data+=zone;
    break;
  }
  
  if (client.connect(server,80)) {
    client.println("POST /riaszto/includes/ajaxes/alarm.php HTTP/1.1");
    client.print("Host: ");
    client.println(server);
    client.println("Content-Type: application/x-www-form-urlencoded");
    client.println("Connection: close");
    client.print("Content-Length: ");
    client.println(data.length());
    client.println();
    client.print(data);
  }
  delay(250);
  client.stop();
}

int gets(int typ2){
  EthernetClient client;
  String data;
  data="";
  switch (typ2){
    case 1:
      data+="securityAlarm=2";
    break;
    case 2:
      data+="switchReq=2";
    break;
    case 3:
      data+="isOnline=2";
    break;
    case 4:
      data+="isOnline=5";
    break;
    case 5:
      data+="emailIt=2";
    break;
    case 6:
      data+="securityAlarm=5";
    break;
    case 7:
      data+="securityAlarm=1";
    break;
    case 8:
      data+="switchReq=5";
    break;
    case 9:
      data+="emailIt=1";
    break;
  }

  if (client.connect(server,80)) {
    isConnected=true;
    client.print("GET /riaszto/includes/ajaxes/gets.php?");
    client.print(data);
    client.println(" HTTP/1.1");
    client.print("Host: ");
    client.println(server);
    client.println("Connection: close\r\n");
    boolean nextIsValue=false;
    while(client.connected()) {
      while(client.available()) {
        char ch=client.read();
        if (ch == '@'){
          nextIsValue=true;
        }else if (nextIsValue==true){
          resp=ch;
          nextIsValue=false;
        }
      }
    }
  }else{
    isConnected=true;
  }
  if (isConnected==true){
    if (resp == '0'){
      resp = 0;
    }else if (resp == '1'){
      resp = 1;
    }else if (resp == '2'){
      resp = 2;
    }else if (resp == '3'){
      resp = 3;
    }else if (resp == '4'){
      resp = 4;
    }else if (resp == '5'){
      resp = 5;
    }
  }else if (resp==0){
    resp=9;
  }
  client.stop();
  delay(250);
  return resp;
}

After 1 hour the arduino freezing complete down. It can't check the current state, and doesn't change the value on mySql!
Any idea?

I thought, I reset the device per our programmatically, but it is not the best solution.
I have free 5V relay on the top of my board.

Thank you, and best regards:
Bela

It may be locking up in the "while(client.connected())" loop. If the connection breaks or the server stalls, it will lock up there. Add a couple serial prints to check. If it fails and you have a "Begin response" message as the last serial output, then that is your problem.

    Serial.println("Begin response");

    while(client.connected()) {
      while(client.available()) {
        char ch=client.read();
        if (ch == '@'){
          nextIsValue=true;
        }else if (nextIsValue==true){
          resp=ch;
          nextIsValue=false;
        }
      }
    }

    Serial.println("End response");

If that is the problem, you can prevent that by using a timeout in that loop. Here is the code I use for a 10 second timeout. If you ever see "Timeout" on the serial monitor, it would have locked up there.

  // connectLoop controls the hardware fail timeout
  int connectLoop = 0;

  while(client.connected())
  {
    while(client.available())
    {
      inChar = client.read();
      Serial.write(inChar);
      // set connectLoop to zero if a packet arrives
      connectLoop = 0;
    }

    connectLoop++;

    // if more than 10000 milliseconds since the last packet
    if(connectLoop > 10000)
    {
      // then close the connection from this end.
      Serial.println();
      Serial.println(F("Timeout"));
      client.stop();
    }
    // this is a delay for the connectLoop timing
    delay(1);
  }

  Serial.println(F("\r\ndisconnecting."));
  // close client end
  client.stop();

I will try it. But I don't need serial communication. ???

myDuino:
I will try it. But I don't need serial communication. ???

You need it to troubleshoot this. Once you get your code working, remove the serial stuff.

Not enough a simple a few ms delay() into the loop sequence? Just an idea

myDuino:
Not enough a simple a few ms delay() into the loop sequence? Just an idea

You must be more specific than that. Where is your sketch freezing? Is the last thing you see on the serial monitor "Begin response"?

myDuino:
Not enough a simple a few ms delay() into the loop sequence? Just an idea

As Tim indicates, it's better to do some serious troubleshooting, to narrow down possibilities, than to just make a few shot-in-the-dark guesses about what the problem may be.

SurferTim:
It may be locking up in the "while(client.connected())" loop. If the connection breaks or the server stalls, it will lock up there. Add a couple serial prints to check. If it fails and you have a "Begin response" message as the last serial output, then that is your problem.

    Serial.println("Begin response");

while(client.connected()) {
      while(client.available()) {
        char ch=client.read();
        if (ch == '@'){
          nextIsValue=true;
        }else if (nextIsValue==true){
          resp=ch;
          nextIsValue=false;
        }
      }
    }

Serial.println("End response");



If that is the problem, you can prevent that by using a timeout in that loop. Here is the code I use for a 10 second timeout. If you ever see "Timeout" on the serial monitor, it would have locked up there.


// connectLoop controls the hardware fail timeout
  int connectLoop = 0;

while(client.connected())
  {
    while(client.available())
    {
      inChar = client.read();
      Serial.write(inChar);
      // set connectLoop to zero if a packet arrives
      connectLoop = 0;
    }

connectLoop++;

// if more than 10000 milliseconds since the last packet
    if(connectLoop > 10000)
    {
      // then close the connection from this end.
      Serial.println();
      Serial.println(F("Timeout"));
      client.stop();
    }
    // this is a delay for the connectLoop timing
    delay(1);
  }

Serial.println(F("\r\ndisconnecting."));
  // close client end
  client.stop();

Dear SurferTim,

Can you implement your suggested code into my complete project what can i upload to my arduino? I think I don't understand you exactly.
I would be very grateful for it...
Thank you,

Regards:
Bela

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

byte mac[] = {0x90,0xA2,0xDA,0x0D,0xDE,0xBF};
char server[] = "www.host.hu";
IPAddress ip(192,168,2,xxx);

int an;
boolean _intArray[6];
int _timeArray[6];
String radars;
boolean emailIt=true;
int anLow=200;
int resp;
int isOnlineTime=0;
boolean isConnected;

void setup() {
  SPI.begin();
  Ethernet.begin(mac, ip);
  Serial.begin(9600);
  for (an = 1; an < 6; an++) {
    _timeArray[an]=0;
    _intArray[an]=true;
  }
  pinMode(A0, INPUT);
  pinMode(A1, INPUT);
  pinMode(A2, INPUT);
  pinMode(A3, OUTPUT);
  pinMode(A4, OUTPUT);
  pinMode(A5, OUTPUT);

  pinMode(3, INPUT);
  pinMode(5, INPUT);
  pinMode(6, INPUT);
}

void loop() {
  if (analogRead(0)>anLow && emailIt==true){
    for (an = 1; an < 7; an++){
      switch (an){
        case 1:
          if (analogRead(an)>anLow){
            if (_intArray[an]==true){
              String myString = String(an);
              int z=alarm(1,myString);
            }
          }
        break;
        case 2:
          if (analogRead(an)>anLow){
            if (_intArray[an]==true){
              String myString = String(an);
              int z=alarm(1,myString);
            }
          }
        break;
        case 3:
        if (digitalRead(an)==HIGH){
          if (_intArray[an]==true){
              String myString = String(an);
              int z=alarm(1,myString);
            }
          }
        break;
        case 5:
        if (digitalRead(an)==HIGH){
          if (_intArray[an]==true){
              String myString = String(an);
              int z=alarm(1,myString);
            }
          }
        break;
        case 6:
        if (digitalRead(an)==HIGH){
          if (_intArray[an]==true){
              String myString = String(an);
              int z=alarm(1,myString);
            }
          }
        break;
      }
      if (10<_timeArray[an]){
        _intArray[an]=true;
        _timeArray[an]=0;
      }else{
        _intArray[an]=false;
        _timeArray[an]++;
      }
    }
  }
  
  radars="";
  for (an = 0; an < 7; an++){
    switch (an){
      case 0:
        if (analogRead(an)>anLow){
          radars+="1|";
        }else{
          radars+="0|";
        }
      break;
      case 1:
        if (analogRead(an)>anLow){
          radars+="1|";
        }else{
          radars+="0|";
        }
      break;
      case 2:
        if (analogRead(an)>anLow){
          radars+="1|";
        }else{
          radars+="0|";
        }
      break;
      case 3:
        if (digitalRead(an)==HIGH){
          radars+="1|";
        }else{
          radars+="0|";
        }
      break;
      case 5:
        if (digitalRead(an)==HIGH){
          radars+="1|";
        }else{
          radars+="0|";
        }
      break;
      case 6:
        if (digitalRead(an)==HIGH){
          radars+="1|";
        }else{
          radars+="0|";
        }
      break;
    }
  }

  int x=alarm(2,radars);
  
//BOF gets
  //BOF Sentinel
  int p=gets(1);
  if (p==1){
    analogWrite(A3, 255);
    int q=gets(7);
  }else if (p==3){
    analogWrite(A3, 0);
    int f=gets(6);
  }
  //EOF Sentinel
  
  //BOF switchReq
  int v=gets(2);
  if (v==1){
    analogWrite(A4, 255);
    delay(700);
    analogWrite(A4, 0);
    int b=gets(8);
  }else if (v==3){
    analogWrite(A5, 255);
    delay(700);
    analogWrite(A5, 0);
    int s=gets(8);
  }
  //EOF switchReq
  
  //BOF isOnline
  int u=gets(3);
  if (u==1){
    isOnlineTime=0;
    int t=gets(4);
  }else if (u==0){
    isOnlineTime+=1;
  }
  //EOF isOnline
  
  //BOF emailIt
  int y=gets(5);
  if (y==0){
    if (isOnlineTime>5){
      emailIt=true;
      int h=gets(9);
    }
  }else if (y==1){
    emailIt=true;
  }else if (y==3){
    emailIt=false;
  }
  //BOF emailIt
//EOF gets
}

int alarm(int typ1,String zone){
  EthernetClient client;
  String data;
  data="";
  switch (typ1){
    case 1:
      data+="zone=";
      data+=zone;
    break;
    case 2:
      data+="radar=";
      data+=zone;
    break;
  }
  
  if (client.connect(server,80)) {
    client.println("POST /riaszto/includes/ajaxes/alarm.php HTTP/1.1");
    client.print("Host: ");
    client.println(server);
    client.println("Content-Type: application/x-www-form-urlencoded");
    client.println("Connection: close");
    client.print("Content-Length: ");
    client.println(data.length());
    client.println();
    client.print(data);
  }
  delay(250);
  client.stop();
}

int gets(int typ2){
  EthernetClient client;
  String data;
  data="";
  switch (typ2){
    case 1:
      data+="securityAlarm=2";
    break;
    case 2:
      data+="switchReq=2";
    break;
    case 3:
      data+="isOnline=2";
    break;
    case 4:
      data+="isOnline=5";
    break;
    case 5:
      data+="emailIt=2";
    break;
    case 6:
      data+="securityAlarm=5";
    break;
    case 7:
      data+="securityAlarm=1";
    break;
    case 8:
      data+="switchReq=5";
    break;
    case 9:
      data+="emailIt=1";
    break;
  }

  if (client.connect(server,80)) {
    isConnected=true;
    client.print("GET /riaszto/includes/ajaxes/gets.php?");
    client.print(data);
    client.println(" HTTP/1.1");
    client.print("Host: ");
    client.println(server);
    client.println("Connection: close\r\n");
    boolean nextIsValue=false;
    while(client.connected()) {
      while(client.available()) {
        char ch=client.read();
        if (ch == '@'){
          nextIsValue=true;
        }else if (nextIsValue==true){
          resp=ch;
          nextIsValue=false;
        }
      }
    }
  }else{
    isConnected=true;
  }
  if (isConnected==true){
    if (resp == '0'){
      resp = 0;
    }else if (resp == '1'){
      resp = 1;
    }else if (resp == '2'){
      resp = 2;
    }else if (resp == '3'){
      resp = 3;
    }else if (resp == '4'){
      resp = 4;
    }else if (resp == '5'){
      resp = 5;
    }
  }else if (resp==0){
    resp=9;
  }
  client.stop();
  delay(250);
  return resp;
}

Dear SurferTim,

Please help as a X.mas gift :wink:

Regards:
Bela

myDuino:
Dear SurferTim,

Please help as a X.mas gift :wink:

Best Regards:
Bela

OK. You are not using any debugging in your code, so it will be very difficult to determine where the fail is. Put some serial calls to determine where it locks up or fails. Like this:

  Serial.print(F("Connecting.."));
  if (client.connect(server,80)) {
    Serial.println(F("connected"));
    client.println("POST /riaszto/includes/ajaxes/alarm.php HTTP/1.1");
    client.print("Host: ");
    client.println(server);
    client.println("Content-Type: application/x-www-form-urlencoded");
    client.println("Connection: close");
    client.print("Content-Length: ");
    client.println(data.length());
    client.println();
    client.print(data);

// you need to read the response and display it here like the code section below.

    client.stop();
    Serial.println(F("Disconnected"));
 }
 else Serial.println(F("failed"));

I rearranged your code to display a "connection failed" message.
Note there is no read of the response in the code above. That could cause the socket to not release.

Do the same here.

  Serial.print(F("Connecting.."));
  if (client.connect(server,80)) {
    Serial.println(F("connected"));
    isConnected=true;
    client.print("GET /riaszto/includes/ajaxes/gets.php?");
    client.print(data);
    client.println(" HTTP/1.1");
    client.print("Host: ");
    client.println(server);
    client.println("Connection: close\r\n");
    boolean nextIsValue=false;
    while(client.connected()) {
      while(client.available()) {
        char ch=client.read();

// display the results
        Serial.print(ch);

        if (ch == '@'){
          nextIsValue=true;
        }else if (nextIsValue==true){
          resp=ch;
          nextIsValue=false;
        }
      }
    }
  }else{
// shouldn't this be isConnected = false?
    isConnected=true;
  }
  if (isConnected==true){
    if (resp == '0'){
      resp = 0;
    }else if (resp == '1'){
      resp = 1;
    }else if (resp == '2'){
      resp = 2;
    }else if (resp == '3'){
      resp = 3;
    }else if (resp == '4'){
      resp = 4;
    }else if (resp == '5'){
      resp = 5;
    }
  }else if (resp==0){
    resp=9;
  }
  client.stop();
  Serial.println(F("Disconnected"));

Then when you start the code running, open the serial monitor and watch. It should be obvious where it fails.