O'Reilly logo

Arduino Android Blueprints by Stefan Buttigieg, Marco Schwartz

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Writing the Arduino sketch

We will now write the code that will receive commands from the Android NFC app. The goal of this code will be to switch the relay on or off when the NFC shield receives a given code from the Android device. As the code for this part is quite long, we will split the code into several parts that will be detailed individually.

The code starts by including the required libraries:

#include "SPI.h"
#include "PN532_SPI.h"
#include "snep.h"
#include "NdefMessage.h"

We will also define on which pin the relay is connected:

#define RELAY_PIN 8

After this, we will define the code that should be received from the Android app to switch the relay on or off:

#define RELAY_ON "oWnHV6uXre"

We will also need to create an instance of the NFC chip: ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required