![](/media/cache/group/V2.png.50x50_q85.png)
FN
Dependencies: mbed RF24Network RF24
main.cpp@4:f70f3b565af0, 2019-01-21 (annotated)
- Committer:
- wesleytiem
- Date:
- Mon Jan 21 15:18:04 2019 +0000
- Revision:
- 4:f70f3b565af0
- Parent:
- 3:d605536db315
- Child:
- 5:bfef4ea383be
asdasd
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 | |
wesleytiem | 4:f70f3b565af0 | 7 | RF24 radio(SPI_MOSI, SPI_MISO, SPI_SCK, D9, SPI_CS ); |
akashvibhute | 0:3982c0e9eda1 | 8 | |
akashvibhute | 0:3982c0e9eda1 | 9 | // Network uses that radio |
akashvibhute | 0:3982c0e9eda1 | 10 | RF24Network network(radio); |
akashvibhute | 0:3982c0e9eda1 | 11 | |
akashvibhute | 0:3982c0e9eda1 | 12 | // Address of our node |
akashvibhute | 3:d605536db315 | 13 | const uint16_t this_node = 01; |
akashvibhute | 0:3982c0e9eda1 | 14 | |
akashvibhute | 0:3982c0e9eda1 | 15 | // Address of the other node |
akashvibhute | 3:d605536db315 | 16 | const uint16_t other_node = 00; |
akashvibhute | 0:3982c0e9eda1 | 17 | |
akashvibhute | 2:926b93a68399 | 18 | // How often to send payload packet to the other unit |
akashvibhute | 3:d605536db315 | 19 | const unsigned long interval = 100; //ms |
akashvibhute | 0:3982c0e9eda1 | 20 | |
akashvibhute | 0:3982c0e9eda1 | 21 | // When did we last send? |
akashvibhute | 0:3982c0e9eda1 | 22 | unsigned long last_sent; |
akashvibhute | 1:5be48a9550c3 | 23 | Timer t; |
akashvibhute | 0:3982c0e9eda1 | 24 | |
akashvibhute | 0:3982c0e9eda1 | 25 | // How many have we sent already |
akashvibhute | 0:3982c0e9eda1 | 26 | unsigned long packets_sent; |
akashvibhute | 1:5be48a9550c3 | 27 | Timer t_packet; |
akashvibhute | 0:3982c0e9eda1 | 28 | |
akashvibhute | 0:3982c0e9eda1 | 29 | // Structure of our payload |
akashvibhute | 2:926b93a68399 | 30 | struct payload_t |
akashvibhute | 0:3982c0e9eda1 | 31 | { |
akashvibhute | 2:926b93a68399 | 32 | unsigned long ms; |
akashvibhute | 2:926b93a68399 | 33 | unsigned long counter; |
akashvibhute | 0:3982c0e9eda1 | 34 | }; |
akashvibhute | 0:3982c0e9eda1 | 35 | |
akashvibhute | 0:3982c0e9eda1 | 36 | |
akashvibhute | 2:926b93a68399 | 37 | int main() |
akashvibhute | 0:3982c0e9eda1 | 38 | { |
wesleytiem | 4:f70f3b565af0 | 39 | pc.baud(115200); |
akashvibhute | 0:3982c0e9eda1 | 40 | wait_ms(1000); |
akashvibhute | 2:926b93a68399 | 41 | |
akashvibhute | 2:926b93a68399 | 42 | pc.printf("mBed RF24Network node: Tx\n"); |
akashvibhute | 0:3982c0e9eda1 | 43 | radio.begin(); |
akashvibhute | 0:3982c0e9eda1 | 44 | network.begin(/*channel*/ 90, /*node address*/ this_node); |
akashvibhute | 0:3982c0e9eda1 | 45 | wait_ms(2000); |
akashvibhute | 1:5be48a9550c3 | 46 | t.start(); |
akashvibhute | 1:5be48a9550c3 | 47 | t_packet.start(); |
akashvibhute | 2:926b93a68399 | 48 | while(1) |
akashvibhute | 0:3982c0e9eda1 | 49 | { |
akashvibhute | 0:3982c0e9eda1 | 50 | // Pump the network regularly |
akashvibhute | 0:3982c0e9eda1 | 51 | network.update(); |
akashvibhute | 2:926b93a68399 | 52 | |
akashvibhute | 1:5be48a9550c3 | 53 | /* If it's time to send a message, send it! */ |
akashvibhute | 1:5be48a9550c3 | 54 | unsigned long now = t.read_ms(); |
akashvibhute | 2:926b93a68399 | 55 | if ( now >= interval ) |
akashvibhute | 1:5be48a9550c3 | 56 | { |
akashvibhute | 1:5be48a9550c3 | 57 | t.reset(); |
akashvibhute | 1:5be48a9550c3 | 58 | |
akashvibhute | 1:5be48a9550c3 | 59 | pc.printf("Sending..."); |
akashvibhute | 1:5be48a9550c3 | 60 | payload_t payload_tx; |
akashvibhute | 1:5be48a9550c3 | 61 | payload_tx.ms = t_packet.read_ms(); |
akashvibhute | 1:5be48a9550c3 | 62 | payload_tx.counter = packets_sent++; |
akashvibhute | 2:926b93a68399 | 63 | |
akashvibhute | 2:926b93a68399 | 64 | |
akashvibhute | 1:5be48a9550c3 | 65 | RF24NetworkHeader header_tx(/*to node*/ other_node); |
akashvibhute | 1:5be48a9550c3 | 66 | bool ok = network.write(header_tx,&payload_tx,sizeof(payload_tx)); |
akashvibhute | 1:5be48a9550c3 | 67 | if (ok) |
wesleytiem | 4:f70f3b565af0 | 68 | pc.printf("ok.\r\n"); |
akashvibhute | 1:5be48a9550c3 | 69 | else |
wesleytiem | 4:f70f3b565af0 | 70 | pc.printf("failed.\r\n"); |
akashvibhute | 2:926b93a68399 | 71 | } |
akashvibhute | 2:926b93a68399 | 72 | |
akashvibhute | 2:926b93a68399 | 73 | |
akashvibhute | 0:3982c0e9eda1 | 74 | } |
akashvibhute | 2:926b93a68399 | 75 | |
akashvibhute | 0:3982c0e9eda1 | 76 | } |