Why did you decide to start working with NFC – what was it that first interested you?
I think it was a neat way to explore using physical things with a game – taking a device or an object and making it more interactable. I was a little bit inspired by Amiibos and Skylanders – those little figures you can buy that have NFC in them. So I was thinking, what can I do to make something myself out of this? What’s a DIY thing to try? And then I realised that the Raspberry Pi has Minecraft and the cool thing about Minecraft Pi is that it has got a whole little Python API, so it’s really easy to use. With a few lines of Python you can create blocks and mess around in the Minecraft world, so I thought that was perfect – I could just put both of these together and make some sort of physical thing to interact with the Minecraft world.
So how exactly does it work?
Basically, it has a little NFC reader, which is this circuit board that connects to the Raspberry Pi and uses an SPI connection (a typical connection for embedded devices), and then the Pi has the GPIO ports, so you can get really good low-level access to devices with that. NFC is a wireless Near Field Communication protocol, so it’s kind of cool in that you can have these little passive devices, like a tag or a card, and you just hold them near the antenna on the RFID reader and it’ll energise them, sending a few little bits of data between them. So the NFC reader is connected to the Pi and there’s some Python code that I wrote – a little library – to interact with the NFC reader. There’s a little program that you run (also in Python) that basically talks to the NFC reader, and it’s in a passive listening mode, so it just waits to see when a card is swiped. Ahead of time, you build these little blocks and put NFC cards inside them and then you scan those – each one has a unique ID associated with it, so once you get the ID then you can have a little configuration that says ‘this ID equals the wood block’, ‘this ID equals the TNT block’, etc. So the program just waits to see a block that’s swiped and then, using the Minecraft API, it tells Minecraft to create a new block wherever the player
is standing now.
So you wrote the library for the Adafruit PN532 RFID breakout that you are using in this project?
Exactly – the Python port of it. There’s also an Arduino port and it’s pretty standard, a typical kind of embedded code. The nice thing with this breakout is that you don’t really have to talk at a super-low level with the different NFC protocols – the chip on here takes care of that for you, so really the library is just talking to the chip and saying, ‘run the listening command’ or ‘run the read or write command’. It abstracts away how to initialise the device and it has a bit of a framing protocol – when you send a command it has a hash and other stuff associated with it to make sure it gets the correct data, so the library takes care of all that for you. You can just look at the datasheet and it tells you all the different commands that it supports, and in order to send those commands you have to frame them in the right way, but the library will do that for you. And even above that there are some higher-level functions that just, for example, wait for a card, and then once you have a card there’s a function that says ‘give me all the data from the card’, so it’s meant to be real simple and easy to use, really make you able to be productive with it.
How much information can you store in these NFC tags? Could you write the layout for a Minecraft building on a chip?
The cards that I was using, the MIFARE Classics, store 1KB of data and I’m not actually even storing much data – it’s just the type of block, a byte or two. But you could actually create some arrangement of blocks, like a pyramid or a house, up to a kilobyte, so you’d have some restrictions in that each block will need a position. You’ll probably need three or four bytes for that (or maybe even three or four 16- or 32-bit values), so it could probably store maybe 100 blocks or so in an arrangement on a 1KB card. But there are bigger cards – I think there are 4KB ones. So it could be a cool thing to support for the future, extend it to support a structure or something that you make ahead of time – the tricky thing is that you’d need some kind of editor, ideally. Minecraft on the Pi is pretty basic, so you would have a bit of a tough time trying to define the structure without just dropping someone down to configuration and saying, ‘Okay, write out the exact position of the blocks’. But I guess if someone were really ambitious, they could make a little 3D editor or something to place the blocks.
Have you experimented using the PN532 in any Raspberry Pi projects outside of Minecraft up to now?
Not too much yet. I was kind of curious to see if we could read Amiibos – what do they actually store on the cards? – and you can actually read them. There’s a community on Reddit trying to hack the Amiibos and actually figure out what the data is, but it’s all encrypted by Nintendo so you can’t really understand it. I was hoping that maybe you could actually clone an Amiibo but the 532 unfortunately doesn’t support writing to the Amiibo; they use special tags – NTAG216, I think – and it’s a weird format, quite hard to find. Nintendo didn’t want people to copy them, obviously!