I have what I believe is a relatively simple question, but can't find much information on the mysensors website about it. I have a slave NRF24L01 and a sensor (RFID) on one arduino board, and I want to set up another, separate NRF24L01 on a different arduino board as the master. What code should I use to get these two sensors to talk to each other and send data about the RFID between each other?
Posts made by codergirl56
-
Creating Wireless Sensor Network
-
RFID and NRF24L01 Wireless Network Coding Issues
I am currently working on a project where I am interfacing an RFID-RC522 with a NRF24L01 transceiver on a single Arduino. Ultimately what I would like to have is one RFID-RC522 and NRF24L01 on one arduino, where the RFID can read the UID off of PICC cards, and the connected NRF24L01 can send the UID data to a master NRF24L01 on a different Arduino controller. Currently I am having issues with debugging and coding.
Here is what I have achieved so far:
For the wiring, I used @BartE Garage Door example for the hardware. Here is what he did: garage door example. The pins are set up as follows:
The NRF24L01+ is connect as suggested on the MySensors site:
pin 2 IRQ (I did not have this one connected as I do not believe it is needed in this project. Correct me if this is wrong).
pin 9 CE
pin 10 CSN/CS
pin 11 MOSI
pin 12 MISO
pin 13 SCKThe RC522 module shares pins: (these I connected together on the same row on a prototype board)
pin 11 MOSI
pin 12 MISO
pin 13 SCKand uses
pin 3.3 V (also shared with the NRF24L01) connected together on prototype board
GND (for ground)
pin 7 (iso 9) for CE
pin 8 (iso 10) for CSN/CS.That is the hardware portion of it. Here is the code I have cut and figured out thus far using the garage door example and own research from the mysenors website. I started off adding line by line of code until I ran into problems. Main issue right now is the gw.begin(); not sure if using this function correctly. One main thing I noticed was I had an error with radio inti begin when I first began. I had to go into config.h and change the data rate on the RF24 to 1MPBS rather than the default 250KBPS. That is involved in this gw.begin() instance, and I put that in but it did not solve the problem.
But there may also be other things I am missing as well. Any help would be greatly appreciated.
The following is the code I have made thus far. Ultimately I would like to scan a UID on the RFID and have it sent to the NRF24L01 module to be read out on the serial monitor AND to send it to a NRF24L01 master module (to be set up on a different arduino.)
#include <SPI.h> //used for SPI bus #include <MFRC522.h> //used for RFID reader #include <MySensor.h> //library that uses SPI #define RST_PIN 7 // MFRC RST is used in pin 7 #define SS_PIN 8 // MFRC SS is used in pin 8 MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance #define RFID 1 //construct instance of NRF24 MyTransportNRF24 transport(9, 10); MySensor gw; //create MySensor instance MyMessage msg(RFID, V_VAR1); //create MyMessage instance; this holds message data //and has helpers for setting payload void setup() { Serial.begin(115200); // Initialize serial communications with the PC // Make sure MFRC will be disabled on the SPI bus pinMode(RST_PIN, OUTPUT); //set the RST pin as output digitalWrite(RST_PIN, LOW); //set RST pin off pinMode(SS_PIN, OUTPUT); //set SS pin as output digitalWrite(SS_PIN, LOW); //set SS pin off //initialize radio and start library gw.begin(incomingMessage, RFID, RF24_1MBPS); gw.sendSketchInfo("RFID Radio", "1.0"); //must present sensors before reporting data to controller gw.present(RFID, S_CUSTOM); gw.wait(125); 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, type, and data blocks...")); } void loop() { //main issue area here; wasn't sure what to do delay(10000); // Wait 10 seconds gw.process(); } void incomingMessage(const MyMessage &message) { // Look for new cards if ( ! mfrc522.PICC_IsNewCardPresent()) { return; } // 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)); Serial.println(F("Scan PICC to see UID, type, and data blocks...")); }Here
Here are the two coding programs for the RFID and NRF24L01 that I am using to base code off of and also what I tested the hardware with initially:
For the RFID: MCRF522 class from Git Hub; Dump Info Example: (I tested this on the RFID separately before interfacing with the NRF24L01 on one arduino):
#include <SPI.h> #include <MFRC522.h> #define RST_PIN 9 // Configurable, see typical pin layout above #define SS_PIN 10 // Configurable, see typical pin layout above MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance void setup() { Serial.begin(9600); // 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, 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; } // Dump debug info about the card; PICC_HaltA() is automatically called mfrc522.PICC_DumpToSerial(&(mfrc522.uid)); }
For the NRF24L01 transceiver, I originally did hardware testing there were two modules--one for receiving and one for transmitting. So two NRF24L01 modules in all. Each was connected to its own Arduino and then connected to the same laptop with two separate USB cables. I ran two separate Arduino environments on two different serial monitors. I used the GettingStarted Example from the RF24 class off of Git Hub. There is one code for one module below. For the other module, I just switched bool radioNumber = 0 to = 1. This website was helpful for this.
/* * Getting Started example sketch for nRF24L01+ radios * This is a very basic example of how to send data from one node to another * Updated: Dec 2014 by TMRh20 */ #include <SPI.h> #include "RF24.h" /****************** User Config ***************************/ /*** Set this radio as radio number 0 or 1 ***/ bool radioNumber = 0; /* Hardware configuration: Set up nRF24L01 radio on SPI bus plus pins 7 & 8 */ RF24 radio(7,8); /**********************************************************/ byte addresses[][6] = {"1Node","2Node"}; // Used to control whether this node is sending or receiving bool role = 0; void setup() { Serial.begin(115200); Serial.println(F("RF24/examples/GettingStarted")); Serial.println(F("*** PRESS 'T' to begin transmitting to the other node")); radio.begin(); // Set the PA Level low to prevent power supply related issues since this is a // getting_started sketch, and the likelihood of close proximity of the devices. RF24_PA_MAX is default. radio.setPALevel(RF24_PA_LOW); // Open a writing and reading pipe on each radio, with opposite addresses if(radioNumber){ radio.openWritingPipe(addresses[1]); radio.openReadingPipe(1,addresses[0]); }else{ radio.openWritingPipe(addresses[0]); radio.openReadingPipe(1,addresses[1]); } // Start the radio listening for data radio.startListening(); } void loop() { /****************** Ping Out Role ***************************/ if (role == 1) { radio.stopListening(); // First, stop listening so we can talk. Serial.println(F("Now sending")); unsigned long start_time = micros(); // Take the time, and send it. This will block until complete if (!radio.write( &start_time, sizeof(unsigned long) )){ Serial.println(F("failed")); } radio.startListening(); // Now, continue listening unsigned long started_waiting_at = micros(); // Set up a timeout period, get the current microseconds boolean timeout = false; // Set up a variable to indicate if a response was received or not while ( ! radio.available() ){ // While nothing is received if (micros() - started_waiting_at > 200000 ){ // If waited longer than 200ms, indicate timeout and exit while loop timeout = true; break; } } if ( timeout ){ // Describe the results Serial.println(F("Failed, response timed out.")); }else{ unsigned long got_time; // Grab the response, compare, and send to debugging spew radio.read( &got_time, sizeof(unsigned long) ); unsigned long end_time = micros(); // Spew it Serial.print(F("Sent ")); Serial.print(start_time); Serial.print(F(", Got response ")); Serial.print(got_time); Serial.print(F(", Round-trip delay ")); Serial.print(end_time-start_time); Serial.println(F(" microseconds")); } // Try again 1s later delay(1000); } /****************** Pong Back Role ***************************/ if ( role == 0 ) { unsigned long got_time; if( radio.available()){ // Variable for the received timestamp while (radio.available()) { // While there is data ready radio.read( &got_time, sizeof(unsigned long) ); // Get the payload } radio.stopListening(); // First, stop listening so we can talk radio.write( &got_time, sizeof(unsigned long) ); // Send the final one back. radio.startListening(); // Now, resume listening so we catch the next packets. Serial.print(F("Sent response ")); Serial.println(got_time); } } /****************** Change Roles via Serial Commands ***************************/ if ( Serial.available() ) { char c = toupper(Serial.read()); if ( c == 'T' && role == 0 ){ Serial.println(F("*** CHANGING TO TRANSMIT ROLE -- PRESS 'R' TO SWITCH BACK")); role = 1; // Become the primary transmitter (ping out) }else if ( c == 'R' && role == 1 ){ Serial.println(F("*** CHANGING TO RECEIVE ROLE -- PRESS 'T' TO SWITCH BACK")); role = 0; // Become the primary receiver (pong back) radio.startListening(); } } } // Loop```
Any help figuring this out would be greatly appreciated!
-
RE: RFID Garage door opener
@BartE thanks for getting back to me. I've looked through the examples on the MFRC522 library. I think the main thing I'm struggling with is setting up and initializing the N24L01 module and the RFID on the same arduino uno. My plan is to have one N24L01 and RFID on one arduino module, and a second N24L01 on a second arduino. I want the RFID to read the UID off of PICC cards and be able to wirelessly transmit the UID between the N24L01 modules. I think it's simpler than the code you have written here, but I'm struggling to simplify the code you have to only what I need. Could you possibly walk through what each function in your program is used for? Thanks in advance.
-
RE: RFID Garage door opener
Hi @BartE - thanks for putting this together! It has been very helpful.
I am working on a project with two wireless MFRC522 modules and RFID Reader. I want to be able to read PICC cards with the RFID reader and send it the UID between the modules. So essentially a more bare version of the code you have written with the garage door opening code. I was wondering if you could help me out in simplifying the code to just reading the UID off the PICC cards and outputting that date between the RCF522 modules.
Thanks in advance.