![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
RF24Network Send example program. Tested on Nucleo-F411.
Dependencies: RF24 RF24Network mbed
main.cpp@3:d605536db315, 2015-11-05 (annotated)
- Committer:
- akashvibhute
- Date:
- Thu Nov 05 05:59:18 2015 +0000
- Revision:
- 3:d605536db315
- Parent:
- 2:926b93a68399
updated with new rf24 library Nov/05/2015
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 | 3:d605536db315 | 13 | RF24 radio(spi_MOSI, spi_MISO, spi_SCK, nrf_CE, nrf_CSN ); |
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 | 3:d605536db315 | 19 | const uint16_t this_node = 01; |
akashvibhute | 0:3982c0e9eda1 | 20 | |
akashvibhute | 0:3982c0e9eda1 | 21 | // Address of the other node |
akashvibhute | 3:d605536db315 | 22 | const uint16_t other_node = 00; |
akashvibhute | 0:3982c0e9eda1 | 23 | |
akashvibhute | 2:926b93a68399 | 24 | // How often to send payload packet to the other unit |
akashvibhute | 3:d605536db315 | 25 | const unsigned long interval = 100; //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 | 0:3982c0e9eda1 | 40 | }; |
akashvibhute | 0:3982c0e9eda1 | 41 | |
akashvibhute | 0:3982c0e9eda1 | 42 | |
akashvibhute | 2:926b93a68399 | 43 | int main() |
akashvibhute | 0:3982c0e9eda1 | 44 | { |
akashvibhute | 0:3982c0e9eda1 | 45 | pc.baud(921600); |
akashvibhute | 0:3982c0e9eda1 | 46 | wait_ms(1000); |
akashvibhute | 2:926b93a68399 | 47 | |
akashvibhute | 2:926b93a68399 | 48 | pc.printf("mBed RF24Network node: Tx\n"); |
akashvibhute | 0:3982c0e9eda1 | 49 | radio.begin(); |
akashvibhute | 0:3982c0e9eda1 | 50 | network.begin(/*channel*/ 90, /*node address*/ this_node); |
akashvibhute | 0:3982c0e9eda1 | 51 | wait_ms(2000); |
akashvibhute | 1:5be48a9550c3 | 52 | t.start(); |
akashvibhute | 1:5be48a9550c3 | 53 | t_packet.start(); |
akashvibhute | 2:926b93a68399 | 54 | while(1) |
akashvibhute | 0:3982c0e9eda1 | 55 | { |
akashvibhute | 0:3982c0e9eda1 | 56 | // Pump the network regularly |
akashvibhute | 0:3982c0e9eda1 | 57 | network.update(); |
akashvibhute | 2:926b93a68399 | 58 | |
akashvibhute | 1:5be48a9550c3 | 59 | /* If it's time to send a message, send it! */ |
akashvibhute | 1:5be48a9550c3 | 60 | unsigned long now = t.read_ms(); |
akashvibhute | 2:926b93a68399 | 61 | if ( now >= interval ) |
akashvibhute | 1:5be48a9550c3 | 62 | { |
akashvibhute | 1:5be48a9550c3 | 63 | t.reset(); |
akashvibhute | 1:5be48a9550c3 | 64 | |
akashvibhute | 1:5be48a9550c3 | 65 | pc.printf("Sending..."); |
akashvibhute | 1:5be48a9550c3 | 66 | payload_t payload_tx; |
akashvibhute | 1:5be48a9550c3 | 67 | payload_tx.ms = t_packet.read_ms(); |
akashvibhute | 1:5be48a9550c3 | 68 | payload_tx.counter = packets_sent++; |
akashvibhute | 2:926b93a68399 | 69 | |
akashvibhute | 2:926b93a68399 | 70 | |
akashvibhute | 1:5be48a9550c3 | 71 | RF24NetworkHeader header_tx(/*to node*/ other_node); |
akashvibhute | 1:5be48a9550c3 | 72 | bool ok = network.write(header_tx,&payload_tx,sizeof(payload_tx)); |
akashvibhute | 1:5be48a9550c3 | 73 | if (ok) |
akashvibhute | 2:926b93a68399 | 74 | pc.printf("ok.\n"); |
akashvibhute | 1:5be48a9550c3 | 75 | else |
akashvibhute | 2:926b93a68399 | 76 | pc.printf("failed.\n"); |
akashvibhute | 2:926b93a68399 | 77 | } |
akashvibhute | 2:926b93a68399 | 78 | |
akashvibhute | 2:926b93a68399 | 79 | |
akashvibhute | 0:3982c0e9eda1 | 80 | } |
akashvibhute | 2:926b93a68399 | 81 | |
akashvibhute | 0:3982c0e9eda1 | 82 | } |