• Medium One

Build a RFID card reader with Adafruit Feather HUZZAH w/ESP32 and Medium One’s Prototyping San

This tutorial uses the Feather HUZZAH w/ESP32 WiFi board as a RFID card reader. ESP32 connects to Medium One’s Prototyping Sandbox so that a new scanned RFID card key can be sent and processed. The sandbox will perform the following operations: add, delete or verify a RFID card key. The user can use the Medium One IoT Controller app on a smartphone to select the card processing mode.

In this Quick Start Guide, you will:

  1. Set up an Arduino environment

  2. Program the Feather HUZZAH w/ESP32 board

  3. Use Medium One Workflows to turn on/off the green or red LED’s, set RFID card process mode, and process a new card ID key received from the card reader

Here’s what you need to get started:

  1. Medium One Prototyping Sandbox Account [Buy Link]

  2. Feather HUZZAH w/ESP32 [Buy-3046]

  3. CP2104 USB Driver [download]

  4. Sparkfun RFID USB Reader [Buy-SEN-09963]

  5. Sparkfun RFID Reader ID-12LA (125 kHz) [Buy-SEN-11827]

  6. 2 Card Keys [Buy-RFID-Tags]

  7. Windows PC or Mac OS (Note, this tutorial is based on MacOS)

  8. WiFi Internet Access

  9. Medium One IoT Controller App [download]

  10. Arduino IDE [download]

STEP 1: SET UP RFID card reader and Adafruit ESP32 Feather Board.

STEP 2: SET UP THE MOBILE APP

Note: These instructions are for the Android app, but the IOS app is similar.

Open the IoT Controller app and click on the plus sign in the bottom corner.

Now you can give your project a name and input your Username, Password, and API Key from the account set up. Select “Sandbox”. Then press “Done”.

If you entered everything correctly, you should see two blue circular icons on the bottom. If you entered something incorrectly, the color of the of the plus circular icon will be red instead of blue.

Next, press “+Add New Widget”.

Select “Switch”. Swipe the whole “switch” block to the right to access the settings.

The stream field is represented by a two-arrow icon. The tag field is represented by a price tag icon. The delete button is represented by a trash can icon.

Press each icon to enter the field. Type “raw” for the stream, and “redledsw” for the tag.

Press “+ Add New Widget” again. Four more similar switches are created. At the end, this is what your screen will look like.

Next, press “+Add New Widget”.

Select “Notification”. Swipe the whole “Notification” block to the right to access the settings.

Press each icon to enter the field. Type “notifications” for the stream, and “message” for the tag.

At the end, there are four “switch” widgets and one “notification” widget. The tag name will appear on each widget.

Checkpoint

Go to your Medium One account and go to the “Dashboard” from the left-hand sidebar. Click “Single User Real Time Events Stream”.

Select your user in the right hand corner of the widget.

Now, on the app, click on the “redledsw” switch to turn it on.

You should see an event show up on the widget.

Click the save icon (floppy disk) in the top right corner of the dashboard.

STEP 3: SET UP THE ESP32 BOARD

Go to Tools -> Board:”Adafruit ESP32 Feather”

Copy and paste the following code from this link into your project.

https://github.com/Medium-One/esp32/tree/master/Sparkfun_125kHz_RFID_Card_Reader/esp32_arduino_c_code

Fill in your Sandbox and WiFi credentials where necessary near the top portion code.

For example, it will look something like this:

STEP 4: PROGRAMMING THE BOARD

Connect your board to your PC via USB.

Next, go to Tools -> Port. If the board is properly connected and the USB driver is installed, you’ll see *SLAB_USBtoUART device listed. Note, on Windows, you’ll need to select the correct com port.

Then click on the upload arrow in the top left corner to compile and flash the board, this may take a few minutes.

Note the output log during this step to see when it’s completed.

Once complete, go back to your Medium One Dashboard, where the Real Time Events Stream should be open. You should eventually receive an event from your board within a minute.

You should go to Tools -> Serial Monitor to see print statements from the board, so that you can see when the board connects to the WiFi. (Use 115200 baud)

You will also receive a heartbeat event from the board every minute.

STEP 5: WRITE WORKFLOW 1: SWITCH LED

Press these two switches to send event data to the sandbox.

The events will appear.

Go to “Config” on the left-hand sidebar in your Medium One account and click “Data Streams”.

Click “raw” and then click the active checkbox next to “redledsw” and “greenledsw”. Then click “Save Data Stream”.

Now go to your Workflow Studio in your Medium One account and click “Create”.

Give your workflow a name. We named ours “LED Control”.

Click the “Tags and Triggers” icon, which is the first icon on the right-hand sidebar.

Click “raw” and drag the “redledsw” and “greenledsw” tags into the main area of the workflow.

Now click on the “Modules” icon (the second icon in the sidebar), click on “Foundation” and drag a “Base Python” module into the workflow.

Double click on the base python block. Click to add the 2nd input called in2. Then press “save”.

Connect the bottom end of the switch tag to the “in1” and “in2” circles on the Base Python module.

Double-click on the Base Python module and copy and paste the following code (follow link) into the Script section, replacing any code that is originally there.

''' This workflow will turn on or off the leds. Last Updated: June 19, 2018 Author: Medium One ''' import MQTT if IONode.is_trigger('in1'):     redledlevel = IONode.get_input('in1')['event_data']['value']     if (redledlevel == "on"):         MQTT.publish_event_to_client('esp32', 'G1:1')     else:         MQTT.publish_event_to_client('esp32', 'G1:0')     log("red led level: " + str(redledlevel)) elif IONode.is_trigger('in2'):        greenledlevel = IONode.get_input('in2')['event_data']['value']     if (greenledlevel == "on"):         MQTT.publish_event_to_client('esp32', 'G2:1')     else:         MQTT.publish_event_to_client('esp32', 'G2:0')    log("green led level: " + str(greenledlevel))

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

'''

This workflow will turn on or off the leds.

Last Updated: June 19, 2018

Author: Medium One

'''

import MQTT

if IONode.is_trigger('in1'):

    redledlevel = IONode.get_input('in1')['event_data']['value']

    if (redledlevel == "on"):

        MQTT.publish_event_to_client('esp32', 'G1:1')

    else:

        MQTT.publish_event_to_client('esp32', 'G1:0')

    log("red led level: " + str(redledlevel))

elif IONode.is_trigger('in2'):   

    greenledlevel = IONode.get_input('in2')['event_data']['value']

    if (greenledlevel == "on"):

        MQTT.publish_event_to_client('esp32', 'G2:1')

    else:

        MQTT.publish_event_to_client('esp32', 'G2:0')   

log("green led level: " + str(greenledlevel))

Now click “Save and Activate”.

Checkpoint

Plug the board in and open the IoT Controller app.

After the board is done setting up (within a minute), turn the app switch on and off. You should see the LED on the board turn on and off.

Unplug the board.

STEP 6: WRITE WORKFLOW 2: Set RFID Card Key Processing Mode (Add, Delete, Verify)

Press these two switches to send event data to the sandbox.

The events will appear.

Go to “Config” from the left-hand sidebar on your Medium One account and click “Data Streams”.

Click “raw” and then click the active checkbox next to “add_user_mode” and “del_user_mode”. Then click “Save Data Stream”.

Create a new stream called notification. Click on “Create New Stream”.

Enter “notifications” for the Name field. Choose “Never Expire.” Click “Save Data Stream.”

The new data stream is created.

Create a new workflow, name it, and drag the raw “add_user_mode” and “del_user_mode” tags into the workflow.

Drag a Base Python module into the workflow.

Double click on the base python block. Click to add the 2nd input called in2. Then press “save”.

Click the “Outputs” icon (third icon on sidebar) and drag one “Processed Stream – Single” output into the workflow.

Double click “Processed Stream – Single” output block.

Choose “notifications” for Datastreams field. Click “Save.”

Connect the blocks as shown below.

Copy and paste the following code into the Script area, replacing any code (follow link below) that was previously there. Then click “Save”.

''' This workflow sets the operation mode: Add/Delete/Verify RFID card key Last Updated: June 19, 2018 Author: Medium One ''' import MQTT import Store adduser = IONode.get_input('in1')['event_data']['value'] deluser = IONode.get_input('in2')['event_data']['value'] if (adduser == 'on' and deluser == 'on'):  # illegal case     new_value = "verify"      msg = "Verify card ID: Please scan your card." elif (adduser == 'on'):      new_value = "add"     msg = "Add card ID: Please scan your card." elif (deluser == 'on'):     new_value = "delete"     msg = "Delete card ID: Please scan your card." else:                  # add off or del off case     new_value = "verify"      msg = "Verify card ID: Please scan your card." Store.set_data("rfid_mode", new_value, -1) # push (bool) to send this notification to push (iOS or Android) IONode.set_output('out1', {"message": msg, "sms":False,"email":False,"push":True}) # for debug purpose log('new value =' ) log(new_value)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

'''

This workflow sets the operation mode: Add/Delete/Verify RFID card key

Last Updated: June 19, 2018

Author: Medium One

'''

import MQTT

import Store

adduser = IONode.get_input('in1')['event_data']['value']

deluser = IONode.get_input('in2')['event_data']['value']

if (adduser == 'on' and deluser == 'on'):  # illegal case

    new_value = "verify" 

    msg = "Verify card ID: Please scan your card."

elif (adduser == 'on'): 

    new_value = "add"

    msg = "Add card ID: Please scan your card."

elif (deluser == 'on'):

    new_value = "delete"

    msg = "Delete card ID: Please scan your card."

else:                  # add off or del off case

    new_value = "verify" 

    msg = "Verify card ID: Please scan your card."

Store.set_data("rfid_mode", new_value, -1)

# push (bool) to send this notification to push (iOS or Android)

IONode.set_output('out1', {"message": msg, "sms":False,"email":False,"push":True})

# for debug purpose

log('new value =' )

log(new_value)

Now click on the “Revisions” icon (second from the bottom of the sidebar).

Click the checkmark under the latest revision to activate the workflow if it’s not activated.

Checkpoint

Go to your Mobile App.

Click the “add_user_mode” switch to change the state from OFF to ON. A message will appear to indicate that the “add a user’s card key” mode is enabled, and the firmware is waiting for a user to scan a card.

Click the “add_user_mode” switch to change the state from ON to OFF. A message will appear to indicate that the “verify a user’s card key” mode is enabled, and the firmware is waiting for a user to scan a card.

Click the “del_user_mode” switch to change the state from OFF to ON. A message will appear to indicate that the “delete a user’s card key” mode is enabled, and the firmware is waiting for a user to scan a card.

Click the “del_user_mode” switch to change the state from ON to OFF. A message will appear to indicate that the “verify a user’s card key” mode is enabled, and the firmware is waiting for a user to scan a card.

STEP 7: WRITE WORKFLOW 3: Process RFID card key.

Place the RFID key over the reader and hold it there until the buzzer sounds a beep. This step will send the “card_key” tag to the cloud.

The “card_key” tag and value appear.

Go to “Config” from the left-hand sidebar on your Medium One account and click “Data Streams”.

Click “raw” and then click the active checkbox next to “card_key.” Then click “Save Data Stream”.

Now go to your Workflow Studio on your Medium One account and click “Create.”

Give your workflow a name. We named ours “Process RFID Card Key.”

Click the “Tags and Triggers” icon, which is the first icon on the right-hand sidebar. Click “raw” and drag the “card_key” tag into the main area of the workflow.

Now click on the “Modules” icon (the second icon in the sidebar), click on “Foundation” and drag a “Base Python” module into the workflow.

Click the “Outputs” icon (third icon on sidebar) and drag one “Processed Stream – Single” output into the workflow.

Double click “Processed Stream – Single” output block.

Choose “notifications” for Datastreams field. Click “Save.”

Connect the blocks as shown below.

Copy and paste the following code into the Script area, replacing any code (follow link below) that was previously there. Then click “Save”.

''' This workflow will process the scanned card key according to the following modes: Add/Delete/Verify RFID card key Last Updated: June 19, 2018 Author: Medium One ''' import MQTT import Store cardkey =  IONode.get_input('in1')['event_data']['value'] secretcardkey = Store.get("cardkey")   # Get the stored secret card key to compare for verify card key mode. verifyflag = True                      # Set verify pass. mode = Store.get("rfid_mode")          # Get the stored card key operation mode. if mode == "add":                      # Add card key mode.     log('Add Mode')     sms_msg = "Add card"     if (secretcardkey == None or secretcardkey == ""):    # Allow to add a new key if the previous secret card key has been deleted first.           Store.set_data("cardkey", cardkey, -1)         verifyflag = True   # Set Pass to indicate that the new secret card key is added successfully.     else:            verifyflag = False  # Set Fail to indicate that the new secret card key is not added. elif mode == "delete":     log('Delete Mode')     sms_msg = "Delete card"          if (secretcardkey == cardkey):   # Delete the current secret card key only if the scanned card key matches it.         Store.set_data("cardkey", "", -1)          verifyflag = True   # Set Pass to indicate that the new secret card key is deleted successfully.     else:            verifyflag = False  # Set Fail to indicate that the new secret card key is not deleted.      elif mode == "verify":     sms_msg = "Verify card"     log('Verify Mode')     if (secretcardkey == cardkey):         verifyflag = True   # Set Pass to indicate that they are matched.     else:    # not matched or secretcardkey is None or ""         verifyflag = False  # Set Pass to indicate that they are matched. else:     sms_msg = "Error Invalid Mode"   #unknown mode     log('Invalid Mode')     verifyflag = False  # Show fail to indicate that there is a problem. # show the card ID    sms_msg = sms_msg + ' (' + cardkey + ').  ' #send the pass/fail flag to MCU, so the MCU can blink the green led or red led to indicate that the operation fails or pass respectively. if (verifyflag):            sms_msg = sms_msg + " Pass!"     MQTT.publish_event_to_client('esp32', 'V1')   else:        sms_msg = sms_msg + " Fail!"     MQTT.publish_event_to_client('esp32', 'V0')                 # push (bool) to send this notification to push (iOS or Android) IONode.set_output('out1', {"message": sms_msg, "sms":False,"email":False,"push":True}) # For debug purpose log('mode =' ) log(verifyflag)    log('verify =' ) log(verifyflag)    log('secret card key =' ) log(secretcardkey)  newsecretcardkey = Store.get("cardkey")        log('new secret card key =' ) log(newsecretcardkey)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

'''

This workflow will process the scanned card key according to the following modes: Add/Delete/Verify RFID card key

Last Updated: June 19, 2018

Author: Medium One

'''

import MQTT

import Store

cardkey =  IONode.get_input('in1')['event_data']['value']

secretcardkey = Store.get("cardkey")   # Get the stored secret card key to compare for verify card key mode.

verifyflag = True                      # Set verify pass.

mode = Store.get("rfid_mode")          # Get the stored card key operation mode.

if mode == "add":                      # Add card key mode.

    log('Add Mode')

    sms_msg = "Add card"

    if (secretcardkey == None or secretcardkey == ""):    # Allow to add a new key if the previous secret card key has been deleted first.  

        Store.set_data("cardkey", cardkey, -1)

        verifyflag = True   # Set Pass to indicate that the new secret card key is added successfully.

    else:   

        verifyflag = False  # Set Fail to indicate that the new secret card key is not added.

elif mode == "delete":

    log('Delete Mode')

    sms_msg = "Delete card"     

    if (secretcardkey == cardkey):   # Delete the current secret card key only if the scanned card key matches it.

        Store.set_data("cardkey", "", -1) 

        verifyflag = True   # Set Pass to indicate that the new secret card key is deleted successfully.

    else:   

        verifyflag = False  # Set Fail to indicate that the new secret card key is not deleted.     

elif mode == "verify":

    sms_msg = "Verify card"

    log('Verify Mode')

    if (secretcardkey == cardkey):

        verifyflag = True   # Set Pass to indicate that they are matched.

    else:    # not matched or secretcardkey is None or ""

        verifyflag = False  # Set Pass to indicate that they are matched.

else:

    sms_msg = "Error Invalid Mode"   #unknown mode

    log('Invalid Mode')

    verifyflag = False  # Show fail to indicate that there is a problem.

# show the card ID   

sms_msg = sms_msg + ' (' + cardkey + ').  '

#send the pass/fail flag to MCU, so the MCU can blink the green led or red led to indicate that the operation fails or pass respectively.

if (verifyflag):       

    sms_msg = sms_msg + " Pass!"

    MQTT.publish_event_to_client('esp32', 'V1')  

else:   

    sms_msg = sms_msg + " Fail!"

    MQTT.publish_event_to_client('esp32', 'V0')                

# push (bool) to send this notification to push (iOS or Android)

IONode.set_output('out1', {"message": sms_msg, "sms":False,"email":False,"push":True})

# For debug purpose

log('mode =' )

log(verifyflag)   

log('verify =' )

log(verifyflag)   

log('secret card key =' )

log(secretcardkey) 

newsecretcardkey = Store.get("cardkey")       

log('new secret card key =' )

log(newsecretcardkey)

Now click on the “Revisions” icon (second from the bottom of the sidebar).

Congrats! You have finished the tutorial.

Click the checkmark under the latest revision to activate the workflow if it’s not activated.

Checkpoint

Go to your Mobile App. Let’s try out each of the different card key modes.

[Add Card Key Mode]

Click the “add_user_mode” switch to change the state from OFF to ON. Wait for the message: “Add card ID: Please scan your card.”

Scan your card. This message will appear: “Add card (<the card key #1>). Pass!” The green LED light will blink once.

[Verify Card Key Mode]

Click the “add_user_mode” switch to change the state from ON to OFF. Wait for the message: “Verify card ID: Please scan your card.”

Scan your card key #1. Because the keys match, this message should appear: “Verify card (<the card key #1>). Pass!” The green LED light blinks once.

Scan your card key #2. Because the keys do not match, this message should appear: “Verify card (<the card key #2>). Fail!” The red LED light blinks once.

[Delete Card Key Mode]

Click the “del_user_mode” switch to change the state from OFF to ON. Wait for the message: “Delete card ID: Please scan your card.”

Scan your card key #1. This message will appear: “Delete card (<the card key #1>). Pass!” The green LED light blinks once.

Click the “del_user_mode” switch to change the state from ON to OFF. Wait for the message: “Verify card ID: Please scan your card.”

[Add Card Key Mode]

Click the “add_user_mode” switch to change the state from OFF to ON. Wait for the message: “Add card ID: Please scan your card.”

Scan your card. This message will appear: “Add card (<the card key #2>). Pass!” The green LED light blinks once.

[Verify Card Key Mode]

Click the “add_user_mode” switch to change the state from ON to OFF. Wait for the message: “Verify card ID: Please scan your card.”

Scan your card key #2. Because the keys match, this message will appear: “Verify card (<the card key #2>). Pass!” The green LED light blinks once.

Scan your card key #1. Because the keys do not match, this message will appear: “Verify card (<the card key #1>). Fail!” The red LED light blinks once.

Congratulations! You have successfully completed the RFID Card Reader tutorial! To learn more about the Medium One Sandbox and all its capabilities go here [Medium-One-Docs].

2 views

© 2018 Medium One   All Rights Reserved

4633 Old Ironsides Dr. #280, Santa Clara, CA  95054                

info@mediumone.com

|

  • LinkedIn - White Circle
  • Twitter - White Circle
  • YouTube - White Circle
  • Google+ - White Circle