RFID and NRF24L01 Wireless Network Coding Issues
-
@evb yes i have 2 nodes (node 3 and node 50) all with MRFC522 right now, the node 3 is working perfectly but i dont like add and remove card system, I just want to change the part of the RFID sketch on node 3 using the sketch above, so i make one node for test (node 50) and put the code
send(lockMsg.set(false));and
send(lockMsg.set(true));to this
void granted ( uint16_t setDelay) { digitalWrite(blueLed, LED_OFF); // Turn off blue LED digitalWrite(redLed, LED_OFF); // Turn off red LED digitalWrite(greenLed, LED_ON); // Turn on green LED digitalWrite(relay, LOW); // Unlock door! send(lockMsg.set(false)); delay(setDelay); // Hold door lock open for given seconds digitalWrite(relay, HIGH); // Relock door send(lockMsg.set(true)); delay(1000); // Hold green LED on for a second }and i add this to node 50
if (message.type == V_LOCK_STATUS) { // Change relay state if (!message.getBool()) { granted(300); } // Write some debug info Serial.print(F("Lock status: ")); Serial.println(message.getBool()); } else { // Write some debug info Serial.print(F("Incoming msg type: ")); Serial.print(message.type); Serial.print(F(" id: ")); Serial.print(message.sensor); Serial.print(F(" content: ")); Serial.println(message.getInt()); }nothing happen...
sketch Node 3
/ // Enable debug prints #define MY_DEBUG #define MY_NODE_ID 3 // Enable and select radio type attached #define MY_RADIO_RF24 //#define MY_RF24_CHANNEL 10 #include <MySensors.h> #include <SPI.h> #include <MFRC522.h> #include <Bounce2.h> #define RF_INIT_DELAY 125 #define ONE_SEC 1000 #define MAX_CARDS 18 #define PROG_WAIT 10 #define HEARTBEAT 10 #define BAUD 115200 /*Pin definitions*/ #define LED_PIN 4 #define GARAGEPIN 2 #define SWITCH_PIN 3 #define RST_PIN 7 // MFRC #define SS_PIN 8 // MFRC MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance MFRC522::Uid olduid; MFRC522::Uid masterkey = { 4,{ 0xEF,0xDB,0x8E,0x79 },0 }; byte countValidCards = 0; MFRC522::Uid validCards[MAX_CARDS]; void ShowCardData(MFRC522::Uid* uid); bool sameUid(MFRC522::Uid* old, MFRC522::Uid* check); void copyUid(MFRC522::Uid* src, MFRC522::Uid* dest); bool isValidCard(MFRC522::Uid* uid); int releasecnt = 0; //#define CHILD_ID_ALARM 2 #define CHILD_ID_LOCK 1 Bounce debouncer = Bounce(); int oldSwitchValue = -1; int switchValue = 0; long timer = -1; bool programmode = false; bool ledon; int programTimer = 0; bool armed = true; unsigned long lastTime = 0; MyMessage lockMsg(CHILD_ID_LOCK, V_LOCK_STATUS); //MyMessage lockArmMsg(CHILD_ID_ALARM, V_ARMED); //MyMessage wrongMsg(CHILD_ID_ALARM, V_TRIPPED); void before() { // Make sure MFRC is disabled from the SPI bus pinMode(RST_PIN, OUTPUT); digitalWrite(RST_PIN, LOW); pinMode(SS_PIN, OUTPUT); digitalWrite(SS_PIN, LOW); } void presentation() { sendSketchInfo("RFID Garage", "1.1"); delay(RF_INIT_DELAY); // Register all sensors to gw (they will be created as child devices) present(CHILD_ID_LOCK, S_LOCK); delay(RF_INIT_DELAY); //present(CHILD_ID_ALARM, S_MOTION); delay(RF_INIT_DELAY); } void setup() { Serial.begin(BAUD); // Initialize serial communications with the PC pinMode(GARAGEPIN, OUTPUT); // Initialise in/output ports // Make sure MFRC will be disabled on the SPI bus /*pinMode(RST_PIN, OUTPUT); digitalWrite(RST_PIN, LOW); pinMode(SS_PIN, OUTPUT); digitalWrite(SS_PIN, LOW);*/ pinMode(LED_PIN, OUTPUT); digitalWrite(LED_PIN, LOW); // Setup the button pinMode(SWITCH_PIN, INPUT); // Activate internal pull-up digitalWrite(SWITCH_PIN, HIGH); // After setting up the button, setup debouncer debouncer.attach(SWITCH_PIN); debouncer.interval(5); // Init mysensors library /*sendSketchInfo("RFID Garage", "1.1"); delay(RF_INIT_DELAY); // Register all sensors to gw (they will be created as child devices) present(CHILD_ID_LOCK, S_LOCK); delay(RF_INIT_DELAY); present(CHILD_ID_ALARM, S_MOTION); delay(RF_INIT_DELAY);*/ recallEeprom(); // Init MFRC RFID sensor SPI.begin(); // Init SPI bus mfrc522.PCD_Init(); // Init MFRC522 ShowReaderDetails(); // Show details of PCD - MFRC522 Card Reader details //send(lockArmMsg.set(armed)); Serial.println(F("Init done...")); } void loop() { timer++; delay(HEARTBEAT); debouncer.update(); // Get the update value int switchValue = debouncer.read(); if (switchValue != oldSwitchValue) { // Send in the new value Serial.print(F("Switch ")); Serial.println(switchValue); if (switchValue && programmode) { lastTime = millis() / 1000; } if (!switchValue && programmode && lastTime > 0) { if ((millis() / 1000) - lastTime > 3) { Serial.println(F("Reset all cards")); countValidCards = 0; blinkFast(50); } else { Serial.println(F("Program off")); digitalWrite(LED_PIN, LOW); programmode = false; storeEeprom(); } } if (!switchValue) { programTimer = 0; } oldSwitchValue = switchValue; } if (programmode && ((timer % (ONE_SEC / HEARTBEAT)) == 0)) { ledon = !ledon; digitalWrite(LED_PIN, ledon); programTimer++; // Stop program mode after 20 sec inactivity if (programTimer > PROG_WAIT) { programmode = false; digitalWrite(LED_PIN, false); Serial.println(F("Program expired")); } } if ((timer % (200 / HEARTBEAT)) == 0) { // Look for new cards if (!mfrc522.PICC_IsNewCardPresent()) { if (releasecnt > 0) { releasecnt--; if (!releasecnt) { olduid.size = 0; Serial.println(F("release")); } } return; } releasecnt = 5; // Select one of the cards if (!mfrc522.PICC_ReadCardSerial()) { return; } // Dump debug info about the card; PICC_HaltA() is automatically called //mfrc522.PICC_DumpToSerial(&(mfrc522.uid)); if (!olduid.size || !sameUid(&(mfrc522.uid), &olduid)) { ShowCardData(&(mfrc522.uid)); copyUid(&(mfrc522.uid), &olduid); if (isValidCard(&olduid)) { OpenDoor(false); } else { if (sameUid(&(mfrc522.uid), &masterkey)) { // Only switch in program mode when mastercard is found AND the program button is pressed if (switchValue) { Serial.println(F("Program mode")); programmode = true; programTimer = 0; lastTime = 0; } } else { if (programmode) { Serial.println(F("new card")); programTimer = 0; if (countValidCards < MAX_CARDS) { // Add card to list... copyUid(&(mfrc522.uid), &validCards[countValidCards]); countValidCards++; blinkFast(15); } } else { Serial.println(F("Invalid card")); //if (armed) { // send(wrongMsg.set(1)); // delay(2000); //send(wrongMsg.set(0)); //} } } } } } } void ShowCardData(MFRC522::Uid* uid) { Serial.print(F("Card UID:")); for (byte i = 0; i < uid->size; i++) { if (uid->uidByte[i] < 0x10) { Serial.print(F(" 0")); } else { Serial.print(F(" ")); } Serial.print(uid->uidByte[i], HEX); } Serial.println(); } void copyUid(MFRC522::Uid* src, MFRC522::Uid* dest) { dest->size = src->size; dest->sak = src->sak; for (byte i = 0; i < src->size; i++) { dest->uidByte[i] = src->uidByte[i]; } } bool sameUid(MFRC522::Uid* old, MFRC522::Uid* check) { if (old->size != check->size) { return false; } for (byte i = 0; i < old->size; i++) { if (old->uidByte[i] != check->uidByte[i]) { return false; } } return true; } bool isValidCard(MFRC522::Uid* uid) { for (byte i = 0; i < countValidCards; i++) { if (validCards[i].size != uid->size) { break; } for (int j = 0; j < uid->size; j++) { if (validCards[i].uidByte[j] != uid->uidByte[j]) { break; } if (j == (uid->size - 1)) { return true; } } } return false; } void storeEeprom() { byte address = 0; saveState(address++, countValidCards); for (byte i = 0; i < countValidCards; i++) { saveState(address++, validCards[i].size); for (byte j = 0; j < 10; j++) { saveState(address++, validCards[i].uidByte[j]); } } } void recallEeprom() { byte address = 0; countValidCards = loadState(address++); if (countValidCards > MAX_CARDS) { Serial.println(F("Not a valid EEPROM reading set to default")); countValidCards = 0; storeEeprom(); return; } for (byte i = 0; i < countValidCards; i++) { validCards[i].size = loadState(address++); for (byte j = 0; j < 10; j++) { validCards[i].uidByte[j] = loadState(address++); } } } void blinkFast(int times) { for (int i = 0; i < times; i++) { ledon = !ledon; digitalWrite(LED_PIN, ledon); delay(100); } } void OpenDoor(bool fakeOpen) { Serial.println(F("Open door!")); send(lockMsg.set(false)); if (!fakeOpen) { digitalWrite(LED_PIN, HIGH); digitalWrite(GARAGEPIN, HIGH); } delay(1000); if (!fakeOpen) { digitalWrite(GARAGEPIN, LOW); digitalWrite(LED_PIN, LOW); } send(lockMsg.set(true)); } 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)")); } Serial.println(""); // When 0x00 or 0xFF is returned, communication probably failed if ((v == 0x00) || (v == 0xFF)) { Serial.println(F("WARNING: Communication failure, is the MFRC522 properly connected?")); } } void receive(const MyMessage &message) { if (message.type == V_LOCK_STATUS) { // Change relay state if (!message.getBool()) { OpenDoor(false); } // Write some debug info Serial.print(F("Lock status: ")); Serial.println(message.getBool()); } /*else { if (message.type == V_ARMED) { // Change relay state armed = message.getBool(); // Write some debug info Serial.print(F("Arm status: ")); Serial.println(message.getBool()); }*/ else { // Write some debug info Serial.print(F("Incoming msg type: ")); Serial.print(message.type); Serial.print(F(" id: ")); Serial.print(message.sensor); Serial.print(F(" content: ")); Serial.println(message.getInt()); } } //}I'm not good at programming that's why I'm asking for help:expressionless: :expressionless:
@Ispandy what happens if you download the same sketch from the working node 3 tot your new node 50?
Adapt of course the node-id!If it works we can exclude a hardware problem, then it must be a software problem.
In that last case, we can check your sketch more deeply. -
@Ispandy what happens if you download the same sketch from the working node 3 tot your new node 50?
Adapt of course the node-id!If it works we can exclude a hardware problem, then it must be a software problem.
In that last case, we can check your sketch more deeply.@evb thank you for the response, and sorry for late reply.
As I mentioned above, that node 3 is running as it should, and I've copied it to node 50 and it's also running normally. The problem is on sketch node 50 when i click the switch in openhab the relay doesn't respond but when i scan the card on rfid, the relay responds. Unlike node 3, the relay responds to both card scans and openhab.
Regards, -
@evb thank you for the response, and sorry for late reply.
As I mentioned above, that node 3 is running as it should, and I've copied it to node 50 and it's also running normally. The problem is on sketch node 50 when i click the switch in openhab the relay doesn't respond but when i scan the card on rfid, the relay responds. Unlike node 3, the relay responds to both card scans and openhab.
Regards,@Ispandy, let resume some things because I'm lost
You copied the sketch code from node 3 (= correctly running node with cardscans and remote control from openhab) to node 50 and you adapted the node id.
This same sketch on node 50 is running correctly with cardscans but not with openhab? Correct?If above is correct: somewhere your openhab configuration for this node 50 is not correct. You say that node 50 is never receiving the openhab command, so there is a problem with your openhab configuration for node 50.