This commit is contained in:
TheJoKlLa 2022-10-20 12:19:25 +02:00
parent 3932b57660
commit 64885e73d4
8 changed files with 375 additions and 313 deletions

View File

@ -6,20 +6,24 @@
NFC::NFC(int pin_ss, int pin_rst) NFC::NFC(int pin_ss, int pin_rst)
{ {
SPI.begin();
rfid = new DESFire(pin_ss, pin_rst); rfid = new DESFire(pin_ss, pin_rst);
SPI.begin();
rfid->PCD_Init(); rfid->PCD_Init();
rfid->PCD_DumpVersionToSerial();
} }
bool NFC::hasNewCard() bool NFC::requestCard()
{ {
if(rfid->PICC_IsNewCardPresent() && rfid->PICC_ReadCardSerial() && rfid->uid.sak == 0x20) byte bufferATQA[2];
{ byte bufferSize = sizeof(bufferATQA);
Serial.println("New Card detected"); rfid->PCD_WriteRegister(rfid->TxModeReg, 0x00);
return true; rfid->PCD_WriteRegister(rfid->RxModeReg, 0x00);
} rfid->PCD_WriteRegister(rfid->ModWidthReg, 0x26);
return false;
MFRC522::StatusCode status = rfid->PICC_RequestA(bufferATQA, &bufferSize);
Serial.printf("PICC_RequestA: 0x%02x\n", status);
return status == MFRC522::STATUS_OK;
} }
MFRC522::Uid NFC::getUID() MFRC522::Uid NFC::getUID()
@ -29,10 +33,28 @@ MFRC522::Uid NFC::getUID()
bool NFC::selectCard() bool NFC::selectCard()
{ {
MFRC522::StatusCode status; MFRC522::StatusCode status = rfid->PICC_Select(&uid);
Serial.printf("PICC_Select: 0x%02x\n", status);
if(status != MFRC522::STATUS_OK)
{
return false;
}
if(rfid->uid.sak != 0x20)
{
Serial.printf("SAK Incorret: 0x%02x\n", rfid->uid.sak);
}
byte ats_buffer[16]; byte ats_buffer[16];
byte ats_buffer_len = 16; byte ats_buffer_len = 16;
status = rfid->PICC_RequestATS(ats_buffer, &ats_buffer_len); status = rfid->PICC_RequestATS(ats_buffer, &ats_buffer_len);
if (status != MFRC522::STATUS_OK) if (status != MFRC522::STATUS_OK)
{ {
@ -40,7 +62,7 @@ bool NFC::selectCard()
return false; return false;
} }
status = rfid->PICC_ProtocolAndParameterSelection(0x00, 0x11, 0x00); status = rfid->PICC_ProtocolAndParameterSelection(0x00, 0x11);
if (status != MFRC522::STATUS_OK) if (status != MFRC522::STATUS_OK)
{ {
Serial.printf("PICC_ProtocolAndParameterSelection: 0x%02x\n", status); Serial.printf("PICC_ProtocolAndParameterSelection: 0x%02x\n", status);
@ -86,6 +108,8 @@ bool NFC::isCardLost()
control += 0x4; control += 0x4;
} }
Serial.printf("Control: %d\n", control);
if(!(control == 13 || control == 14)) if(!(control == 13 || control == 14))
{ {
Serial.println("Card Lost"); Serial.println("Card Lost");
@ -99,7 +123,7 @@ bool NFC::isCardLost()
MFRC522::StatusCode NFC::Transceive(byte* command, byte command_len, byte* response, byte* response_len) MFRC522::StatusCode NFC::Transceive(byte* command, byte command_len, byte* response, byte* response_len)
{ {
byte buffer[APDU_BUFFER_SIZE + 4]; byte buffer[63 + 4];
byte buffer_len = command_len + 4; byte buffer_len = command_len + 4;
MFRC522::StatusCode status; MFRC522::StatusCode status;
@ -114,12 +138,10 @@ MFRC522::StatusCode NFC::Transceive(byte* command, byte command_len, byte* respo
return status; return status;
} }
// Serial.println(buffer_len); Serial.println(buffer_len);
// char command_h[APDU_BUFFER_SIZE*2] = { 0 }; printbytes(buffer, buffer_len);
// bytes2chars(buffer, buffer_len, command_h, true);
// Serial.println(command_h);
status = rfid->PCD_TransceiveData(buffer, buffer_len, response, response_len); status = rfid->PCD_TransceiveData(buffer, buffer_len, response, response_len, NULL, 0, true);
if (status != MFRC522::STATUS_OK) if (status != MFRC522::STATUS_OK)
{ {
Serial.printf("PCD_TransceiveData: 0x%02x\n", status); Serial.printf("PCD_TransceiveData: 0x%02x\n", status);

View File

@ -15,7 +15,7 @@ class NFC
public: public:
DESFire* rfid; DESFire* rfid;
NFC(int pin_ss, int pin_rst); NFC(int pin_ss, int pin_rst);
bool hasNewCard(); bool requestCard();
bool selectCard(); bool selectCard();
bool deselectCard(); bool deselectCard();
bool isCardLost(); bool isCardLost();

View File

@ -59,3 +59,12 @@ void bytes2chars(byte* array, byte array_len, char* str, bool msb)
} }
} }
} }
void printbytes(byte* array, byte array_len)
{
for(int i = 0; i < array_len; i++)
{
Serial.printf("%02x", array[i]);
}
Serial.println();
}

View File

@ -8,4 +8,6 @@ void chars2bytes(char* str, byte* array, bool msb);
void byte2char(byte* array, char* str); void byte2char(byte* array, char* str);
void bytes2chars(byte* array, byte array_len, char* str, bool msb); void bytes2chars(byte* array, byte array_len, char* str, bool msb);
void printbytes(byte* array, byte array_len);
#endif #endif

View File

@ -1,145 +1,197 @@
#include <Arduino.h>
#include <ArduinoLog.h>
#include "config.h"
#include "pins.h"
#include "nfc.h"
#include "otaproxy.h"
#include "helpers.h"
#include "Desfire.h"
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <WiFiClient.h>
#include <PubSubClient.h>
#include <SPI.h>
#include <SSD1306Wire.h>
WiFiClient espClient;
//Config_Data config;
PubSubClient* mqtt;
NFC* nfc;
OTAProxy* ota;
void setup_wifi()
{
delay(10);
Serial.println("Connecting Wifi ...");
WiFi.mode(WIFI_STA);
WiFi.begin(WLAN_SSID, WLAN_PASS);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
}
randomSeed(micros());
Serial.println("WiFi connected");
}
void reconnect()
{
while (!mqtt->connected())
{
String clientId = "FabReader_";
clientId += String(FABREADERID);
Serial.println("Connecting MQTT ...");
if (mqtt->connect(clientId.c_str()))
{
Serial.println("MQTT connected");
char id[6] = "00000";
sprintf(id, "%05d", FABREADERID);
mqtt->publish("fabreader", id);
char topic_requestOTA[] = "fabreader/00000/requestOTA";
sprintf(topic_requestOTA, "fabreader/%05d/requestOTA", FABREADERID);
mqtt->subscribe(topic_requestOTA);
char topic_stopOTA[] = "fabreader/00000/stopOTA";
sprintf(topic_stopOTA, "fabreader/%05d/stopOTA", FABREADERID);
mqtt->subscribe(topic_stopOTA);
} else {
Serial.print("failed, rc=");
Serial.print(mqtt->state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}
void callback(char* topic, byte* payload, unsigned int length)
{
Serial.println("Receive Message");
Serial.println(topic);
if(ota->hasActiveOTA())
{
ota->continueOTA(topic, payload, length);
}
}
void setup()
{
Serial.begin(115200);
Serial.print("\n\n\n");
Serial.println("Booting ...");
pinMode(PIN_BUZZER, OUTPUT);
pinMode(PIN_BUTTON, INPUT);
pinMode(PIN_ETH_SPI_SS, OUTPUT);
digitalWrite(PIN_ETH_SPI_SS, HIGH);
Serial.println("Connecting NFC ...");
nfc = new NFC(PIN_RFID_SPI_SS, PIN_RFID_RST);
if(!(nfc->rfid->PCD_PerformSelfTest()))
{
Serial.println("NFC Test failed");
}
Serial.println("NFC connected");
// setup_wifi();
// mqtt = new PubSubClient(espClient);
// mqtt->setServer(MQTT_BROKER, 1883);
// mqtt->setCallback(callback);
// ota = new OTAProxy(mqtt, nfc, FABREADERID);
}
void loop()
{
// if(!(nfc->hasCardSelected()) && nfc->hasNewCard() && nfc->selectCard())
// {
// // DESFire::mifare_desfire_tag tag;
// // tag.pcb = 0x0A;
// // tag.cid = 0x00;
// // DESFire::mifare_desfire_aid_t aid;
// // aid.data[0] = 0x42;
// // aid.data[1] = 0x41;
// // aid.data[2] = 0x46;
// // DESFire::StatusCode status = nfc->rfid->MIFARE_DESFIRE_SelectApplication(&tag, &aid);
// // Serial.printf("%02x\n", status.mfrc522);
// // char command_h[63*2] = "905a00000342414600";
// // byte command[63] = { 0 };
// // byte command_len = strlen(command_h)/2;
// // byte response[63] = { 0 };
// // byte response_len = 0;
// // char response_h[63*2] = { 0 };
// // Serial.print("Command: ");
// // Serial.println(command_h);
// // chars2bytes(command_h, command, true);
// // MFRC522::StatusCode status = nfc->Transceive(command, command_len, response, &response_len);
// // Serial.printf("PICC_Tranceive: 0x%02x\n", status);
// // bytes2chars(response, response_len, response_h, true);
// // Serial.print("Response: ");
// // Serial.println(response_h);
// }
// if(nfc->hasCardSelected() && nfc->isCardLost())
// {
// nfc->deselectCard();
// }
if( nfc->hasCardSelected() ) {
if( nfc->isCardLost() ) {
nfc->deselectCard();
Serial.println("Stop");
}
}
else {
if( nfc->hasNewCard() ) {
if( nfc->selectCard() ) {
Serial.println("Run");
}
}
}
/* /*
* -------------------------------------------------------------------------------------------------------------------- if(!(nfc->hasCardSelected()) && nfc->hasNewCard())
* Example sketch/program showing how to read data from a PICC to serial. {
* -------------------------------------------------------------------------------------------------------------------- if(nfc->selectCard())
* This is a MFRC522 library example; for further details and other examples see: https://github.com/miguelbalboa/rfid {
* Serial.println("Run");
* Example sketch/program showing how to read data from a PICC (that is: a RFID Tag or Card) using a MFRC522 based RFID }
* Reader on the Arduino SPI interface. }
*
* When the Arduino and the MFRC522 module are connected (see the pin layout below), load this sketch into Arduino IDE if(nfc->hasCardSelected() && nfc->isCardLost())
* then verify/compile and upload it. To see the output: use Tools, Serial Monitor of the IDE (hit Ctrl+Shft+M). When {
* you present a PICC (that is: a RFID Tag or Card) at reading distance of the MFRC522 Reader/PCD, the serial output Serial.println("Stop");
* will show the ID/UID, type and any data blocks it can read. Note: you may see "Timeout in communication" messages }
* when removing the PICC from reading distance too early.
*
* If your reader supports it, this sketch/program will read all the PICCs presented (that is: multiple tag reading).
* So if you stack two or more PICCs on top of each other and present them to the reader, it will first output all
* details of the first and then the next PICC. Note that this may take some time as all data blocks are dumped, so
* keep the PICCs at reading distance until complete.
*
* @license Released into the public domain.
*
* Typical pin layout used:
* -----------------------------------------------------------------------------------------
* MFRC522 Arduino Arduino Arduino Arduino Arduino
* Reader/PCD Uno/101 Mega Nano v3 Leonardo/Micro Pro Micro
* Signal Pin Pin Pin Pin Pin Pin
* -----------------------------------------------------------------------------------------
* RST/Reset RST 9 5 D9 RESET/ICSP-5 RST
* SPI SS SDA(SS) 10 53 D10 10 10
* SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16
* SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14
* SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15
*/ */
#include <SPI.h> // if (!mqtt->connected())
#include <MFRC522.h> // {
#include <Desfire.h> // reconnect();
// }
// mqtt->loop();
#define RST_PIN 9 // Configurable, see typical pin layout above // if(ota->hasActiveOTA() && nfc->isCardLost())
#define SS_PIN 10 // Configurable, see typical pin layout above // {
// ota->cancelOTA();
// }
#include "Pins.h" // if(!ota->hasActiveOTA() && nfc->hasNewCard())
// {
DESFire mfrc522(PIN_RFID_SPI_SS, PIN_RFID_RST); // Create MFRC522 instance // ota->startOTA();
// }
void setup() {
Serial.begin(115200); // Initialize serial communications with the PC
while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522
mfrc522.PCD_DumpVersionToSerial(); // Show details of PCD - MFRC522 Card Reader details
Serial.println(F("Scan PICC to see UID, SAK, type, and data blocks..."));
}
void loop() {
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent()) {
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial()) {
return;
}
if (mfrc522.uid.sak != 0x20) {
// Dump debug info about the card; PICC_HaltA() is automatically called
mfrc522.PICC_DumpToSerial(&(mfrc522.uid));
return;
}
// Show an extra line
Serial.println();
DESFire::mifare_desfire_tag tag;
DESFire::StatusCode response;
tag.pcb = 0x0A;
tag.cid = 0x00;
memset(tag.selected_application, 0, 3);
// Make sure none DESFire status codes have DESFireStatus code to OK
response.desfire = DESFire::MF_OPERATION_OK;
byte ats[16];
byte atsLength = 16;
response.mfrc522 = mfrc522.PICC_RequestATS(ats, &atsLength);
if ( ! mfrc522.IsStatusCodeOK(response)) {
Serial.println(F("Failed to get ATS!"));
Serial.println(mfrc522.GetStatusCodeName(response));
Serial.println(response.mfrc522);
mfrc522.PICC_HaltA();
return;
}
// TODO: Should do checks but since I know my DESFire allows and requires PPS...
// PPS1 is ommitted and, therefore, 0x00 is used (106kBd)
response.mfrc522 = mfrc522.PICC_ProtocolAndParameterSelection(0x00, 0x11);
if ( ! mfrc522.IsStatusCodeOK(response)) {
Serial.println(F("Failed to perform protocol and parameter selection (PPS)!"));
Serial.println(mfrc522.GetStatusCodeName(response));
mfrc522.PICC_HaltA();
return;
}
// MIFARE DESFire should respond to a GetVersion command
DESFire::MIFARE_DESFIRE_Version_t desfireVersion;
response = mfrc522.MIFARE_DESFIRE_GetVersion(&tag, &desfireVersion);
if ( ! mfrc522.IsStatusCodeOK(response)) {
Serial.println(F("Failed to get a response for GetVersion!"));
Serial.println(mfrc522.GetStatusCodeName(response));
mfrc522.PICC_HaltA();
return;
}
// Dump MIFARE DESFire version information.
// NOTE: KEEP YOUR CARD CLOSE TO THE READER!
// This method takes some time and the card will be read
// once output ends! If you remove the card too fast
// a timeout will occur!
mfrc522.PICC_DumpMifareDesfireVersion(&tag, &desfireVersion);
mfrc522.PICC_DumpMifareDesfireMasterKey(&tag);
DESFire::mifare_desfire_aid_t aids[MIFARE_MAX_APPLICATION_COUNT];
byte applicationCount = 0;
response = mfrc522.MIFARE_DESFIRE_GetApplicationIds(&tag, aids, &applicationCount);
if ( ! mfrc522.IsStatusCodeOK(response)) {
Serial.println(F("Failed to get application IDs!"));
Serial.println(mfrc522.GetStatusCodeName(response));
mfrc522.PICC_HaltA();
return;
}
// Dump all applications
for (byte aidIndex = 0; aidIndex < applicationCount; aidIndex++) {
mfrc522.PICC_DumpMifareDesfireApplication(&tag, &(aids[aidIndex]));
}
// Call PICC_HaltA()
mfrc522.PICC_HaltA();
Serial.println();
} }

View File

@ -0,0 +1,145 @@
/*
* --------------------------------------------------------------------------------------------------------------------
* Example sketch/program showing how to read data from a PICC to serial.
* --------------------------------------------------------------------------------------------------------------------
* This is a MFRC522 library example; for further details and other examples see: https://github.com/miguelbalboa/rfid
*
* Example sketch/program showing how to read data from a PICC (that is: a RFID Tag or Card) using a MFRC522 based RFID
* Reader on the Arduino SPI interface.
*
* When the Arduino and the MFRC522 module are connected (see the pin layout below), load this sketch into Arduino IDE
* then verify/compile and upload it. To see the output: use Tools, Serial Monitor of the IDE (hit Ctrl+Shft+M). When
* you present a PICC (that is: a RFID Tag or Card) at reading distance of the MFRC522 Reader/PCD, the serial output
* will show the ID/UID, type and any data blocks it can read. Note: you may see "Timeout in communication" messages
* when removing the PICC from reading distance too early.
*
* If your reader supports it, this sketch/program will read all the PICCs presented (that is: multiple tag reading).
* So if you stack two or more PICCs on top of each other and present them to the reader, it will first output all
* details of the first and then the next PICC. Note that this may take some time as all data blocks are dumped, so
* keep the PICCs at reading distance until complete.
*
* @license Released into the public domain.
*
* Typical pin layout used:
* -----------------------------------------------------------------------------------------
* MFRC522 Arduino Arduino Arduino Arduino Arduino
* Reader/PCD Uno/101 Mega Nano v3 Leonardo/Micro Pro Micro
* Signal Pin Pin Pin Pin Pin Pin
* -----------------------------------------------------------------------------------------
* RST/Reset RST 9 5 D9 RESET/ICSP-5 RST
* SPI SS SDA(SS) 10 53 D10 10 10
* SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16
* SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14
* SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15
*/
#include <SPI.h>
#include <MFRC522.h>
#include <Desfire.h>
#define RST_PIN 9 // Configurable, see typical pin layout above
#define SS_PIN 10 // Configurable, see typical pin layout above
#include "Pins.h"
DESFire mfrc522(PIN_RFID_SPI_SS, PIN_RFID_RST); // Create MFRC522 instance
void setup() {
Serial.begin(115200); // Initialize serial communications with the PC
while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522
mfrc522.PCD_DumpVersionToSerial(); // Show details of PCD - MFRC522 Card Reader details
Serial.println(F("Scan PICC to see UID, SAK, type, and data blocks..."));
}
void loop() {
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent()) {
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial()) {
return;
}
if (mfrc522.uid.sak != 0x20) {
// Dump debug info about the card; PICC_HaltA() is automatically called
mfrc522.PICC_DumpToSerial(&(mfrc522.uid));
return;
}
// Show an extra line
Serial.println();
DESFire::mifare_desfire_tag tag;
DESFire::StatusCode response;
tag.pcb = 0x0A;
tag.cid = 0x00;
memset(tag.selected_application, 0, 3);
// Make sure none DESFire status codes have DESFireStatus code to OK
response.desfire = DESFire::MF_OPERATION_OK;
byte ats[16];
byte atsLength = 16;
response.mfrc522 = mfrc522.PICC_RequestATS(ats, &atsLength);
if ( ! mfrc522.IsStatusCodeOK(response)) {
Serial.println(F("Failed to get ATS!"));
Serial.println(mfrc522.GetStatusCodeName(response));
Serial.println(response.mfrc522);
mfrc522.PICC_HaltA();
return;
}
// TODO: Should do checks but since I know my DESFire allows and requires PPS...
// PPS1 is ommitted and, therefore, 0x00 is used (106kBd)
response.mfrc522 = mfrc522.PICC_ProtocolAndParameterSelection(0x00, 0x11);
if ( ! mfrc522.IsStatusCodeOK(response)) {
Serial.println(F("Failed to perform protocol and parameter selection (PPS)!"));
Serial.println(mfrc522.GetStatusCodeName(response));
mfrc522.PICC_HaltA();
return;
}
// MIFARE DESFire should respond to a GetVersion command
DESFire::MIFARE_DESFIRE_Version_t desfireVersion;
response = mfrc522.MIFARE_DESFIRE_GetVersion(&tag, &desfireVersion);
if ( ! mfrc522.IsStatusCodeOK(response)) {
Serial.println(F("Failed to get a response for GetVersion!"));
Serial.println(mfrc522.GetStatusCodeName(response));
mfrc522.PICC_HaltA();
return;
}
// Dump MIFARE DESFire version information.
// NOTE: KEEP YOUR CARD CLOSE TO THE READER!
// This method takes some time and the card will be read
// once output ends! If you remove the card too fast
// a timeout will occur!
mfrc522.PICC_DumpMifareDesfireVersion(&tag, &desfireVersion);
mfrc522.PICC_DumpMifareDesfireMasterKey(&tag);
DESFire::mifare_desfire_aid_t aids[MIFARE_MAX_APPLICATION_COUNT];
byte applicationCount = 0;
response = mfrc522.MIFARE_DESFIRE_GetApplicationIds(&tag, aids, &applicationCount);
if ( ! mfrc522.IsStatusCodeOK(response)) {
Serial.println(F("Failed to get application IDs!"));
Serial.println(mfrc522.GetStatusCodeName(response));
mfrc522.PICC_HaltA();
return;
}
// Dump all applications
for (byte aidIndex = 0; aidIndex < applicationCount; aidIndex++) {
mfrc522.PICC_DumpMifareDesfireApplication(&tag, &(aids[aidIndex]));
}
// Call PICC_HaltA()
mfrc522.PICC_HaltA();
Serial.println();
}

View File

@ -1,168 +0,0 @@
#include <Arduino.h>
#include <ArduinoLog.h>
#include "config.h"
#include "pins.h"
#include "nfc.h"
#include "otaproxy.h"
#include "helpers.h"
#include "Desfire.h"
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <WiFiClient.h>
#include <PubSubClient.h>
#include <SPI.h>
#include <SSD1306Wire.h>
WiFiClient espClient;
//Config_Data config;
PubSubClient* mqtt;
NFC* nfc;
OTAProxy* ota;
void setup_wifi()
{
delay(10);
Serial.println("Connecting Wifi ...");
WiFi.mode(WIFI_STA);
WiFi.begin(WLAN_SSID, WLAN_PASS);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
}
randomSeed(micros());
Serial.println("WiFi connected");
}
void reconnect()
{
while (!mqtt->connected())
{
String clientId = "FabReader_";
clientId += String(FABREADERID);
Serial.println("Connecting MQTT ...");
if (mqtt->connect(clientId.c_str()))
{
Serial.println("MQTT connected");
char id[6] = "00000";
sprintf(id, "%05d", FABREADERID);
mqtt->publish("fabreader", id);
char topic_requestOTA[] = "fabreader/00000/requestOTA";
sprintf(topic_requestOTA, "fabreader/%05d/requestOTA", FABREADERID);
mqtt->subscribe(topic_requestOTA);
char topic_stopOTA[] = "fabreader/00000/stopOTA";
sprintf(topic_stopOTA, "fabreader/%05d/stopOTA", FABREADERID);
mqtt->subscribe(topic_stopOTA);
} else {
Serial.print("failed, rc=");
Serial.print(mqtt->state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}
void callback(char* topic, byte* payload, unsigned int length)
{
Serial.println("Receive Message");
Serial.println(topic);
if(ota->hasActiveOTA())
{
ota->continueOTA(topic, payload, length);
}
}
void setup()
{
Serial.begin(115200);
Serial.print("\n\n\n");
Serial.println("Booting ...");
pinMode(PIN_BUZZER, OUTPUT);
pinMode(PIN_BUTTON, INPUT);
pinMode(PIN_ETH_SPI_SS, OUTPUT);
digitalWrite(PIN_ETH_SPI_SS, HIGH);
Serial.println("Connecting NFC ...");
nfc = new NFC(PIN_RFID_SPI_SS, PIN_RFID_RST);
if(!(nfc->rfid->PCD_PerformSelfTest()))
{
Serial.println("NFC Test failed");
}
Serial.println("NFC connected");
// setup_wifi();
// mqtt = new PubSubClient(espClient);
// mqtt->setServer(MQTT_BROKER, 1883);
// mqtt->setCallback(callback);
// ota = new OTAProxy(mqtt, nfc, FABREADERID);
}
void loop()
{
if(!(nfc->hasCardSelected()) && nfc->hasNewCard() && nfc->selectCard())
{
// DESFire::mifare_desfire_tag tag;
// tag.pcb = 0x0A;
// tag.cid = 0x00;
// DESFire::mifare_desfire_aid_t aid;
// aid.data[0] = 0x42;
// aid.data[1] = 0x41;
// aid.data[2] = 0x46;
// DESFire::StatusCode status = nfc->rfid->MIFARE_DESFIRE_SelectApplication(&tag, &aid);
// Serial.printf("%02x\n", status.mfrc522);
char command_h[APDU_BUFFER_SIZE*2] = "905a00000342414600";
byte command[APDU_BUFFER_SIZE] = { 0 };
byte command_len = strlen(command_h)/2;
byte response[APDU_BUFFER_SIZE] = { 0 };
byte response_len = 0;
char response_h[APDU_BUFFER_SIZE*2] = { 0 };
Serial.print("Command: ");
Serial.println(command_h);
chars2bytes(command_h, command, true);
MFRC522::StatusCode status = nfc->Transceive(command, command_len, response, &response_len);
Serial.printf("PICC_Tranceive: 0x%02x\n", status);
// bytes2chars(response, response_len, response_h, true);
// Serial.print("Response: ");
// Serial.println(response_h);
}
if(nfc->hasCardSelected() && nfc->isCardLost())
{
nfc->deselectCard();
}
// if (!mqtt->connected())
// {
// reconnect();
// }
// mqtt->loop();
// if(ota->hasActiveOTA() && nfc->isCardLost())
// {
// ota->cancelOTA();
// }
// if(!ota->hasActiveOTA() && nfc->hasNewCard())
// {
// ota->startOTA();
// }
}