Add some security to your projects

Node-RED App

Node-RED [3] is a visual programming environment that lets you create applications by dragging and dropping blocks (nodes) on the screen. Logic flows are created by connecting wires between the different nodes.

Raspbian Jessie comes with Node-RED preinstalled since November 2015. The base Node-RED installation includes support for the Raspberry Pi Keyboard and GPIO, but you can install many other useful libraries.

To check that Node-RED is installed and working, go to a terminal window and type in the following:

node-red start &

Once Node-RED starts, you use your web browser to build applications. If you are working directly on your Pi, enter 127.0.0.1:1880 in the URL address box of your browser. You drop nodes from the left pallet pane into the large flow window in the middle and wire them together in the correct order. Click the Deploy button in the upper right to start your program.

For this project we wanted to remotely control a powered device with RFID cards. There are many smart power switches on the market that you can use. Node-RED has a large library of nodes, so check to see if your device is supported. If your device does not have a library node, you can also call an Execute Command node to control the device. For our setup (Figure 5) we connected a light to a Belkin Wemo Switch [4]. Node-RED has a Wemo library node, but to be generic we used an Execute Command node.

For the logic (Figure 6) you can use the following nodes:

Figure 6: How Node-RED reads in the RFID card number and executes an action if valid.
  • Pi Keyboard – reads one key code at a time
  • Function – custom code to convert key codes to an RFID number
  • File In – reads in the valid RFID text file
  • Function – checks the RFID card against the file list, and defines an action to run
  • Execute Command – runs the required command line statement

The Pi Keyboard node returns the key code and not the actual ASCII characters. So for example a key code of 11 is a "0", and a key code of 28 is the Enter key. By double clicking on the first Function node you can edit its name and add code to convert the key codes into an ASCII string.

Listing 3 shows the "Get Card ID" logic. Lines 1 and 2 define context variables that retain data values. Line 10 checks the key action, only "down" key actions are considered. Some if statements are used to translate the different key codes to an ASCII string. Line 13 creates a new topic (card_id). This step is important because it allows for additional data to be passed without using the message payload. Once the card ID string is properly assembled then the message is sent out, (line 15).

Listing 3

Get Card ID logic

01 var card_id = context.get('card_id') || '';
02 var newcard = context.get('newcard') || '0';
03
04 if (newcard == 1) {
05   context.set('card_id', '');
06   context.set('newcard', card_id);
07 }
08 var keycode = msg.payload;
09
10 if (msg.action = 'down') {
11   if (keycode == 28) {
12     context.set ('newcard', 1);
13     msg.card_id = card_id;
14     msg.payload = card_id;
15     return msg;
16   } else if (keycode == 11) {
17     card_id = card_id + '0';
18   } else {
19     card_id = card_id + (keycode - 1);
20   }
21   context.set ('card_id', card_id);
22 }

The File In contains the file name of the valid RFIDs (Figure 7).

Figure 7: The File In node definition.

The second Function contains the logic to check the current RFID card to the list of valid IDs, and if the card is valid then an action is carried out. The action or command line statement can be dynamically passed as the payload to the Execute Command node. (For simpler applications the Execute Command node can have commands directly defined in the node itself).

You can see the "Check ID" logic in Listing 4. On line 1 a variable is loaded with the msg.payload, which contains the content of the RFID lookup file. The current RFID number is in the msg.card_id topic, and this is read into a variable on line 2. The current ID card is checked against the text file on line 4, and if the card is valid a custom command is sent as the msg.payload. The command statement on line 5 is based on the Wemo equipment we're using. For other devices you will need to check the vendor's documentation for their specific commands.

Listing 4

Check ID logic

01 var validIDs= msg.payload;
02 var thecard = msg.card_id;
03
04 if (validIDs.indexOf(thecard) > -1) {
05   msg.payload = "wemo switch Switch1 toggle";
06   return msg;
07 }

Summary

The RFID card reader hardware will also work with PCs, the Arduino Yun, and Arduino USB shields.

Storing valid RFIDs in an unencrypted file and having the USB card reader send the data as clear text is extremely insecure for most high level applications. However if you are looking for a low cost technology for simple projects, a set of 125 kHz RFID cards and a USB card reader could be your answer.

Buy this article as PDF

Express-Checkout as PDF

Pages: 3

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content