Arduino restarts itself during receiving data through W5100 Ethernet Shield

Hi guys! :slight_smile:
We're projecting a magnect lock control activated by fingerprint reading. Well, we want to verify the ID acquired by fingerprint reader in database (however, the code showed below doesn't have the database commands) from local server. We are using a LCD display to show the messages. That's the problem: everytime the Arduino receives the data sended by server, it restarts itself. We are using an atMEGA328 and we have already tried using the Arduino MEGA and it still doesn't work.
Could someone help us to solve this problem please?

That's the code and I'm ready to answer any questions about our circuit and code: (I've supressed some parts of code to make it easier to understand.) But I also attached the complete code in the case of someone wants to see the complete one.

/*            
 * link to verify the data sended to server 
 * http://robotica.ufscar.br/empeltec.txt
 */ 

#include <Adafruit_Fingerprint.h>
#include <SoftwareSerial.h>
#include <Wire.h>
#include <DS3231.h>
#include <LiquidCrystal_I2C.h>  
#include <LCD.h>
#include <SPI.h>
#include <SPI.h>
#include <Ethernet.h>
#include <utility/w5100.h>
#include <stdio.h>


//fingerprint reader variables
[...]

//LCD defs
[...]

//Buttons and actuators
#define enroll 5
#define del 6
#define up 7 
#define down 8 //these buttons up and down activate the system to verify the fingerprint
#define LED 9
//#define LOCK 14

//Ethernet variables
int i = 0, k = 0, liberado = 0, ctrlEth=0; //liberado is a variable that control if the entry is authorized
char c, msg[200]; 

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
char server[] = "www.robotica.ufscar.br";

// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 0, 177);

EthernetClient client;

static void my_callback () { //that function receives the sended data from server
  msg[i] = c;
  i++;
}

static void teste(){ //that function verifies if the message "liberado" (that means authorized) was sent from server
    if (c == 'L')
      k = 1;
    if ((c == 'i')&&(k == 1))
      k = 2;
    if ((c == 'b')&&(k == 2))
      k = 3;
    if ((c == 'e')&&(k == 3))
      k = 4;
    if ((c == 'r')&&(k == 4))
      k = 5;
    if ((c == 'a')&&(k == 5))
      k = 6;
    if ((c == 'd')&&(k == 6))
      k = 7;
    if ((c == 'o')&&(k == 7))
      k = 8;
    if (k == 8) 
    {
      liberado = 1;
      delay(1000);
      k = 0;
    }
    else
      liberado = 0;
}

//-------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------

void setup() {
  Serial.begin(115200);
  finger.begin(57600);
  [...]
  
  [...]
  pinMode(up,INPUT_PULLUP);
  pinMode(down,INPUT_PULLUP);
  
  if (finger.verifyPassword()) { //verifies if the fingerprint reader is activated
  [...] 
   }
  
  else {
  [...]
   }
  
  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    lcd.clear();
    lcd.setCursor(5,0);
    lcd.print("Conexao");
    lcd.setCursor(5,1);
    lcd.print("falhou.");
    //Serial.println("Failed to configure Ethernet using DHCP");
    // try to congifure using IP address instead of DHCP:
    Ethernet.begin(mac, ip);
  }
  // give the Ethernet shield a second to initialize:
  delay(1000);
  lcd.clear();
  lcd.setCursor(2,0);
  lcd.print("Conectando..");
  //Serial.println("connecting...");

  //3 seconds timeout for connection
  W5100.setRetransmissionTime(0x07D0);
  W5100.setRetransmissionCount(3);
  
  temp = millis();
  utemp = millis();
 
}

void loop() {
  temp=utemp;
  delay(100);
  
  if (digitalRead(up)==0 || digitalRead(down)==0)  
  {
    delay(100);
    ctrl1=0;
    ctrl2=0;
    while(ctrl1==0)
    {
      if(ctrl2==0) {
        temp = millis();
        utemp = millis();
      }
      //Serial.println("Coloque o dedo para verificar");
      lcd.setCursor(0,0);
      lcd.print("Coloque o dedo");
      lcd.setCursor(0,1);
      lcd.print("para verificar");
      ctrl2=1;
      checkKeys();
      int result = getFingerprintIDez();
      teste();
      if(result >= 0)
      {
        //Serial.println("Acesso liberado!");
        //Serial.print("ID ");
        //Serial.println(id);
        ctrl1=1;
        ctrl2=0;
        if (client.connect(server, 80)) { //starts the communication with server
        //Serial.println("connected");
        //Serial.println();
          lcd.clear();
          lcd.setCursor(3,0);
          lcd.print("Autenticando");
          delay(1000);
           // Make a HTTP request:
          String url = "GET /empeltec.php?id=" + String(result) + " HTTP/1.0"; //sends the ID acquired by fingerprint reader to server
          client.println(url);
          client.println("Host: www.robotica.ufscar.br");
          client.println("Connection: close");
          client.println();
          while(client.available()) 
          {  
            c = client.read();
            my_callback();
            teste();        
           }
          } 
        else {
          // if you didn't get a connection to the server:
          lcd.clear();
          lcd.setCursor(4,0);
          lcd.print("Conexao");
          lcd.setCursor(4,1);
          lcd.print("falhou!");
          liberado=2;
          delay(1000);
        }
  
        if(liberado == 1) //liberado==1 means that the server authorized the ID sended
      {
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("Liberado");
        lcd.setCursor(10,0);
        lcd.print("ID ");
        lcd.setCursor(13,0);
        lcd.print(result);
        lcd.setCursor(0,1);
        lcd.print("Registrado");
        digitalWrite(LED, HIGH);
        //digitalWrite(TRANCA, HIGH);
        delay(5000);
        digitalWrite(LED, LOW);
        //digitalWrite(TRANCA, LOW);
        liberado=0;
        i = 0;
      }
        else if (liberado == 2){ //liberado==2 means that the server unauthorized the entry but the fingerprint read s
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("Liberado");
        lcd.setCursor(10,0);
        lcd.print("ID ");
        lcd.setCursor(13,0);
        lcd.print(result);
        lcd.setCursor(0,1);
        lcd.print("Nao registrado");
        digitalWrite(LED, HIGH);
        //digitalWrite(TRANCA, HIGH);
        delay(5000);
        digitalWrite(LED, LOW);
        //digitalWrite(TRANCA, LOW);
        liberado=0;
        i = 0;
        }
        
        else {
          lcd.clear();
          lcd.setCursor(0,0);
          lcd.print("Negado");
        }
        if (!client.connected()) {
            client.stop();
            delay(3000);
            }
        break;
      }
        temp = millis();
        if (temp >= (utemp+15000))
        {
          ctrl1 = 1;
          ctrl2 = 0;
          temp = utemp;
        }
    }
  ctrl2=0; 
  }
  checkKeys(); //this function verify the state of the buttons
}

void checkKeys() {
   [...]
}

int getFingerprintIDez() {
   [...]
}

void Enroll()  {
   [...]
}

uint8_t getFingerprintEnroll() {
   [...]
}

void delet()  {
   [...]
}

uint8_t deleteFingerprint(uint8_t id)  {
   [...]
}

MagLockProject.ino (18.7 KB)

The symptom that you describe is often an indication that you are writing past the bounds of an array.

Or the use of String (capital S) :wink:

@matheusren, please post your complete code. If too big, you can attach it.

PS
Given you a karma for using code tags in your first post.

groundFungus:
The symptom that you describe is often an indication that you are writing past the bounds of an array.

First of all, thanks for answering! :slight_smile:
So should I change the array size? I will try that and I'll report you later

sterretje:
Or the use of String (capital S) :wink:

@matheusren, please post your complete code. If too big, you can attach it.

PS
Given you a karma for using code tags in your first post.

Thanks for answering! :slight_smile:
Do you mean at this command line?

String url = "GET /empeltec.php?id=" + String(result) + " HTTP/1.0"; //sends the ID acquired by fingerprint reader to server

About posting the complete code, it's done :wink:

I don't think String (capital S) will be the culprit here.

I'm not going to dig through your code to find where the variable i is used. Global variables should never be single character. The letter i occurs 761 times in your code.

static void my_callback () { //that function receives the sended data from server

msg[i] = c;
  i++;
}

Do you anywhere in your code check the value of i? Or reset it back to 0? Your above code should check if i is less than 200 before storing c in the message.

sterretje:
I don't think String (capital S) will be the culprit here.

I'm not going to dig through your code to find where the variable i is used. Global variables should never be single character. The letter i occurs 761 times in your code.
Do you anywhere in your code check the value of i? Or reset it back to 0? Your above code should check if i is less than 200 before storing c in the message.

Well, the variable i is set to zero at the beginning of the code and after every calling of this function, i is reset to zero.
Is it enough or some verification is needed?

About the variable name, I'm so sorry for that. It was so clear for us! Haha But obviously, it's an awful programming practice, I'll change that. :stuck_out_tongue: