Page 1 of 1
Forum

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is most likely no translated for the post!




Arduino - RFID and ...
 
Share:
Notifications
Clear all

Arduino - RFID and Arduino (TSX topic)

Page 1 / 2

 Hans
(@hans)
Noble Member Admin
Joined: 8 years ago
Posts: 1832
Topic starter  

Continuation of the topic by TSX:

I have done some editing but it couldn’t read the RFID tag. 

#include <SoftwareSerial.h>
#include <SPI.h>
#include <Ethernet.h>
 
SoftwareSerial rfidSerial(2, 3); // (RX, TX)
String myTag, myTagClean;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = {10, 1, 1, 2}; // ip in lan assigned to arduino
//byte gateway[] = {10, 1, 1, 254 }; // internet access via router
byte subnet[] = {255, 255, 255, 0 }; //subnet mask
char server[] = "10.1.1.1";
EthernetClient client;
 
void setup() {
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
  Serial.println("Hello World : RFID reader, from hardware serial");
  // set the data rate for the SoftwareSerial port
  rfidSerial.begin(9600); // communication speed of ID-12 reader
  rfidSerial.println("Hello World : RFID reader, from software serial");
  //while (!Serial) {
  //; // wait for serial port to connect. Needed for Leonardo only
  //}
}
void loop(){
  // read serial if available
  if (rfidSerial.available()) {
 
    // read and cast to char incoming data
    char incoming = (char)rfidSerial.read(); 
 
    //Serial.print("receiving " );
    //Serial.print(incoming);
    //Serial.print(" => ");
    //Serial.print(incoming, DEC);
    //Serial.println(' '); // Serial.print(10, BYTE);
 
    // packet structure, see ID-12 datasheet
    // STX A B C D E F G H I J CR LF ETX
    // 2 - - - - - - - - - - 13 10 3
 
    // if I read the final delimiter
    if(incoming == 3){
 
      Serial.println("Incoming Tag");
      //Serial.println(myTag);
      //Serial.println(myTag.length());
 
      // clean up String
      // we skip the first char, and take the following 12 chars
      myTagClean = myTag.substring(1, 13);
 
      // reset myTag
      myTag = "";      
 
      Serial.println(myTagClean);
      //Serial.println(myTagClean.length());
 
      // test and compare
      if(myTagClean == "13004C8ED302"){
        Serial.println("White Card"); 
      }
 
      if(myTagClean == "01023C245D46"){
        Serial.println("Keychain"); 
      }
 
    }
 
    // standard char, we add it to the String
    else{
     
      myTag = String (myTag + incoming);
    }
 
  }
  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    // try to congifure using IP address instead of DHCP:
    Ethernet.begin(mac, ip);
  }
  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");
  Serial.println("connected");
  
  // if you get a connection, report back via serial:
  if (client.connect(server, 80)) {
   
    // Make a HTTP request:
    client.print("GET /test/add_data.php?");
    //client.println(myTag);                 
    client.println("myTagClean= ");
    client.println(myTagClean);
    //client.print( "9999999" );
    client.println( " HTTP/1.1");
    client.print( "Host: " );
    client.println(server);
    client.println( "Connection: close" );
    client.println();
    client.println();
    client.stop();
  } 
  else {
    // kf you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}


ReplyQuote
 Hans
(@hans)
Noble Member Admin
Joined: 8 years ago
Posts: 1832
Topic starter  

Let's strip the code a little bit to make it more readable:

#include <SoftwareSerial.h>
#include <SPI.h>
#include <Ethernet.h>
 
SoftwareSerial rfidSerial(2, 3); // (RX, TX)
String myTag, myTagClean;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = {10, 1, 1, 2}; // ip in lan assigned to arduino
//byte gateway[] = {10, 1, 1, 254 }; // internet access via router
byte subnet[] = {255, 255, 255, 0 }; //subnet mask
char server[] = "10.1.1.1";
EthernetClient client;
 
void setup() {
  Serial.begin(9600);
  Serial.println("Hello World : RFID reader, from hardware serial");
  rfidSerial.begin(9600); // communication speed of ID-12 reader
  rfidSerial.println("Hello World : RFID reader, from software serial");
}
void loop(){
  if (rfidSerial.available()) {
   char incoming = (char)rfidSerial.read(); 
 
   if(incoming == 3){
 
      Serial.println("Incoming Tag");
 
      myTagClean = myTag.substring(1, 13);
      myTag = "";      
      Serial.println(myTagClean);
 
      if(myTagClean == "13004C8ED302"){
        Serial.println("White Card"); 
      }
  else if(myTagClean == "01023C245D46"){
        Serial.println("Keychain"); 
      }
 
    }
  else{
      myTag = String (myTag + incoming);
    }
 
  }
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    Ethernet.begin(mac, ip);
  }
  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");
  Serial.println("connected");
  
  // if you get a connection, report back via serial:
  if (client.connect(server, 80)) {
   
    // Make a HTTP request:
    client.print("GET /test/add_data.php?");
    client.println("myTagClean= ");
    client.println(myTagClean);
    client.println( " HTTP/1.1");
    client.print( "Host: " );
    client.println(server);
    client.println( "Connection: close" );
    client.println();
    client.println();
    client.stop();
  } 
  else {
    Serial.println("connection failed");
  }
}

ReplyQuote
 Hans
(@hans)
Noble Member Admin
Joined: 8 years ago
Posts: 1832
Topic starter  

First things I notice:

1) You commented out the network Gateway. Not sure if things will work when you do that.

2) I do not know where rfidSerial is defined, is that from the SoftwareSerial library?

OK, next step: what is happening (or not happening).

Do you get the RFID to work at all? I mean do you get output on the serial monitor?


ReplyQuote
(@tsx111)
New Member
Joined: 5 years ago
Posts: 0
 

Thanks for moving it to the a new topic

1) I'm not sure what do you mean by commenting out of the gateway?

2) It is defined from the SoftwareSerial library. This is where I refer https://www.arduino.cc/en/Tutorial/SoftwareSerialExample

After uploading, the serial monitor shows that it's connected. When I tried to using the RFID tag on the reader, the content of the tag didn't show up. 


ReplyQuote
 Hans
(@hans)
Noble Member Admin
Joined: 8 years ago
Posts: 1832
Topic starter  

No problem ...

Well in your code it says:

//byte gateway[] = {10, 1, 1, 254 }; // internet access via router

which should probably be

byte gateway[] = {10, 1, 1, 254 }; // internet access via router

As for the problem you're running into: so the RFID reading part is not working, or not producing any data.
Do you have working example code?

Normally I'd make sure that at least each item individually works.

So one sketch for RFID reading, to make sure our hardware works.
And one for networking, so we know the network part works.

After both work, we should see how to merge them.
I do however have no experience with RFID readers - I might have one laying around, but I'd have to look for that one.

What mode RFID reader/cards do you have?


ReplyQuote


 Hans
(@hans)
Noble Member Admin
Joined: 8 years ago
Posts: 1832
Topic starter  

I meant: what kind of RFID do you have?


ReplyQuote
(@tsx111)
New Member
Joined: 5 years ago
Posts: 0
 

Actually this line is not in used...

//byte gateway[] = {10, 1, 1, 254 }; // internet access via router

The code below is what I used for reading the RFID tag.

#include <SoftwareSerial.h>
SoftwareSerial rfidSerial(2, 3); // (RX, TX)
String myTag, myTagClean;
 
void setup(){
  Serial.begin(9600);
  Serial.println("Starting Staff Attendance Using RFID...");
 
  // set the data rate for the SoftwareSerial port
  rfidSerial.begin(9600); // communication speed of ID-12 reader
  rfidSerial.println("Starting Staff Attendance Using RFID...");
}
 
void loop(){
 
  if (rfidSerial.available()) {
    char incoming = (char)rfidSerial.read(); 
    if(incoming == 3){
 
      Serial.println("Incoming Tag");
      myTagClean = myTag.substring(1, 13);
 
      // reset myTag
      myTag = "";      
 
      Serial.println(myTagClean);
 
      // test and compare
      if(myTagClean == "13004C8ED302"){
        Serial.println("White Card"); 
      }
 
      if(myTagClean == "01023C245D46"){
        Serial.println("Keychain"); 
      }
 
    }
 
    // standard char, we add it to the String
    else{
      myTag = String (myTag + incoming);
    }
 
  }
 
}


ReplyQuote
(@tsx111)
New Member
Joined: 5 years ago
Posts: 0
 

This is the library that I have been using to make a http request to the wampserver:

//Final Year Project - Japan Malaysian Technical Institute (JMTi)
//Title : Staff Attendance Using RFID
//Written and developed by : Mohammad Syafiq bin Yusaini
//Group members : Tan Soo Xian
// : Siew Johnnie
//Example sketch for RFID ID-20LA reader
//===================================================================
/*
 */
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = {10, 1, 1, 2}; // ip in lan assigned to arduino
//byte gateway[] = {10, 1, 1, 254 }; // internet access via router
byte subnet[] = {255, 255, 255, 0 }; //subnet mask
char server[] = "10.1.1.1";
EthernetClient client;
void setup() {
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    // try to congifure using IP address instead of DHCP:
    Ethernet.begin(mac, ip);
  }
  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");
  // if you get a connection, report back via serial:
  if (client.connect(server, 80)) {
    Serial.println("connected");
    // Make a HTTP request:
    client.print("GET /test/add_data.php?");
    //client.println(myTag);                 
    client.print("myTag=");
    client.print( "5665656" );
    client.println( " HTTP/1.1");
    client.print( "Host: " );
    client.println(server);
    client.println( "Connection: close" );
    client.println();
    client.println();
    client.stop();
  } 
  else {
    // kf you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}
void loop()
{
  // if there are incoming bytes available 
  // from the server, read them and print them:
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }
  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    // do nothing forevermore:
    while(true);
  }
}

I have tried out both of the libraries separately and both of them are working fine. The problem now is to merge of the libraries. 


ReplyQuote
 Hans
(@hans)
Noble Member Admin
Joined: 8 years ago
Posts: 1832
Topic starter  

I'd first start with a clean ethernet sketch, something like the code below, which tries to submit a tag, waits 10 seconds (so we do not keep pushing data repeatedly to the server).

#include <UIPEthernet.h> // Used for Ethernet
// * ETHERNET SETTING *
// Arduino Uno pins: 10 = CS, 11 = MOSI, 12 = MISO, 13 = SCK
// Ethernet MAC address - must be unique on your network - MAC Reads T4A001 in hex (unique in your network)
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// For the rest we use DHCP (IP address and such)
char server[] = "192.168.1.100"; // IP Adres (or name) of server to dump data to
byte ip[] = {10, 1, 1, 2}; // ip in lan assigned to arduino
byte gateway[] = {10, 1, 1, 254 }; // internet access via router
byte subnet[] = {255, 255, 255, 0 }; //subnet mask
char server[] = "10.1.1.1";
EthernetClient client;
void setup() {
  Serial.begin(9600);
  Ethernet.begin(mac);
  Serial.print("IP Address : ");
  Serial.println(Ethernet.localIP());
  Serial.print("Subnet Mask : ");
  Serial.println(Ethernet.subnetMask());
  Serial.print("Default Gateway IP: ");
  Serial.println(Ethernet.gatewayIP());
  Serial.print("DNS Server IP : ");
  Serial.println(Ethernet.dnsServerIP());
}
void loop() {
  // if you get a connection, report back via serial:
  if (client.connect(server, 80)) {
    Serial.println("-> Connected");
    // Make a HTTP request:
    client.print( "GET /testserver/arduino_temperatures/add_data.php?");
    client.print("myTagClean= ");
    client.print("123456"); // just a test value
    client.println( " HTTP/1.1");
    client.print( "Host: " );
    client.println(server)
    client.println( "Connection: close" );
    client.println();
    client.println();
    client.stop();
  }
  else {
    // you didn't get a connection to the server:
    Serial.println("--> connection failed/n");
  } delay(10000); // wait 10 seconds so it doesn't overload the server }

ReplyQuote
 Hans
(@hans)
Noble Member Admin
Joined: 8 years ago
Posts: 1832
Topic starter  

Just notice a mistake in that code:

client.print( "GET /testserver/arduino_temperatures/add_data.php?");

should be:

client.print("GET /test/add_data.php?");

ReplyQuote


 Hans
(@hans)
Noble Member Admin
Joined: 8 years ago
Posts: 1832
Topic starter  

I also noticed the use of client.println when writing to the ethernet connection, which probably should have been a client.write (see the example I just posted).

Anyhow, next step is to take the RFID example and bring the reading part out of the loop, something like this (I cannot test this). Note that this can be done more efficient, I'm just trying to illustrate how I'd normally would merge two sketches.

I hope I did everything OK with the boolean return value of ReadRFID(). It compiles allright though. (please test)

#include <SoftwareSerial.h>
SoftwareSerial rfidSerial(2, 3); // (RX, TX)
String myTag, myTagClean;
 
void setup(){
  Serial.begin(9600);
  Serial.println("Starting Staff Attendance Using RFID...");
 
  // set the data rate for the SoftwareSerial port
  rfidSerial.begin(9600); // communication speed of ID-12 reader
  rfidSerial.println("Starting Staff Attendance Using RFID...");
}
 
void loop(){
  if(ReadRFID()) {
    if(myTagClean == "13004C8ED302"){
      Serial.println("White Card"); 
    }
    else if(myTagClean == "01023C245D46"){
      Serial.println("Keychain"); 
    }
  }
 
}
boolean ReadRFID() {
  if (rfidSerial.available()) {
    char incoming = (char)rfidSerial.read(); 
    if(incoming == 3){
      Serial.println("ReadRFID: Incoming Tag");
      myTagClean = myTag.substring(1, 13);
      myTag = "";      
      Serial.println(myTagClean);
     return true;
    }
    else{
      myTag = String (myTag + incoming);
      return false;
    }
  }
}

ReplyQuote
 Hans
(@hans)
Noble Member Admin
Joined: 8 years ago
Posts: 1832
Topic starter  

If the function ReadRFID() worked (in the previous example/test), the the code could look something like the code below.

We setup all the Ethernet and RFID stuff, and keep calling the ReafRFID() function (a delay in between might be desired)

(I noticed another mistake in the previous code: remove the first line with "char server" - it's in there twice, which is wrong)

#include <UIPEthernet.h> // Used for Ethernet
#include <SoftwareSerial.h> // Used for RFID
// ETHERNET
// Arduino Uno pins: 10 = CS, 11 = MOSI, 12 = MISO, 13 = SCK
// Ethernet MAC address - must be unique on your network - MAC Reads T4A001 in hex (unique in your network)
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = {10, 1, 1, 2}; // ip in lan assigned to arduino
byte gateway[] = {10, 1, 1, 254 }; // internet access via router
byte subnet[] = {255, 255, 255, 0 }; //subnet mask
char server[] = "10.1.1.1";
EthernetClient client;
// RFID
SoftwareSerial rfidSerial(2, 3); // (RX, TX)
String myTag, myTagClean;
boolean ReadRFID() {
  if (rfidSerial.available()) {
    char incoming = (char)rfidSerial.read(); 
    if(incoming == 3){
      Serial.println("ReadRFID: Incoming Tag");
      myTagClean = myTag.substring(1, 13);
      myTag = "";      
      Serial.println(myTagClean);
    return true;
    }
    else{
      myTag = String (myTag + incoming);
      return false;
    }
  }
}
void setup() {
  Serial.begin(9600);
  Ethernet.begin(mac);
  Serial.print("IP Address : ");
  Serial.println(Ethernet.localIP());
  Serial.print("Subnet Mask : ");
  Serial.println(Ethernet.subnetMask());
  Serial.print("Default Gateway IP: ");
  Serial.println(Ethernet.gatewayIP());
  Serial.print("DNS Server IP : ");
  Serial.println(Ethernet.dnsServerIP());
  
  rfidSerial.begin(9600); // communication speed of ID-12 reader
  rfidSerial.println("Starting Staff Attendance Using RFID...");
}
void loop() {
  // if RFID read an ID ...
  if(ReadRFID()) {
    // ... and if we get a connection, send the RFID code to the server:
    if (client.connect(server, 80)) {
      Serial.println("-> Connected");
      // Make a HTTP request:
      client.print( "GET /test/add_data.php?");
      client.print("myTagClean= ");
      client.print(myTagClean); // <--- Maybe let the server decide to do with that number?
      client.println( " HTTP/1.1");
      client.print( "Host: " );
      client.println(server);
      client.println( "Connection: close" );
      client.println();
      client.println();
      client.stop();
    }
    else {
      // you didn't get a connection to the server:
      Serial.println("--> connection failed/n");
    }
  }
}

Note that I moved the ReadRFID() function to the front, seems required so the compiler knows the function definition properly. This means that it should have failed in the previous example as well,... Like I said: I cannot really test any of this ... 

Also note that gateway definition is probably needed for Ethernet to work properly.


ReplyQuote
(@tsx111)
New Member
Joined: 5 years ago
Posts: 0
 

Thanks for helping. I tried the code that had been merged. The RFID tag did show in the serial monitor but it sent an empty input to the database.

<?php
    // Connect to MySQL
    include("conn.inc");
    // Prepare the SQL statement
    $SQL = "INSERT INTO test(id) VALUES ('".$_GET["myTagClean"]."')";     
    // Execute SQL statement
    mysql_query($SQL);
?>


ReplyQuote
 Hans
(@hans)
Noble Member Admin
Joined: 8 years ago
Posts: 1832
Topic starter  

Maybe it's the space between table name (test) and field name (id):

<?php
    // Connect to MySQL
    include("conn.inc");
    // Prepare the SQL statement
    $SQL = "INSERT INTO test (id) VALUES ('".$_GET["myTagClean"]."')";     
    // Execute SQL statement
    mysql_query($SQL);
?>

ReplyQuote
(@tsx111)
New Member
Joined: 5 years ago
Posts: 0
 

Thanks. After removing the space made it work.


ReplyQuote


Page 1 / 2

Like what you see and you'd like to help out? 

The best way to help is of course by assisting others with their questions here in the forum, but you can also help us out in other ways:

- Do your shopping at Amazon, it will not cost you anything extra but may generate a small commission for us,
- send a cup of coffee through PayPal ($5, $10, $20, or custom amount),
- become a Patreon,
- donate BitCoin (BTC), or BitCoinCash (BCH).

Share: