Hi, this is Wayne again with a topic “Arduino NFC – Don Coleman”.
To get started with nfc, the first thing we need is a lot of times. We use an nfc tag. These are some tags here, small tag and, basically a tag stores data. We can read data to it. We can read data from it.
We can write data to it. Uh think about tags is hold a very small amount of data, usually measured in bytes, rather than megabytes or gigabytes. This tag here holds about 700 bytes. This one will hold about 39 bytes. The tag has an antenna in it. There’S no battery or anything and the tag is powered from the uh initiator or the reader, so things that we can use for readers, android phones and today, we’ll be talking about arduinos with nfc shields on them. So, with an nfc tag, you could choose to write your data just as raw bytes under the tag. The problem with that is that different tag, manufacturers they have different specs and the tags behave differently.
So the nfc forum came up with this thing, called nfc data exchange or ndef, and that’s a common format that we can use for reading and writing on any tags. It’S basically a way we can structure our data so that we know that other programs can interpret it. There’S two main building blocks to um uh to end f, there’s end of message, which is basically one or more records and the nf record.
It contains a payload of data, which is the data that we care about and has metadata that tells us how to interpret that data. So if we look at example, i want to write the string hello world onto a tag, there’s something called the type name format and that’s metadata about the type. So there’s a spec called well known. So we’ll pick well known and then there’s a type called t for text and then there’s rules for encoding that payload. We need the number of bytes in the language code, the language code and then the actual string itself.
Don’T worry too much about the details, because the libraries we use will take care a lot of that for you, so we’re using arduino uno here and i have two different shields. The first one is the adafruit shield, and so it plugs right into the top and the second one is the seed studio, nfc shield and the seed studio booth is actually right behind us. So if you want to learn more about that shield, you can talk to those guys. After so in order to interact with the code we’re going to write some arduino programs or sketches there’s a pn532 library. Pn532 is the chipset that’s used in both of these shields. We’Re using the seed studio library it was originally based on the adafruit library and then seed studio extended it so it supports both shields.
The thing with pn532 is very low level, we’d be just sending a bunch of bytes to the commands, and it’s not necessarily the easiest thing to use. So i wrote a library on top of that, the ndf library, what the ndf library does is. It provides a higher level abstraction. It gives you objects for end of messages, nf records.
It gives you helper methods for creating and decoding messages. So if we’re going to use that library we’d be in our in our arduino ide, and we need to do some setup um all of this code that i’m going to show here is in the samples, if you install the ndf library but i’ll talk through a Little bit of it, the most important thing to see here is now at the bottom, we’re getting an instance of the nfc adapter. That’S our handle that we use to talk back and forth with the chip and send information in the setup. We do need to initialize the library.
If you don’t call nfc begin, you won’t be able to scan for tags and then, if we want to write a tag, we don’t want to try to write a tag until a tag is physically over the device. So there’s an nfc tag, present method which basically it says, hey. If there’s a tag there, then we do something if there’s no tag there we’ll skip over this code. So if we wanted to write that string, hello, world we’d create a new message. We’D use a helper function, add text record to add that text in there and then we tell the nfc adapter hey write that data onto the tag. Similarly, when we’re reading, we don’t want to read until there’s actually a tag there.
So if we look at tag present and instead of reading an ndf message, we actually get a tag object back. The reason we get a tag object is because a tag doesn’t necessarily have to have an ndf message in it, but it probably does so tag. Has a method called print which will just print out a bunch of debug information for us this is really good when you’re doing development, and so, if you look down at the bottom there, it says hello world, that’s interesting for finding out what’s on a tag, but It’S not necessarily helpful if we wanted to control some hardware or something with it so right here i have the adafruit shield and i have a strip of some neopixel lights on there and then i have some nfc tags here, which i coded earlier today and i Have what’s called a my media record on them and i made up a mime type called text led. So that way, my application will only read the tags that i wanted to and in the payload the data i put in there is a red, green and blue value of a color. So this one zero zero 255 is for blue, and so the idea is that when i scan this tag, i want the lights to turn blue.
When i scan other tags, i want it to turn different colors, so this would be in our main processing loop. We need to do a little more work when a tag is available. We get the tag from the adapter from the tag we get the message and then from the ndf message.
We get the first record. You can have many many records, but we’re just dealing with one record today and then the next parts, where i do a couple checks, is this a tag that i care about the type name format. Is it a my media tag and it is a record type text led, so if it has that, i can parse the data out and set the lights. So here we’ll switch over and uh the lights on here, and so, as we hold the tag over the lights will light up blue. I have a different tag. This is either green or red, so we have green and we have red now. This tag here has some different data on it, so what i did is i programmed it where, if it found a tag that it didn’t like it would just shut all the lights off, so that was a very small amount of code to be able to do That um there’s examples of all this code with the ndf project to get you started, so that was using a tag and going to a nfc shield, which is pretty cool, and then i could take that tag. I could read that tag with my phone and do something with it, but the real cool thing is: can i take my phone and go directly to an nfc shield last year, when i talked about it, it wasn’t a good enough state to demo this year, uh Due to the some of the hard work that the seed studio guys did on their libraries, we can actually do that right now. This is only supported with the seed studio hardware, because you need to be running an spi. I think you could make the 84 shield do it if you switch that to spi, but i haven’t tried yet so we’re using something called simple ndf exchange protocol or snap, which is uh the where that matters to use in the setup code. Before we created an nfc adapter here, if you look down, we create a snap object and that snap object is our handle for sending data back and forth between the two shields. So if we want to send a message from the arduino to my phone, we could create a message.
We’Re using the same message object. We could add a uri record and then we just need to get the size. We need to encode it and device and tell the nfc adapter to write that message.
The difference here. We don’t have to wait for the peer to become available. We can have this code and we can say, write it and then, when appear comes into the uh into the range it’ll send the data. Similarly, if i want to send data from my phone to the arduino, we say read and we try to read the data into a buffer and that’ll block until it actually reads data. We check the size to make sure we actually got a good message.
Sometimes, if you’re moving stuff around you don’t always get a good read, and if we do you parse it into an ndf message and then the code will be the same. Before i can get my uh mime type. I can get the data out and we can turn the lights on. So here we have another demo. So, okay, that’s a little bit washed out. I have three sliders on here where i can control the sliders and it sets a preview color and it says the data value that i’m going to send the 25500. So if i hold, this, hang on the demos are not cooperating once again.
There we go so it sends the data you’ll notice that there was a little bit i’ll try to do this again slowly, so you can see so if we try to set it to blue, i put it on there and this antenna is a little more finicky. I need to touch to beam and then it sends the data across and it turns the lights to blue. So, as you can see, it doesn’t work quite as smoothly as the tags, but i think if i had a mount to hold the antenna in the right place, we can get that to work a little bit better, but the code is still very simple.
So if you’re more interested in doing more things with nfc, i wrote a book for o’reilly with uh brian jepson and tom igo, and we go into a lot of how to use nfc with arduino and also how to use it with phones and raspberry pi’s. So if you’re interested in that i’ll be standing over here, you can come talk to me if you want to learn more, so these slides are online. Now, if you go to github github or don dot, github dot, io slides, you can download the slides, there’s links to all the sample code in there or, if you have any questions, feel free to reach out to me via email or twitter and i’ll definitely help You out with your project, but thank you very much. .