Hello Everyone,
I worked on a RFID sensors with mysensors to be able at the end to control automation in my house when we come/leave home. For the moment, i'm still at prototype stage, but I'm able to communicate to jeedom everything I need almost as i want it.
For this I'm using Jeedom as a controller.
Since i had a lot of trouble to make it work together and a difficult time to find example/documentation on this 2 modules combined (mfrc522 + nrf24l01+), i think it would be nice to share my experience.
I know there is an example using other RFID reader with I2C but i already had 2 MFRC522 available and i like challenge !
this code is based on: https://github.com/miguelbalboa/rfid/blob/master/examples/AccessControl/AccessControl.ino
in summary:
-
I discover nrf24f01+ has a lot of trouble to share SPI with other device
-
I discover mysensors is using EEPROM so if you don't use mysensors eeprom function, you will mess everything up !
-
power regulators for nrf24l01+ are life saver
-
I learn a lot of things with this project, thanks mysensors
goals:
- have a (almost) secure RFID way to control my home : alarms, lights when i arrive or leave, secure some doors from my daughter etc
what is done:
- prototype using breadboard
- network encryption using mysensors
- secure exchange (using RFID ID ...) through mysensors
- Jeedom check
improvements to make:
- signing with mysensors
- code factorisation ? it's too huge ! cant even enable whole debug anymore ...
what it does:
- use a master card to add/remove rfid tag from the node
- check if RFID tag is known from the node.
- if it's known it request a salt from the controler
- then it will hash the ID with the salt using MD5 then sent it to the Controller with the salt.
on the controller side :
- the controller must know the RFID TAG ID
- hash the ID with the salt and check if the result is the same than the node sent. if yes: start some scenarios, if not, send an alert.
Security :
- be aware that myfare classic can be copy
- to prevent network replay i implemented a simple hash + salt challenge. it's not hack proof 100% but i didnt like the ID to go through the air. It would have been way to easy to hack (just sniff packet and replay then).
i'm not an expert in Arduino nor mysensors so please, feel free to give me feedback.
if you want more details i will be happy to share
i code a simple scenario on jeedom to check hash value. if you want, i can share it too
/*
Original source: https://github.com/miguelbalboa/rfid
This example showing a complete Door Access Control System
Simple Work Flow (not limited to) :
+---------+
+----------------------------------->READ TAGS+^------------------------------------------+
| +--------------------+ |
| | | |
| | | |
| +----v-----+ +-----v----+ |
| |MASTER TAG| |OTHER TAGS| |
| +--+-------+ ++-------------+ |
| | | | |
| | | | |
| +-----v---+ +----v----+ +----v------+ |
| +------------+READ TAGS+---+ |KNOWN TAG| |UNKNOWN TAG| |
| | +-+-------+ | +-----------+ +------------------+ |
| | | | | | |
| +----v-----+ +----v----+ +--v--------+ +-v----------+ +------v----+ |
| |MASTER TAG| |KNOWN TAG| |UNKNOWN TAG| |GRANT ACCESS| |DENY ACCESS| |
| +----------+ +---+-----+ +-----+-----+ +-----+------+ +-----+-----+ |
| | | | | |
| +----+ +----v------+ +--v---+ | +--------------->
+-------+EXIT| |DELETE FROM| |ADD TO| | |
+----+ | EEPROM | |EEPROM| | |
+-----------+ +------+ +-------------------------------+
Use a Master Card which is act as Programmer then you can able to choose card holders who will granted access or not
* **Easy User Interface**
Just one RFID tag needed whether Delete or Add Tags. You can choose to use Leds for output or Serial LCD module to inform users.
* **Stores Information on EEPROM**
Information stored on non volatile Arduino's EEPROM memory to preserve Users' tag and Master Card. No Information lost
if power lost. EEPROM has unlimited Read cycle but roughly 100,000 limited Write cycle.
* **Security**
To keep it simple we are going to use Tag's Unique IDs. It's simple and not hacker proof.
/*
my implementation using soft SPI
----------------------------------------
MFRC522 Arduino
Reader/PCD Nano v3
Signal Pin Pin
-------------------------------------------
RST/Reset(CE) RST D9
SPI SS(CSN) SDA(SS) D10
SPI MOSI MOSI A1
SPI MISO MISO A2
SPI SCK SCK A0
rc522 is alimented with 3.3V
RF24l01+ is alimented using 3.3V
/!\ if using regulator for nrf24l01+, dont forget to aliment it in 5v instead of 3.3V
*/
/*
Implemented:
- encryption
- soft spi for mysensors
- rc522 rfid
- send encrypted id (using md5 + salt (not the best because it should be based on time + seed but require time sync)
*/
/*
Todo:
- signing
- implement a more secure authentification rather than using UID
- send add/remove id to gateway
*/
// Enable debug prints mysensors to serial monitor
//#define MY_DEBUG
// Enable debug print for this program, also allow to register a new master card
//#define DEBUG_ON
#ifdef DEBUG_ON
#define DEBUG_PRINT(x) Serial.print(x)
#define DEBUG_PRINTLN(x) Serial.println(x)
#else
#define DEBUG_PRINT(x)
#define DEBUG_PRINTLN(x)
#endif
#define MY_RADIO_NRF24
#define RFID_HASH1 1
#define RFID_HASH2 2
#define RFID_HASH3 3
//#define RFID 4
//#define MY_NODE_ID 42
#define BAUDR 115200
#define MY_BAUD_RATE BAUDR
#define MY_ENCRYPTION_SIMPLE_PASSWD "randomstring"
// mysensors: Define this to use a software based SPI driver which allows more freedom in pin selection for the (supported) radio module.
#define MY_SOFTSPI
// mysensors: Soft SPI SCK pin.
#define MY_SOFT_SPI_SCK_PIN (14)
// mysensors: Soft SPI MISO pin.
#define MY_SOFT_SPI_MISO_PIN (16)
// mysensors: Soft SPI MOSI pin.
#define MY_SOFT_SPI_MOSI_PIN (15)
#include <SPI.h> // RC522 Module uses SPI protocol
#include <MFRC522.h> // Library for Mifare RC522 Devices
#include <MySensors.h>
#include <MD5.h>
#ifdef COMMON_ANODE
#define LED_ON LOW
#define LED_OFF HIGH
#else
#define LED_ON HIGH
#define LED_OFF LOW
#endif
#define redLed 7 // Set Led Pins
#define greenLed 6
#define blueLed 5
//#define relay 4 // Set Relay Pin
#define wipeB 3 // Button pin for WipeMode
bool programMode = false; // initialize programming mode to false
uint8_t successRead; // Variable integer to keep if we have Successful Read from Reader
byte storedCard[4]; // Stores an ID read from EEPROM
byte readCard[4]; // Stores scanned ID read from RFID Module
byte masterCard[4]; // Stores master card's ID read from EEPROM
byte dumpedCard[4];
static const uint8_t MAGIC = 168;
// Create MFRC522 instance.
/*
#define SS_PINRC 10
#define RST_PINRC 9*/
#define SS_PINRC 4
#define RST_PINRC 2
MFRC522 mfrc522(SS_PINRC, RST_PINRC);
///////////////////////////////////////// Setup ///////////////////////////////////
void setup() {
//release spi for nrf24l01+
pinMode(RST_PINRC, OUTPUT);
digitalWrite(RST_PINRC, LOW);
pinMode(SS_PINRC, OUTPUT);
digitalWrite(SS_PINRC, LOW);
//Protocol Configuration
//Serial.begin(9600); // Initialize serial communications with PC
Serial.begin(BAUDR); // Initialize serial communications with PC
SPI.begin(); // MFRC522 Hardware uses SPI protocol
mfrc522.PCD_Init(); // Initialize MFRC522 Hardware
//Arduino Pin Configuration
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
pinMode(blueLed, OUTPUT);
pinMode(wipeB, INPUT_PULLUP); // Enable pin's pull up resistor
digitalWrite(redLed, LED_OFF); // Make sure led is off
digitalWrite(greenLed, LED_OFF); // Make sure led is off
digitalWrite(blueLed, LED_OFF); // Make sure led is off
//If you set Antenna Gain to Max it will increase reading distance but require a better power source than arduino itself
//mfrc522.PCD_SetAntennaGain(mfrc522.RxGain_max);
Serial.println(F("Access Control v2.0")); // For debugging purposes
#ifdef DEBUG_ON
ShowReaderDetails(); // Show details of PCD - MFRC522 Card Reader details
checkWipe();
#endif
// Check if master card defined, if not let user choose a master card
// This also useful to just redefine the Master Card
// You can keep other EEPROM records just write other than MAGIC to EEPROM address 1
// EEPROM address 1 should hold magical number which is 'MAGIC'
if (loadState(1) != MAGIC) {
#ifdef DEBUG_ON
DEBUG_PRINTLN(F("No Master Card Defined"));
DEBUG_PRINTLN(F("Scan A PICC to Define as Master Card"));
do {
successRead = getID(); // sets successRead to 1 when we get read from reader otherwise 0
digitalWrite(blueLed, LED_ON); // Visualize Master Card need to be defined
delay(200);
digitalWrite(blueLed, LED_OFF);
delay(200);
}
while (!successRead); // Program will not go further while you not get a successful read
saveCard(2, readCard);
saveState(0, 0);
saveState(1, MAGIC); // Write to EEPROM we defined Master Card.
DEBUG_PRINTLN(F("Master Card Defined"));
#else
Serial.println(F("SYSTEM HALTED: No Master Card."));
// Visualize system is halted
digitalWrite(greenLed, LED_OFF); // Make sure green LED is off
digitalWrite(blueLed, LED_OFF); // Make sure blue LED is off
digitalWrite(redLed, LED_ON); // Turn on red LED
while (true); // do not go further
#endif
}
DEBUG_PRINTLN(F("-------------------"));
loadCard(2, masterCard);
#ifdef DEBUG_ON
dumpIDs();
#endif
randomSeed(analogRead(7));
DEBUG_PRINTLN(F("-------------------"));
DEBUG_PRINTLN(F("Everything is ready"));
DEBUG_PRINTLN(F("Waiting PICCs to be scanned"));
// Serial.print("stack");
// Serial.println(freeMemory());
cycleLeds(); // Everything ready lets give user some feedback by cycling leds
}
///////////////////////////////////////// Mysensors presentation ///////////////////////////////////
MyMessage MsgHash1(RFID_HASH1, V_TEXT);
MyMessage MsgHash2(RFID_HASH2, V_TEXT);
//MyMessage MsgLock(RFID, V_LOCK_STATUS);
MyMessage MsgSALT(RFID_HASH3, V_TEXT);
void presentation()
{
// Send the sketch version information to the gateway and Controller
sendSketchInfo("RFID Sensor", "2.3");
// Register all sensors to gw (they will be created as child devices)
present(RFID_HASH1, S_INFO);
present(RFID_HASH2, S_INFO);
present(RFID_HASH3, S_INFO);
// present(RFID, S_LOCK);
}
///////////////////////////////////////// Main Loop ///////////////////////////////////
void loop () {
// Serial.print("stack");
// Serial.println(freeMemory());
do {
successRead = getID(); // sets successRead to 1 when we get read from reader otherwise 0
if (programMode) {
cycleLeds(); // Program Mode cycles through Red Green Blue waiting to read a new card
}
else {
normalModeOn(); // Normal mode, blue Power LED is on, all others are off
}
}
while (!successRead); //the program will not go further while you are not getting a successful read
if (programMode) {
if ( isMaster(readCard) ) { //When in program mode check First If master card scanned again to exit program mode
/*
DEBUG_PRINTLN(F("Master Card Scanned"));
DEBUG_PRINTLN(F("Exiting Program Mode"));
DEBUG_PRINTLN(F("-----------------------------"));
*/
programMode = false;
return;
}
else {
if ( findID(readCard) ) { // If scanned card is known delete it
DEBUG_PRINTLN(F("I know this PICC, removing..."));
deleteID(readCard);
DEBUG_PRINTLN("-----------------------------");
DEBUG_PRINTLN(F("Scan a PICC to ADD or REMOVE to EEPROM"));
}
else { // If scanned card is not known add it
DEBUG_PRINTLN(F("I do not know this PICC, adding..."));
writeID(readCard);
DEBUG_PRINTLN(F("-----------------------------"));
DEBUG_PRINTLN(F("Scan a PICC to ADD or REMOVE to EEPROM"));
}
}
}
else {
if ( isMaster(readCard)) { // If scanned card's ID matches Master Card's ID - enter program mode
programMode = true;
/*
DEBUG_PRINTLN(F("Hello Master - Entered Program Mode"));
#ifdef DEBUG_ON
dumpIDs();
#endif
DEBUG_PRINTLN(F("Scan a PICC to ADD or REMOVE to EEPROM"));
DEBUG_PRINTLN(F("Scan Master Card again to Exit Program Mode"));
DEBUG_PRINTLN(F("-----------------------------"));
*/
}
else {
if ( findID(readCard) ) { // If not, see if the card is in the EEPROM
// DEBUG_PRINTLN(F("Welcome, You shall pass"));
granted();
}
else { // If not, show that the ID was not valid
// DEBUG_PRINTLN(F("You shall not pass"));
denied();
}
}
}
}
///////////////////////////////////////// Access Granted ///////////////////////////////////
//bool status = false;
uint8_t deniedCount = 1;
void granted () {
digitalWrite(blueLed, LED_OFF); // Turn off blue LED
digitalWrite(redLed, LED_OFF); // Turn off red LED
digitalWrite(greenLed, LED_ON); // Turn on green LED
request(RFID_HASH3, V_TEXT);
wait(5000);
/*char answer[12] = "XXXYYYYYYYY";
int16_t randNumber = random(100, 999);
sprintf(answer, "%u", randNumber);
byte_to_hexa(card, 4, answer + 3);
unsigned char* hash = MD5::make_hash(answer);
//generate the digest (hex encoding) of our hash
char *md5str = MD5::make_digest(hash, 16);
free(hash);
//print it on our serial monitor
// DEBUG_PRINT("value:");
// DEBUG_PRINT(answer);
// DEBUG_PRINT(", hash:");
// DEBUG_PRINTLN(md5str);
//mysensors
send(MsgSALT.set(randNumber));
send(MsgHash2.set(md5str + 15));
md5str[15] = '\0';
send(MsgHash1.set(md5str));
//Give the Memory back to the System if you run the md5 Hash generation in a loop
free(md5str);
// send(MsgLock.set(status ? 0 : 1));
// status = !status;
delay(1000); // Hold green LED on for a second
deniedCount = 1;*/
}
///////////////////////////////////////// Access Denied ///////////////////////////////////
void denied() {
digitalWrite(greenLed, LED_OFF); // Make sure green LED is off
digitalWrite(blueLed, LED_OFF); // Make sure blue LED is off
digitalWrite(redLed, LED_ON); // Turn on red LED
//mysensors
char answer[12] = "KO XXXXXXXX";
byte_to_hexa(readCard, 4, answer + 3);
//mysensors
send(MsgHash1.set(answer));
delay(1000 * deniedCount);
deniedCount++;
}
///////////////////////////////////////// Get PICC's UID ///////////////////////////////////
uint8_t getID() {
// Getting ready for Reading PICCs
if ( ! mfrc522.PICC_IsNewCardPresent()) { //If a new PICC placed to RFID reader continue
return 0;
}
if ( ! mfrc522.PICC_ReadCardSerial()) { //Since a PICC placed get Serial and continue
return 0;
}
// There are Mifare PICCs which have 4 byte or 7 byte UID care if you use 7 byte PICC
// I think we should assume every PICC as they have 4 byte UID
// Until we support 7 byte PICCs
Serial.println(F("Scanned PICC's UID:"));
for ( uint8_t i = 0; i < 4; i++) { //
readCard[i] = mfrc522.uid.uidByte[i];
Serial.print(readCard[i], HEX);
}
Serial.println("");
mfrc522.PICC_HaltA(); // Stop reading
return 1;
}
void ShowReaderDetails() {
// Get the MFRC522 software version
byte v = mfrc522.PCD_ReadRegister(mfrc522.VersionReg);
Serial.print(F("MFRC522 Software Version: 0x"));
Serial.print(v, HEX);
if (v == 0x91)
Serial.print(F(" = v1.0"));
else if (v == 0x92)
Serial.print(F(" = v2.0"));
else
Serial.print(F(" (unknown),probably a chinese clone?"));
Serial.println("");
// When 0x00 or 0xFF is returned, communication probably failed
if ((v == 0x00) || (v == 0xFF)) {
Serial.println(F("SYSTEM HALTED: Check connections."));
// Visualize system is halted
digitalWrite(greenLed, LED_OFF); // Make sure green LED is off
digitalWrite(blueLed, LED_OFF); // Make sure blue LED is off
digitalWrite(redLed, LED_ON); // Turn on red LED
while (true); // do not go further
}
}
///////////////////////////////////////// Cycle Leds (Program Mode) ///////////////////////////////////
void cycleLeds() {
digitalWrite(redLed, LED_OFF); // Make sure red LED is off
digitalWrite(greenLed, LED_ON); // Make sure green LED is on
digitalWrite(blueLed, LED_OFF); // Make sure blue LED is off
delay(200);
digitalWrite(redLed, LED_OFF); // Make sure red LED is off
digitalWrite(greenLed, LED_OFF); // Make sure green LED is off
digitalWrite(blueLed, LED_ON); // Make sure blue LED is on
delay(200);
digitalWrite(redLed, LED_ON); // Make sure red LED is on
digitalWrite(greenLed, LED_OFF); // Make sure green LED is off
digitalWrite(blueLed, LED_OFF); // Make sure blue LED is off
delay(200);
}
//////////////////////////////////////// Normal Mode Led ///////////////////////////////////
void normalModeOn () {
digitalWrite(blueLed, LED_ON); // Blue LED ON and ready to read card
digitalWrite(redLed, LED_OFF); // Make sure Red LED is off
digitalWrite(greenLed, LED_OFF); // Make sure Green LED is off
}
//////////////////////////////////////// Read an ID from EEPROM //////////////////////////////
void readID( uint8_t number ) {
uint8_t start = (number * 4 ) + 2; // Figure out starting position
loadCard(start, storedCard);
}
///////////////////////////////////////// Add ID to EEPROM ///////////////////////////////////
void writeID( byte a[] ) {
if ( !findID( a ) ) { // Before we write to the EEPROM, check to see if we have seen this card before!
uint8_t num = loadState(0); // Get the numer of used spaces, position 0 stores the number of ID cards
uint8_t start = ( num * 4 ) + 6; // Figure out where the next slot starts
num++; // Increment the counter by one
saveState( 0, num ); // Write the new count to the counter
saveCard(start, a);
successWrite();
Serial.println(F("Write Success"));
}
else {
failedWrite();
Serial.println(F("Write Failed!"));
}
}
///////////////////////////////////////// Remove ID from EEPROM ///////////////////////////////////
void deleteID( byte a[] ) {
if ( !findID( a ) ) { // Before we delete from the EEPROM, check to see if we have this card!
failedWrite(); // If not
Serial.println(F("Delete Failed!"));
}
else {
uint8_t num = loadState(0); // Get the numer of used spaces, position 0 stores the number of ID cards
uint8_t slot; // Figure out the slot number of the card
uint8_t start; // = ( num * 4 ) + 6; // Figure out where the next slot starts
uint8_t looping; // The number of times the loop repeats
uint8_t j;
uint8_t count = loadState(0); // Read the first Byte of EEPROM that stores number of cards
slot = findIDSLOT( a ); // Figure out the slot number of the card to delete
start = (slot * 4) + 2;
looping = ((num - slot) * 4);
num--; // Decrement the counter by one
saveState( 0, num ); // Write the new count to the counter
for ( j = 0; j < looping; j++ ) { // Loop the card shift times
saveState( start + j, loadState(start + 4 + j)); // Shift the array values to 4 places earlier in the EEPROM
}
successDelete();
Serial.println(F("Delete Success"));
}
}
///////////////////////////////////////// Check Bytes ///////////////////////////////////
bool checkTwo ( byte a[], byte b[] ) {
for ( uint8_t k = 0; k < 4; k++ ) { // Loop 4 times
if ( a[k] != b[k] ) { // IF a != b then false, because: one fails, all fail
return false;
}
}
return true;
}
///////////////////////////////////////// Find Slot ///////////////////////////////////
uint8_t findIDSLOT( byte find[] ) {
uint8_t count = loadState(0); // Read the first Byte of EEPROM that
for ( uint8_t i = 1; i <= count; i++ ) { // Loop once for each EEPROM entry
readID(i); // Read an ID from EEPROM, it is stored in storedCard[4]
if ( checkTwo( find, storedCard ) ) { // Check to see if the storedCard read from EEPROM
// is the same as the find[] ID card passed
return i; // The slot number of the card
}
}
}
///////////////////////////////////////// Find ID From EEPROM ///////////////////////////////////
bool findID( byte find[] ) {
uint8_t count = loadState(0); // Read the first Byte of EEPROM that
for ( uint8_t i = 1; i <= count; i++ ) { // Loop once for each EEPROM entry
readID(i); // Read an ID from EEPROM, it is stored in storedCard[4]
if ( checkTwo( find, storedCard ) ) // Check to see if the storedCard read from EEPROM
return true;
}
return false;
}
///////////////////////////////////////// Write Success to EEPROM ///////////////////////////////////
// Flashes the green LED 3 times to indicate a successful write to EEPROM
void successWrite() {
digitalWrite(blueLed, LED_OFF); // Make sure blue LED is off
digitalWrite(redLed, LED_OFF); // Make sure red LED is off
digitalWrite(greenLed, LED_OFF); // Make sure green LED is on
delay(200);
digitalWrite(greenLed, LED_ON); // Make sure green LED is on
delay(200);
digitalWrite(greenLed, LED_OFF); // Make sure green LED is off
delay(200);
digitalWrite(greenLed, LED_ON); // Make sure green LED is on
delay(200);
digitalWrite(greenLed, LED_OFF); // Make sure green LED is off
delay(200);
digitalWrite(greenLed, LED_ON); // Make sure green LED is on
delay(200);
}
///////////////////////////////////////// Write Failed to EEPROM ///////////////////////////////////
// Flashes the red LED 3 times to indicate a failed write to EEPROM
void failedWrite() {
digitalWrite(blueLed, LED_OFF); // Make sure blue LED is off
digitalWrite(redLed, LED_OFF); // Make sure red LED is off
digitalWrite(greenLed, LED_OFF); // Make sure green LED is off
delay(200);
digitalWrite(redLed, LED_ON); // Make sure red LED is on
delay(200);
digitalWrite(redLed, LED_OFF); // Make sure red LED is off
delay(200);
digitalWrite(redLed, LED_ON); // Make sure red LED is on
delay(200);
digitalWrite(redLed, LED_OFF); // Make sure red LED is off
delay(200);
digitalWrite(redLed, LED_ON); // Make sure red LED is on
delay(200);
}
///////////////////////////////////////// Success Remove UID From EEPROM ///////////////////////////////////
// Flashes the blue LED 3 times to indicate a success delete to EEPROM
void successDelete() {
digitalWrite(blueLed, LED_OFF); // Make sure blue LED is off
digitalWrite(redLed, LED_OFF); // Make sure red LED is off
digitalWrite(greenLed, LED_OFF); // Make sure green LED is off
delay(200);
digitalWrite(blueLed, LED_ON); // Make sure blue LED is on
delay(200);
digitalWrite(blueLed, LED_OFF); // Make sure blue LED is off
delay(200);
digitalWrite(blueLed, LED_ON); // Make sure blue LED is on
delay(200);
digitalWrite(blueLed, LED_OFF); // Make sure blue LED is off
delay(200);
digitalWrite(blueLed, LED_ON); // Make sure blue LED is on
delay(200);
}
////////////////////// Check readCard IF is masterCard ///////////////////////////////////
// Check to see if the ID passed is the master programing card
bool isMaster( byte test[] ) {
return checkTwo(test, masterCard);
}
bool monitorWipeButton(uint32_t interval) {
uint32_t now = (uint32_t)millis();
while ((uint32_t)millis() - now < interval) {
// check on every half a second
if (((uint32_t)millis() % 500) == 0) {
if (digitalRead(wipeB) != LOW)
return false;
}
}
return true;
}
/* save a card (card is an array of 4) into eeprom starting from startPos
0: count
1: magic (MAGIC)
2 - 5 : master
n - (n+4): cards
*/
void saveCard(uint8_t startPos, byte *card) {
for ( uint8_t j = 0; j < 4; j++ ) { // Loop 4 times
saveState( startPos + j, card[j] ); // Write scanned PICC's UID to EEPROM, start from address 3
}
}
void loadCard(uint8_t startPos, byte *card) {
for ( uint8_t i = 0; i < 4; i++ ) // Read Card's UID from EEPROM
card[i] = loadState(startPos + i); // Write it to the card
}
#ifdef DEBUG_ON
void dumpIDs() {
if (loadState(1) != MAGIC) {
Serial.println("Wrong Magic Number");
return;
}
uint8_t start = 0;
uint8_t num = loadState(0);
Serial.print("ID Count:");
Serial.print(num);
Serial.println("");
Serial.print("Master ID:");
loadCard(2, dumpedCard);
for (uint8_t i = 0; i < 4; i++)
Serial.print(dumpedCard[i], HEX);
Serial.println("");
for (uint8_t card = 0; card < num; card++) {
start = (card * 4 ) + 6;
Serial.print("card ");
Serial.print(card + 1);
Serial.print(":");
loadCard(start, dumpedCard);
for (uint8_t i = 0; i < 4; i++)
Serial.print(dumpedCard[i], HEX);
Serial.println("");
}
Serial.println("=== END dump ===");
}
void checkWipe() {
//Wipe Code - If the Button (wipeB) Pressed while setup run (powered on) it wipes EEPROM
if (digitalRead(wipeB) == LOW) { // when button pressed pin should get low, button connected to ground
digitalWrite(redLed, LED_ON); // Red Led stays on to inform user we are going to wipe
Serial.println(F("Wipe Button Pressed, you have 5sec"));
bool buttonState = monitorWipeButton(5000); // Give user enough time to cancel operation
if (buttonState == true && digitalRead(wipeB) == LOW) { // If button still be pressed, wipe EEPROM
Serial.println(F("Starting Wiping"));
saveState(0, 0 ); // light wipe. only erase number
saveState(1, 0); // light wipe. only erase magic number
Serial.println(F("EEPROM Successfully Wiped"));
digitalWrite(redLed, LED_OFF); // visualize a successful wipe
delay(200);
digitalWrite(redLed, LED_ON);
delay(200);
digitalWrite(redLed, LED_OFF);
delay(200);
digitalWrite(redLed, LED_ON);
delay(200);
digitalWrite(redLed, LED_OFF);
}
else {
Serial.println(F("Wiping Cancelled")); // Show some feedback that the wipe button did not pressed for 5 seconds
digitalWrite(redLed, LED_OFF);
}
}
}
#endif
void byte_to_hexa(byte array[], unsigned int len, char buffer[])
{
for (unsigned int i = 0; i < len; i++)
{
byte nib1 = (array[i] >> 4) & 0x0F;
byte nib2 = (array[i] >> 0) & 0x0F;
buffer[i * 2 + 0] = nib1 < 0xA ? '0' + nib1 : 'A' + nib1 - 0xA;
buffer[i * 2 + 1] = nib2 < 0xA ? '0' + nib2 : 'A' + nib2 - 0xA;
}
buffer[len * 2] = '\0';
}
void receive(const MyMessage &message)
{
if (message.type != V_TEXT)
return;
char* salt = message.getString();
char answer[12] = "XXXYYYYYYYY";
for (uint8_t i = 0; i < 4; i++)
answer[i] = salt[i];
byte_to_hexa(readCard, 4, answer + 3);
unsigned char* hash = MD5::make_hash(answer);
//generate the digest (hex encoding) of our hash
char *md5str = MD5::make_digest(hash, 16);
free(hash);
// DEBUG_PRINT("value:");
// DEBUG_PRINT(answer);
// DEBUG_PRINT(", hash:");
// DEBUG_PRINTLN(md5str);
Serial.print("sel: ");
Serial.println(message.getString());
//mysensors
send(MsgSALT.set(salt));
send(MsgHash2.set(md5str + 15));
md5str[15] = '\0';
send(MsgHash1.set(md5str));
//Give the Memory back to the System if you run the md5 Hash generation in a loop
free(md5str);
// send(MsgLock.set(status ? 0 : 1));
// status = !status;
//delay(1000); // Hold green LED on for a second
deniedCount = 1;
// Serial.print("stack");
// Serial.println(freeMemory());
}
/*
//https://learn.adafruit.com/memories-of-an-arduino/measuring-free-memory
#ifdef __arm__
// should use uinstd.h to define sbrk but Due causes a conflict
extern "C" char* sbrk(int incr);
#else // __ARM__
extern char *__brkval;
#endif // __arm__
int freeMemory() {
char top;
#ifdef __arm__
return &top - reinterpret_cast<char*>(sbrk(0));
#elif defined(CORE_TEENSY) || (ARDUINO > 103 && ARDUINO != 151)
return &top - __brkval;
#else // __arm__
return __brkval ? &top - __brkval : &top - __malloc_heap_start;
#endif // __arm__
}
*/
when you test it for the first time, dont forget to enable DEBUG_ON to register a master card