From 64885e73d4a029b1e3a0e45a3cece4ce7732c445 Mon Sep 17 00:00:00 2001 From: TheJoKlLa Date: Thu, 20 Oct 2022 12:19:25 +0200 Subject: [PATCH] BackUp --- src/FabReader_v2/README.txt | 2 +- src/FabReader_v2/src/NFC.cpp | 58 +++-- src/FabReader_v2/src/NFC.h | 2 +- src/FabReader_v2/src/helpers.cpp | 9 + src/FabReader_v2/src/helpers.h | 2 + src/FabReader_v2/src/main.cpp | 302 +++++++++++++++----------- src/FabReader_v2/src/main.cpp.desfire | 145 +++++++++++++ src/FabReader_v2/src/main.cpp.old | 168 -------------- 8 files changed, 375 insertions(+), 313 deletions(-) create mode 100644 src/FabReader_v2/src/main.cpp.desfire delete mode 100644 src/FabReader_v2/src/main.cpp.old diff --git a/src/FabReader_v2/README.txt b/src/FabReader_v2/README.txt index 144730a..bd2911a 100644 --- a/src/FabReader_v2/README.txt +++ b/src/FabReader_v2/README.txt @@ -16,7 +16,7 @@ After new Card is on Reader: "Request" <- from Server "Response" -> to Server -Repeat Request and Response until: +Repeat Request and Response until: "Stop" <- from Server "Cancel" -> to Server diff --git a/src/FabReader_v2/src/NFC.cpp b/src/FabReader_v2/src/NFC.cpp index f17ec56..37617fd 100644 --- a/src/FabReader_v2/src/NFC.cpp +++ b/src/FabReader_v2/src/NFC.cpp @@ -6,20 +6,24 @@ NFC::NFC(int pin_ss, int pin_rst) { - SPI.begin(); - rfid = new DESFire(pin_ss, pin_rst); + + SPI.begin(); rfid->PCD_Init(); + rfid->PCD_DumpVersionToSerial(); } -bool NFC::hasNewCard() +bool NFC::requestCard() { - if(rfid->PICC_IsNewCardPresent() && rfid->PICC_ReadCardSerial() && rfid->uid.sak == 0x20) - { - Serial.println("New Card detected"); - return true; - } - return false; + byte bufferATQA[2]; + byte bufferSize = sizeof(bufferATQA); + rfid->PCD_WriteRegister(rfid->TxModeReg, 0x00); + rfid->PCD_WriteRegister(rfid->RxModeReg, 0x00); + rfid->PCD_WriteRegister(rfid->ModWidthReg, 0x26); + + 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() @@ -29,10 +33,28 @@ MFRC522::Uid NFC::getUID() 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_len = 16; + + + status = rfid->PICC_RequestATS(ats_buffer, &ats_buffer_len); if (status != MFRC522::STATUS_OK) { @@ -40,7 +62,7 @@ bool NFC::selectCard() return false; } - status = rfid->PICC_ProtocolAndParameterSelection(0x00, 0x11, 0x00); + status = rfid->PICC_ProtocolAndParameterSelection(0x00, 0x11); if (status != MFRC522::STATUS_OK) { Serial.printf("PICC_ProtocolAndParameterSelection: 0x%02x\n", status); @@ -71,7 +93,7 @@ bool NFC::isCardLost() uint8_t control = 0x00; for(int i=0; i<3; i++) { - if(!rfid->PICC_IsNewCardPresent()) + if( !rfid->PICC_IsNewCardPresent() ) { if(rfid->PICC_ReadCardSerial()) { @@ -86,6 +108,8 @@ bool NFC::isCardLost() control += 0x4; } + Serial.printf("Control: %d\n", control); + if(!(control == 13 || control == 14)) { 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) { - byte buffer[APDU_BUFFER_SIZE + 4]; + byte buffer[63 + 4]; byte buffer_len = command_len + 4; MFRC522::StatusCode status; @@ -114,12 +138,10 @@ MFRC522::StatusCode NFC::Transceive(byte* command, byte command_len, byte* respo return status; } - // Serial.println(buffer_len); - // char command_h[APDU_BUFFER_SIZE*2] = { 0 }; - // bytes2chars(buffer, buffer_len, command_h, true); - // Serial.println(command_h); + Serial.println(buffer_len); + printbytes(buffer, buffer_len); - 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) { Serial.printf("PCD_TransceiveData: 0x%02x\n", status); diff --git a/src/FabReader_v2/src/NFC.h b/src/FabReader_v2/src/NFC.h index 04b0237..747e304 100644 --- a/src/FabReader_v2/src/NFC.h +++ b/src/FabReader_v2/src/NFC.h @@ -15,7 +15,7 @@ class NFC public: DESFire* rfid; NFC(int pin_ss, int pin_rst); - bool hasNewCard(); + bool requestCard(); bool selectCard(); bool deselectCard(); bool isCardLost(); diff --git a/src/FabReader_v2/src/helpers.cpp b/src/FabReader_v2/src/helpers.cpp index 0067c9d..6df7bd3 100644 --- a/src/FabReader_v2/src/helpers.cpp +++ b/src/FabReader_v2/src/helpers.cpp @@ -58,4 +58,13 @@ void bytes2chars(byte* array, byte array_len, char* str, bool msb) byte2char(&array[array_len-2 - i], &str[i*2]); } } +} + +void printbytes(byte* array, byte array_len) +{ + for(int i = 0; i < array_len; i++) + { + Serial.printf("%02x", array[i]); + } + Serial.println(); } \ No newline at end of file diff --git a/src/FabReader_v2/src/helpers.h b/src/FabReader_v2/src/helpers.h index 3e2d5b9..c6e3b42 100644 --- a/src/FabReader_v2/src/helpers.h +++ b/src/FabReader_v2/src/helpers.h @@ -8,4 +8,6 @@ void chars2bytes(char* str, byte* array, bool msb); void byte2char(byte* array, char* str); void bytes2chars(byte* array, byte array_len, char* str, bool msb); +void printbytes(byte* array, byte array_len); + #endif \ No newline at end of file diff --git a/src/FabReader_v2/src/main.cpp b/src/FabReader_v2/src/main.cpp index a157253..812d2a4 100644 --- a/src/FabReader_v2/src/main.cpp +++ b/src/FabReader_v2/src/main.cpp @@ -1,145 +1,197 @@ -/* - * -------------------------------------------------------------------------------------------------------------------- - * 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 +#include +#include "config.h" +#include "pins.h" +#include "nfc.h" +#include "otaproxy.h" +#include "helpers.h" +#include "Desfire.h" + +#include +#include + +#include +#include #include -#include -#include +#include -#define RST_PIN 9 // Configurable, see typical pin layout above -#define SS_PIN 10 // Configurable, see typical pin layout above +WiFiClient espClient; +//Config_Data config; -#include "Pins.h" +PubSubClient* mqtt; +NFC* nfc; +OTAProxy* ota; -DESFire mfrc522(PIN_RFID_SPI_SS, PIN_RFID_RST); // Create MFRC522 instance +void setup_wifi() +{ + delay(10); + Serial.println("Connecting Wifi ..."); -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...")); + WiFi.mode(WIFI_STA); + WiFi.begin(WLAN_SSID, WLAN_PASS); + + while (WiFi.status() != WL_CONNECTED) + { + delay(500); + } + + randomSeed(micros()); + Serial.println("WiFi connected"); } -void loop() { - // Look for new cards - if ( ! mfrc522.PICC_IsNewCardPresent()) { - return; - } +void reconnect() +{ + while (!mqtt->connected()) + { + String clientId = "FabReader_"; + clientId += String(FABREADERID); - // Select one of the cards - if ( ! mfrc522.PICC_ReadCardSerial()) { - return; - } + 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); - if (mfrc522.uid.sak != 0x20) { - // Dump debug info about the card; PICC_HaltA() is automatically called - mfrc522.PICC_DumpToSerial(&(mfrc522.uid)); - return; - } + char topic_requestOTA[] = "fabreader/00000/requestOTA"; + sprintf(topic_requestOTA, "fabreader/%05d/requestOTA", FABREADERID); + mqtt->subscribe(topic_requestOTA); - // Show an extra line - Serial.println(); + 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); + } + } +} - DESFire::mifare_desfire_tag tag; - DESFire::StatusCode response; +void callback(char* topic, byte* payload, unsigned int length) +{ + Serial.println("Receive Message"); + Serial.println(topic); + if(ota->hasActiveOTA()) + { + ota->continueOTA(topic, payload, length); + } +} - 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); +void setup() +{ + Serial.begin(115200); + Serial.print("\n\n\n"); + Serial.println("Booting ..."); - mfrc522.PICC_HaltA(); - return; - } + pinMode(PIN_BUZZER, OUTPUT); + pinMode(PIN_BUTTON, INPUT); + pinMode(PIN_ETH_SPI_SS, OUTPUT); + digitalWrite(PIN_ETH_SPI_SS, HIGH); - // 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; - } + 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"); - // 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); + // setup_wifi(); - mfrc522.PICC_DumpMifareDesfireMasterKey(&tag); + // mqtt = new PubSubClient(espClient); + // mqtt->setServer(MQTT_BROKER, 1883); + // mqtt->setCallback(callback); - 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; - } + // ota = new OTAProxy(mqtt, nfc, FABREADERID); +} - // Dump all applications - for (byte aidIndex = 0; aidIndex < applicationCount; aidIndex++) { - mfrc522.PICC_DumpMifareDesfireApplication(&tag, &(aids[aidIndex])); - } - - // Call PICC_HaltA() - mfrc522.PICC_HaltA(); - Serial.println(); +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()) + { + if(nfc->selectCard()) + { + Serial.println("Run"); + } + } + + if(nfc->hasCardSelected() && nfc->isCardLost()) + { + Serial.println("Stop"); + } + */ + + // if (!mqtt->connected()) + // { + // reconnect(); + // } + // mqtt->loop(); + + // if(ota->hasActiveOTA() && nfc->isCardLost()) + // { + // ota->cancelOTA(); + // } + + // if(!ota->hasActiveOTA() && nfc->hasNewCard()) + // { + // ota->startOTA(); + // } } \ No newline at end of file diff --git a/src/FabReader_v2/src/main.cpp.desfire b/src/FabReader_v2/src/main.cpp.desfire new file mode 100644 index 0000000..a157253 --- /dev/null +++ b/src/FabReader_v2/src/main.cpp.desfire @@ -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 +#include +#include + +#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(); +} \ No newline at end of file diff --git a/src/FabReader_v2/src/main.cpp.old b/src/FabReader_v2/src/main.cpp.old deleted file mode 100644 index e76e1b4..0000000 --- a/src/FabReader_v2/src/main.cpp.old +++ /dev/null @@ -1,168 +0,0 @@ -#include -#include - -#include "config.h" -#include "pins.h" -#include "nfc.h" -#include "otaproxy.h" -#include "helpers.h" -#include "Desfire.h" - -#include -#include - -#include -#include -#include -#include - -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(); - // } -} \ No newline at end of file