Book Image

Arduino Android Blueprints

Book Image

Arduino Android Blueprints

Overview of this book

Table of Contents (17 chapters)
Arduino Android Blueprints
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

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:

PN532_SPI pn532spi(SPI, 10);
SNEP nfc(pn532spi);

To store data coming from the Android phone via NFC, we will create a char buffer:

uint8_t ndefBuf[128];

In the setup() function of the sketch, we will start the serial communications:

Serial.begin(9600...