RFID Electron and GPS tracker

electron
Tags: #<Tag:0x00007f038f5cb108>

#1

Hi,

I’m connecting a RFID to electron and I also have a ASSET TRACKER V2.

When I connecting RFID on the ASSET Tracker board, it does not work and doesn’t send the scan events.

What am I doing wrong, please help.

Thanks,
Kishan

#include <MFRC522.h>
#include <AssetTracker.h>

#define SS_PIN      A2
#define RST_PIN     D2

int transmittingData = 1;

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

// Creating an AssetTracker named 't' for us to reference
AssetTracker t = AssetTracker();

void setup() {
    
    t.begin();
    t.gpsOn();
    
    Particle.publish("setup", "1");
    SPI.begin();
    SPI.setClockDivider(SPI_CLOCK_DIV8);
    
    mfrc522.setSPIConfig();
    mfrc522.PCD_Init();
    
}

void loop() {
    
    delay(5000);

    t.updateGPS();

    // Proceed only if card scanned
    if ( ! mfrc522.PICC_IsNewCardPresent()) {
    	return;
    }
    
    if ( ! mfrc522.PICC_ReadCardSerial()) {
    	return;
    }
    
    String cardID = "";
    
    for (byte i = 0; i < mfrc522.uid.size; i++) {
        cardID += String(mfrc522.uid.uidByte[i] < 0x10 ? "0" : "");
        cardID += String(mfrc522.uid.uidByte[i], HEX);
    }

    // Log.info("Scan Completed: " + cardID);
    Particle.publish("rfid_scan", cardID);
    mfrc522.PICC_HaltA();
    
    // Send GPS
    if (t.gpsFix()) {
        // Only publish if we're in transmittingData mode 1;
        if (transmittingData) {
            // Short publish names save data!
            Particle.publish("G", t.readLatLon(), 60, PRIVATE);
        }
    }
}


#3

Does the library sample work?
https://build.particle.io/libs/MFRC522/0.1.4/tab/example/DumpInfo.ino


#4

Yes, if I remove the Asset Tracker, it works fine.


#5

One reason might be that the accelerometer on the AssetTracker also uses SPI and the two settings may not be compatible.
You could either use SPI.beginTransaction() or move your RFID reader to SPI1

But if you want to go down the rabbit hole, you could look at the implementation of both libraries and figure out what exactly is the reason and work around that.

BTW, we usually recommend using AssetTrackerRK library as it it more advanced and reliable than the somewhat dated AssetTracker library.


#6

SPI.beginTransaction() - did not work just using the electron without connecting the AssetTracker so I’m using SPI.begin()

how do I use SPI1? I changed the connectors to SPI1 but doesn’t the code needs to say it has to initialize for SPI1 somewhere?

I’m very new to this, appreciate your help.


#7

It’s a pitty, but MFRC522 lib is not kitted out with a constructor that allows to choose another SPI interface. Neither does is support transactions.

Maybe if you file an issue on the GitHub repo @peekay123 may be so kind to update the library that way :wink:


#8

You also cannot use pin A2 for the MFRC522 SS pin. It’s already used for the AssetTracker LIS3DH SS pin, so you need to select a different pin. Any free GPIO can be used (assuming it’s connected properly).