USB Rfid reader connected to yun's host usb port

Hi everyone,

I am a total newbie in the arduino world and would like some simple guidance.

I aquired an USB RFID kit from sparkfun that I connect to the host port of the arduino.

I installed the appropriate drivers and now I am able to do 'cat /dev/ttyUSB0' and see the reads in the terminal (logged in via ssh to linino).

I would like to know how I can write a sketch that can read this device. I imagine I would have to do some python coding on the linino side as well, but I am lost as to where/how to start. maybe anyone has some example...

I think I should use the Process library, and have it run a python script that polls for reads. However, I also have a TFT display that I want to have some responsiveness, so I can't do a "blocking" process.run() and have the python script loop until it gets an rfid read. And the process.runAsynchronously is something I haven't quite grasped how it works (no example??)

Back in the days I used to play around with PICs 16f84 I would just use interrupts, but I have no idea how I could do something like this here...

Thanks so much for the attention everyone!!! :slight_smile:

Update:

I managed to get a small PoC running.

I had to “opkg install pyserial” before starting, btw.

The python script:

#!/usr/bin/python

import serial
import sys
sys.path.insert(0, '/usr/lib/python2.7/bridge/')

from bridgeclient import BridgeClient as bridgeclient
                                                    
bridgecli = bridgeclient()

ser = serial.Serial('/dev/ttyUSB0', timeout=1)  # open serial port

while 1 == 1:
  rfidcode = ser.readline()
  rfidcode = rfidcode.rstrip()
  if len(rfidcode) > 1: # I don't know why but with len(rfidcode)>0, after it read the first time it would send an empty string...
    bridgecli.put('rfidcode', rfidcode)
ser.close()             # close port

And the sketch on the arduino

#include <Process.h>
#include <TouchScreen.h>
#include <TFTShield.h>
#include <Bridge.h>


// Touchscreen calibration.
// You don't need to change these values.
#define TS_MINX 140
#define TS_MAXX 900
#define TS_MINY 120
#define TS_MAXY 940

// display configuration.
// You don't need to change these values.
#define FG_COLOR        GREEN

// GLOBAL VARS
TouchScreen ts = TouchScreen(A3, A2, A1, A0, 300);

char rfidCode[14];
char rfidLastRead[14];

void setup() {
  // Start the bridge.
  Bridge.begin();

  // Initialize display and set orientation.
  Tft.init();
  Tft.setDisplayDirect(UP2DOWN);
  
  // Display a loading message.
  Tft.drawString("LOADING...", MAX_X - 20, 20, 1, FG_COLOR);
  
  startRfidReader();
  
  draw_display(); 
}

void loop() {
  int len = 0;
  len = Bridge.get("rfidcode", rfidCode, 14);
  if (len > 0) {
    strcpy(rfidLastRead, rfidCode);
    draw_display();
    Bridge.put("rfidcode","");
  }
  // Short delay to keep touchscreen checks responsive.
  delay(5);
}

void startRfidReader() {
  Process p;
  p.runShellCommandAsynchronously("python /mnt/sda1/arduino/donato/rfid/rfid.py");
}

// Draw the display for the specified time and alarm time.
void draw_display() {
  Tft.paintScreenBlack();
  Tft.drawString(rfidLastRead, MAX_X - 10, 10, 2, FG_COLOR);
}

I am however pretty disappointed with speed, I wanted to have a very fast rfid reader so I could quickly do inventory at my store. I think I will have to think about bypassing the usb altogether and maybe having the RFID board talk directly to the arduino – in which case I will probablu downgrade to a UNO (cheaper than the Yun) and get a new TFT (faster than Seeedstudio’s V1.0 I am currently using) that has an SD card input that I could store the scans.

Wow, this arduino is kind of addictive stuff… On to making a Facebook like counter using Temboo!

I am however pretty disappointed with speed, I wanted to have a very fast rfid reader so I could quickly do inventory at my store. I think I will have to think about bypassing the usb altogether and maybe having the RFID board talk directly to the arduino -- in which case I will probablu downgrade to a UNO (cheaper than the Yun) and get a new TFT (faster than Seeedstudio's V1.0 I am currently using) that has an SD card input that I could store the scans.

The reader you have is UART interface, USB's speed is 480Mb/s and UART's speed is 0.5 Mb/s. The USB might not be bottleneck.

The tag you use is LF (125Khz), now days the HF tag (NFC tag) become more popular and low cost.

Plan B:

Use NFC enable Android phone ( I pickup one at $40 on Black Friday)

Load NFC Inventory software,

https://play.google.com/store/apps/details?id=hlv.cnst.inventory_nfc&hl=en

read UID of tags and export to CSV file when scanning completely ( by pressing "Stop Scanning" button) automatically.
You can share exported data file via Skype, Gmail...

Dear @sonnyyu,

Thank you for the comments! Indeed the bottleneck being uart makes sense.

I will look into the NFC option indeed.

regards,

Hello,

I have a similar situation, thanks for the code I use it. Very useful for me.
But I need to send commands to the rfid how to do it correctly?

I make the array of bytes in Arduino sketch

byte readerConnection[] = {0xA0, 0x03, 0xFF, 0x72, 0xE3};

And add line to python code

ser.write('readerConnection', readerConnection)

So now is'look like that

#!/usr/bin/python

import serial
import sys
sys.path.insert(0, '/usr/lib/python2.7/bridge/')

from bridgeclient import BridgeClient as bridgeclient
                                                    
bridgecli = bridgeclient()

ser = serial.Serial('/dev/ttyUSB0', timeout=1)  # open serial port
ser.write('readerConnection', readerConnection)

while 1 == 1:
  rfidcode = ser.readline()
  rfidcode = rfidcode.rstrip()
  if len(rfidcode) > 1: 
    bridgecli.put('rfidcode', rfidcode)
ser.close()             # close port