ETHERNET SHIELD RFID PROBLEM

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:

  1. Sending data alone without an RFID works.
  2. 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 :(”);*

* }*
//---------------------------------------------------------------------------

}

  1. Sending data alone without an RFID works.
  2. Defining SDA and RESET of RFID to pins 8 and 9 respectively. .......... COMPONENTS USED: Arduino MEGA 2560

Mega's I2C is on pins 20,21. I don't think you can change it, I don't know how you would try, and why would you want to?

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.

I recall there is some problem with library support of the various buses. Does your library support SPI?

Hi thanks for your reply.

First I changed the RFID's SDA and RESET not the I2C if I am not mistaken. I changed the pins for SDA from 53 to 8. As I have read in this article https://www.arduino.cc/en/Main/ArduinoEthernetShieldV1, pin 53 must be left unused or the SPI would not work. Additionally I have used pins 50-52 for the RFID MFRC522 and now I think it must also be left open?? Please help me.

Second would you please enlighten me which library should I use. Currently my library supports SPI.

leopena_94: First I changed the RFID's SDA and RESET not the I2C if I am not mistaken. I changed the pins for SDA from 53 to 8.

You are very mistaken. SDA refers to the i2c bus and is on 20,21, as I said before. If you are using pin 8 for SS of the RFID, that sounds fair enuff, so long as the SS is connected to pin 8 If you are trying to select SD on your Ethernet shield, try pin 4

As I have read in this article https://www.arduino.cc/en/Main/ArduinoEthernetShieldV1, pin 53 must be left unused or the SPI would not work.

No it doesn't, it says

it must be kept as an output or the SPI interface won't work.

If you are using the current IDE, you can ignore this anyway. You will see this, it's absence, in the examples in the SD section of the IDE. I'm afraid I can't comment on the library. It should be OK if RFID runs on SPI. I think your immediate problem is that you don't know the difference between I2C and SPI.

Sir Nick thank you for the clarification.

I was implementing the use of Ethernet shield to send the rfid tag to a php file then pass in to the database so I am not going to use the SD of the ethernet shield, but thanks for the advice I will take note of that.

Sir please correct me If I am wrong, is it the SPI use of both peripherals(ethernet shield w5100 and rfid MFRC522) the problem?

If so, what kind of action should I take? I am very sorry, I just started using arduino recently and I still have little knowledge on it.

HI Sir Nick. Just found out something.

SPI.begin(); Ethernet.begin(mac, ip, gateway, subnet);

If I initialize the SPI first before the ethernet. the RFID works while the Ethernet shield does not

On the other hand, Ethernet.begin(mac, ip, gateway, subnet); SPI.begin();

If I do it this way ethernet works while the RFID does not.

Can I make something that would make the two not interfere with each other?

leopena_94: is it the SPI use of both peripherals(ethernet shield w5100 and rfid MFRC522) the problem?

I don't know anything about RFIDs. I am assuming that the W5100 and the RFID are both SPI devices, and so - no. The SPI bus is a bus, this means that a swag of devices can ride on it. All you have to do is have each device use a unique pin to select it. In the case of the W5100, I believe the SS pin 10 select command is in the library, but I haven't I haven't used IOT for ages, and have forgotten most of it. Ironically, my main use of the shield is currently for the SD, hence my bias.

As I said, your talk about SDA pins, i.e. I2C bus, suggests your immediate problem is that you don't know the difference between I2C and SPI. If your RFID really has a SDA pin, you need to change buses, and get on the I2C.

I understand you have had the RFID working, but you need to clarify that, since you can hardly have it working by using the wrong bus.

leopena_94: SPI.begin(); Ethernet.begin(mac, ip, gateway, subnet);

If I initialize the SPI first before the ethernet. the RFID works while the Ethernet shield does not

On the other hand, Ethernet.begin(mac, ip, gateway, subnet); SPI.begin();

If I do it this way ethernet works while the RFID does not.

OK. IF RFID works and does exactly what you want (forgetting all the PHP stuff), then there is clearly nothing wrong with it, you DO know what bus it uses, and the code relevant to it is kosher. That's a good start

AND it appears you have Ethernet working and, by itself, it does exactly what you want.

I have never experienced your problem, but it sounds like you have pin conflict.

I ASSUME both devices are SPI, with W5100 using the ICSP cluster and RFID on SPI pins 50,51,52, and can only guess you have a pin conflict with SS. A further guess is that RFID SS is on pin 53, which is in fact common with the SS on the ICSP cluster. Since you have no choice about the ICSP cluster, this means you have to relocate the SS off pin 53, and adjust the code. (a guess) Hope that helps!