In this article we will talk about how scanning an NFC tag can send commands to your hardware*.
* As long as you have an Android phone
If you know how to do this in iOS (ie using an iPhone) please leave me a note below.
What is NFC?From the Android API guides:
Near Field Communication (NFC) is a set of short-range wireless technologies, typically requiring a distance of 4cm or less to initiate a connection. NFC allows you to share small payloads of data between an NFC tag and an Android-powered device, or between two Android-powered devices.
How can we use NFC tags?The simplest use case for NFC tags is when you use your phone to scan one. In turn, your phone can do something about it. For instance: let's say you have an NFC tag in the dashboard of your car, you can scan it to put your phone in driving mode, or opening the maps app, or even both.
For more examples, you can refer to this lifehacker article.
How can we use NFC tags to control our hardware?In our case, we could use an NFC tag to make our phone send a command to our hardware.
In the case of this article, I would use my Garage Commander project as an example:
when I scan an NFC glued to the exterior side of my garage door, the phone would send a REST command to the Particle dev kit controlling the garage and the door would open or close.
Alright, so how do we do it?step 1:
Install the Trigger and Tasker apps in your phone.
Yes, Tasker is a paid app, but if you are not ready to commit to it you can download the free version that would give you 7 days of joy.
step 2:
In the Tasker app we create a task that will send an HTTP POST request to the Particle Cloud to open or close the garage door. In case you are not controlling a Particle, but another piece of hardware like a Raspberry Pi, the HTTP request will be sent to it instead.
Let's create the task. Open the Tasker app and:
https://api.particle.io/v1/devices/<PARTICLE DEVICE ID>/garage_open
access_token=<YOUR PARTICLE ACCESS TOKEN>
Please note that garage_open is the function we want to trigger with this POST request. For more info on the functions available remember to check the particle code in the example project.
Here are some screenshots to guide you:
If you get stuck, visit this thread for some help or leave me a note below.
step 3:
In the Trigger app we will create a task to fire off a task in Tasker. At the end of the task creation, the app will ask us to scan the NFC tag so it can save the task in it.
Here is exactly what I did in the Trigger app:
- create new task
- hit next on the "Add one or more triggers" screen
- add an action
- select Tasker -> Tasker Task then next
- hit the magnifier so you can select the task in Tasker
- select OpenGarage and tap on "ADD TO TASK"
- hit Done
- hit NFT in "Add one or more triggers" and Next
- hit done in "Add restrictions" -> Next -> Next -> Done (there is no need for a second, switch task)
- Scan your NFC tag to save the task date in it
- Hit Done on top
- you are finally done
You can take a look at great full tutorial here.
Test itNow we should have a fully working setup. So go ahead and scan the NFC tag with your phone. If all goes well, the Trigger app should pop up a notification that mentions the task was executed, and hopefully the HTTP command was sent to our hardware and our hardware reacted to it.
In my case, my garage door is now opening.
Final wordsI wrote this article to show you how you could use the power of the NFC tags in case you are curious about them as I once was.
As it turns out, at least for me, NFC tags are a great idea but not a great way of controlling your hardware. I find myself trying very hard to scan that NFC tag glued in my garage, changing the position of my phone, so it finally scans it. When it succeeds, most of the time I find myself wondering what did I do wrong with this NFC tag that takes me so much effort or time to scan.
Perhaps it's the NFC tag I have, which is a very old one (I got it as a gift from a friend more than 5 years ago and I was always looking for a good enough use for it). Maybe NFC tags got so much better today. Another problem could be that I glued it straight in the stone of the side of the garage, and that impacts the magnetic fields between my phone and the tag.
Anyway, in my experience so far, NFC Tags are great ideally but somewhat a pain to use... sorry, I had to warn you ;)
If it works better for you please let me know in a comment below.
That's itFor concrete examples of this article, take a closer look at my Garage Commander project.
If something is not clear, feel free to leave me a note below.
thank you,
Gustavo.
Psst: please find my other projects here.
HelpIf you require professional help with your projects, don't hesitate to write me a line about your needs at gusgonnet@gmail.com. Thank you!
Comments