Hi I would like to ask for help regarding RFID AND ETHERNET. The goal is to send the RFID tag/value to a php file then manipulate it to then be stored in the database. The RFID tag can be read. However when transferring the data to the php it just does not work.
THINGS I've tried:
- Sending data alone without an RFID works.
- Defining SDA and RESET of RFID to pins 8 and 9 respectively. As I have read online, there seems to be a problem with the SPI usage. I would like to ask if it is really the SPI being the root cause of the problem.
COMPONENTS USED:
Arduino MEGA 2560
RFID RC522
HERE IS THE CODE:
#include <Ethernet.h>
#include <MFRC522.h>
#include <SPI.h>
#include <Servo.h>
#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define SDA_DIO 8
#define RESET_DIO 9
MFRC522 RC522(SDA_DIO, RESET_DIO);
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); //
int servoPin = 2;
Servo servo;
int servoAngle = 0; // servo position in degrees
const byte ROWS = 4; // Four rows
const byte COLS = 3; // Three columns
// Define the Keymap
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'#','0','*'}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { 33, 32, 31, 30 };
// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 36, 35, 34 };
// Create the Keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
#define Password_Lenght 7
char Data[Password_Lenght]; // 6 is the number of chars it can hold + the null char = 7
char Master[Password_Lenght] = "123456";
byte data_count = 0, master_count = 0;
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 1, 20);
IPAddress myDns(192,168,1, 1);
IPAddress gateway(192, 168, 1, 10);
IPAddress subnet(255, 255, 255, 0);
char server[] = "192.168.1.19";
char dataStr[12]; //create array to store the response. for example : SomeValue1=0
char c;
String call;
String content= "";
int x = 0; //varpiable for looping
EthernetClient client;
boolean alreadyConnected = false; // whether or not the client was connected previously
// disable SD card
void setup()
{
pinMode(4, OUTPUT);
digitalWrite(4, HIGH);
SPI.begin();
/* Initialise the RFID reader */
Ethernet.begin(mac, ip, gateway, subnet);
RC522.PCD_Init();
servo.attach(servoPin);
lcd.begin(16,2);
lcd.backlight();//Power on the back light
lcd.setCursor(0,0); //we start writing from the first row first column
lcd.print(" RFID: "); //16 characters per line
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
}
void loop()
{
/* Has a card been detected? /
if (RC522.PICC_IsNewCardPresent())
{
/ If so then get its serial number */
RC522.PICC_ReadCardSerial();
RC522.PICC_ReadCardSerial();
byte letter;
for (byte i = 0; i < RC522.uid.size; i++)
{
content.concat(String(RC522.uid.uidByte < 0x10 ? " 0" : " "));
_ content.concat(String(RC522.uid.uidByte*, HEX));*_
* }//content stored*
* lcd.setCursor(0,1); //we start writing from the first row first column*
* lcd.print(content); *
* delay(1000);*
* lcd.clear();*
* lcd.setCursor(0,0); //we start writing from the first row first column*
* lcd.print("Enter Passcode: "); *
phpCONNECT();
*content = ""; *
}
KEYPADFUNCTION();
}
void clearData()
{
* while(data_count !=0)
_ { // This can be used for any array size,_
Data[data_count--] = 0; //clear array for new data*
* }*
* lcd.setCursor(0,0); //we start writing from the first row first column*
lcd.print(" RFID: "); //16 characters poer line
* return;*
}
void KEYPADFUNCTION()
{
//---------------------------------------------------------------------------
* lcd.setCursor(0,1);*
* char customKey = kpd.getKey();*
* if (customKey) // makes sure a key is actually pressed, equal to (customKey != NO_KEY)
_ {_
Data[data_count] = customKey; // store char into data array*
* lcd.setCursor(data_count,1); // move cursor to show each new char*
* lcd.print(Data[data_count]); // print char at said cursor*
* data_count++; // increment data array by 1 to store new char, also keep track of the number of chars entered*
* }*
* if(data_count == Password_Lenght-1) // if the array index is equal to the number of expected chars, compare data to master*
* {*
* lcd.clear();*
* lcd.setCursor(0, 0);*
* lcd.print("Password is ");*
* if(!strcmp(Data, Master)){ // equal to (strcmp(Data, Master) == 0)*
* lcd.print("Good");*
* for(servoAngle = 0; servoAngle < 180; servoAngle++) //move the micro servo from 0 degrees to 180 degrees*
* { *
* servo.write(servoAngle); *
* delay(10); *
* }*
* delay(10000);*
* for(servoAngle = 180; servoAngle > 0; servoAngle--) //now move back the micro servo from 0 degrees to 180 degrees*
* { *
* servo.write(servoAngle); *
* delay(10); *
* }*
* }*
* else*
* lcd.print("Bad");*
* delay(1000);// added 1 second delay to make sure the password is completely shown on screen before it gets cleared.*
* lcd.clear();*
* clearData(); *
* }*
*//--------------------------------------------------------------------------- *
}
void phpCONNECT()
{
//---------------------------------------------------------------------------
Serial.println("Response: ");
Serial.println(content);
if (client.connect(server, 100)) {
* client.print("GET /etherartenetbeans/updateone.php?"); // This*
* client.print("value="); // This*
* client.print("%27");*
* client.print(content);*
* client.print("%27");*
* client.println(" HTTP/1.1");*
* client.println("Host: 192.168.1.19");*
* client.println("Content-Type: application/x-www-form-urlencoded");*
* client.println("Connection: close");*
* client.println(); // Empty line*
* client.println(); // Empty line*
* Serial.println("gumana ga");*
* Serial.println("WORKING???");*
* client.stop();*
* }*
* else {*
* // If Arduino can't connect to the server (your computer or web page)*
* //Serial.println("--> connection failed\n");*
* Serial.println("indi gumana ga");*
* Serial.println("NOT WORKING :(");*
* }*
//---------------------------------------------------------------------------
}