![](/media/cache/group/isen_med_yncrea.jpg.50x50_q85.jpg)
Copy off node 1 to behave node 2
Dependencies: mbed RF24Network RF24
main.cpp@1:5be48a9550c3, 2015-07-06 (annotated)
- Committer:
- akashvibhute
- Date:
- Mon Jul 06 04:03:48 2015 +0000
- Revision:
- 1:5be48a9550c3
- Parent:
- 0:3982c0e9eda1
- Child:
- 2:926b93a68399
Bi-directional communication with Arduino (Teensy3.1)!!!
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 | 0:3982c0e9eda1 | 3 | #include <nRF24L01P_Maniacbug.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 nrf_IRQ PB_0 |
akashvibhute | 0:3982c0e9eda1 | 10 | #define spi_SCK D3 |
akashvibhute | 0:3982c0e9eda1 | 11 | #define spi_MOSI D4 |
akashvibhute | 0:3982c0e9eda1 | 12 | #define spi_MISO D5 |
akashvibhute | 0:3982c0e9eda1 | 13 | |
akashvibhute | 0:3982c0e9eda1 | 14 | //RF24 radio(D11, D12, D13, D10, D9); |
akashvibhute | 0:3982c0e9eda1 | 15 | RF24 radio(spi_MOSI, spi_MISO, spi_SCK, nrf_CSN, nrf_CE); |
akashvibhute | 0:3982c0e9eda1 | 16 | |
akashvibhute | 0:3982c0e9eda1 | 17 | // Network uses that radio |
akashvibhute | 0:3982c0e9eda1 | 18 | RF24Network network(radio); |
akashvibhute | 0:3982c0e9eda1 | 19 | |
akashvibhute | 0:3982c0e9eda1 | 20 | // Address of our node |
akashvibhute | 1:5be48a9550c3 | 21 | const uint16_t this_node = 1; |
akashvibhute | 0:3982c0e9eda1 | 22 | |
akashvibhute | 0:3982c0e9eda1 | 23 | // Address of the other node |
akashvibhute | 1:5be48a9550c3 | 24 | const uint16_t other_node = 0; |
akashvibhute | 0:3982c0e9eda1 | 25 | |
akashvibhute | 0:3982c0e9eda1 | 26 | // How often to send 'hello world to the other unit |
akashvibhute | 0:3982c0e9eda1 | 27 | const unsigned long interval = 100; //ms |
akashvibhute | 0:3982c0e9eda1 | 28 | |
akashvibhute | 0:3982c0e9eda1 | 29 | // When did we last send? |
akashvibhute | 0:3982c0e9eda1 | 30 | unsigned long last_sent; |
akashvibhute | 1:5be48a9550c3 | 31 | Timer t; |
akashvibhute | 0:3982c0e9eda1 | 32 | |
akashvibhute | 0:3982c0e9eda1 | 33 | // How many have we sent already |
akashvibhute | 0:3982c0e9eda1 | 34 | unsigned long packets_sent; |
akashvibhute | 1:5be48a9550c3 | 35 | Timer t_packet; |
akashvibhute | 0:3982c0e9eda1 | 36 | |
akashvibhute | 0:3982c0e9eda1 | 37 | // Structure of our payload |
akashvibhute | 0:3982c0e9eda1 | 38 | struct payload_t |
akashvibhute | 0:3982c0e9eda1 | 39 | { |
akashvibhute | 0:3982c0e9eda1 | 40 | unsigned long ms; |
akashvibhute | 0:3982c0e9eda1 | 41 | unsigned long counter; |
akashvibhute | 0:3982c0e9eda1 | 42 | |
akashvibhute | 0:3982c0e9eda1 | 43 | float vector_4d[4]; |
akashvibhute | 0:3982c0e9eda1 | 44 | }; |
akashvibhute | 0:3982c0e9eda1 | 45 | |
akashvibhute | 0:3982c0e9eda1 | 46 | |
akashvibhute | 0:3982c0e9eda1 | 47 | int main() |
akashvibhute | 0:3982c0e9eda1 | 48 | { |
akashvibhute | 0:3982c0e9eda1 | 49 | pc.baud(921600); |
akashvibhute | 0:3982c0e9eda1 | 50 | wait_ms(1000); |
akashvibhute | 0:3982c0e9eda1 | 51 | |
akashvibhute | 0:3982c0e9eda1 | 52 | |
akashvibhute | 0:3982c0e9eda1 | 53 | |
akashvibhute | 0:3982c0e9eda1 | 54 | pc.printf("mBed RF24 network node - Rx only\n"); |
akashvibhute | 0:3982c0e9eda1 | 55 | radio.begin(); |
akashvibhute | 0:3982c0e9eda1 | 56 | network.begin(/*channel*/ 90, /*node address*/ this_node); |
akashvibhute | 0:3982c0e9eda1 | 57 | wait_ms(2000); |
akashvibhute | 1:5be48a9550c3 | 58 | t.start(); |
akashvibhute | 1:5be48a9550c3 | 59 | t_packet.start(); |
akashvibhute | 0:3982c0e9eda1 | 60 | while(1) |
akashvibhute | 0:3982c0e9eda1 | 61 | { |
akashvibhute | 0:3982c0e9eda1 | 62 | // Pump the network regularly |
akashvibhute | 0:3982c0e9eda1 | 63 | network.update(); |
akashvibhute | 0:3982c0e9eda1 | 64 | |
akashvibhute | 0:3982c0e9eda1 | 65 | // Is there anything ready for us? |
akashvibhute | 0:3982c0e9eda1 | 66 | while ( network.available() ) |
akashvibhute | 0:3982c0e9eda1 | 67 | { |
akashvibhute | 0:3982c0e9eda1 | 68 | // If so, grab it and print it out |
akashvibhute | 0:3982c0e9eda1 | 69 | RF24NetworkHeader header_rx; |
akashvibhute | 0:3982c0e9eda1 | 70 | payload_t payload_rx; |
akashvibhute | 0:3982c0e9eda1 | 71 | network.read(header_rx,&payload_rx,sizeof(payload_rx)); |
akashvibhute | 0:3982c0e9eda1 | 72 | 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 | 73 | } |
akashvibhute | 1:5be48a9550c3 | 74 | |
akashvibhute | 1:5be48a9550c3 | 75 | /* If it's time to send a message, send it! */ |
akashvibhute | 1:5be48a9550c3 | 76 | unsigned long now = t.read_ms(); |
akashvibhute | 1:5be48a9550c3 | 77 | if ( now >= interval ) |
akashvibhute | 1:5be48a9550c3 | 78 | { |
akashvibhute | 1:5be48a9550c3 | 79 | t.reset(); |
akashvibhute | 1:5be48a9550c3 | 80 | |
akashvibhute | 1:5be48a9550c3 | 81 | pc.printf("Sending..."); |
akashvibhute | 1:5be48a9550c3 | 82 | //payload_t payload_tx = { millis(), packets_sent++, "Hello from node 0" }; |
akashvibhute | 1:5be48a9550c3 | 83 | payload_t payload_tx; |
akashvibhute | 1:5be48a9550c3 | 84 | payload_tx.ms = t_packet.read_ms(); |
akashvibhute | 1:5be48a9550c3 | 85 | payload_tx.counter = packets_sent++; |
akashvibhute | 1:5be48a9550c3 | 86 | for(int i=0;i<=3;i++) |
akashvibhute | 1:5be48a9550c3 | 87 | { |
akashvibhute | 1:5be48a9550c3 | 88 | payload_tx.vector_4d[i] = i + 1.00f; |
akashvibhute | 1:5be48a9550c3 | 89 | } |
akashvibhute | 1:5be48a9550c3 | 90 | |
akashvibhute | 1:5be48a9550c3 | 91 | |
akashvibhute | 1:5be48a9550c3 | 92 | RF24NetworkHeader header_tx(/*to node*/ other_node); |
akashvibhute | 1:5be48a9550c3 | 93 | bool ok = network.write(header_tx,&payload_tx,sizeof(payload_tx)); |
akashvibhute | 1:5be48a9550c3 | 94 | if (ok) |
akashvibhute | 1:5be48a9550c3 | 95 | pc.printf("ok.\n"); |
akashvibhute | 1:5be48a9550c3 | 96 | else |
akashvibhute | 1:5be48a9550c3 | 97 | pc.printf("failed.\n"); |
akashvibhute | 1:5be48a9550c3 | 98 | } |
akashvibhute | 1:5be48a9550c3 | 99 | |
akashvibhute | 1:5be48a9550c3 | 100 | |
akashvibhute | 0:3982c0e9eda1 | 101 | } |
akashvibhute | 0:3982c0e9eda1 | 102 | |
akashvibhute | 0:3982c0e9eda1 | 103 | } |