'Arduino MEGA, LCD and SSR is frozen after 24 hours

I'm going to tell you my problem with arduino.

I am making an access rfdi system to enable a water pump, the procedure is as follows: I bring the rfid keyring closer to the reader, I make a sql query to the server and if it returns the data, I enable the pump and later register that user is served water.

Everything works fine for 18-24 hours, after that time the screen freezes and the arduino doesn't respond in any way or run the program. To get it to work again I have to restart it. Maybe I thought it was a memory problem, but I don't know. On build I have Sketch (18%) and memory (22%).

I don't know where the problem is.

#include <U8glib.h>
#include <SPI.h>
#include <EthernetENC.h>
#include <Wiegand.h> //PROTOCOLO P/ RFDI
#include <ArduinoJson.h>

U8GLIB_ST7920_128X64_1X u8g(6, 5, 4 ,7); //LCD  Enable, RW, RS, RESET  

#define FALSE 0      
#define TRUE  1 
long int rawToken;

char c;
char pageAdd[64];
char user[64];

int totalCount = 0;

String ID = "";
String readString = ""; 
String clientegraba = "";
String terrenograba = "";

bool inicio = true;
bool errorbit = false;

byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

// CONFIG IP ARDUINO
IPAddress ip(192,168,0,210);
IPAddress gateway(192, 168, 0, 1);
IPAddress subnet(255, 255, 255, 0);

// IP Y PUERTO SERVIDOR
IPAddress server(192,168,0,104); 
char serverName[] = "192.168.0.104";
int serverPort = 80;


EthernetClient client;
WIEGAND wg;



void setup() {
  
  Serial.begin(9600);
  
  pinMode(34,OUTPUT); //OUT PUMP
  digitalWrite(34,LOW);
  
  welcome(); //PANTALLA BIENVENIDA
  
  //GATE A
  wg.D0PinA =2;   
  wg.D1PinA =3;   
  //GATE B
  wg.D0PinB =18;  
  wg.D1PinB =19;   
  //GATE C
  wg.D0PinC =20;  
  wg.D1PinC =21;  

  // Reader enable
  wg.begin(TRUE, FALSE, FALSE);  // wg.begin(GateA , GateB, GateC)
 
  // disable SD SPI
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);

  // Start ethernet
  Serial.println(F("Iniciando ethernet..."));
  Ethernet.begin(mac, ip, gateway, gateway, subnet);
  Serial.println(Ethernet.localIP());
  
  delay(2000);
  Serial.println(F("Listo"));
  ingresellave();
}

void loop()
{
  
  if(wg.available()){
    errorbit = false;
    espere();  //WAITING SCREEN
    ID = String(wg.getCode());   //OBTIENE ID DE TARJETA
    String data = "/test.php?cliente=" + ID;  //CONCAT ID
    sprintf(pageAdd,data.c_str(),totalCount);
    Serial.println(pageAdd);
    if(!getPage(server,serverPort,pageAdd)){
      Serial.print(F("Falla "));
    }else{
      Serial.print(F("Paso "));
    }
    totalCount++;
    Serial.println(totalCount,DEC);
  }

  if(ID != ""){
      if(clientegraba != ""){
        grabacarga(clientegraba,terrenograba);
      }else{
        if(errorbit == false){
          noexiste();
          delay(3000);
          ID = "";
          ingresellave();
        }
      }
  }
  delay(200);
}


byte getPage(IPAddress ipBuf,int thisPort, char *page)
{
  int inChar;
  char outBuf[128];
  Serial.print(F("Conectando..."));
  if(client.connect(ipBuf,thisPort) == 1){
    Serial.println(F("Conectado"));
    sprintf(outBuf,"GET %s HTTP/1.1",page);
    client.println(outBuf);
    sprintf(outBuf,"Host: %s",serverName);
    client.println(outBuf);
    client.println(F("Connection: close\r\n"));
  }else{
    error();
    errorbit = true;
    Serial.println(F("Error en servidor"));
    return 0;
  }

  // connectLoop controls the hardware fail timeout
  int connectLoop = 0;
  boolean reader = false;
  while(client.connected())
  {
    while(client.available())
    { 
        String line = client.readStringUntil('\n');
        if(reader){
          StaticJsonBuffer<200> jsonBuffer;
          JsonObject& datos = jsonBuffer.parseObject(line);
          String apellido = datos["name"];
          String cliente = datos["cliente"];
          clientegraba = cliente;
          long total = datos["total"];
          long libres = datos["libres"];
          String terreno = datos["terreno"];
          terrenograba = terreno;
          String costo = datos["costo"];
          float price = costo.toFloat();
          usuario(apellido,total,libres,price);
          Serial.println("User: " + apellido);
          Serial.println("Cliente: " + cliente);
          Serial.println("Tarjeta: " + ID);
          Serial.println("Terreno: " + terreno);
          Serial.println("Costo: " + String(costo));
          Serial.println("Total: " + String(total));
          Serial.println("Libres: " + String(libres));
          if(apellido != ""){
            digitalWrite(34,HIGH);  //PUMP START
            delay(1000);
            digitalWrite(34,LOW);   //PUMP STOP
          }
        }
        if(line == "\r") {
            reader = true;
            break;
        }
     // 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();
  Serial.println(F("Desconectando."));
  // close client end
  client.stop();
  delay(100);
  return 1;
}

void grabacarga(String cliente, String terreno){
  
    String data = "/graba.php?cliente=" + cliente + "&tarjeta=" + ID + "&terreno='" + terreno + "'";
    sprintf(pageAdd,data.c_str(),totalCount);
    Serial.println(pageAdd);
    if(!getPage(server,serverPort,pageAdd)) Serial.print(F("Falla "));
    else Serial.print(F("Paso "));
    totalCount++;
    Serial.println(totalCount,DEC);
    ID = "";
    terrenograba = "";
    clientegraba = "";
    inicio = true;
    delay(9000);
    ingresellave();
}


void welcome() {
  u8g.setRot180();
  u8g.setFont(u8g_font_unifont);
  u8g.firstPage();  
  do {
    u8g.drawStr(18,22,"SISTEMA");
    u8g.drawStr(15,50,"ACCESO RFID");
  } while( u8g.nextPage() );
}


void ingresellave() {
  u8g.setFont(u8g_font_unifont);
  u8g.firstPage();  
  do {
    u8g.drawStr(0,10,"COLOQUE EL BIDON");
    u8g.drawStr(2,35,"Y LUEGO INGRESE");
    u8g.drawStr(25,60,"UNA LLAVE");
  } while( u8g.nextPage() );
}

void error() {
  u8g.setFont(u8g_font_unifont);
  u8g.firstPage();  
  do {
    u8g.drawStr(40,10,"ERROR");
    u8g.drawStr(33,35,"INTENTE");
    u8g.drawStr(22,60,"NUEVAMENTE");
  } while( u8g.nextPage() );
}

void espere() {
  u8g.setFont(u8g_font_unifont);
  u8g.firstPage();  
  do {
    u8g.drawStr(25,35,"ESPERE...");

  } while( u8g.nextPage() );
}

void usuario(String user, long total, long libres, float costo) {
  
  u8g.firstPage();  
  do {
    u8g.setFont(u8g_font_6x10);
    u8g.drawStr(0,10,user.c_str());
    
    String carga = "Carga:" + String(total) + "/" + String(libres);
    u8g.setFont(u8g_font_unifont);
    u8g.drawStr(15,33,carga.c_str());
    if(total > libres){
       long precio = total - libres;
       float costototal = precio * costo;
       Serial.println(costototal);
       String valor = "Costo:$" + String(costototal);
       u8g.drawStr(0,60,valor.c_str());
    }

 
  } while( u8g.nextPage() );
}


void noexiste(){
  u8g.setFont(u8g_font_unifont);
  u8g.firstPage();  
  do {
    u8g.drawStr(34,22,"USUARIO");
    u8g.drawStr(12,50,"NO REGISTRADO");
  } while( u8g.nextPage() );
  
}

The system power is from a 12V 4.5A source and the Arduino Mega along with the screen(LCD12864A) are powered from the 5V pin through an external LM2596 regulator. The screen data pins are connected to pins 4,5,6 and 7, the rfid reader to pins 2 and 3 and the water pump output is pin 34 which has a 2222a pnp transistor connected with a 1K base resistor, emitter to ground and collector to an SSR-24DA solid state relay.

Thank you.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source