RFID datas saving problem?

RFID datas saving to MYSQL database but not stability. For example: I read RFID and then saving firstly… Second read and NOT SAVING… Sometimes, first read saving, second read saving, third read NOT SAVING…

This code is run, but not perfectly… Help pls…

rfid.ino (4.83 KB)

Not going to bother downloading and looking at your sketch.

Suggestion:
Make sure you are reliably reading RFID data by simply outputing to Serial Monitor.
Once you know the reading is reliably then you can save to database.

When you have code, its best to post it directly in your question as so:

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

#include <HttpClient.h>
#define SS_PIN 5 //FOR RFID SS PIN BECASUSE WE ARE USING BOTH ETHERNET SHIELD AND RS-522
#define RST_PIN 9
#define No_Of_Card 3
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
Servo myservo;
int pos = 0; 
int buzzerPin = 2;
char server[] = "www.kodlamaegitimi.biz";   //YOUR SERVER
IPAddress ip(192, 168, 137, 1);
EthernetClient client;
SoftwareSerial mySerial(6,7);     
MFRC522 rfid(SS_PIN,RST_PIN);
MFRC522::MIFARE_Key key; 
byte id[No_Of_Card][4]={
  {240,135,55,166},             //RFID NO-1
  {252,41,131,99},             //RFID NO-2
  {151,94,80,84}              //RFID NO-3
};
byte id_temp[3][3];
byte i;
int j=0;
void setup()
{
  
  Serial.begin(9600);
  mySerial.begin(9600);
  SPI.begin();
  rfid.PCD_Init();
  for(byte i=0;i<6;i++)
  {
    key.keyByte[i]=0xFF;
  }
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    Ethernet.begin(mac, ip);
  }
  delay(1000);
  Serial.println("connecting...");
  myservo.attach(8);
 }
void loop()
{int m=0;
  if(!rfid.PICC_IsNewCardPresent())
  return;
  if(!rfid.PICC_ReadCardSerial())
  return;
  
for(i=0;i<4;i++)
  {
   id_temp[0][i]=rfid.uid.uidByte[i]; 
             delay(50);
  }
  
   for(i=0;i<No_Of_Card;i++)
  {
          if(id[i][0]==id_temp[0][0])
          {
            if(id[i][1]==id_temp[0][1])
            {
              if(id[i][2]==id_temp[0][2])
              {
                if(id[i][3]==id_temp[0][3])
                {
                  
                  Serial.print("your card no :");
                  for(int s=0;s<4;s++)
                  {
                    Serial.print(rfid.uid.uidByte[s]);
                    Serial.print(" ");
                   
                  }
                  Serial.println("\nVALID");
                  Sending_To_DB();     
                  girisSes();
                  ac();
                   j=0;         
                         
                  rfid.PICC_HaltA(); 
                  rfid.PCD_StopCrypto1();   
                   
                  return;
                                   
                  
                            
                }
              }
            }
          }
   else
   {j++;
    if(j==No_Of_Card)
    {
      Serial.println("inVALID");
      Sending_To_DB();
      engellendiSes();
      
      Serial.print("your card no :");
                  for(int s=0;s<4;s++)
                  {
                    Serial.print(rfid.uid.uidByte[s]);
                    Serial.print(" ");
                   
                  }      
      j=0;
    }
   }
  }
  
     // Halt PICC
  rfid.PICC_HaltA();
// Stop encryption on PCD
  rfid.PCD_StopCrypto1();
 }

void ac() {
  for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(3);                       // waits 15ms for the servo to reach the position
  }
   for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(3);                       // waits 15ms for the servo to reach the position
  }
}

void girisSes()
{  
    tone(2, 494);
    delay(40);
    noTone(buzzerPin);
    delay(20);  
}

void engellendiSes()
{  
    for (int i=0;i<2;i++)
    {
    tone(2, 494);
    delay(20);
    noTone(buzzerPin);
    delay(20);  }
}

void Sending_To_DB()   //CONNECTING WITH MYSQL
 {
   if (client.connect(server, 80)) {
    //Serial.println("connected");
    // Make a HTTP request:  
      client.print("GET /rfid/rfid_read.php?allow=");     //YOUR URL
    if(j!=No_Of_Card)
    {
      client.print('1');
      
    }
    else
    {
      client.print('0');
     
    }
    
    client.print("&id=");
    for(int s=0;s<4;s++)
                  {
                    client.print(rfid.uid.uidByte[s]);
                                  
                  }
    client.print(" ");      //SPACE BEFORE HTTP/1.1
    client.print("HTTP/1.1");
    client.println();
    client.println("Host: www.kodlamaegitimi.biz");
    client.println("Connection: close");
    client.println();
    printDec(rfid.uid.uidByte,rfid.uid.size);
    
    
  } 
  
  if (client.connected()) {
    client.stop();
    // if you didn't get a connection to the server:
   // Serial.println("connection failed");
  }
  client.stop();
 }


 void printDec(byte *buffer, byte bufferSize) {
  for (byte i = 0; i < bufferSize; i++) {
    Serial.print(buffer[i] < 0x10 ? " 0" : " ");
    Serial.print(buffer[i], DEC);
  }
}

freaklabs:
When you have code, its best to post it directly in your question as so:

#include <Servo.h>

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

#include <HttpClient.h>
#define SS_PIN 5 //FOR RFID SS PIN BECASUSE WE ARE USING BOTH ETHERNET SHIELD AND RS-522
#define RST_PIN 9
#define No_Of_Card 3
byte mac = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
Servo myservo;
int pos = 0;
int buzzerPin = 2;
char server = “www.kodlamaegitimi.biz”;  //YOUR SERVER
IPAddress ip(192, 168, 137, 1);
EthernetClient client;
SoftwareSerial mySerial(6,7);   
MFRC522 rfid(SS_PIN,RST_PIN);
MFRC522::MIFARE_Key key;
byte id[No_Of_Card][4]={
  {240,135,55,166},            //RFID NO-1
  {252,41,131,99},            //RFID NO-2
  {151,94,80,84}              //RFID NO-3
};
byte id_temp[3][3];
byte i;
int j=0;
void setup()
{
 
  Serial.begin(9600);
  mySerial.begin(9600);
  SPI.begin();
  rfid.PCD_Init();
  for(byte i=0;i<6;i++)
  {
    key.keyByte[i]=0xFF;
  }
  if (Ethernet.begin(mac) == 0) {
    Serial.println(“Failed to configure Ethernet using DHCP”);
    Ethernet.begin(mac, ip);
  }
  delay(1000);
  Serial.println(“connecting…”);
  myservo.attach(8);
}
void loop()
{int m=0;
  if(!rfid.PICC_IsNewCardPresent())
  return;
  if(!rfid.PICC_ReadCardSerial())
  return;
 
for(i=0;i<4;i++)
  {
  id_temp[0][i]=rfid.uid.uidByte[i];
            delay(50);
  }
 
  for(i=0;i<No_Of_Card;i++)
  {
          if(id[i][0]==id_temp[0][0])
          {
            if(id[i][1]==id_temp[0][1])
            {
              if(id[i][2]==id_temp[0][2])
              {
                if(id[i][3]==id_temp[0][3])
                {
                 
                  Serial.print(“your card no :”);
                  for(int s=0;s<4;s++)
                  {
                    Serial.print(rfid.uid.uidByte[s]);
                    Serial.print(" “);
                 
                  }
                  Serial.println(”\nVALID");
                  Sending_To_DB();   
                  girisSes();
                  ac();
                  j=0;       
                       
                  rfid.PICC_HaltA();
                  rfid.PCD_StopCrypto1(); 
                 
                  return;
                                 
                 
                           
                }
              }
            }
          }
  else
  {j++;
    if(j==No_Of_Card)
    {
      Serial.println(“inVALID”);
      Sending_To_DB();
      engellendiSes();
     
      Serial.print(“your card no :”);
                  for(int s=0;s<4;s++)
                  {
                    Serial.print(rfid.uid.uidByte[s]);
                    Serial.print(" ");
                 
                  }     
      j=0;
    }
  }
  }
 
    // Halt PICC
  rfid.PICC_HaltA();
// Stop encryption on PCD
  rfid.PCD_StopCrypto1();
}

void ac() {
  for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable ‘pos’
    delay(3);                      // waits 15ms for the servo to reach the position
  }
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable ‘pos’
    delay(3);                      // waits 15ms for the servo to reach the position
  }
}

void girisSes()

    tone(2, 494);
    delay(40);
    noTone(buzzerPin);
    delay(20); 
}

void engellendiSes()

    for (int i=0;i<2;i++)
    {
    tone(2, 494);
    delay(20);
    noTone(buzzerPin);
    delay(20);  }
}

void Sending_To_DB()  //CONNECTING WITH MYSQL
{
  if (client.connect(server, 80)) {
    //Serial.println(“connected”);
    // Make a HTTP request: 
      client.print(“GET /rfid/rfid_read.php?allow=”);    //YOUR URL
    if(j!=No_Of_Card)
    {
      client.print(‘1’);
     
    }
    else
    {
      client.print(‘0’);
   
    }
   
    client.print("&id=");
    for(int s=0;s<4;s++)
                  {
                    client.print(rfid.uid.uidByte[s]);
                                 
                  }
    client.print(" ");      //SPACE BEFORE HTTP/1.1
    client.print(“HTTP/1.1”);
    client.println();
    client.println(“Host: www.kodlamaegitimi.biz”);
    client.println(“Connection: close”);
    client.println();
    printDec(rfid.uid.uidByte,rfid.uid.size);
   
   
  }
 
  if (client.connected()) {
    client.stop();
    // if you didn’t get a connection to the server:
  // Serial.println(“connection failed”);
  }
  client.stop();
}

void printDec(byte *buffer, byte bufferSize) {
  for (byte i = 0; i < bufferSize; i++) {
    Serial.print(buffer[i] < 0x10 ? " 0" : " ");
    Serial.print(buffer[i], DEC);
  }
}

Thanks. I couldn’ t find it yesterday.

ieee488:
Not going to bother downloading and looking at your sketch.

Suggestion:
Make sure you are reliably reading RFID data by simply outputing to Serial Monitor.
Once you know the reading is reliably then you can save to database.

I 'am reliably reading RFID because then run servo and buzzer. I think problem Sending_To_DB() function. But i can't solve the problem.

Now that you know you have reliable data to send to your database via HTTP request, I would test your endpoint to make sure it is reliably receiving the data. My guess is that it is.

When you have confirmed that you will then want to read your HTTP response to see what your server is telling you on the failures.

You can read the HTTP response using the same Ethernet client you sent the request. You read it in just like a serial connection.

This tutorial Robin2 has provided is extremely helpful for that.

You can also debug your HTTP endpoints with an HTTP client like Advanced REST Client for complex HTTP requests. I really recommend something like this for frequent or complex endpoint debugging.

Thee_Captain:
Now that you know you have reliable data to send to your database via HTTP request, I would test your endpoint to make sure it is reliably receiving the data. My guess is that it is.

When you have confirmed that you will then want to read your HTTP response to see what your server is telling you on the failures.

You can read the HTTP response using the same Ethernet client you sent the request. You read it in just like a serial connection.

This tutorial Robin2 has provided is extremely helpful for that.

You can also debug your HTTP endpoints with an HTTP client like Advanced REST Client for complex HTTP requests. I really recommend something like this for frequent or complex endpoint debugging.

Thanks. I will read this article.