Go Down

Topic: [HELP] Can't store rfid data to database via ethernet (Read 1 time) previous topic - next topic

dharmanjarrahman

Hi there ,
I desperately need some help.
I'm working on RFID stuff. I need to store data read from RFID reader (MFRC522) and store them directly into the database in my laptop via ethernet. I guess I got stucked on the http request part. I'm pretty sure that the data read from RFID is just fine. And the communication between my arduino and my laptop is well-connected. But the data apparently still didn't stored yet to the database. These are my programs on Arduino :

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

byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x01 }; // RESERVED MAC ADDRESS
byte ip[] = { 192, 168, 1, 110};
byte server [] = { 192, 168, 1, 106 }; //ip address of the server you will connect to

EthernetClient client;

#define SS_PIN 7 //Arduino Uno for Slave Select
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);

char rfid[6];

void setup() {
       
Serial.begin(9600);
        SPI.begin();
        mfrc522.PCD_Init(); //RFID Reader Initialization
        Serial.println("Your current balance is : ");
}

void loop(){

   // Prepare key - all keys are set to FFFFFFFFFFFFh at chip delivery from the factory.
        MFRC522::MIFARE_Key key;
        for (byte i = 0; i < 6; i++) key.keyByte[i] = 0xFF;
       
        // Look for new cards
        if ( ! mfrc522.PICC_IsNewCardPresent()) {
                return;
        }

        // Select one of the cards
        if ( ! mfrc522.PICC_ReadCardSerial())    return;       

        byte buffer[18]; 
        byte block  = 1;
        byte status;
        //Serial.println("Authenticating using key A...");
        status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(mfrc522.uid));
        if (status != MFRC522::STATUS_OK) {
           Serial.print("PCD_Authenticate() failed: ");
           Serial.println(mfrc522.GetStatusCodeName(status));
           return;
        }
       
        // Read block
byte byteCount = sizeof(buffer);
status = mfrc522.MIFARE_Read(block, buffer, &byteCount);
if (status != MFRC522::STATUS_OK) {
    Serial.print("MIFARE_Read() failed: ");
    Serial.println(mfrc522.GetStatusCodeName(status));
}
        else  // Dump data
        {
        for (byte index = 0; index < 6; index++) {
            if(buffer[index] < 16) Serial.print("0");
            rfid[index] = buffer[index];
}
        mfrc522.PICC_HaltA(); // Halt PICC
        mfrc522.PCD_StopCrypto1();  // Stop encryption on PCD
        Serial.println("Nah");
        Serial.println(rfid);
//        byte rfids;
//        rfids = Serial.println(rfid);
//        data = "temp1=" + rfid;
        Serial.println("Nih");
        Serial.println(rfid);
        Ethernet.begin(mac, ip);
        httpRequest(rfid);}
}
     

void httpRequest(char* rfid) {
if (client.connect(server, 3306)) { // SERVER ADDRESS
                Serial.println("Connected");
client.print("GET /foodcourt/add.php?rfid=");
                client.print(rfid);
                client.print("&ip=192.168.1.110 HTTP/1.1");
            client.println("Host: 192.168.1.106"); // SERVER ADDRESS HERE
                client.println();
                client.println("");
                client.println();
Serial.print("GET /foodcourt/add.php?rfid="); //I put the add.php file inside htdocs folder on xampp.
                Serial.println(rfid);
                Serial.println("Host: 192.168.1.106"); // SERVER ADDRESS HERE
                Serial.print("Printed on Arduino : ");
                Serial.println(rfid);
                Serial.println("");
                client.stop();}
                else{
                  Serial.println("");
                  Serial.println("Connection failed");
                  Serial.println("");
                  client.stop();}
}


And these are my php code :

Code: [Select]
<?php
    include(
"connect.php");
   

   
$link=Connection();

$rfid=$_GET['rfid'];

$query "INSERT INTO `tes_ethernet` (`no_meja`) 
VALUES ('"
.$rfid."')"
   

   
mysql_query($query,$link);
mysql_close($link);

   
header("Location: index.php");
?>



And the index.php program is below (basically this program was made just to show any data from the database. Just to make sure whether the data has been stored or not) :

Code: [Select]
<?php

include(
"connect.php"); 

$link=Connection();

$result=mysql_query("SELECT * FROM `tes_ethernet`",$link);
?>


<html>
   <head>
      <title>RFID Data</title>
   </head>
<body>
   <h1>Balance</h1>

   <table border="1" cellspacing="1" cellpadding="1">
<tr>
<td>&nbsp;Saldo&nbsp;</td>
</tr>

      <?php 
  if($result!==FALSE){
     while($row mysql_fetch_array($result)) {
        printf("<tr><td> &nbsp;%s </td></tr>"
           $row["no_meja"]);
     }
     mysql_free_result($result);
     mysql_close();
  }
      
?>


   </table>
</body>
</html>


Hopefully anyone could help me for solving this. It's gonna be really helpful.
Thanks before.

PaulS

Code: [Select]
        Ethernet.begin(mac, ip);
You do NOT do this every time you want to send data. You do it ONCE in setup().

You test the return code, too.

Code: [Select]
        httpRequest(rfid);}
It hardly seems necessary to pass a global variable to the function. It IS necessary to properly format your code. The } does NOT go jammed up against the statement. It DOES go on a new line.

Code: [Select]
            client.println("Host: 192.168.1.106"); // SERVER ADDRESS HERE
Is your PC hosting multiple domains? If not, this statement is not needed.

Code: [Select]
                client.println();
                client.println("");
                client.println();

The correct number of blank lines to send is 0, not 3.

Each time you connect to the server and send data, you need to read the response from the server. Failing to do that will block the socket. Do that 4 times, and you'll need to reset the Arduino to get it communicating again.

Why are you sending two values to the PHP script when it expects only one?

Are you running Apache on the PC? What do the logs tell you is happening?

You can use echo() and other statements in the PHP script, and send information back to the client. You will, then, need to read that data on the Arduino.

You can open and close files, and write logging information from the PHP script.

There are so many things we don't know now that it is hard to tell you where to begin debugging the problem.

One thing you should do is make sure that the php script is working properly entirely from the PC before you try calling it from the Arduino.

http://192.168.1.106/foodcourt/add.php?rfid=xxxxxx&ip=192.168.1.110
entered in a browser URL field should make the add.php script add xxxxxx to the database. If it does, then the problem is with the Arduino code. If it doesn't, then the problem is with the php script.
The art of getting good answers lies in asking good questions.

Go Up