Radio-frequency identification (RFID) is a popular way to transfer an ID code wirelessly by using electromagnetic fields. This technology is often put to use in projects because it is so common in the industry. The RFID tags can be used to identify employees or objects, and it is fairly inexpensive to implement. We will create a script by using the Phidgets RFID reader to move a servo, which will symbolize unlocking a door. The following image shows the RFID reader that I will be using for this example:
We will use the same Ruby gems that we imported for the last example. Note the similarities with the previous script. This shows how dynamic the code that we are using can be.
We will start by requiring the gems that we need, as we did previously. This is necessary to interact with the RFID reader and control the servo:
#!/usr/bin/ruby require 'rubygems' require 'phidgetsffi'
We will define the RFID reader and servo in order to use them, as follows:
puts "Library Version: #{Phidgets::FFI.library_version}" puts "Waiting for Phidgets..." # Define the phidgets rfid = Phidgets::RFID.new adv = Phidgets::AdvancedServo.new
We will now handle the servo functions, as follows:
adv.on_attach do |device, obj| puts "Servos Initialized" device.advanced_servos[0].engaged = true sleep 1 end adv.on_detach do |device, obj| puts "#{device.attributes.inspect} detached" end adv.on_error do |device, obj, code, description| puts "Error #{code}: #{description}" end
We will also need to handle the on_attach
function for the RFID reader. This is going to enable the antenna and the LED to indicate that it is switched on. The antenna will listen for the chip and read the code that comes with it:
rfid.on_attach do |device, obj| puts "RFID Activated" rfid.antenna = true rfid.led = true sleep 1 end
We will want to actually work with the tag when it is presented. The tag can be a card, coin, or really anything with the actual RFID chip inside it. The on_tag
function will be as follows:
rfid.on_tag do |device, tag, obj| # Interact with the servo if tag == "4d004b113e" puts "Authorized for #{tag}" 3.times do max = adv.advanced_servos[0].position_max adv.advanced_servos[0].position = rand(max) sleep 0.5 end else puts "Tag #{tag} unauthorized" end end
We will also handle the on_tag_lost
function. We won't do much in this function. We will just provide an output that notifies that the tag was removed, as follows:
rfid.on_tag_lost do |device, tag, obj| puts "Tag #{tag} removed" end
We will now wait for the tag to be presented:
# Waiting for the RFID puts "Present ID tag....." gets.chomp
What the on_attach
function is doing is this—when a tag is presenting to the antenna, it triggers the function. The tag is evaluated to see whether it equals 4d004b113e
. Then we will move the servo three random times.
This can be easily modified to evaluate the tag against a company database, authenticate a user, and then open a locked door. We will finish our script by closing the connection to the servo controller and RFID reader, as follows:
# Closing the connection puts 'DONE' rfid.close sleep 2 adv.advanced_servos[0].engaged = false sleep 1 adv.close
3.141.47.163