We have launched a new Community! Please post any new forum topics there. This forum will remain available as an archive, but we hope you enjoy the new experience and feature set the latest NCD Community will offer!

EM-18 RFID

EM-18 is great. (But be aware: this is not an i2c device.)

This simple example for Arduino works for Photon: https://electrosome.com/em-18-rfid-reader-arduino-uno/

But you have to do these 4 little fixes to get it work with Particle Photon:

1. Add in setup a line "Serial1.begin(9600);"

 

2. REPLACE "if(Serial.available())" WITH "if(Serial1.available())"
3. REPLACE  "while(Serial.available() && count < 12)" WITH  "while(Serial1.available() && count < 12)"        
4. REPLACE  "input[count] = Serial.read();" WITH "input[count] = Serial1.read();"

 

 

 

B

Thanks Jakob.

Yes RFID is a fun project.

We are working on codes for it. 

We have an article on instructable about it, which you can find over here

http://www.instructables.com/id/Interface-RFID-Shield-With-Arduino/

thanks

 

Dale Adams's picture

I am unable to get this working with the code above using EM18+Particle+SparkI2C Board+StackedHeader REV.A.

I have also been attempting with the SeeedRFID library and gotten recognition that it's working but incorrectly. I have changed the rfiduart.ino to what is below. I get an output but it's one number at a time till it gets to the last number then it stops. Can't seem to figure this out and more than likely it's something simple that I'm over looking. Any help would be appreaciated.

// RFID_UART.ino

#if !defined (PARTICLE)
#include <SoftwareSerial.h>
#endif
#include <SeeedRFID.h>

#define RFID_RX_PIN 10
#define RFID_TX_PIN 11

#define DEBUG
#define TEST
#define PARTICLE

SeeedRFID RFID(RFID_RX_PIN, RFID_TX_PIN);	// Serial1 used for Particle
RFIDdata tag;

void setup() {
    Serial.begin(9600);
	Serial1.begin(9600);
	Serial1.println("Hello, double bk!");
}

void loop() { 
	if(RFID.isAvailable()){
		tag = RFID.data();
		Serial1.print("RFID card number: ");
		Serial1.println(RFID.cardNumber());
#ifdef TEST
	Serial1.print("RFID raw data: ");
	for(int i=0; i<tag.dataLen; i++){
	    Serial1.print(tag.raw[i], HEX);
	    Serial1.print('\t');
		}
#endif
	}
}

SeeedRFID:read() function, and the RFID raw data:

30
41
30
30
37
31
34
37
32
33
31
46

 

T

Hi Dale,

Would it be possible for you to post a photo of your hardware setup so we can see how everything is hooked up?

Dale Adams's picture

Here ya go! It's connected to my 2008 MacBook Pro at the moment.

 

T

Hi Dale,

The first thing that jumps out is the RFID scanner in this setup is actually connected to Serial1.  Serial is connected to the USB port on the Photon so that is where you should do your prints.  When your code prints to Serial1 it is sending that to the RFID scanner.  You will more than likely need to alter the RFID library you are using to use Serial1 for interfacing with the RFID scanner.

If that does not do it please let me know.

Thank you Dale,

Travis Elliott

Dale Adams's picture

The code I posted above is incorrect then? Sorry if I'm misunderstanding.

T

Try this code(warning untested but should work) It will publish an event to the Particle cloud which you can monitor at https://console.particle.io/logs:

void setup(){

     Serial1.begin(9600);

}

void loop(){

    if(Serial1.available() != 0){
        delay(50);
        if(Serial1.available() == 12){
            int fobID[12];
            for(int i = 0; i < 12; i++){
                fobID[i] = Serial1.read();
            }
            sprintf(fob, "%d %d %d %d %d %d %d %d %d %d %d %d", fobID[0], fobID[1], fobID[2], fobID[3], fobID[4], fobID[5], fobID[6], fobID[7], fobID[8], fobID[9], fobID[10], fobID[11]);
            Particle.publish("RFID", fob);
            delay(100);
        }
    }

}

Dale Adams's picture

 error: 'fob' was not declared in this scope

 

I added just in case:

#define fob 

char fobID[12];

Still getting this which I can't seem to figure out. 

error: expected primary-expression before ',' token
             sprintf(fob, "%d %d %d %d %d %d %d %d %d %d %d %d", fobID[0], fobID[1], fobID[2], fobID[3], fobID[4], fobID[5], fobID[6], fobID[7], fobID[8], fobID[9], fobID[10], fobID[11]);

 

 

 

 

Dale Adams's picture

Serial.print(); is printing to the command line now using

particle serial monitor

Ended up going back to the original with an added line or two.

int count = 0;                                          // count = 0
char input[12];                                         // character array of size 12 
boolean flag = 0;                                       // flag =0
void setup()
{
   Serial.begin(9600);
   Serial1.begin(9600);                                  // begin serial port with baud rate 9600bps
}
void loop()
{
   if(Serial1.available())
   {
      count = 0;
      while(Serial1.available() && count < 12)         // Read 12 characters and store them in input array
      {
         input[count] = Serial1.read();
         count++;
         delay(5);
      }
      Serial1.print(input);                             // Print RFID tag number 
      Serial.print(input);
      Particle.publish("RFID", input);
      
      if((input[0] ^ input[2] ^ input[4] ^ input[6] ^ input[8] == input[10]) && 
         (input[1] ^ input[3] ^ input[5] ^ input[7] ^ input[9] == input[11]))
            Serial.println(" No Error");
      else
            Serial.println("Error");      
   }
}

 

T

That works.

Dale Adams's picture

Hey Trevor, have you ever run into the problem with MQTT interfering with the RFID reading. If I run the script below everything works fine and I get the correct RFID number but when I turn on the MQTT connection with just 

client.connect("sparkclient"); 

in void setup only

I get an extra 2 characters like this: 0A007147231F�0

Any idea what could be causing this?



#include "MQTT.h"
#include "blynk.h"

#define PIXEL_COUNT 60
#define PIXEL_PORT D2
#define PIXEL_TYPE WS2812B
#define BLYNK_RED "#FF0000"
#define BLYNK_WHITE "#FFFFFF"
#define BLYNK_GREEN "#008000"
#define BLYNK_BLUE "#0000FF"

int count = 0;                                          // count = 0
char input[12];                                         // character array of size 12 
boolean flag = 0;                                       // flag =0

byte server[] = { 172, 24, 1, 1 };
MQTT client(server, 1883, callback);

char auth[] = "auth_token_here";

// recieve message
void callback(char* topic, byte* payload, unsigned int length) {
    char p[length + 1];
    memcpy(p, payload, length);
    p[length] = NULL;
/*
    if (!strcmp(p, "RED"))
        RGB.color(255, 0, 0);
    else if (!strcmp(p, "GREEN"))
        RGB.color(0, 255, 0);
    else if (!strcmp(p, "BLUE"))
        RGB.color(0, 0, 255);
    else
        RGB.color(255, 255, 255);
    delay(1000);
  */  
}


void setup()
{
   Serial.begin(9600);
   Serial1.begin(9600);                                  // begin serial port with baud rate 9600bps
   
   //client.connect("sparkclient");
   Blynk.begin(auth);
   
   Blynk.setProperty(V9, "color", BLYNK_WHITE);

}

void AirlockControl(){
   if(Serial1.available())
   {
      count = 0;
      while(Serial1.available() && count < 12)         // Read 12 characters and store them in input array
      {
         input[count] = Serial1.read();
         count++;
         delay(5);
      }
     
      Particle.publish("RFID Access Request ", input);
     /* 
      if (client.isConnected()) {
        client.publish("rfid/access/","rfid request");
        }*/
      Blynk.setProperty(V9, "color", BLYNK_BLUE);   
      delay(1000);
      
      
      if ( strcmp(input,"0A007147231F") == 1) {
          
              Particle.publish("RFID Access Granted ", input);
              //client.publish("rfid/access/","access granted");
              
              Blynk.setProperty(V9, "color", BLYNK_GREEN);
      }else{
              Particle.publish("RFID Access Denied ", input);
              //client.publish("rfid/access/","access denied");
              
              Blynk.setProperty(V9, "color", BLYNK_RED);
              delay(2000);
              Blynk.setProperty(V9, "color", BLYNK_WHITE);
      }
   } 
}
 
void loop()
{
    //if (client.isConnected())
    //    client.loop();
        
    AirlockControl();
    Blynk.run();
}