Hello,
I have problem sending data from rfid card to MySql. I'm using arduino uno+ethernet shield+RC522. For the connection, i'm using PHP script. I put the card on the reader. The reader can read the card and in the serial monitor said that it's connected but when I open the database (WAMP server), the ID is not store in the database. I don't know what's wrong with it.
Can you help me?
Arduino code:
/*
Arduino-Interface-with-MySQL-for-storing-RFID-access-details (ARDUINO UNO,ETHERNET SHIELD, RFID MFRC522 & MYSQL)
The circuit:
- Typical pin layout used:(MFRC 522)
-
- MFRC522 Arduino Arduino Arduino Arduino Arduino
- Reader/PCD Uno/101 Mega Nano v3 Leonardo/Micro Pro Micro
- Signal Pin Pin Pin Pin Pin Pin
-
- RST/Reset RST 9 9 D9 RESET/ICSP-5 RST
- SPI SS SDA(SS) 4/10 4/53 D10 10 10
- SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16
- SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14
- SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15
created 21 NOV 2016
by SOUMYA RANJAN PANDA
For any help contact info@deligence.com
*/
#include<SPI.h>
#include<MFRC522.h>
#include<SoftwareSerial.h>
#include <Ethernet.h>
#include <LiquidCrystal.h>
#include <MySQL_Connection.h>
const int rs = 7, en = 6, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
#define SDA_PIN 8
#define RST_PIN 9
#define LED_PIN A0
#define LED_PIN A1
#define LED_PIN A2
#define LED_PIN A3
#define LED_PIN A4
#define LED_PIN A5
#define No_Of_Card 3
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
char server[] = "LAPTOP-CUBH48E0"; //YOUR SERVER
IPAddress ip(192,168,137,2);
IPAddress server_addr(192,168,137,1); // IP of the MySQL server here
char user[] = "dee"; // MySQL user login username
char password[] = "1234";
EthernetClient client;
MySQL_Connection conn((Client *)&client);
SoftwareSerial mySerial(8,9);
MFRC522 rfid(SDA_PIN,RST_PIN);
MFRC522::MIFARE_Key key;
byte id[No_Of_Card][4]={
//{0x24,0xED8,0x68,0x18}, //RFID NO-1
{0x92,0xBC,0x66,0xA2}, //RFID NO-2
{0X41,0XF5,0XE8,0XDB}
};
byte id_temp[3][3];
byte i;
int j=0;
void setup()
{
//pinMode(LED_PIN,OUTPUT);
//digitalWrite(LED_PIN,LOW);
lcd.begin(16, 2);
lcd.setCursor(3, 1);
lcd.print("Welcome!!");
lcd.setCursor(0, 0);
lcd.print("Circuit is Ready");
delay(2500);
lcd.clear();
Serial.begin(9600);
mySerial.begin(9600);
SPI.begin();
rfid.PCD_Init();
Serial.println("Put your card to the reader...");
Serial.println();
pinMode(A0, OUTPUT);
pinMode(A1, OUTPUT);
pinMode(A2, OUTPUT);
pinMode(A3, OUTPUT);
pinMode(A4, OUTPUT);
pinMode(A5, OUTPUT);
for(byte i=0;i<6;i++)
{
key.keyByte*=0xFF;*
- }*
- if (Ethernet.begin(mac) == 0) {*
- Serial.println("Failed to configure Ethernet using DHCP");*
- Ethernet.begin(mac, ip);*
- }*
- delay(1000);*
- Serial.println("connecting...");*
}
void loop()
{
-
digitalWrite(A4, HIGH);*
-
lcd.clear();*
-
lcd.setCursor(0, 0);*
-
lcd.print("Put your card to");*
-
lcd.setCursor(0, 1);*
-
lcd.print("the reader......");*
-
delay(300);*
-
int m=0;*
-
if(!rfid.PICC_IsNewCardPresent())*
-
return;*
-
if(!rfid.PICC_ReadCardSerial())*
-
return;*
-
for(i=0;i<4;i++)*
-
{*
id_temp[0]=rfid.uid.uidByte*;*
* delay(50);*
* }*
* for(i=0;i<No_Of_Card;i++)
_ {_
if(id[0]==id_temp[0][0])
_ {_
if(id[1]==id_temp[0][1])
_ {_
if(id[2]==id_temp[0][2])
_ {_
if(id[3]==id_temp[0][3])
_ {
Serial.print("UID:");
for(int s=0;s<4;s++)
{
Serial.print(rfid.uid.uidByte~~); Serial.print(" ");~~*_
~~ }~~
~~ Serial.println("\nVALID");~~
~~ Sending_To_DB();~~
~~ Serial.println("Authorised access");~~
~~ digitalWrite(A3, HIGH);~~
~~ delay (300);~~
~~ digitalWrite(A3, LOW);~~
~~ lcd.setCursor(0, 0);~~
~~ lcd.print("ID: ");~~
~~ lcd.setCursor(0, 1);~~
~~ lcd.print("Authorized access");~~
~~ return;~~
~~ j=0;~~
~~ rfid.PICC_HaltA(); rfid.PCD_StopCrypto1(); return;~~
~~ }~~
~~ }~~
~~ }~~
~~ }~~
~~ else~~
~~ {j++;~~
~~ if(j==No_Of_Card)~~
~~ {~~
~~ Serial.println("inVALID");~~
~~ Sending_To_DB();~~
~~ Serial.println("Access denied");~~
~~ digitalWrite(A1, HIGH);~~
~~ delay (500);~~
~~ digitalWrite(A1, LOW);~~
~~ lcd.setCursor(0, 0);~~
~~ lcd.print("ID : ");~~
~~ lcd.setCursor(0, 1);~~
~~ lcd.print("Access denied");~~
~~ return;~~
~~ j=0;~~
~~ }~~
~~ }~~
~~ }~~
~~ // Halt PICC~~
~~ rfid.PICC_HaltA();~~
~~ // Stop encryption on PCD~~
~~ rfid.PCD_StopCrypto1();~~
}
void Sending_To_DB() //CONNECTING WITH MYSQL
{
~~ if (client.connect(server, 80)) {~~
~~ Serial.println("connected");~~
~~ client.available();~~
~~ client.read();~~
~~ client.print(" "); //SPACE BEFORE HTTP/1.1~~
~~ client.print("HTTP/1.1");~~
~~ client.println();~~
~~ client.println("Host: 192.168.137.1");~~
~~ // Make a HTTP request:~~
~~ client.print("GET /attendance_system/phparduino.php?allow="); //This GET Request accesses my PHP code which is stored in a attendance system folder inside my www folder in my wamp server~~
~~ 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~~);~~
~~ }~~
~~ client.println("Connection: close");~~
~~ client.println();~~
~~ } else {~~
~~ // if you didn't get a connection to the server:~~
~~ Serial.println("connection failed");~~
~~ }~~
~~ client.stop();~~
}
PHP script:
<?php
class rfid {
~~ public $link='';~~
~~ function __construct($allow, $id) {~~
~~ $link->connect();~~
~~ $link->storeInDB($allow, $id);~~
~~ }~~
~~ function connect() {~~
~~ $sql = mysqli_connect('localhost','dee','1234','arduino') or die($link->error);~~
~~ $link = mysqli_connect('localhost','dee','1234','arduino') or die('Cannot connect to the DB');~~
~~ mysqli_select_db($sql, 'arduino') or die('Cannot select the DB');~~
~~ }~~
~~ function storeInDB($allow, $id) {~~
~~ $link = mysqli_connect('localhost','dee','1234','arduino') or die($link->error);~~
~~ $sql = "INSERT INTO attendance_list VALUES ('$_GET[rfid]')";~~
~~ $result = $sql($link->query($sql)) or die('Errant query: '.$sql);~~
~~ if($result) {~~
~~ echo "Added successfully";~~
~~ }~~
~~ }~~
}
~~ if($_GET['$allow'] != '' and $_GET['$id'] != '') {~~
~~ $rfid=new rfid($_GET['$allow'],$_GET['$sid']);~~
~~ }~~
/*class rfid{
~~ function show(){~~
~~ $fh=fopen('test.txt','a+');~~
~~ fwrite($fh,'hi');~~
~~ return 'hi';~~
~~ }~~
}
$rfid=new rfid();
$rfid->show();*/
?>