Example RF24Network receive program. Tested on Nucleo 411.
Dependencies: RF24 RF24Network mbed
main.cpp@2:608cf8c5c55e, 2015-07-06 (annotated)
- Committer:
- akashvibhute
- Date:
- Mon Jul 06 05:23:41 2015 +0000
- Revision:
- 2:608cf8c5c55e
- Parent:
- 1:5be48a9550c3
- Child:
- 3:e9c4d66da50c
Example RF24Network receive program. Tested on Nucleo 411
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
akashvibhute | 0:3982c0e9eda1 | 1 | #include "mbed.h" |
akashvibhute | 0:3982c0e9eda1 | 2 | #include <RF24Network.h> |
akashvibhute | 2:608cf8c5c55e | 3 | #include <RF24.h> |
akashvibhute | 0:3982c0e9eda1 | 4 | |
akashvibhute | 0:3982c0e9eda1 | 5 | Serial pc(USBTX, USBRX); |
akashvibhute | 0:3982c0e9eda1 | 6 | |
akashvibhute | 0:3982c0e9eda1 | 7 | #define nrf_CE D9 |
akashvibhute | 0:3982c0e9eda1 | 8 | #define nrf_CSN D10 |
akashvibhute | 0:3982c0e9eda1 | 9 | #define spi_SCK D3 |
akashvibhute | 0:3982c0e9eda1 | 10 | #define spi_MOSI D4 |
akashvibhute | 0:3982c0e9eda1 | 11 | #define spi_MISO D5 |
akashvibhute | 0:3982c0e9eda1 | 12 | |
akashvibhute | 0:3982c0e9eda1 | 13 | RF24 radio(spi_MOSI, spi_MISO, spi_SCK, nrf_CSN, nrf_CE); |
akashvibhute | 0:3982c0e9eda1 | 14 | |
akashvibhute | 0:3982c0e9eda1 | 15 | // Network uses that radio |
akashvibhute | 0:3982c0e9eda1 | 16 | RF24Network network(radio); |
akashvibhute | 0:3982c0e9eda1 | 17 | |
akashvibhute | 0:3982c0e9eda1 | 18 | // Address of our node |
akashvibhute | 1:5be48a9550c3 | 19 | const uint16_t this_node = 1; |
akashvibhute | 0:3982c0e9eda1 | 20 | |
akashvibhute | 0:3982c0e9eda1 | 21 | // Address of the other node |
akashvibhute | 1:5be48a9550c3 | 22 | const uint16_t other_node = 0; |
akashvibhute | 0:3982c0e9eda1 | 23 | |
akashvibhute | 0:3982c0e9eda1 | 24 | // When did we last send? |
akashvibhute | 0:3982c0e9eda1 | 25 | unsigned long last_sent; |
akashvibhute | 0:3982c0e9eda1 | 26 | |
akashvibhute | 0:3982c0e9eda1 | 27 | // How many have we sent already |
akashvibhute | 0:3982c0e9eda1 | 28 | unsigned long packets_sent; |
akashvibhute | 0:3982c0e9eda1 | 29 | |
akashvibhute | 0:3982c0e9eda1 | 30 | // Structure of our payload |
akashvibhute | 2:608cf8c5c55e | 31 | struct payload_t |
akashvibhute | 0:3982c0e9eda1 | 32 | { |
akashvibhute | 2:608cf8c5c55e | 33 | unsigned long ms; |
akashvibhute | 2:608cf8c5c55e | 34 | unsigned long counter; |
akashvibhute | 2:608cf8c5c55e | 35 | |
akashvibhute | 2:608cf8c5c55e | 36 | float vector_4d[4]; |
akashvibhute | 0:3982c0e9eda1 | 37 | }; |
akashvibhute | 0:3982c0e9eda1 | 38 | |
akashvibhute | 0:3982c0e9eda1 | 39 | |
akashvibhute | 2:608cf8c5c55e | 40 | int main() |
akashvibhute | 0:3982c0e9eda1 | 41 | { |
akashvibhute | 0:3982c0e9eda1 | 42 | pc.baud(921600); |
akashvibhute | 0:3982c0e9eda1 | 43 | wait_ms(1000); |
akashvibhute | 2:608cf8c5c55e | 44 | |
akashvibhute | 2:608cf8c5c55e | 45 | |
akashvibhute | 2:608cf8c5c55e | 46 | |
akashvibhute | 2:608cf8c5c55e | 47 | pc.printf("mBed RF24Network node\n"); |
akashvibhute | 0:3982c0e9eda1 | 48 | radio.begin(); |
akashvibhute | 0:3982c0e9eda1 | 49 | network.begin(/*channel*/ 90, /*node address*/ this_node); |
akashvibhute | 0:3982c0e9eda1 | 50 | wait_ms(2000); |
akashvibhute | 2:608cf8c5c55e | 51 | |
akashvibhute | 2:608cf8c5c55e | 52 | while(1) |
akashvibhute | 0:3982c0e9eda1 | 53 | { |
akashvibhute | 0:3982c0e9eda1 | 54 | // Pump the network regularly |
akashvibhute | 0:3982c0e9eda1 | 55 | network.update(); |
akashvibhute | 2:608cf8c5c55e | 56 | |
akashvibhute | 0:3982c0e9eda1 | 57 | // Is there anything ready for us? |
akashvibhute | 2:608cf8c5c55e | 58 | while ( network.available() ) |
akashvibhute | 0:3982c0e9eda1 | 59 | { |
akashvibhute | 0:3982c0e9eda1 | 60 | // If so, grab it and print it out |
akashvibhute | 0:3982c0e9eda1 | 61 | RF24NetworkHeader header_rx; |
akashvibhute | 0:3982c0e9eda1 | 62 | payload_t payload_rx; |
akashvibhute | 0:3982c0e9eda1 | 63 | network.read(header_rx,&payload_rx,sizeof(payload_rx)); |
akashvibhute | 0:3982c0e9eda1 | 64 | pc.printf("Received packet # %d at %d ms, message: V4 %f, %f, %f, %f \n",payload_rx.counter,payload_rx.ms, payload_rx.vector_4d[0],payload_rx.vector_4d[1],payload_rx.vector_4d[2],payload_rx.vector_4d[3]); |
akashvibhute | 0:3982c0e9eda1 | 65 | } |
akashvibhute | 1:5be48a9550c3 | 66 | |
akashvibhute | 0:3982c0e9eda1 | 67 | } |
akashvibhute | 2:608cf8c5c55e | 68 | |
akashvibhute | 0:3982c0e9eda1 | 69 | } |