Go Down

Topic: Arduino UNO with RFID MFRC522 Connecting with XAMPP Localhost. (Read 2738 times) previous topic - next topic

karlandrewleonor

Hi guys I'm new here in arduino. I am trying to display the data that I've received in Arduino to XAMPP localhost database. The connection is okay, but the PHP connection got a problem. What seems to be the problem here ? I'm guessing that the problem is the PHP file

Here's the code of  Arduino:
Code: [Select]
#include<SPI.h>
#include<MFRC522.h>
#include<SoftwareSerial.h>
#include <Ethernet.h>
#define SS_PIN 4 //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 };
char server[] = "192.168.254.102";   //YOUR SERVER
IPAddress ip(192, 168, 254, 106);
EthernetClient client;
SoftwareSerial mySerial(8,9);     
MFRC522 rfid(SS_PIN,RST_PIN);
MFRC522::MIFARE_Key key;
byte id[No_Of_Card][4]={
  {53,69,32,119},             //RFID NO-1
  {112,224,72,84},             //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...");
 }
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();
                  j=0;
                           
                            rfid.PICC_HaltA(); rfid.PCD_StopCrypto1();   return;
                }
              }
            }
          }
   else
   {j++;
    if(j==No_Of_Card)
    {
      Serial.println("inVALID");
      Sending_To_DB();
      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");
    // Make a HTTP request:
    client.print("GET /rfid/MASTER_CODE/ethernet.php?id='22'");     //YOUR URL
//    if(j!=No_Of_Card)
//    {
//      client.print('1');
//    }
//    else
//    {
//      client.print('0');
//    }
    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: 192.168.254.102");
    client.println("Connection: close");
    client.println();
  } else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
  client.stop();
 }

 


I don't receive error from my arduino code.


Here's my PHP connection code:
Code: [Select]

<?php
class rfid{
 public 
$link='';
 function 
__construct($id){
  
$this->connect();
  
$this->storeInDB($id);
 }
 
 function 
connect(){
  
$this->link mysql_connect('localhost','root','') or die('Cannot connect to the DB');
  
mysql_select_db('rfid') or die('Cannot select the DB');
 }
 
 function 
storeInDB($id){
  
$query "insert into tb_rfid set rfid='".$id."'";
  
$result mysql_query($query) or die('Errant query:  '.$query);
 }
 
}


?>




Sorry for my ignorance.

PaulS

Quote
I am trying to display the data that I've received in Arduino to XAMPP localhost database.
If you knew what localhost meant, you'd know that that is not going to happen.

Code: [Select]
#include<SPI.h>
#include<MFRC522.h>
#include<SoftwareSerial.h>
#include <Ethernet.h>


Why dosome lines havespaces while othersdonot?

Code: [Select]
#define RST_PIN 9

SoftwareSerial mySerial(8,9);     

You're doing software serial on the reset pin for the RFID reader? Bizarre. Does that actually work?

Code: [Select]
{int m=0;
NOTHING follows the { on the same line!

Code: [Select]
  for(i=0;i<4;i++)
  {
   id_temp[0][i]=rfid.uid.uidByte[i];
             delay(50);
  }

Why




on




earth




do




you




delay




between




copying




each




byte?

Code: [Select]
          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])
                {

Ever heard of memcmp()? Of for loops?

Code: [Select]
    client.print("GET /rfid/MASTER_CODE/ethernet.php?id='22'");     //YOUR URL
//    if(j!=No_Of_Card)
//    {
//      client.print('1');
//    }
//    else
//    {
//      client.print('0');
//    }
    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();

Add Serial.print() statements to print the exact same data. Show us what you get. Do you think that those statements will do what you expect? I don't.

Look at a typical URL. Following the ? are name=value pairs, separated by commas.

Does your code produce data like that?

bemtele

Hi guys I'm new here in arduino. I am trying to display the data that I've received in Arduino to XAMPP localhost database. The connection is okay, but the PHP connection got a problem. What seems to be the problem here ? I'm guessing that the problem is the PHP file

Here's the code of  Arduino:
Code: [Select]
#include<SPI.h>
#include<MFRC522.h>
#include<SoftwareSerial.h>
#include <Ethernet.h>
#define SS_PIN 4 //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 };
char server[] = "192.168.254.102";   //YOUR SERVER
IPAddress ip(192, 168, 254, 106);
EthernetClient client;
SoftwareSerial mySerial(8,9);     
MFRC522 rfid(SS_PIN,RST_PIN);
MFRC522::MIFARE_Key key;
byte id[No_Of_Card][4]={
  {53,69,32,119},             //RFID NO-1
  {112,224,72,84},             //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...");
 }
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();
                  j=0;
                           
                            rfid.PICC_HaltA(); rfid.PCD_StopCrypto1();   return;
                }
              }
            }
          }
   else
   {j++;
    if(j==No_Of_Card)
    {
      Serial.println("inVALID");
      Sending_To_DB();
      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");
    // Make a HTTP request:
    client.print("GET /rfid/MASTER_CODE/ethernet.php?id='22'");     //YOUR URL
//    if(j!=No_Of_Card)
//    {
//      client.print('1');
//    }
//    else
//    {
//      client.print('0');
//    }
    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: 192.168.254.102");
    client.println("Connection: close");
    client.println();
  } else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
  client.stop();
 }

 


I don't receive error from my arduino code.


Here's my PHP connection code:
Code: [Select]

<?php
class rfid{
 public 
$link='';
 function 
__construct($id){
  
$this->connect();
  
$this->storeInDB($id);
 }
 
 function 
connect(){
  
$this->link mysql_connect('localhost','root','') or die('Cannot connect to the DB');
  
mysql_select_db('rfid') or die('Cannot select the DB');
 }
 
 function 
storeInDB($id){
  
$query "insert into tb_rfid set rfid='".$id."'";
  
$result mysql_query($query) or die('Errant query:  '.$query);
 }
 
}


?>




Sorry for my ignorance.
'''mysql_connect'''

the above statement is obsolete and its no longer use by PHP.. but you can use ''mysqli_connect'' instead

Go Up