I'm using an RC522 RFID reader with an Arduino UNO. There is a function?? (or whatever it's called :)) in Miguel Balboa's new MFRC522.h library which is mfrc522.uid.uidByte that prints the bytes of the unique ID. The repetition of the function results in 8 piece of HEX digits that are from the 2 numbers that each function outputs (2+2+2+2=8). Whenever I use the line of codes that are commented out, the serial monitor outputs them successfully. So for example, 12345678 is the unique id, each array displayed 12, then 34, then, 56, then 78 which is 12345678 in the serial. which is fast and unnoticeable besides that there are no spaces included when on output. The thing is, I want to output all of these into a single variable.(Please help me also on how to concatenate) How can I do this? An attempt was done below (not commented out) but it outputs only the number 16, consistently, which is I think due to a data type problem. The variables are initialized as int. The "function" which I found in the library is initialized as byte (correct me If I'm wrong). But when the variables are initialized as byte, I cannot upload the sketch since there are errors. Please help. (The reason I need it to be saved into a single variable is because I am placing it in the GET method of an Ethernet Client. I don't think the bare function can do that. (Or if it can, can you please help with the code? :)) Thanks
* *// Dump UID //Serial.print("Card UID:"); //Serial.print(mfrc522.uid.uidByte[0], HEX); //Serial.print(mfrc522.uid.uidByte[1], HEX); //Serial.print(mfrc522.uid.uidByte[2], HEX); //Serial.print(mfrc522.uid.uidByte[3], HEX); serNum0={mfrc522.uid.uidByte[0], HEX}; serNum1={mfrc522.uid.uidByte[1], HEX}; serNum2={mfrc522.uid.uidByte[2], HEX}; serNum3={mfrc522.uid.uidByte[3], HEX}; Serial.print(serNum0); Serial.print(serNum1); Serial.print(serNum2); Serial.print(serNum3);* *
Did you mean
serNum0= mfrc522.uid.uidByte[0];
etc ?
but it outputs only the number 16,
Assigning 16 to a variable will tend to do that.
If you only want to work with 4-byte uid's (they can be 4, 7, or 10 bytes) you can store the value in an unsigned long:
unsigned long uid = (mfrc522.uid.uidByte[0]<<24UL) |
(mfrc522.uid.uidByte[1]<<16UL) |
(mfrc522.uid.uidByte[2]<<8UL) |
mfrc522.uid.uidByte[3];
AWOL:
Did you meanserNum0= mfrc522.uid.uidByte[0];
etc ?
Assigning 16 to a variable will tend to do that.
Yeah yeah something like that is what I'm pertaining about. But I want all to be in a single variable. So continuing that snippet in mind, i need something like
finalnumber=serNum0+serNum1+serNum2+serNum3
the finalnumber being the final variable (concatenated). I've not any single "16" in my code.
Actually, I'm using the variable to be sent via client.print via ethernet since the code,
mfrc522.uid.uidByte[i], HEX
is not outputted in the client, and also, i still need variable since using these will only output 2 numbers since the procedure is in a for loop.
the finalnumber being the final variable (concatenated).
- does NOT concatenate things any more than plus on your calculator does.
I'm sorry PaulS
PaulS:
- does NOT concatenate things any more than plus on your calculator does.
Sorry it's just an idea not the actual code, I'm here because I don't know how to do those.
johnwasser:
If you only want to work with 4-byte uid's (they can be 4, 7, or 10 bytes) you can store the value in an unsigned long:unsigned long uid = (mfrc522.uid.uidByte[0]<<24UL) |
(mfrc522.uid.uidByte[1]<<16UL) |
(mfrc522.uid.uidByte[2]<<8UL) |
mfrc522.uid.uidByte[3];
Is it possible that the uid be in HEX? I used the same card but it outputs 7219 instead of the 44991C33 which I'm used to (since it is the output from the default example of the lib) Am I missing some bytes? or 7219 is exactly the same as 44991C33. I don't understand since I've converted 7219 and 44991C33
from DEC to HEX and vice versa just to prove that they're equal but can't seem to prove it. Am I correct with my assumptions? Please reply, I'm stuck. Thanks alot.
unsigned long uid = ((unsigned long)mfrc522.uid.uidByte[0]<<24UL) |
((unsigned long)mfrc522.uid.uidByte[1]<<16UL) |
(mfrc522.uid.uidByte[2]<<8UL) |
mfrc522.uid.uidByte[3];
?
AWOL:
unsigned long uid = ((unsigned long)mfrc522.uid.uidByte[0]<<24UL) |
((unsigned long)mfrc522.uid.uidByte[1]<<16UL) |
(mfrc522.uid.uidByte[2]<<8UL) |
mfrc522.uid.uidByte[3];
?
I was hoping that left side of the '<<' operator would get promoted to unsigned long to match the UL constants on the right side of the '<<' operator. I guess not.
There is no benefit to be gained from storing the data in a single variable.
byte goodTag[] = {0x44, 0x99, 0x1C, 0x33};
if(memcmp(mfrc522.uid.uidByte, goodTag, 4) == 0)
{
// The tag is good
}
Manipulating the bytes to store them in a single variable would still require the same number of lines of code to compare the data - one to define a good value and one to compare the read value to the good value.
I need it to be in the GET query. This is my code for now. This is working but I converted them into string. But some argue that it's not reliable to use the string data type. Although this is working, I am still looking for the HEX values since this is uniform in size. It's always 8 digit. When using DEC, I receive some 9 and some 8 digits. and I want my DB structure to be uniform. Anyways, I hope you could help me Thanks a lot. (My another problem now is that the uID is not sent to the web server on the PC but it is serially printed and the client is certified to be connected. but it does not reflect on my DB. I'm using XAMPP, Firewall off, IP both correct. Port is 80 and manual encoding of the argument is working, like localhost/add_data_php?rfid=12345678 [it does reflect on the DB]
Maybe because it needs delay? or on or off of the SS pins of each? your help will be much appreciated. :))
#include <SPI.h>
#include <MFRC522.h>
#include <Ethernet.h>
#define SS_PIN 9
#define RST_PIN 8
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress server(192,168,0,104); // numeric IP for server (no DNS) (reduce sketch size)
IPAddress ip(192,168,0,103); //numeric IP of the ETHERNET shield (STATIC)
EthernetClient client; //Initialize the Ethernet client library(port 80 is HTTP default):
int first=0;
int counter=0;
String uID;
void setup() {
Serial.begin(9600); // Initialize serial communications with the PC
// disable SD SPI
pinMode(4, OUTPUT);
digitalWrite(4, HIGH);
// disable w5100 SPI
pinMode(10, OUTPUT);
digitalWrite(10, HIGH);
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card
//Serial.println("Scan a MIFARE Classic PICC to demonstrate Value Blocks.");
Ethernet.begin(mac, ip); //we used a STATIC address to start ETHERNET
// print your local IP address:
Serial.print("My IP address: ");
for (byte thisByte = 0; thisByte < 4; thisByte++) {
// print the value of each byte of the IP address:
Serial.print(Ethernet.localIP()[thisByte], DEC);
Serial.print(".");
}
// give the Ethernet shield a second to initialize:
delay(1000);
Serial.println("connecting...");
}
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;
}
// Now a card is selected. The UID and SAK is in mfrc522.uid.
// Dump UID
//Serial.print("Card UID:");
int val1=(mfrc522.uid.uidByte[0]);
int val2=(mfrc522.uid.uidByte[1]);
int val3=(mfrc522.uid.uidByte[2]);
int val4=(mfrc522.uid.uidByte[3]);
String valA=String(val1);
String valB=String(val2);
String valC=String(val3);
String valD=String(val4);
uID=valA+valB+valC+valD;
Serial.print(uID);
Serial.println();
counter=counter+1;
Serial.print(counter);
//}
Serial.println();
// Halt PICC
mfrc522.PICC_HaltA();
// Stop encryption on PCD
mfrc522.PCD_StopCrypto1();
if (counter>first)
{//delay ifs
// enable w5100 SPI
if (client.connect(server, 80)) { //start of IF
Serial.println("connected");
// Make a HTTP request:
client.print("GET /add_data.php?rfid="); //dont make these println
client.print(uID); //dont make these println
client.println(" HTTP/1.1");
client.print("Host: ");
client.println(server);
client.println("Connection: close");
client.println();
Serial.print("SENT");
Serial.println();
first++;
Serial.print(first);
Serial.println();
client.stop();
} //end OF IF
}//delay ifs
}