Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
O

Oniric

@Oniric
About
Posts
6
Topics
2
Shares
0
Groups
0
Followers
0
Following
0

Posts

Recent Best Controversial

  • Example RFID MFRC522 + mysensors with "some" security
    O Oniric

    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 ! :skull_and_crossbones:

    • 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

    My Project

  • NRF24l01+, EEPROM and shared SPI = brick module ?
    O Oniric

    HEllo,

    i used this one : https://github.com/mysensors/MySensors/blob/master/examples/ClearEepromConfig/ClearEepromConfig.ino

    ok intersting what you said, isnt it a magic number somewhere on the eeprom at boot to make sure this doesnt happen ? (if i understand correclty what you said : if i wipe my eeprom with 0x0, it might be wrong cause at boot it will think I'm Node_0, my parent is Node_0...) ?

    it seems i used the right one tho, but not at the beginning, maybe it would explain the weird behavior i had issue to understand because at first i used my own made wiper and of course i put 0x0 ! which would explain the result using the rf24 library debug printer full of 0.

    thanks again :-)

    Development

  • NRF24l01+, EEPROM and shared SPI = brick module ?
    O Oniric

    so ... i'm completly lost ... i made several test.... nothing worked... i look through the rf24 library to get more debug from the module itself ...

    i found out the function: void RF24::printDetails ( void )

    i tried it several time on my "brick" nrf24 module ... it shows everything at 0 and got stuck on a send function (obviously).
    i even tried without the module plug ... same result (everything at 0)

    i cried a little, then tried the same code on the same arduino but with a new module ...
    i got a result

    19:42:05.014 -> STATUS		 = 0x0e RX_DR=0 TX_DS=0 MAX_RT=0 RX_P_NO=7 TX_FULL=0
    19:42:05.082 -> RX_ADDR_P0-1	 = 0x4141417852 0xc2c2c2c2c2
    19:42:05.115 -> RX_ADDR_P2-5	 = 0xc3 0xc4 0xc5 0xc6
    19:42:05.182 -> TX_ADDR		 = 0x4141417852
    19:42:05.182 -> RX_PW_P0-6	 = 0x20 0x00 0x00 0x00 0x00 0x00
    19:42:05.250 -> EN_AA		 = 0x3f
    19:42:05.250 -> EN_RXADDR	 = 0x03
    19:42:05.284 -> RF_CH		 = 0x4c
    19:42:05.284 -> RF_SETUP	 = 0x27
    19:42:05.318 -> CONFIG		 = 0x0e
    19:42:05.318 -> DYNPD/FEATURE	 = 0x00 0x00
    19:42:05.353 -> Data Rate	 = 250KBPS
    19:42:05.387 -> Model		 = nRF24L01+
    19:42:05.421 -> CRC Length	 = 16 bits
    19:42:05.421 -> PA Power	 = PA_MAX
    

    i then tried to replug my "brick" module ... and this time ... it works ... i'm lost ! lol
    anyway...

    in case someone got the same issue and want to try my "fix" (can't call it a fix, but work on 2 "brick" nrf24 module for mysterious reason).

    most of the code is useless (then send & Co) but i let it to make sure it initiate a send.

    
    
    // SimpleTx - the master or the transmitter
    
    #include <SPI.h>
    #include <nRF24L01.h>
    #include <RF24.h>
    
    #define FAILURE_HANDLING
    #define CE_PIN   9
    #define CSN_PIN 10
    
    const byte slaveAddress[5] = {'R','x','A','A','A'};
    
    
    RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
    
    char dataToSend[10] = "Message 0";
    char txNum = '0';
    
    
    unsigned long currentMillis;
    unsigned long prevMillis;
    unsigned long txIntervalMillis = 1000; // send once per second
    
    
    static FILE uartout = {0} ;
    void setup() {
    
        Serial.begin(9600);
    
        Serial.println("SimpleTx Starting");
    // fill in the UART file descriptor with pointer to writer.
       fdev_setup_stream (&uartout, uart_putchar, NULL, _FDEV_SETUP_WRITE);
    
       // The uart is the standard output device STDOUT.
       stdout = &uartout ;
        radio.begin();
        radio.setDataRate( RF24_250KBPS );
        radio.setRetries(3,5); // delay, count
        radio.openWritingPipe(slaveAddress);
        
    }
    
    //====================
    bool printed = false;
    void loop() {
        currentMillis = millis();
        if (currentMillis - prevMillis >= txIntervalMillis) {
            send();
            prevMillis = millis();
        }
    
        if (!printed){
          printed=!printed;
          radio.printDetails();
          Serial.println("ok");
        }
        
    }
    
    //====================
    
    void send() {
    
        bool rslt;
        rslt = radio.write( &dataToSend, sizeof(dataToSend) );
            // Always use sizeof() as it gives the size as the number of bytes.
            // For example if dataToSend was an int sizeof() would correctly return 2
    
        Serial.print("Data Sent ");
        Serial.print(dataToSend);
        if (rslt) {
            Serial.println("  Acknowledge received");
            updateMessage();
        }
        else {
            Serial.println("  Tx failed");
        }
    }
    
    //================
    
    void updateMessage() {
            // so you can see that new data is being sent
        txNum += 1;
        if (txNum > '9') {
            txNum = '0';
        }
        dataToSend[8] = txNum;
    }
    
    
    // create a output function
    // This works because Serial.write, although of
    // type virtual, already exists.
    static int uart_putchar (char c, FILE *stream)
    {
        Serial.write(c) ;
        return 0 ;
    }
    

    and after i tried this mysensor code that failed before (with a working gateway) and worked after the code above... :

    /**
     * The MySensors Arduino library handles the wireless radio link and protocol
     * between your home built sensors/actuators and HA controller of choice.
     * The sensors forms a self healing radio network with optional repeaters. Each
     * repeater and gateway builds a routing tables in EEPROM which keeps track of the
     * network topology allowing messages to be routed to nodes.
     *
     * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
     * Copyright (C) 2013-2015 Sensnology AB
     * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
     *
     * Documentation: http://www.mysensors.org
     * Support Forum: http://forum.mysensors.org
     *
     * This program is free software; you can redistribute it and/or
     * modify it under the terms of the GNU General Public License
     * version 2 as published by the Free Software Foundation.
     *
     *******************************
     *
     * REVISION HISTORY
     * Version 1.0 - Henrik Ekblad
     *
     * DESCRIPTION
     * Motion Sensor example using HC-SR501
     * http://www.mysensors.org/build/motion
     *
     */
    
    // Enable debug prints
    #define MY_DEBUG
    
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    
    
    #include <MySensors.h>
    
    uint32_t SLEEP_TIME = 5000; // Sleep time between reports (in milliseconds)
    #define CHILD_ID 1   // Id of the sensor child
    
    // Initialize motion message
    MyMessage msg(CHILD_ID, V_TRIPPED);
    
    void setup()
    {
    	
    }
    
    void presentation()
    {
    	// Send the sketch version information to the gateway and Controller
    	sendSketchInfo("basic test", "1.0");
    
    	// Register all sensors to gw (they will be created as child devices)
    	present(CHILD_ID, S_MOTION);
    }
    bool tripped = true;
    void loop()
    {
    	send(msg.set(tripped?"1":"0"));  // Send tripped value to gw
      tripped=!tripped;
    	delay(SLEEP_TIME);
    }
    

    thanks again @rejoe2

    hope this will help someone, or if someone can explain to me the "magic" behind this ?

    Development

  • NRF24l01+, EEPROM and shared SPI = brick module ?
    O Oniric

    well ...

    i just tried to use the dedicated flash eeprom sketch ... and nothing changed :(
    i also tried to plug the nrf24l01 module to another arduino with a basic sketch... it fails init ...

    is there another way to directly hard reset the nrf24 module ? or did i really brick it ? ... i'm scared to try my main sketch again, because if it doesnt come from eeprom wrong usage, i'm screwed ! :scream: )

    does a wrong manipulation of eeprom can brick a nrf24 module ?

    Development

  • NRF24l01+, EEPROM and shared SPI = brick module ?
    O Oniric

    @rejoe2 thanks ! i didnt see there was a specific sketch for it...
    i will try this tonight. concerning the I_DEBUG, just for my understanding, if i put the define with "E", it will automatically clear some setting in the eeprom so it's less invasive than the dedicated clear sketch. did i understand it right ?

    thanks a lot, it make me crazy for 2 days, but at least i found out some new features on mysensors ;-)

    Development

  • NRF24l01+, EEPROM and shared SPI = brick module ?
    O Oniric

    Hello Everyone,

    i'm currently working on building a new module using an RFID module + NRF24L01+ using mysensors.

    for my RFID module, i'm using the EEPROM to store card ID and mysensors to communicate to my jeedom who came home etc.
    both devices work with SPI that i manage to share (did I ?).
    in the EEPROM i store some data from 0 to 20 (5cards) using the EEPROM library (read/write)

    my code is based on this example with addition of mysensors and some minor correction and simplification (i will share it later if that could help)
    https://github.com/miguelbalboa/rfid/tree/master/examples/AccessControl

    and i got a weird behavior:

    • when i plugged the device the first time, everything work fine but the master card as been forgotten, so i got to store it again in the eeprom (write 5 byte from 0) but otherwise enverything work fine on mysensors side or rfid

    • when i unplggued and plugged again a second time, same behavior (that's when i started to feel something was off :sweat_smile: )

    • and when i do it again... this time, the mysensors library refuse to start (init fail in loop ...).

    to make sure i didnt brick my code (cause i change some minor thing between each plugged), i changed the nrf module, and it start to work again but same exact behavior : 3 power on/off and no more mysensors init ...

    with this behavior, i started some research on internet and find out that mysensors is also using the eeprom to store some information ... first fail ! i then tried to wipe the eeprom with a simple write 0 everywhere. but it didnt work .. my nrf modules are still brick ...

    i found into config.h this information:

    /**
     * @def MY_SPECIAL_DEBUG
     * @brief Define MY_SPECIAL_DEBUG to enable support for I_DEBUG messages.
     *
     * I_DEBUG messages are sent from the controller to the node, which responds with the requested
     * data. The request can be one of the following:
     * - 'R': routing info (only repeaters): received msg XXYY (as stream), where XX is the node and YY
     *   the routing node
     * - 'V': CPU voltage
     * - 'F': CPU frequency
     * - 'M': free memory
     * - 'E': clear MySensors EEPROM area and reboot (i.e. "factory" reset)
     */
    //#define MY_SPECIAL_DEBUG
    

    if i want to make sure to clear the eeprom for mysensors, i should put #define MY_SPECIAL_DEBUG 'E' and/or 'M' onto my node ? i dont understand the "I_DEBUG messages are sent from the controller to the node, which responds with the requested" ?
    i dont think it will fix my issue, but it worse the try.

    to correct my code i will try to use :
    Saving state
    You can save a state (in local EEPROM) which is good for actuators to "remember" state between power cycles. You have 256 bytes to play with. Note that there is a limitation on the number of writes the EEPROM can handle (~100 000 cycles).

    void saveState(uint8_t pos, uint8_t value);
    pos - The position to store value in (0-255)
    value - Value to store in position

    Retrieving a state (from local EEPROM).
    uint8_t loadState(uint8_t pos);
    pos - The position to fetch from EEPROM (0-255)

    is it really to save custom value ? i don't understand why we talk about "state". i don't want to brick a new one ! :confounded:

    finally my real questions:

    • does it already happen to you this kind of behavior ? and how did you fixed it ? perhaps i'm missleading myself with eeprom. Maybe the shared SPI is the issue ?
    • is using saveState/loadstate is the correct way to use eeprom when using mysensors ?
    • is there a way to hard reset a nrf24 module ? i cant believe the nrf module is completly brick since voltage is stable and it's not 3 plug/unplgged that will brick them ...

    thanks a lot for your help and the great work on mysensors :)

    Development
  • Login

  • Don't have an account? Register

  • Login or register to search.
  • First post
    Last post
0
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular