![](/media/cache/group/V2.png.50x50_q85.png)
FN
Dependencies: mbed RF24Network RF24
main.cpp@2:926b93a68399, 2015-07-06 (annotated)
- Committer:
- akashvibhute
- Date:
- Mon Jul 06 05:24:49 2015 +0000
- Revision:
- 2:926b93a68399
- Parent:
- 1:5be48a9550c3
- Child:
- 3:d605536db315
Example RF24Network Receive program. Tested on Nucleo F411
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:926b93a68399 | 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 | 2:926b93a68399 | 19 | const uint16_t this_node = 0; |
akashvibhute | 0:3982c0e9eda1 | 20 | |
akashvibhute | 0:3982c0e9eda1 | 21 | // Address of the other node |
akashvibhute | 2:926b93a68399 | 22 | const uint16_t other_node = 1; |
akashvibhute | 0:3982c0e9eda1 | 23 | |
akashvibhute | 2:926b93a68399 | 24 | // How often to send payload packet to the other unit |
akashvibhute | 2:926b93a68399 | 25 | const unsigned long interval = 1000; //ms |
akashvibhute | 0:3982c0e9eda1 | 26 | |
akashvibhute | 0:3982c0e9eda1 | 27 | // When did we last send? |
akashvibhute | 0:3982c0e9eda1 | 28 | unsigned long last_sent; |
akashvibhute | 1:5be48a9550c3 | 29 | Timer t; |
akashvibhute | 0:3982c0e9eda1 | 30 | |
akashvibhute | 0:3982c0e9eda1 | 31 | // How many have we sent already |
akashvibhute | 0:3982c0e9eda1 | 32 | unsigned long packets_sent; |
akashvibhute | 1:5be48a9550c3 | 33 | Timer t_packet; |
akashvibhute | 0:3982c0e9eda1 | 34 | |
akashvibhute | 0:3982c0e9eda1 | 35 | // Structure of our payload |
akashvibhute | 2:926b93a68399 | 36 | struct payload_t |
akashvibhute | 0:3982c0e9eda1 | 37 | { |
akashvibhute | 2:926b93a68399 | 38 | unsigned long ms; |
akashvibhute | 2:926b93a68399 | 39 | unsigned long counter; |
akashvibhute | 2:926b93a68399 | 40 | |
akashvibhute | 2:926b93a68399 | 41 | float vector_4d[4]; |
akashvibhute | 0:3982c0e9eda1 | 42 | }; |
akashvibhute | 0:3982c0e9eda1 | 43 | |
akashvibhute | 0:3982c0e9eda1 | 44 | |
akashvibhute | 2:926b93a68399 | 45 | int main() |
akashvibhute | 0:3982c0e9eda1 | 46 | { |
akashvibhute | 0:3982c0e9eda1 | 47 | pc.baud(921600); |
akashvibhute | 0:3982c0e9eda1 | 48 | wait_ms(1000); |
akashvibhute | 2:926b93a68399 | 49 | |
akashvibhute | 2:926b93a68399 | 50 | pc.printf("mBed RF24Network node: Tx\n"); |
akashvibhute | 0:3982c0e9eda1 | 51 | radio.begin(); |
akashvibhute | 0:3982c0e9eda1 | 52 | network.begin(/*channel*/ 90, /*node address*/ this_node); |
akashvibhute | 0:3982c0e9eda1 | 53 | wait_ms(2000); |
akashvibhute | 1:5be48a9550c3 | 54 | t.start(); |
akashvibhute | 1:5be48a9550c3 | 55 | t_packet.start(); |
akashvibhute | 2:926b93a68399 | 56 | while(1) |
akashvibhute | 0:3982c0e9eda1 | 57 | { |
akashvibhute | 0:3982c0e9eda1 | 58 | // Pump the network regularly |
akashvibhute | 0:3982c0e9eda1 | 59 | network.update(); |
akashvibhute | 2:926b93a68399 | 60 | |
akashvibhute | 1:5be48a9550c3 | 61 | /* If it's time to send a message, send it! */ |
akashvibhute | 1:5be48a9550c3 | 62 | unsigned long now = t.read_ms(); |
akashvibhute | 2:926b93a68399 | 63 | if ( now >= interval ) |
akashvibhute | 1:5be48a9550c3 | 64 | { |
akashvibhute | 1:5be48a9550c3 | 65 | t.reset(); |
akashvibhute | 1:5be48a9550c3 | 66 | |
akashvibhute | 1:5be48a9550c3 | 67 | pc.printf("Sending..."); |
akashvibhute | 1:5be48a9550c3 | 68 | payload_t payload_tx; |
akashvibhute | 1:5be48a9550c3 | 69 | payload_tx.ms = t_packet.read_ms(); |
akashvibhute | 1:5be48a9550c3 | 70 | payload_tx.counter = packets_sent++; |
akashvibhute | 2:926b93a68399 | 71 | for(int i=0; i<=3; i++) |
akashvibhute | 1:5be48a9550c3 | 72 | { |
akashvibhute | 1:5be48a9550c3 | 73 | payload_tx.vector_4d[i] = i + 1.00f; |
akashvibhute | 1:5be48a9550c3 | 74 | } |
akashvibhute | 2:926b93a68399 | 75 | |
akashvibhute | 2:926b93a68399 | 76 | |
akashvibhute | 1:5be48a9550c3 | 77 | RF24NetworkHeader header_tx(/*to node*/ other_node); |
akashvibhute | 1:5be48a9550c3 | 78 | bool ok = network.write(header_tx,&payload_tx,sizeof(payload_tx)); |
akashvibhute | 1:5be48a9550c3 | 79 | if (ok) |
akashvibhute | 2:926b93a68399 | 80 | pc.printf("ok.\n"); |
akashvibhute | 1:5be48a9550c3 | 81 | else |
akashvibhute | 2:926b93a68399 | 82 | pc.printf("failed.\n"); |
akashvibhute | 2:926b93a68399 | 83 | } |
akashvibhute | 2:926b93a68399 | 84 | |
akashvibhute | 2:926b93a68399 | 85 | |
akashvibhute | 0:3982c0e9eda1 | 86 | } |
akashvibhute | 2:926b93a68399 | 87 | |
akashvibhute | 0:3982c0e9eda1 | 88 | } |