Following on from the initial creation of the nRF Multi-Network, I’ve had a bit more time to re-think the design. I was starting to think of having each nRF request for nearby nRFs, exchange nRFs addresses that each one can see and then have them build a routing table of the possible paths to take to each other.
However that’s a bit too much effort, so instead I’ve decided that if an nRF wants to forward a packet, that it should forward it to all nRFs and the nRFs that receive it should forward it onto all others and so on. For the moment I wanted to test out checking for neighbouring nRFs so they would just reply back to the sender only.
Our updated packet array elements is now:
#define FROM 0 #define TO 1 #define TYPE 2 // Stores all the types like ACK, NOACK, FWD, NEIGHOURREQ, etc #define TTL 3 // Time to live, to be used to de-incremented at each forward #define DATA 4 #define DATAEND 31
I’ve added a button which will trigger the nRF to request for neighbouring nRFs.
neighbourNRFcount = 0; ... data_out[FROM] = MYADDR; data_out[TO] = ALLADDR; // Send to All addresses data_out[TYPE] = NEIGHBOURREQ; // Request all nRFs to reply back data_out[DATA] = 0; mirf_transmit_data(); // Wait for replies for 50 ms (To do: Add a proper timer) for (int x = 0; x < 20; x++) { mirf_listen_5ms_timeout(); receive_packet_and_process(); } // Blink LED for each nRF that replies back for (int x = 0; x < neighbourNRFcount; x++) { PORTB |= (1<<PB2); _delay_ms(500); PORTB &= ~(1<<PB2); _delay_ms(500); } ... // Neighbouring NRF response, add from address to list else if (data_in[TO] == MYADDR && data_in[TYPE] == NEIGHBOUR_RESPOND) { // Check the from address doesn't exist already bool fromExists = false; for (int x = 0; x < neighbourNRFcount; x++) { if (neighbourNRF[x] == data_in[FROM]) { fromExists = true; break; } } if (fromExists == false) { // Add to list neighbourNRF[neighbourNRFcount] = data_in[FROM]; neighbourNRFcount++; }
We transmit the data as normal, then start listening from replies. When we receive a reply, we add it to the array of nRFs received.
// Neighbouring NRF request, reply to it if (data_in[TYPE] == NEIGHBOURREQ) { ... data_out[FROM] = MYADDR; // From our address data_out[TO] = data_in[FROM]; // Send back to the address that we received this packet from data_out[TYPE] = NEIGHBOUR_RESPOND; // Set type // Wait a random amount of time (0 to 3.767ms) _delay_us(rand() / 10); // Send the ACK mirf_transmit_data(); }
When we receive the request, we delay for a random amount of time to give other nRFs a chance to send their response. Download nRF24_Multi-Network_TX-RX_v0.2
https://www.youtube.com/watch?v=br4BurV5uoY
Each blink corresponds to how many neighbouring nRF it finds.