Green Robot Machinery
Published © LGPL

Automated Parking System

Automated parking system using RFID and Ultrasonic sensors.

AdvancedWork in progress5,953
Automated Parking System

Things used in this project

Hardware components

Spark Core
Particle Spark Core
×1
RFID Module (MFRC-522)
RFID reader and tags
×1
ultrasonic sensor module
Ultrasonic Transceiver
×1

Story

Read more

Schematics

Circuit Connections

Ultrasonic Sensor Pin Connections:
ECHOPIN 6
TRIGPIN 5

*RFID Module Pin layout should be as follows:
* Signal -- Pin -- Pin
* Signal -- SPARK -- MFRC522 board
* ---------------------------------------------------------------------------
* Reset -- ANY (D2) -- RST
* SPI SS -- ANY (A2) -- SDA
* SPI MOSI -- A5 -- MOSI
* SPI MISO -- A4 -- MISO
* SPI SCK -- A3 -- SCK

Code

Parking Code

C/C++
Include the MFRC 522.h library from the web Ide. Then use the following code
/*    SMART PARKING
 *
 *
 *   Automated Parking Using RFID and Ultrasonic Sensor
 * 
 *  Version 1.2 April,2015
 *
 *
 */
 
/*-----------------------------------------------------------------------------
 * Pin layout should be as follows:
 * Signal     Pin              Pin               Pin			Pin
 *            Arduino Uno      Arduino Mega      SPARK			MFRC522 board
 * ---------------------------------------------------------------------------
 * Reset      9                5                 ANY (D2)		RST
 * SPI SS     10               53                ANY (A2)		SDA
 * SPI MOSI   11               51                A5				MOSI
 * SPI MISO   12               50                A4				MISO
 * SPI SCK    13               52                A3				SCK
 */
//#include <SPI.h>

/***************************************************************************************/

                        /*Ultrasonic variables*/

/***************************************************************************************/

#include "ultrasonicsensor.h"

//#define LEDPin 13 // Onboard LED


#define SLOT_ERROR    0
#define SLOT_EMPTY    1
#define SLOT_OCCUPIED 2
#define SLOT_UNUSABLE 3
#define SLOT_RESERVED 4


int maxDistance = 600;  // Maximum range needed
int minimumRange = 0;   // Minimum range needed
int distance;           // Distance of the object from the sensor
int pollDelay = 1000;   // Set Poll delay of 1 second by default
int parkStatus;
int prevParkStatus;
int minDistance;

/***************************************************************************************/

                        /*RFID variables*/

/***************************************************************************************/


#define MAXRFIDTAGS 10
int currtag = 0;
int nrfidtags = 0;
char rfidtags[MAXRFIDTAGS][10];

#define SS_PIN SS
#define RST_PIN D2

MFRC522 mfrc522(SS_PIN, RST_PIN);	// Create MFRC522 instance.



/***************************************************************************************/

                        /*setup*/

/***************************************************************************************/
void setup() 
{ 
    /*RFID*/
    Serial.begin(9600);
	SPI.begin();			// Init SPI bus
	SPI.setClockDivider(SPI_CLOCK_DIV8);
	mfrc522.PCD_Init();	// Init MFRC522 card
	Serial.println("Scan PICC to see UID and type...");



 /*ultrasonic distance meter*/
// Set pin modes for Echo and Trigger pins 
 ultrasonicsensorInit();

 
 // Expose required functions and variables
 Spark.function("setSlotParam", setSlotParam);
 
 Spark.function("setSlotstatus", setSlotStatus);
 
 Spark.variable("distance", &distance, INT);
 
 Spark.variable("parkStatus", &parkStatus, INT);
 
 parkStatus = SLOT_EMPTY;
 prevParkStatus = SLOT_EMPTY;
 minDistance = 100;

}
void checkRFIDval(MFRC522 mfrc522);
   
/***************************************************************************************/

                       /*loop*/

/***************************************************************************************/
void loop() 
{


 /*******************************************************/
                     /* RFID*/
/*******************************************************/
	// Look for new cards
	if ( ! mfrc522.PICC_IsNewCardPresent()) 
	{
	  
		return;
		
	}

	// Select one of the cards
	if ( ! mfrc522.PICC_ReadCardSerial()) 
	{
		return;
	}
	
	// Dump debug info about the card. PICC_HaltA() is automatically called.
  	mfrc522.PICC_DumpToSerial(&(mfrc522.uid));
   // checkRFIDval(mfrc522);
   
   



 /*******************************************************/
              /* Ultrasonic*/
/*******************************************************/
  if (parkStatus != SLOT_UNUSABLE)
  {
      prevParkStatus = parkStatus;
      parkStatus = getSlotStatus();
  } 
  
  if ((prevParkStatus == SLOT_OCCUPIED) && (parkStatus == SLOT_EMPTY))
      Spark.publish("slotEvent", "EMPTY");                              //publish the rfid tag no. when status changes
      checkRFIDval(mfrc522);
      
  if ((prevParkStatus == SLOT_EMPTY) && (parkStatus == SLOT_OCCUPIED))
      Spark.publish("slotEvent", "OCCUPIED");                         
       checkRFIDval(mfrc522);
       
  if ((prevParkStatus == SLOT_RESERVED) && (parkStatus == SLOT_OCCUPIED))
      Spark.publish("slotEvent", "OCCUPIED");                         
       checkRFIDval(mfrc522);
       
  if (parkStatus == SLOT_ERROR)
      Spark.publish("slotEvent", "ERROR");
 
 //  Delay before next reading.
 delay(pollDelay);
}
 
 
/***************************************************************************************/

                        /*RFID functions*/

/***************************************************************************************/

void checkRFIDval(MFRC522 mfrc522)
{
    /*initialization*/
    char publishString[40];
    int i;
    char rfidstr[15];
    rfidstr[0] = 0;
    char s[10];
    int tagfound = 0;
    int publish = 0;
      

    for (i = 0; i < mfrc522.uid.size; i++)
    {
        
        sprintf(s, "%02x", mfrc522.uid.uidByte[i]);
        strcat( &rfidstr[i*2] , s);
    }
 

    for (i = 0; i < nrfidtags;i++)
    {

        if (strcmp (rfidstr, rfidtags[i]) == 0)
        {
            Serial.println("FOUND RFID = ");
            Serial.println(rfidstr); //for debugging
            if(publish==0)
            {
            sprintf(publishString,"{\"RFIDVAL\": %s}",rfidstr); //string to publish in json format
            Spark.publish("RFIDVAL",publishString);//publish that RFID tag
            publish=1;
            }
            tagfound = 1;
           break;
        }
    }      

}
/***************************************************************************************/

                        /*Ultrasonic functions*/

/***************************************************************************************/

/*function to get the slot status*/
int getSlotStatus()
{
     int stat;
     distance = getDistance();
 
     if (distance < minDistance)
     {
         stat = SLOT_ERROR;
     }
     else if (distance < maxDistance)
     {
         stat = SLOT_OCCUPIED;
         Spark.publish("status_occupied");
         
     }
     else
     {
         stat = SLOT_EMPTY;
     }
     return stat;
}


/*set delay and other parameters*/
int setSlotParam(String command)
{
    int retval;
    
    if (command.substring(0, 4) == "stat=")
       retval = setSlotStatus( command.substring(5) );
       
    if (command.substring(0, 4) == "dely=")
       retval = setPollDelay( command.substring(5) );
       
    if (command.substring(0, 4) == "mind=")
       retval = setMinDistance( command.substring(5) ); 
       
    return retval;       
}    
       
    
/* Allow the slot status to be set  over the internet */
int setSlotStatus(String strStatus)
{
   if (strStatus == "UNSABLE")
      parkStatus = SLOT_UNUSABLE;
      
   if (strStatus == "RESERVED")
      parkStatus = SLOT_RESERVED;
      
   if (strStatus == "EMPTY")
      parkStatus = SLOT_EMPTY;
      
    if (strStatus == "OCCUPIED")
      parkStatus = SLOT_OCCUPIED;
 
   return parkStatus;
}

/*Allow rhe minimum distance to be set over the internet */
int  setMinDistance(String strDistance)
{
   minDistance = strDistance.toInt();
   return minDistance;
}

/*Allow delay to be set over the internet*/
int setPollDelay(String strPollDelay)
{
   pollDelay = strPollDelay.toInt();
   return pollDelay;
}




/*Ultrasonic Sensor*/

void ultrasonicsensorInit()
{
 pinMode(TRIGPIN, OUTPUT);
 pinMode(ECHOPIN, INPUT);
}

int getDistance()
{
   long duration;
   
 
   sendTriggerPulse(TRIGPIN);
   duration = pulseIn(ECHOPIN);
   double distance = duration/58.2; //Distance formula
   Serial.println("diatance = "); 
   Serial.println(distance); //for debugging
   return (int) distance;
 
}

void sendTriggerPulse(int pin)
{
    
   digitalWrite(TRIGPIN, HIGH); 
   delayMicroseconds(10); 
   digitalWrite(TRIGPIN, LOW);
    
}


long pulseIn(int pin)
{
    long startTime, endTime, duration;
    waitForEcho(ECHOPIN, HIGH, 100);
    startTime = micros();
    waitForEcho(ECHOPIN, LOW, 100);
    endTime = micros();
    duration = endTime - startTime;
    return duration;
}

 /* Time out function*/
void waitForEcho(int pin, int value, long timeout)
{

    long giveupTime = millis() + 100;
    while (digitalRead(pin) != value && millis() < giveupTime);
}

Ultrasonic header file

C/C++
Include this header as well for the program to work. Name it as ultrasonic.h
#define ECHOPIN 6 // Echo Pin
#define TRIGPIN 5 // Trigger Pin
void ultrasonicsensorInit();
int getDistance();

ENTRANCE

C/C++
/***********************RFID logic*******************/
/***************************************************/

/*----------------------------------------------------------------------------- Nicola Coppola
 * Pin layout should be as follows:
 * Signal     Pin              Pin               Pin			Pin
 *            Arduino Uno      Arduino Mega      SPARK			MFRC522 board
 * ---------------------------------------------------------------------------
 * Reset      9                5                 ANY (D2)		RST
 * SPI SS     10               53                ANY (A2)		SDA
 * SPI MOSI   11               51                A5				MOSI
 * SPI MISO   12               50                A4				MISO
 * SPI SCK    13               52                A3				SCK
 *
 * The reader can be found on eBay for around 5 dollars. Search for "mf-rc522" on ebay.com. 
 */
//#include <SPI.h>


#define SS_PIN SS
#define RST_PIN D2

MFRC522 mfrc522(SS_PIN, RST_PIN);	// Create MFRC522 instance.

#define  MAXRFIDTAGS        100
#define  MAXSLOTS           20
#define  NUM_NORMAL_SLOTS   10
#define  NUM_PRIORITY_SLOTS  8
#define  NUM_DISABLED_SLOTS  2

#define  LED_NORMAL_PIN      1
#define  LED_PRIORITY_PIN    2
#define  LED_DISABLED_PIN    3
#define  GATE_LED_PIN        4


//***************************************************
// RFID Cache Object Defintion
// **********************************************************************************
// Each Tag Information will contain the RFID TAG and the parking level. The level
// can be "normal", "priority", "Disabled"
typedef struct RFIDTag
{
    String rfidtag;
    String level;
} RFIDTAG;


// RFID CaCHE Class and member functions
class RFIDCACHE {

public:

    void   addRFID(String rfidstr, String levl);
    void   delRFID(String rfidstr);
    int    chkRFID(String rfidstr);
    int    modRFID(String rfidstr, String levl);
    void   print();
    String getRFID(int index);
    int    chklevel(String rfidstr);
    String getlevel(String rfidstr);

    RFIDCACHE();

private:

    RFIDTAG  rfidtags[MAXRFIDTAGS];    // RFID Tags and associated levels
    int      nrfidtags;                // Current number of rfidtags. The max can be MAXRFIDTAGS
    int      normalrfids;
    int      priorityrfids;
    int      disabledrfids;
};

int led = 13;


#define SLOT_ERROR    0
#define SLOT_EMPTY    1
#define SLOT_OCCUPIED 2
#define SLOT_UNUSABLE 3
#define SLOT_RESERVED 4


int        nrfidtags = 0;                   // Total number of RFID tags in the cache
int        gateOpen = 0;                    // Should the gate be opened?
int        delyVal;                        // default loop() cycle time in milliseconds
RFIDCACHE rfidcache;



// RFID Cache Modules Begin
// RFID Cache constructor
RFIDCACHE::RFIDCACHE()
{
     nrfidtags = 0;
}    

// Compare the RFID tag obtained with the RFID entries in the local cache. 
// Returns the index if the tag is found in the cache, else -1
int RFIDCACHE:: chkRFID(String rfidstr)
{
          
    int i;
    for (i = 0; i < nrfidtags; i++)
    {
        if (rfidstr == rfidtags[i].rfidtag)
        {
           return i;
        }
    }
    return -1;
}

// Compare the RFID tag obtained with the RFID entries in the local cache. 
// Returns 1 if the tag is found in the cache, else 0
/*
int RFIDCACHE:: chklevel(String rfidstr)
{
          
    int i;
    for (i = 0; i < nrfidtags; i++)
    {
        if ((rfidstr == rfidtags[i].rfidtag) && (rfidtags[i].level == slotPrivilage))
           return 1;
    }
    return 0;
}
*/

String RFIDCACHE:: getlevel(String rfidstr)
{

    int i;
    for (i = 0; i < nrfidtags; i++)
    {
        if (rfidstr == rfidtags[i].rfidtag)
        {
           return rfidtags[i].level;
        }
    }
    return "normal";
}


int RFIDCACHE:: modRFID(String rfidstr, String levl)
{
          
    int i;
    for (i = 0; i < nrfidtags; i++)
    {
        if (rfidstr == rfidtags[i].rfidtag) 
        {
           rfidtags[i].level = levl;
           return 1;
        }
    }
    return 0;
}

// Print the RFIDs over Serial terminal
void RFIDCACHE:: print()
{
          
    int i;
    Serial.print("RFID TAGS: ");
    for (i = 0; i < nrfidtags; i++)
        Serial.print(rfidtags[i].rfidtag + " ");
    Serial.println("");
}

// Get the RFIDs for a particular index
String RFIDCACHE:: getRFID(int index)
{         
    return rfidtags[index].rfidtag;
}

// Add RFID tag to the cache. Uses LRU for replacing if the cache is full.
void RFIDCACHE:: addRFID(String rfidstr, String levl = "normal")
{
          
    static int currtag = 0;  // index of the latest added RFID tag in the cache

    // RFID tag already in cache. Return
    if (chkRFID(rfidstr) >= 0) return ;
               
    // If current index has reached the end of the cache, set the current index to 0
    if (currtag == MAXRFIDTAGS)
        currtag = 0;
       // Serial.print("currtag");
    // Place the new RFID in the current index and increment current index
    rfidtags[currtag].rfidtag = rfidstr;
    rfidtags[currtag].level   = levl;
    currtag++;

    // Increment number of rfidtags in the cache. 
    if (nrfidtags != MAXRFIDTAGS)
        nrfidtags++;
}

// Delete RFID tag entry from the cache.
void RFIDCACHE:: delRFID(String rfidstr)
{
    int i;
    int delIndex = nrfidtags;

    if (nrfidtags == 0)
       return;
    for (i = 0; i < nrfidtags;i++)
        if (rfidstr == rfidtags[i].rfidtag)
           delIndex = i;

    nrfidtags--;
    
    for (i = delIndex; i < nrfidtags; i++)
    {
        rfidtags[i].rfidtag = rfidtags[i+1].rfidtag;
        rfidtags[i].level   = rfidtags[i+1].level;    
    }
}

//*******************************************************************************
// End RFID Cache management object defintion
//*******************************************************************************


//***************************************************
// PARKED RFID LIST MAnagement
// **********************************************************************************
// Each Tag Information will contain the RFID TAG and the parking level. The level
// can be "normal", "priority", "Disabled"
typedef struct parkedtag
{
    String rfidtag;
    int    isParked;
} PARKEDTAG;


// RFID CaCHE Class and member functions
class PARKEDRFID {

public:

    int    addRFID(String rfidstr);
    int    delRFID(String rfidstr);
    int    chkRFID(String rfidstr);
    void   print();
    String getRFID(int );
    int    chkAvailable(String rfidstr);
    int    isSlotAvailable(String level);


    PARKEDRFID();

private:

    PARKEDTAG  rfidtags[MAXSLOTS];       // RFID Tags and associated levels
    int        nrfidtags;                // Current number of rfidtags. The max can be MAXRFIDTAGS
    int        normalrfids;
    int        priorityrfids;
    int        disabledrfids;
};


PARKEDRFID parkedrfid;                     // RFIDs of cars in the parking lot


// PARKEDRFID Modules Begin
// RFID Cache constructor
PARKEDRFID ::PARKEDRFID()
{
    int i;
    nrfidtags = 0;
    normalrfids = 0;
    priorityrfids = 0;
    disabledrfids = 0;

    for (i = 0; i < MAXSLOTS; i++)
    {
        rfidtags[i].rfidtag = "";
        rfidtags[i].isParked = 0;
    }
}    

// Compare the RFID tag obtained with the RFID entries in the local cache. 
// Returns the index if the tag is found in the cache, else -1
int PARKEDRFID:: chkRFID(String rfidstr)
{
          
    int index;
    for (index = 0; index < MAXSLOTS; index++)
        if ((rfidstr == rfidtags[index].rfidtag) && (rfidtags[index].isParked == 1))
           return index;
    return -1;
}

// Compare the RFID tag obtained with the RFID entries in the local cache. 
// Returns 1 if the tag is found in the cache, else 0
int PARKEDRFID :: chkAvailable(String rfidstr)
{
          
    int avail = 0;
    String level; 

    level = "" + rfidcache.getlevel(rfidstr);

    // "normal" RFIDs can park only in normal slots
    if (level == "normal")
         avail = normalrfids;
    // "priority" RFIDs can park only  normal slots and priority slot
    if (level == "priority")
         avail = priorityrfids + normalrfids;
    // "disabled" RFIDs can park in any slot
    if (level == "disabled")
         avail = disabledrfids + priorityrfids + normalrfids;   
    return avail;
}


// Print the RFIDs over Serial terminal
void PARKEDRFID:: print()
{
          
    int i;
    Serial.print("RFID TAGS: ");
    for (i = 0; i < MAXSLOTS; i++)
        Serial.print(rfidtags[i].rfidtag + " ");
    Serial.println("");
}

// Check the availability in different types of slots
int PARKEDRFID:: isSlotAvailable(String level)
{         
   
    if ( (level == "normal"   && normalrfids < NUM_NORMAL_SLOTS)   ||
         (level == "priority" && priorityrfids < NUM_PRIORITY_SLOTS) ||
         (level == "disabled" && disabledrfids < NUM_DISABLED_SLOTS))
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

// Add RFID tag to the cache. Uses LRU for replacing if the cache is full.
int PARKEDRFID :: addRFID(String rfidstr)
{
             
    int index;
    String level;

    // Is it a legal RFID. If not, return. 
    if (rfidcache.chkRFID(rfidstr) < 0) return 0;

    // RFID tag already in Parked List. Return
    if (parkedrfid.chkRFID(rfidstr) >= 0) return 0;
    
    level = "" + rfidcache.getlevel(rfidstr);

    // No place to park the car, return.
    if (parkedrfid.isSlotAvailable(level) == 0)
        return 0;
    
    // Get the first free entry in the list of parked rfids. This is done by checking the isAvailable flag.
    for (index = 0; index < MAXSLOTS; index++)
       if (rfidtags[index].isParked == 0)           
           break;

    // STore the new rfids in the free slot and mark the list entry as full.
    rfidtags[index].rfidtag = rfidstr;
    rfidtags[index].isParked = 1;


    // Increment counters based on priority
    if (level == "normal")
      digtalWrite(LED_NORMAL_PIN ,HIGH);
      delay(100);
      digitalWrite(LED_NORMAL_PIN ,LOW);
       normalrfids++;
    if (level == "priority")
     digtalWrite(LED_PRIORITY_PIN  ,HIGH);
      delay(100);
      digitalWrite(LED_PRIORITY_PIN  ,LOW);
       priorityrfids++;
    if (level == "disabled")
     digtalWrite(LED_DISABLED_PIN ,HIGH);
      delay(100);
      digitalWrite(LED_DISABLED_PIN ,LOW);
       disabledrfids++;
    if (nrfidtags != MAXSLOTS)
       nrfidtags++;
    
    return 1;
                    
}

// Delete RFID tag entry from the cache.
int PARKEDRFID :: delRFID(String rfidstr)
{

    int delindex;
    String level;

    // RFID tag already is not in cache. Return
    delindex = chkRFID(rfidstr);
    if (delindex < 0) return 0;

    // Make the isAvailable flag as 0 to indicate that the list entry is free.
    rfidtags[delindex].isParked = 0;

    // Decrement counters based on RFID priority
    level = "" + rfidcache.getlevel(rfidstr);
    if (level == "normal")
       normalrfids--;
    if (level == "priority")
       priorityrfids--;
    if (level == "disabled")
       disabledrfids--;
    if (nrfidtags != 0)
       nrfidtags--;

    return delindex;
}

//*******************************************************************************
// End PARKED RFID Cache management object defintion
//*******************************************************************************





void lightLed()
{
    
    digitalWrite(LED_NORMAL_PIN, parkedrfid.isSlotAvailable("normal"));
    digitalWrite(LED_PRIORITY_PIN, parkedrfid.isSlotAvailable("priority"));
    digitalWrite(LED_DISABLED_PIN, parkedrfid.isSlotAvailable("disabled"));   
}

void openGate()
{
   digitalWrite(GATE_LED_PIN, HIGH);   
   delay(3000);
   digitalWrite(GATE_LED_PIN, LOW);   
}

void closeGate()
{
}



//*******************************************************************************
//  End magnetic sensor functions. 
//*******************************************************************************
 
void setup() 
{  

      pinMode(LED_NORMAL_PIN, OUTPUT);
      pinMode(LED_PRIORITY_PIN, OUTPUT);
      pinMode(LED_DISABLED_PIN, OUTPUT);
      pinMode(GATE_LED_PIN, OUTPUT);
      Serial.begin(9600);
      SPI.begin();                                    // Init SPI bus
      SPI.setClockDivider(SPI_CLOCK_DIV8);  
      mfrc522.PCD_Init();                             // Init MFRC522 card
      Serial.println("Scan PICC to see UID and type...");
      Spark.function("SetParams", setParams); // Function that the server can call to set variables.
}

   
void loop() 
{

      String publishString;

      // Check if someone has tapped with the RFID. This can be for getting in or getting out
      if (checkAndProcessRFID() == 1)
      {
          mfrc522.PICC_DumpToSerial(&(mfrc522.uid));
          openGate();                   
      }

      lightLed();

      // There is nothing to be done if carParked == 0 && slotStatus == FREE or carParked == 1 and SlotStatus =  OCCUPIED
      // NO RFID Check required for normal slots
     
      delay(delyVal); 
}         


// Function to be used for setting SlotParams
int setParams(String args)
{

    int retval = 1;
    String cmd, cmdarg1, cmdarg2;
    char   cargs[30], ccmd[20], carg1[20], carg2[20];
    
    args.toCharArray(cargs, 30);
    sscanf(cargs, "%s%s%s", ccmd, carg1, carg2);
    
    cmd = ccmd;
    cmdarg1 = carg1;
    cmdarg2 = carg2;
    
    // Format of the args : <command> <arg1> <arg2>
    // Example 
    // delrfid <RFID>
    // addrfid <RFID> <level>
    // modrfid <RFID> <level>
    // delyval <delay value in milliseconds>

  
    if (cmd == "delrfid")
    {
       rfidcache.delRFID(cmdarg1);
    }
    if (cmd == "addrfid")
    {
       rfidcache.addRFID(cmdarg1, cmdarg2 );
    }
    else if (cmd == "modrfid")
    {
       rfidcache.modRFID(cmdarg1, cmdarg2 );
    }
    else if (cmd == "delyval")
    {
       delyVal = cmdarg1.toInt();
    }
    else 
    {
       retval = 0;
    }    
   
    return retval;;
}




// This function takes the card id information provided on the serial port. 
// It checks if the card is available in the local cache. If 
// there is a match found, publish the rfid tag value.
int checkAndProcessRFID()
{
    int  i;
    String rfidstr;               // Placeholder to get the RFID value in string format
    char s1[20], s2[20];
    int  gateOpen = 0;
    
    // Look for card flash If no card is found, 
    // return to the function after delyVal time units
    if ( ! mfrc522.PICC_IsNewCardPresent()) 
       return 0;

    // if card has been found, but no data is coming off the serial port on card id, return
    if ( ! mfrc522.PICC_ReadCardSerial()) 
         return 0;

    // Somebody has flashed a card. Check and process RFID tag value against 
    // the stored ones. RFID value is contained in the uid field. It is in 
    // binary format stored byte by byte in uiDByte At the end of the loop, 
    // rfidstr contains the RFID value as a string of hex numbers
    for (i = 0; i < mfrc522.uid.size; i++)
    {
        
        sprintf(s1, "%02x", mfrc522.uid.uidByte[i]);
        strcat(&s2[i*2], s1);
    }
    rfidstr = "" + String(s2);

    // Check if the vehicle is trying to get out.
    if (parkedrfid.chkRFID(rfidstr) >= 0)
    {
        // found in the list. delete the RFID. Indicate to the server that the car with RFID exited
        parkedrfid.delRFID(rfidstr);
        String publishString =  rfidstr + "exited"; // Put it in the JSON format                   
        Spark.publish( "RFIDVAL" , publishString);              // publish that RFID tag   
        Serial.println("Car exited with RFID =  ");
        Serial.println(rfidstr);  
    }
    else
    {
        // Vehicle is getting in. Check if there is space.
        if (parkedrfid.addRFID(rfidstr) == 1)
        {
           String publishString =  rfidstr + "entered";   // Put it in the JSON format   
           Spark.publish( "RFIDVAL" , publishString);              // publish that RFID tag   
           Serial.println("Car exited with RFID =  ");
           gateOpen = 1;
           Serial.println(rfidstr); 
        }   
    }

  
    /*
    if (gateOpen == 1)
    {
        String publishString = "{\"RFIDVAL\":" + rfidstr + "}"; // Put it in the JSON format
        Spark.publish( "RFIDVAL" , publishString);              // publish that RFID tag         
    }
    */
    
 
    return gateOpen;
}

Credits

Green Robot Machinery

Green Robot Machinery

7 projects • 41 followers
Green Robot Machinery Private Limited

Comments