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 :
#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 :
<?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) :
<?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> Saldo </td>
</tr>
<?php
if($result!==FALSE){
while($row = mysql_fetch_array($result)) {
printf("<tr><td> %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.