Back to Parent

#include "RFID.h"

/* Define the pins used for the SS (SDA) and RST (reset) pins for BOTH hardware and software SPI */
/* Change as required */
#define SS_PIN_1      A1      // Same pin used as hardware SPI (SS)
#define SS_PIN_2      A2      // Same pin used as hardware SPI (SS)
#define RST_PIN     D3

/* Define the pins used for the DATA OUT (MOSI), DATA IN (MISO) and CLOCK (SCK) pins for SOFTWARE SPI ONLY */
/* Change as required and may be same as hardware SPI as listed in comments */
#define MOSI_PIN    A5      // hardware SPI: A5
#define MISO_PIN    A4      //     "     " : A4
#define SCK_PIN     A3      //     "     " : A3

#define SERVOPIN D0
Servo latch;
int close = 10;
int open = 90;

int angle = open;

/* Create an instance of the RFID library */
#if defined(_USE_SOFT_SPI_)
    RFID RC522_1(SS_PIN_1, RST_PIN, MOSI_PIN, MISO_PIN, SCK_PIN);    // Software SPI
#else
    RFID RC522_1(SS_PIN_1, RST_PIN);                                 // Hardware SPI
#endif

#if defined(_USE_SOFT_SPI_)
    RFID RC522_2(SS_PIN_2, RST_PIN, MOSI_PIN, MISO_PIN, SCK_PIN);    // Software SPI
#else
    RFID RC522_2(SS_PIN_2, RST_PIN);                                 // Hardware SPI
#endif



void setup()
{
  Serial.begin(9600);
  latch.attach(SERVOPIN);
  latch.write(angle);

#if !defined(_USE_SOFT_SPI_)
  /* Enable the HW SPI interface */
  SPI.setDataMode(SPI_MODE0);
  SPI.setBitOrder(MSBFIRST);
  SPI.setClockDivider(SPI_CLOCK_DIV8);
  SPI.begin();
#endif

  /* Initialise the RFID reader */
  RC522_1.init();
  RC522_2.init();
}

void loop()
{
  /* Has a card been detected? */
  if (RC522_1.isCard())
  {
    /* If so then get its serial number */
    RC522_1.readCardSerial();

    if (angle == open) {
      Particle.publish("diot/2018/brello/umbrella_status","returned");
      angle = close;
      latch.write(angle);
    }

    /* Output the serial number to the UART */
    Serial.print("Umbrella RFID: ");
    for(uint8_t i = 0; i <= 4; i++)
    {
      Serial.print(RC522_1.serNum[i],HEX);
      Serial.print(" ");
    }
    Serial.println();
  }

  if (RC522_2.isCard())
  {
    /* If so then get its serial number */
    RC522_2.readCardSerial();

    if (angle == close) {
      Particle.publish("diot/2018/brello/umbrella_status","taken");
      angle = open;
      latch.write(angle);
    }

    /* Output the serial number to the UART */
    Serial.print("Key Fob RFID: ");
    for(uint8_t i = 0; i <= 4; i++)
    {
      Serial.print(RC522_2.serNum[i],HEX);
      Serial.print(" ");
    }
    Serial.println();
  }

  delay(500);
}
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0