Hey all,
I’m attempting to build an RFID scanner using the Particle Core. There seems to be some inconsistent issues with emitting events.
After booting the device it’ll be able to emit events when a fob is tapped, but occasionally after some amount of time not being used then it won’t emit events. After resetting the device it then begins to work again.
Here’s the code I’m working with:
// This #include statement was automatically added by the Particle IDE.
#include <SparkJson.h>
// This #include statement was automatically added by the Particle IDE.
#include <MFRC522.h>
// This #include statement was automatically added by the Particle IDE.
// This #include statement was automatically added by the Particle IDE.
#include <MFRC522.h>
#define SS_PIN A2
#define RST_PIN D3
int red = D0;
int blue = D1;
int green = D2;
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
void myHandler(const char *event, const char *data) {
// Handle the integration response
digitalWrite(blue, LOW);
String str = String(data);
Serial.println(str);
if (str == "true") {
digitalWrite(green, HIGH);
delay(1000);
digitalWrite(green, LOW);
} else {
digitalWrite(red, HIGH);
delay(1000);
digitalWrite(red, LOW);
}
}
void setup() {
Serial.begin(9600); // Initialize serial communications with the PC
Serial.println("Hello!");
SPI.begin();
SPI.setClockDivider(SPI_CLOCK_DIV8);
mfrc522.PCD_Init();
Particle.subscribe("hook-response/rfid_scan", myHandler, MY_DEVICES);
Particle.publish("device_setup", "Device has booted", 60, PRIVATE);
pinMode(red, OUTPUT);
pinMode(green, OUTPUT);
pinMode(blue, OUTPUT);
}
void loop() {
// Look for new cards, if nothing found, quit
if ( ! mfrc522.PICC_IsNewCardPresent()) {
return;
}
// Select one of the cards, if nothing found, quit
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);
}
digitalWrite(blue, HIGH);
Particle.publish("rfid_scan", cardID, 60, PRIVATE);
Serial.println(cardID);
mfrc522.PICC_HaltA();
}
As you can see I’m not using any sleeps, so any ideas on what I might be doing wrong?