Projects

Distributed RFID

Oliver Higgins

Issue 6, December 2017

This article includes additional downloadable resources.
Please log in to access.

Log in

Expanding a straight forward concept to allow broader applications of RFID for access control.

The Internet of Things (IoT) continues to be central in the maker space, but how simple is it to build something that we can interact with? How do we create an IoT environment when we don’t have an internet?

In previous issues, we have looked at an Arduino-powered alarm system. The system was quite complex but performed some great functions to educate and give insight into how alarms and IoT worked. We wanted to take some of those fundamental ideas and develop a bare bones setup that could provide remote access to the user and set up a platform that could serve to create a distributed network.

THE BROAD OVERVIEW

Our primary goal of this project is to create a system by which we can unlock a door remotely. This can be through the RFID system or through the internet to enable remote unlocking. You may have lost your keys or even left them inside the house – you know, on the back of the door so you don’t forget them! Or you may have a friend who needs to drop something over or check up on your cat while you’re away on holidays. This system will ensure you don’t have to give out any keys and even enables you to remotely log in when somebody accesses your locking mechanism!

For this particular project, we’re using the ESP8266, specifically the NodeMCU development kit. The microcontroller monitors two aspects for us: first, the RFID unit that is connected to the SPI pins. It also monitors any incoming clients via the internet. When we swipe a known card or activate the unlock function via the web app, the NodeMCU will activate a relay that will then trigger off a door strike.

NodeMCU diagram

HOW IT WORKS

NODEMCU

The NodeMCU enables us to build a small standalone RFID and door strike, which we can control with our mobile device. The NodeMCU is an open source development platform built around the ESP8266. The ESP8266 is a low-cost WiFi module that comes in various guises depending on your requirements. One of the most difficult and highly googled aspects of the ESP8266 is programming or flashing it. If you have used any of the Arduino microcontrollers before you will know it’s as simple as clicking the upload button. To this effect, the ESP8266 is not that simple. It requires specific pins to be pulled low and then programming can take place using a TTL-USB interface. For a beginning user, this can be daunting. The most common and low cost ESP8266 module is the ESP01, which only has a small number of GPIO. This is where the NodeMCU shines. Once you have set up the ESP8266 for Arduino, you can use the NodeMCU with WiFi as if you were programming any other board in Arduino. In addition, the board itself deals with which pins need to be pulled, making the act of programming the unit automatic. A couple of things of note: the NodeMCU runs on 3.3V. While it has a 5V in line, all the connected devices must work within this range. Also, the pin assignment is not the same as the Arduino; you will find that the GPIO numbers used in Arduino do not correspond directly to the numbers marked on the development kit.

For this project, we will look at two variations. The first will act as part of our home network, connecting to your existing infrastructure. We can then access it via a browser or your phone. It will also be accessible to trigger via the RFID. We will then set up the NodeMCU to work in Access Point mode. This uses the same circuit; however, it does not interface with the RFID as we will use it to automatically “detect” when a client connects and disengages the door.

When being utilised in the network via the traditional method of connecting to your router or access point he NodeMCU and operate as both a client and a server. It will depend on your project design as to which would be the most appropriate, but we would use it in a server mode when its receiving information or commands from devices. In this case, it will be from any of the devices on our network that can send an HTTP request. This is the implementation that we will be using in this case as it provides a simple application that can be easily customised for other projects that you may choose to undertake in the future. Another option is to use MQ Telemetry Transport (MQTT). It is a publish/subscribe, extremely simple and lightweight messaging protocol, designed for constrained devices and low-bandwidth, high-latency or unreliable networks. MQTT offers a clean and simple code interface, but it does require the use of a broker. This limits what you can implement in a closed environment unless you are prepared to set up the required infrastructure.

This standalone version enables us to take advantage of the ESP8266’s cloud and interface, using the same web technologies. When acting as a standalone unit, the system runs in a loop checking for two things; firstly, has the RFID scanner been swiped since the last loop? If it has then is the card ID one that matches our list? If it does, then activate our green LED and trip the relay. If the ID is not on the list then our red LED lights up and the system reverts to the loop. It continues and checks to see if there is currently a client connected via the network, and if there is, it checks the stream to see if there is a current request to parse. If it corresponds to the command for “unlock”, then the system LED goes green, and the door strike is triggered.

RFID

Radio Frequency Identification (RFID) uses electromagnetic fields to automatically identify and track tags attached to objects. The technology itself can be classed as active and passive IDs, with passive IDs used in a situation such as door swipes or tap-and-go credit card transactions. These tags use the nearby RFID reader’s radio waves to collect energy and use it to power the transmission of the encoded data. The RFID unit used in this project is the RFID-RC522. This is a very common unit designed for interfacing with microcontrollers such as the Arduino. The unit is capable of reading from a very close distance, and you can even embed data on the tags.

The technology itself is built on Soviet cold war research and is now used in so many parts of our lives, some so small you would not even know you are using it. Hitachi manufacture a unit .01 inches square. They have even been used by researchers to infiltrate the close huddles of penguins in the Antarctic. How? Remote control control RFID readers disguised as penguins. Internet of Penguins anyone?

ACCESS POINT

The ESP8266 can also act as an access point. In this context, it allows us to set up a network in an environment that may not have WiFi or a network connection. In the context of this project, we can exploit the system’s ability to inform us of the current clients that are connected. We will setup a network SSID and password, and once the system identifies that it has an active connection, then it will trigger the door strike. To apply this to the real world, we would connect our phone or device to the same network connected to the door strike, and make sure it connects itself automatically. This way when we come home from work and the phone reconnects itself to the network, the door automatically unlocks itself. Once we leave home and the phone disconnects, the door strike reengages and the door is now locked.

The ESP8266 is able to give us the total number of clients attached, their IP address and even the MAC address. This information could prove invaluable in setting up a bidirectional protocol, but in the the context of this project it means that we are able to isolate a particular device from another. Using this idea we can set out particular functions such as open the garage door when device A connects or turn on a external light when device B connects. It also means that we can datalog and track the movements of devices as they come in contact with our circuit.

Node MCU diagram

THE BUILD

Parts Required: Jaycar Altronics
1 x NodeMCU - -
1 x RFID-RC522 XC4506 Z6356
1 x Red LED ZD0100 Z0700
1 x Green LED ZD0120 Z0701
1 x 1K Resistor RR0572 R7558
1 x 1N4148 Diode ZR1100 Z0101
1 x 2N3904 Transistor ZT2326 Z1200
1 x Relay XC4419 Z6325
1 x Door Strike LA5078 S5385
NODEMCU CIRCUIT
D0 LED01
D1 LED02
D2 Relay
D3 RST
D4 SDA(SS)
D5 SCK
D6 MISO
D7 MOSI

The circuit is based on the NodeMCU. Depending on how you choose to implement this circuit you will need to have a voltage regulator on the circuit. Be mindful that this circuit is based upon a 3.3V system. If you are powering it up with the USB during the development stage, there is enough power to run the NodeMCU, the RFID scanner and the LEDs. The relay will not switch on at 3V, so we need to use a transistor to switch the relay. The relay circuit will work as described, but we have not specified a relay, as it will depend on your input voltages and the door strike voltages to make the right selection for your relay.

Schematic if using standard relay
Schematic if using standard relay.
Wiring diagram using relay module
Wiring diagram using relay module (fitted with transistor and diode).

Once you have placed the NodeMCU into the breadboard place the RFID unit nearby. Depending on the unit you have, you may need to solder the headers in place. If so, remember to do it so that your pin assignment is facing up, as this makes it easier to connect. Once in place, connect the RST to D3, SDA (or SS depending on the unit) to D4, SCK to D5, MISO to D6 and MOSI to D7. When in place, connect the G=green LED to D0 – given this is 3.3V we can get away with not using a resistor. Repeat for the red LED to D1. Next, we have the option to add a third LED to D2, to visualise the relay output or connect the transistor/relay circuit and door strike. We would recommend using the LED initially, at least until you have completed the setup.

THE CODE

STAND ALONE

To setup the standalone access to the web server, we need to specify the SSID and password of your wireless network:

const char *ssid = "XXXXX"; 
const char *pass = "XXXXX";

Next, we need to set up the RFID Cards. If you run the dumpinfo.ino file that is included in the MRF422 library you will get the RFID UID. Otherwise, upload the 006_DIST_RFID_007_Server.ino file.

Once uploaded, open the serial monitor and scan a card. The card will be “invalid” as far as the project is concerned, but it will echo the UID to the screen. Copy and paste this into the sketch. Be mindful of adding a “,” and the “ around the variables used in the array. This is all that is required to set up the code itself.

String keyValues[2] = { "138:111:55:217", "190:135:192:73" } 

We then setup the RFID unit; this is the initiation of the SPI bus.

SPI.begin();         // Init SPI bus
mfrc522.PCD_Init();  // Init MFRC522

Next we start the WIFI server. The code will attempt to connect 10 times with a half-second delay between each one. Once we are connected the details of the connection are sent to the serial monitor.

WiFi.begin(ssid, pass);
int retries = 0;
while ((WiFi.status() != WL_CONNECTED) && (retries < 10)) {
  retries++;
  delay(500);
  Serial.print(".");
}
if (WiFi.status() == WL_CONNECTED) {
  Serial.println(F("WiFi connected"));
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
}
// Start the server
server.begin();

The system is now active and the main loop is in place. This loop checks both the RFID for new card scans and client connections.

We scan the card and then activate the serial bus to pull the data from the card. We have included the dumbbytearray() function in the code to drop some of the data to the serial monitor to show what data is being processed.

if ( ! mfrc522.PICC_IsNewCardPresent() ) { 
  delay(50); 
  return; 

if ( ! mfrc522.PICC_ReadCardSerial() ) { 
  delay(50); 
  return; 

To check the network, we have created the chkEthernet array. This checks to see if there is a current client connected. If it is, then we read the request char array and look for a particular string. In this case, it is the “?undefined=U”.

if (req.indexOf("?undefined=U") != -1) 
  //Unlock Strike 
  doorStrike(); 

The system then loops back onto itself and repeats the process.

WIRELESS ACCESS POINT

At the very beginning of the Arduino file, you will find something that is not all that common. We need to wrap our include for the ESP8266 specific library to make sure the include is a plain C, not C++. To do this, we need to use the "extern C" function.

extern "C" {
  #include<user_interface.h>
}

But why do we need to do this? The library, in particular, is built using C. While the syntax of C and C++ are very similar there are considerable differences in the way the compiler engine works. At a fundamental level, C is a procedural language and as such does not support classes and objects.

So while Arduino is in C++, the Espressif SDK is in plain C. This means when we want to use a specific library, in this case, the user_interface we need to be explicit in that we are calling a C library.

As with any ESP8266 based unit, the NodeMCU can work as a standalone access point. This is how many multimedia devices will communicate with your phone, as the data speeds are much higher than is possible with Bluetooth. We can modify the current installation to give us an automated door lock; you could easily connect anything you like, to automatically switch on or off upon connection – but it’d be pretty cool to get home and have your door unlock automatically and then relock itself when you leave!

Rather than setting the NodeMCU (Or ESP8266) as a server, we change the access setup:

#include <ESP8266WiFi.h>

To the following:

#include <ESP8266WebServer.h>

If alarm bells are starting to ring because security is an issue for you, then we need to be mindful that the authentication takes place the first time you connect your phone to the AP. Yes, this means that after that, the system will act as soon as your phone or device connects. If you are concerned, then make the password more complex, or do not attach this project to anything of significance or value.

We set up the password with the following (noting you can leave it as nothing if you just want to connect without a password):

const char *ssid = "DIYODE";
const char *password = "DIYODE1234";
ESP8266WebServer server(80);

In setup, just need the minimum to start the process:

WiFi.softAP(ssid, password);
server.on("/", handleRoot);
server.begin();

Once this is complete then our AP is ready to go.

At the simplest level, we have two things left to do. We need to find out if we have anyone connected and if we do then let’s do something. We make an assumption here that if somebody has connected to the AP, then they have privileges to activate the system.

To work out if we have anybody connected, we call this from the main loop.

stat_info = wifi_softap_get_station_info();
  int number_client;
  number_client = wifi_softap_get_station_num();
  if (number_client > 0) {
    Serial.println("Connected...");
    digitalWrite(D2, HIGH);
  }else{
    digitalWrite(D2, LOW);
  }

If you are using the previous circuit, then you do not need to worry about the RFID still being connected as it is not utilised at all in this sketch. If you are just using the built-in LED then you can change D2 to D0.

Open up the serial monitor, and you will be presented with the current IP:

Configuring access point...AP IP address: 192.168.4.1
HTTP server started

This way whenever you get home, or your phone connects, the door strike will activate and let you enter. It will remain active until the device disconnects and then closes the door. On a practical level, you would probably not use this in a home environment; however, it does show how easily you can interface with a device. We have included a simple web landing page, that you can access in your browser and deliver information to the user or even add a settings page. The NodeMCU also includes the SPIFFS file system for the internal memory. This is a great starting point to add data logging to your project.