Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Arduino Android Blueprints

You're reading from   Arduino Android Blueprints Get the best out of Arduino by interfacing it with Android to create engaging interactive projects

Arrow left icon
Product type Paperback
Published in Dec 2014
Publisher Packt
ISBN-13 9781784390389
Length 250 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Toc

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...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image