Attendance system problem

Hi,
I am working on one project. It should be some type of attendance system with arduino uno, ethernet shield, rfid rc-522 reader for nfc cards, and solenoid lock.
It should work as follows:

  • reading an uid of an nfc card
  • sending uid to verify to server
  • if uid is in the server the lock will open for a few seconds
  • if lock opens the time of open is written in database

But i have a problem connecting ethernet shield to database. Database is situated on localhost (xampp) for now. All php codes are working i have tested them.

Is there anyone who could help?
Thanks and have a great day :grinning:

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip (192, 168, 1, 103);
EthernetClient client;

#define SS_PIN 7
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);

#define RELAY_PIN 3

void setup() {
  Ethernet.begin(mac, ip);
  mfrc522.PCD_Init();
  pinMode(RELAY_PIN, OUTPUT);
  digitalWrite(RELAY_PIN, HIGH);
}
void loop() {
  if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial()) {
    Serial.print("UID tag :");
    String uid= "";
    byte letter;
    for (byte i = 0; i < mfrc522.uid.size; i++) 
    {
     Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
     Serial.print(mfrc522.uid.uidByte[i], HEX);
     uid.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
     uid.concat(String(mfrc522.uid.uidByte[i], HEX));
  }
    if (verifyUID(uid)) {
      digitalWrite(RELAY_PIN, HIGH);
      delay(1000);
      digitalWrite(RELAY_PIN, LOW);
      addAccessTime(uid);
    }
  }
}

boolean verifyUID(String uid) {
  if (client.connect("database_server", 80)) {
    client.println("GET /verify_card.php?uid=" + uid + " HTTP/1.1");
    client.println("Host: 192.168.1.100");
    client.println("Connection: close");
    client.println();
    while (client.connected()) {
      while (client.available()) {
        char c = client.read();
        if (c == '1') {
          return true;
        }
      }
    }
  }
  return false;
}
void addAccessTime(String uid) {
  if (client.connect("database_server", 80)) {
    client.println("GET /story_time.php?uid=" + uid + " HTTP/1.1");
    client.println("Host: 192.168.1.100");
    client.println("Connection: close");
    client.println();
    client.stop();
  }
}

What is the problem, exactly?

i think that ethernet shield cannot connect to the server (database) to verify these uids

Have you tried some sketch that makes some simple internet connection? Is there no diagnostic or error information that you can show us or tell us about?

Tell us about all the things you have tried, and what happened.

I have tried different variations of the code with same core (connecting - verification - action). I have also tried some cutted codes only for successfull connection. None of them was working.
The ethernet shield is connected to the network and have also obtained an ip address with no problem.
I'm aware that the codes can look catastrophic and with a lot of errors, but I'm still a newbie.

For example:

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 100);
EthernetClient client;

void setup() {
  Ethernet.begin(mac, ip);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  Serial.begin(9600);
  delay(1000);
  Serial.println("connecting...");

  if (client.connect("192.168.1.100", 80)) {
    Serial.println("connected");
    client.println("GET / HTTP/1.1");
    client.println("Host: 192.168.1.100");
  } else {
    Serial.println("connection failed");
  }
}

void loop() {
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    for(;;)
      ;
  }
}

Yes, but what about the Ethernet library example sketches?

Details? How do you know it was connected?

Through ping from computer, it can be also seen directly on the router in connected devices.

Thanks, it's useful sometimes to get basics out of the way. People sometimes don't see the importance.

Yeah, I have no problem to give u as much informations as i can if u could help me somehow.

Have you tried a logging only sketch? One that has no NFC code at all, just logs dummy values?

yes but still couldnt connect to database

Post that one. It reduces the scope of the problem.

i already did, didnt i? or this is something different?
not sure

You know, but we don't. If you don't know, better test it again on your hardware to be sure. I don't see any attempt to connect or upload to a database there, though... so that would explain why it doesn't connect. :slight_smile: That one only connects to a host. No more.

ou you mean that, no i havent tried that because i couldnt connect to databese in the very first code. there was a serial prints and conditions similar to the second one but the result were the same. i will try to find it but not sure, if not i will try to make a new one.

The general idea is, connect to the host, send a dummy record to the database, quit. No NFC code at all.

It's to rule out the possibility that the NFC code is creating the problem. Also reduce clutter to make it easier to troubleshoot.

maybe something like this?

#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,100);
EthernetClient client;

void setup() {
  Ethernet.begin(mac, ip);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  Serial.begin(9600);
  delay(1000);
  Serial.println("connecting...");

  if (client.connect("192.168.1.100", 80)) {
    Serial.println("connected");
    client.println("GET /addRecord.php?name=John&age=35 HTTP/1.1");
    client.println("Host: 192.168.1.100");
    client.println("Connection: close");
    client.println();
  } else {
    Serial.println("connection failed");
  }
}


void loop() {
     if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    for(;;)
      ;
  }
}

PHP

<?php
  $servername = "localhost";
  $username = "Arduino";
  $password = "ArduinoMaHeslo";
  $dbname = "test";
  $conn = new mysqli($servername, $username, $password, $dbname);

  if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
  }

  $name = $_GET['name'];
  $age = $_GET['age'];

  $sql = "INSERT INTO users (name, age) VALUES ('$name', '$age')";

  if ($conn->query($sql) === TRUE) {
    echo "Record added successfully";
  } else {
    echo "Error: " . $sql . "<br>" . $conn->error;
  }

  $conn->close();
?>

this have a same result like the previous

What result was that? Does that produce an error log you can post?

connection failed