hello guys. i have an arduino uno+wifi shield+nfc and a reed sensor.. i want the nfc shield recognise a card and if its correct the password will take a value from reed sensor(if door is open and closed) and sent this value to xively... when i run the code, arduino stops transfer data(stop printing) in serial monitor and the code stucks. This is my code:
#include <SPI.h>
#include <WiFi.h>
#include <Wire.h>
#include <Adafruit_NFCShield_I2C.h>
#include <HttpClient.h>
#include <Xively.h>
#define IRQ (2)
#define RESET (3) // Not connected by default on the NFC Shield
Adafruit_NFCShield_I2C nfc(IRQ, RESET);
int sensPin = 12; //pin for reed sensor
int val = 0; // variable for reading the Input Pin status
char ssid[] = "INTERNET"; // your network SSID (name)
int status = WL_IDLE_STATUS;
char xivelyKey[] = "ovE7dyYYkX0v14SbBnNgDa4UKdbaVZmZMhAxgqoqLUynQYsh";
char sensorId[] = "reedsensor";
XivelyDatastream datastreams[] = {XivelyDatastream(sensorId, strlen(sensorId), DATASTREAM_FLOAT),};
XivelyFeed feed(1127679460, datastreams, 1 /* number of datastreams */);
WiFiClient client;
XivelyClient xivelyclient(client);
void setup(void) {
pinMode(9, OUTPUT); // set pin 9 as output for piezo sound
pinMode(sensPin, INPUT); // Set Sensor as input
Serial.begin(9600);
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
status = WiFi.begin(ssid);
// wait 10 seconds for connection:
delay(1000 );
}
Serial.println("Connected to wifi");
printWifiStatus();
Serial.println("Looking for PN532...");
nfc.begin();
uint32_t versiondata = nfc.getFirmwareVersion();
if (! versiondata) {
Serial.print("Didn't find PN53x board");
while (1); // halt
}
// Got ok data, print it out!
Serial.print("Found chip PN5");
Serial.println((versiondata>>24) & 0xFF, HEX);
Serial.print("Firmware ver. ");
Serial.print((versiondata>>16) & 0xFF, DEC);
Serial.print('.');
Serial.println((versiondata>>8) & 0xFF, DEC);
// configure board to read RFID tags
nfc.SAMConfig();
Serial.println("Waiting for an ISO14443A Card ...");
}
void loop(void) {
uint8_t success; // Flag to check if there was an error with the PN532
uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID
uint8_t uidLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type)
uint8_t currentblock; // Counter to keep track of which block we're on
bool authenticated = false; // Flag to indicate if the sector is authenticated
uint8_t data[16]; // Array to store block data during reads
uint8_t d[16] = { 0x00, 0x00, 0x03, 0x0C, 0xD1, 0x01, 0x08, 0x55, 0x05, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34 };
//array with the default password
int i;
int counter= 0 ; //flag for detection correct password
pinMode(sensPin, INPUT); // set pin12 as input.
// Keyb on NDEF and Mifare Classic should be the same
uint8_t keyuniversal[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);
if (success) {
// Display some basic information about the card
Serial.println("Found an ISO14443A card");
Serial.print(" UID Length: ");
Serial.print(uidLength, DEC);
Serial.println(" bytes");
Serial.print(" UID Value: ");
nfc.PrintHex(uid, uidLength);
Serial.println("");
if (uidLength == 4)
{
// We probably have a Mifare Classic card ...
Serial.println("Seems to be a Mifare Classic card (4 byte UID)");
// We try to read the data on block that we have wrote. (block=4, sector 1)
for (currentblock = 4; currentblock < 5; currentblock++)
{
// Check if this is a new block so that we can reauthenticate
if (nfc.mifareclassic_IsFirstBlock(currentblock)) authenticated = false;
// If the sector hasn't been authenticated, do so first
if (!authenticated)
{
// Starting of a new sector ... try to to authenticate
Serial.print("------------------------Sector ");
Serial.print(currentblock/4, DEC);
Serial.println("-------------------------");
if (currentblock == 0)
{
success = nfc.mifareclassic_AuthenticateBlock (uid, uidLength, currentblock, 1, keyuniversal);
}
else
{
success = nfc.mifareclassic_AuthenticateBlock (uid, uidLength, currentblock, 1, keyuniversal);
}
if (success)
{
authenticated = true;
}
else
{
Serial.println("Authentication error");
}
}
// If we cant authedicate..
if (!authenticated)
{
Serial.print("Block ");
Serial.print(currentblock, DEC);
Serial.println(" unable to authenticate");
}
else
{
// authedicate done... read data from currentblock
success = nfc.mifareclassic_ReadDataBlock(currentblock, data);
if (success)
{
// Read successful
Serial.print("Block ");
Serial.print(currentblock, DEC);
if (currentblock < 10)
{
Serial.print(" ");
}
else
{
Serial.print(" ");
}
// print data from store array
nfc.PrintHexChar(data, 16);
}
else
{
// Failure
Serial.print("Block ");
Serial.print(currentblock, DEC);
Serial.println(" unable to read this block");
}
}
}
}
else
{
Serial.println("This isn't a Mifare Classic card");
}
}
delay(1000);
int door; //flag for open-close door
val = digitalRead(sensPin); // read input value for reed sensor
for (i = 0; i < 16; i++)
{
if (data[i] == d[i])
{
counter = counter+1; // flag for authenticate correct password
}
}
if (counter == 16)
{
Serial.println("Door is Open");
beep(100);
door=1;
upload(door);
}
else{
Serial.println("Door is Closed");
beep(50);
door=0;
upload(door);
}
// Wait a bit before trying again
Serial.println("\n\nSend a character to run again");
Serial.flush();
while (!Serial.available());
while (Serial.available()) {
Serial.read();
}
Serial.flush();
}
void beep(unsigned char delayms){ // function to produce sound
analogWrite(9, 20); // Almost any value can be used except 0 and 255
// experiment to get the best tone
delay(delayms); // wait for a delayms ms
analogWrite(9, 0); // 0 turns it off
delay(delayms); // wait for a delayms ms
}
void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}
void upload(int door){
datastreams[0].setFloat(door);
Serial.println("Read sensor value ");
Serial.println(datastreams[0].getFloat());
Serial.println("Uploading it to Xively");
int ret = xivelyclient.put(feed, xivelyKey);
Serial.println("xivelyclient.put returned ");
Serial.println(ret);
Serial.println();
delay (1000);
}
do anyone knows the reason of this?