RF24Network Send example program.
Dependencies: xtoff RF24Network mbed
Fork of RF24Network_Send by
main.cpp@4:bc1126d78e55, 2018-02-12 (annotated)
- Committer:
- pietor
- Date:
- Mon Feb 12 16:50:37 2018 +0000
- Revision:
- 4:bc1126d78e55
- Parent:
- 3:d605536db315
- Child:
- 5:e6067799a414
Verzender;
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> |
pietor | 4:bc1126d78e55 | 4 | #include <string> |
akashvibhute | 0:3982c0e9eda1 | 5 | |
akashvibhute | 0:3982c0e9eda1 | 6 | Serial pc(USBTX, USBRX); |
akashvibhute | 0:3982c0e9eda1 | 7 | |
pietor | 4:bc1126d78e55 | 8 | #define nrf_CE p9 |
pietor | 4:bc1126d78e55 | 9 | #define nrf_CSN p8 |
pietor | 4:bc1126d78e55 | 10 | #define spi_SCK p7 |
pietor | 4:bc1126d78e55 | 11 | #define spi_MOSI p5 |
pietor | 4:bc1126d78e55 | 12 | #define spi_MISO p6 |
akashvibhute | 0:3982c0e9eda1 | 13 | |
akashvibhute | 3:d605536db315 | 14 | RF24 radio(spi_MOSI, spi_MISO, spi_SCK, nrf_CE, nrf_CSN ); |
akashvibhute | 0:3982c0e9eda1 | 15 | RF24Network network(radio); |
akashvibhute | 0:3982c0e9eda1 | 16 | |
akashvibhute | 3:d605536db315 | 17 | const uint16_t this_node = 01; |
akashvibhute | 3:d605536db315 | 18 | const uint16_t other_node = 00; |
akashvibhute | 3:d605536db315 | 19 | const unsigned long interval = 100; //ms |
akashvibhute | 0:3982c0e9eda1 | 20 | unsigned long last_sent; |
akashvibhute | 1:5be48a9550c3 | 21 | Timer t; |
akashvibhute | 0:3982c0e9eda1 | 22 | |
akashvibhute | 0:3982c0e9eda1 | 23 | unsigned long packets_sent; |
akashvibhute | 1:5be48a9550c3 | 24 | Timer t_packet; |
akashvibhute | 0:3982c0e9eda1 | 25 | |
pietor | 4:bc1126d78e55 | 26 | DigitalIn pb(p25); |
pietor | 4:bc1126d78e55 | 27 | |
pietor | 4:bc1126d78e55 | 28 | |
akashvibhute | 0:3982c0e9eda1 | 29 | // Structure of our payload |
akashvibhute | 2:926b93a68399 | 30 | struct payload_t |
akashvibhute | 0:3982c0e9eda1 | 31 | { |
pietor | 4:bc1126d78e55 | 32 | bool reedsensor; |
pietor | 4:bc1126d78e55 | 33 | int milligram; |
akashvibhute | 0:3982c0e9eda1 | 34 | }; |
akashvibhute | 0:3982c0e9eda1 | 35 | |
akashvibhute | 0:3982c0e9eda1 | 36 | |
akashvibhute | 2:926b93a68399 | 37 | int main() |
akashvibhute | 0:3982c0e9eda1 | 38 | { |
pietor | 4:bc1126d78e55 | 39 | pb.mode(PullUp); |
pietor | 4:bc1126d78e55 | 40 | pc.baud(9600); |
akashvibhute | 0:3982c0e9eda1 | 41 | wait_ms(1000); |
akashvibhute | 2:926b93a68399 | 42 | |
pietor | 4:bc1126d78e55 | 43 | pc.printf("mBed RF24Network node: Tx\n\r"); |
akashvibhute | 0:3982c0e9eda1 | 44 | radio.begin(); |
pietor | 4:bc1126d78e55 | 45 | network.begin(90,this_node); |
akashvibhute | 0:3982c0e9eda1 | 46 | wait_ms(2000); |
akashvibhute | 1:5be48a9550c3 | 47 | t.start(); |
akashvibhute | 1:5be48a9550c3 | 48 | t_packet.start(); |
akashvibhute | 2:926b93a68399 | 49 | while(1) |
akashvibhute | 0:3982c0e9eda1 | 50 | { |
akashvibhute | 0:3982c0e9eda1 | 51 | network.update(); |
akashvibhute | 1:5be48a9550c3 | 52 | unsigned long now = t.read_ms(); |
pietor | 4:bc1126d78e55 | 53 | if (!pb) |
akashvibhute | 1:5be48a9550c3 | 54 | { |
akashvibhute | 1:5be48a9550c3 | 55 | t.reset(); |
akashvibhute | 1:5be48a9550c3 | 56 | |
akashvibhute | 1:5be48a9550c3 | 57 | pc.printf("Sending..."); |
akashvibhute | 1:5be48a9550c3 | 58 | payload_t payload_tx; |
pietor | 4:bc1126d78e55 | 59 | payload_tx.reedsensor = !pb; |
pietor | 4:bc1126d78e55 | 60 | payload_tx.milligram = 521000; |
akashvibhute | 2:926b93a68399 | 61 | |
akashvibhute | 2:926b93a68399 | 62 | |
pietor | 4:bc1126d78e55 | 63 | RF24NetworkHeader header_tx(other_node); |
pietor | 4:bc1126d78e55 | 64 | |
akashvibhute | 1:5be48a9550c3 | 65 | bool ok = network.write(header_tx,&payload_tx,sizeof(payload_tx)); |
akashvibhute | 1:5be48a9550c3 | 66 | if (ok) |
pietor | 4:bc1126d78e55 | 67 | pc.printf("ok.\n\r"); |
akashvibhute | 1:5be48a9550c3 | 68 | else |
pietor | 4:bc1126d78e55 | 69 | pc.printf("failed.\n\r"); |
akashvibhute | 2:926b93a68399 | 70 | } |
akashvibhute | 0:3982c0e9eda1 | 71 | } |
akashvibhute | 0:3982c0e9eda1 | 72 | } |