Oftentimes the best way to learn is to get your hands dirty. In this blog, I’ll show you how I built an elastically scalable and highly-secure RFID IoT solution for less than $200 and two hours’ worth of work. How? By using rapid prototyping tools like Raspberry Pi and AWS IoT.
Here’s what I used:
- The mfrc522 python open-source RFID library code for on-device IoT operations
- Amazon Web Services (AWS) for cloud hosting services
- AWS SDK for Python to securely publish MQTT messages when RFID scans happen
- AWS IoT Core for MQTT endpoints, IoT certificates, and data ingestion services
- Amazon S3 to store historical RFID scans
- AWS Glue to prepare and load RFID data for analytics
- Amazon Athena for ad-hoc RFID scan queries
- Amazon Quick Sight for data visualizations and the RFID dashboard
Before you begin buying things, think about how much you want to solder. I decided that’s too much real work for me and bought RC522 readers with the pins already mounted on the board. Thanks to Gus at PiMyLifeUp and his awesome How to set up a Raspberry Pi RFID RC522 Chip post, I was able to get the two Pi’s wired to the two RC522 readers, connected to my wifi network, and ready for IoT code in 30 minutes.
Now the fun part. Making it all work together. It turned out to be a simple 10 step process.
How to Connect Raspberry Pi to AWS IoT
Step 1 – Configure your Raspberry Pi
I won’t go into detail on how to make this happen as there are endless other posts and communities to help you out. Find a community that works for you, turn on your Pi and experiment. You’ll get it or you’ll find someone to help. Personally, once I found VNC Connect by RealVNC I knew I was ready to begin harnessing the power of AWS and to start seeing data flowing into IoT Core.
*The building you see is MentorMate’s technology center of excellence in Sofia, Bulgaria. It is the background image of one of my Raspberry Pis.
Step 2 – Setting up the mfrc522 python code
I’m not a python developer but I do read code and I’m not afraid to experiment. I used the Pis browser to download python and the mfrc522 open-source RFID library. This library has very simple Read.py and Write.py files in it that allow you to learn to update, debug, and run RFID scans. After a few print(‘GOT HERE”) statements I had scans coming into my Python script from the mfrc522 scanner.
I decided to name my scanners Front Door RFID and BackDoor RFID and configured the key cards and fobs to all have values that would end up being useful to my audience. Keep this in mind as we proceed. Your audience matters most!
Step 3 – Provision your things in AWS IoT Core
AWS IoT Core is a service from AWS that lets connected devices easily and securely interact with other AWS services. It scales elastically while giving you the ability to monitor what’s going on inside it. I created a thing in the IoT console which involved the easy step of issuing an X.509 Certification and enabling permissions for my unique RFID reader and only allowing it to utilize this certificate.
I really appreciate how far left AWS IoT shifted security. It’s all part of the process and ensures you don’t skip any important security steps in the name of getting something done. After this step, I had encryption setup via my new certificate, the Amazon Resource Name (ARN) of my thing, and MQTT endpoints for my thing. Awesome!
Step 4 – Put the AWS IoT Certificate on your PI
In order to allow your thing to publish MQTT messages into AWS IoT Core, you need to place the Certificate, Private Key, and AWS Root CA certificate on your thing somewhere. Since this is a demo solution, I put them in my demo folder. Remember this location as you’ll need to update the python code that publishes the message with it.
Step 5 – Add AWSIoTMQTTClient from AWS Python SDK to mfrc522 solution
I ran “pip install AWSIoTPythonSDK” from the PIs command line while in my demo folder to get the SDK installed. After that, I created a new RFIDEvent.py file and copied my Read.py code into it. Once that was working, it was time to configure the AWS code. For me, I experimented with the configuration settings from the SDK documentation until I had a host (MQTT endpoint from IoT Core), certPath, clientId, and Topic to publish. To get mine to work I needed to set the port to 8883 when creating the endpoint.
SIDE NOTE: A new AWS Python SDK is out now that makes this process easier.
Step 6 – Push scans to MQTT endpoints
With all of this ready, it was time to adjust the RFID read code to push the actual message. I created a try/catch block and a while look to continually look for incoming RFID card or fob scans and a JSON structure to ensure AWS IoT received the incoming card id, time, and location from which the scan originated. Then publish to AWS and validate!
You can validate incoming MQTT in AWS IoT in a variety of ways. You can check for activity in your thing dashboard, view your AWS IoT Monitoring dashboard, open the MQTT test client and subscribe to your topic, or enable CloudWatch logging. I recommend you start with the MQTT test client.
After you see traffic here, you’ll be ready for the next step.
Step 7 – AWS IoT Rule
The power of IoT is not in the ability to communicate, it is in the ability to act. AWS IoT has a serverless rules engine that quickly allows you to integrate with the most common of their services.
If you can’t find something check the roadmap as it might be coming soon or just integrate with AWS Lambda and call what you need to call. For me, I wanted to populate an S3 bucket.
Make sure to update your Rule Query statement to select incoming messages with the Topic you have in your thing/device/instrument code and then validate you are getting JSON files for each message in your bucket.
Step 8 – Query your data with Athena
At this point, you are “done” with IoT code. It’s working. Now you need to answer some questions with it. What learning can come from this data? In order to experiment with finding these answers, you need to be able to query your new dataset. Using AWS Glue, I created a crawler to scan my S3 bucket:
and create a database with tables I could query using Amazon Athena:
Athena is a serverless query service that lets you run standard SQL queries against your target. For me, I use it to learn my data and prepare myself to visualize it during Step 9.
Step 9 – Make a data set and a dashboard with Amazon QuickSight
Amazon QuickSight allows you to quickly pull data from S3 into a dataset that you can visualize and distribute through whichever channel your end users request.
Step 10 – Dream Big
Now that you have the plumbing working and the pretty lights turned on, go inspire someone. IoT solutions are changing our lives in countless ways. The data they produce is fascinating and will help you solve personal or professional challenges when used properly.
If you’d like some help creating IoT customer experiences that build trust and produce a return on investment, let us know.