Michiel Van Endert
/
nRF24L01P_Project
Project wireless
Fork of nRF24L01P_Project by
main.cpp@3:1f92895432d2, 2015-05-13 (annotated)
- Committer:
- mvanendert
- Date:
- Wed May 13 07:22:11 2015 +0000
- Revision:
- 3:1f92895432d2
- Parent:
- 2:178e089b3ce9
- Child:
- 4:25a500ef61fc
Project Wireless ;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Owen | 0:a51a6e7da590 | 1 | #include "mbed.h" |
Owen | 0:a51a6e7da590 | 2 | #include "nRF24L01P.h" |
Owen | 0:a51a6e7da590 | 3 | |
Owen | 0:a51a6e7da590 | 4 | Serial pc(USBTX, USBRX); // tx, rx |
Owen | 0:a51a6e7da590 | 5 | |
Owen | 0:a51a6e7da590 | 6 | nRF24L01P my_nrf24l01p(p5, p6, p7, p8, p9, p10); // mosi, miso, sck, csn, ce, irq |
Owen | 0:a51a6e7da590 | 7 | |
Owen | 0:a51a6e7da590 | 8 | DigitalOut myled1(LED1); |
Owen | 0:a51a6e7da590 | 9 | DigitalOut myled2(LED2); |
mvanendert | 3:1f92895432d2 | 10 | AnalogIn LM35(p15); |
mvanendert | 3:1f92895432d2 | 11 | AnalogIn LDR(p16); |
mvanendert | 3:1f92895432d2 | 12 | |
mvanendert | 3:1f92895432d2 | 13 | #define TRANSFER_SIZE 32 |
mvanendert | 3:1f92895432d2 | 14 | |
mvanendert | 3:1f92895432d2 | 15 | char txData[TRANSFER_SIZE], rxData[TRANSFER_SIZE]; |
mvanendert | 3:1f92895432d2 | 16 | |
mvanendert | 3:1f92895432d2 | 17 | int txDataCnt = 0; |
mvanendert | 3:1f92895432d2 | 18 | int rxDataCnt = 0; |
mvanendert | 3:1f92895432d2 | 19 | |
mvanendert | 3:1f92895432d2 | 20 | float AvgTemp = 0; |
mvanendert | 3:1f92895432d2 | 21 | float AvgLight = 0; |
mvanendert | 3:1f92895432d2 | 22 | float TemperatureC = 0; |
mvanendert | 3:1f92895432d2 | 23 | float TemperatureF = 0; |
mvanendert | 3:1f92895432d2 | 24 | float a[10]; |
mvanendert | 3:1f92895432d2 | 25 | |
mvanendert | 3:1f92895432d2 | 26 | int i = 0; |
mvanendert | 3:1f92895432d2 | 27 | |
Owen | 0:a51a6e7da590 | 28 | |
Owen | 0:a51a6e7da590 | 29 | int main() { |
Owen | 0:a51a6e7da590 | 30 | |
Owen | 0:a51a6e7da590 | 31 | // The nRF24L01+ supports transfers from 1 to 32 bytes, but Sparkfun's |
mvanendert | 3:1f92895432d2 | 32 | // "Nordic Serial Interface Board" (http://www.sparkfun.com/products/9019) |
mvanendert | 3:1f92895432d2 | 33 | // Only handles 4 byte transfers in the ATMega code. |
Owen | 0:a51a6e7da590 | 34 | |
mvanendert | 3:1f92895432d2 | 35 | /* |
Owen | 0:a51a6e7da590 | 36 | my_nrf24l01p.powerUp(); |
ninoderkinderen | 2:178e089b3ce9 | 37 | |
ninoderkinderen | 2:178e089b3ce9 | 38 | //my_nrf24l01p.setRxAddress(0xE7E7E7E7E7,5); |
ninoderkinderen | 2:178e089b3ce9 | 39 | my_nrf24l01p.setRfFrequency(2450); |
mvanendert | 3:1f92895432d2 | 40 | my_nrf24l01p.setRfOutputPower(0); |
mvanendert | 3:1f92895432d2 | 41 | |
Owen | 0:a51a6e7da590 | 42 | |
mvanendert | 3:1f92895432d2 | 43 | //my_nrf24l01p.enableAutoRetransmit(1000, 5); //enableAutoRetransmit ( int delay, int count); |
mvanendert | 3:1f92895432d2 | 44 | // delay the delay between restransmits, in uS (250uS..4000uS) |
mvanendert | 3:1f92895432d2 | 45 | // count number of retransmits before generating an error (1..15) |
mvanendert | 3:1f92895432d2 | 46 | |
mvanendert | 3:1f92895432d2 | 47 | //Enable autoAcknowledge |
mvanendert | 3:1f92895432d2 | 48 | my_nrf24l01p.enableAutoAcknowledge(0); |
mvanendert | 3:1f92895432d2 | 49 | my_nrf24l01p.enableAutoAcknowledge(1); |
mvanendert | 3:1f92895432d2 | 50 | my_nrf24l01p.enableAutoAcknowledge(2); |
mvanendert | 3:1f92895432d2 | 51 | my_nrf24l01p.enableAutoAcknowledge(3); |
mvanendert | 3:1f92895432d2 | 52 | my_nrf24l01p.enableAutoAcknowledge(4); |
mvanendert | 3:1f92895432d2 | 53 | my_nrf24l01p.enableAutoAcknowledge(5); |
mvanendert | 3:1f92895432d2 | 54 | |
Owen | 0:a51a6e7da590 | 55 | // Display the (default) setup of the nRF24L01+ chip |
mvanendert | 3:1f92895432d2 | 56 | pc.printf( "nRF24L01+ Frequency : %d MHz\r\n", my_nrf24l01p.getRfFrequency() ); |
Owen | 0:a51a6e7da590 | 57 | pc.printf( "nRF24L01+ Output power : %d dBm\r\n", my_nrf24l01p.getRfOutputPower() ); |
mvanendert | 3:1f92895432d2 | 58 | pc.printf( "nRF24L01+ Data Rate : %d kbps\r\n", my_nrf24l01p.getAirDataRate() ); |
mvanendert | 3:1f92895432d2 | 59 | pc.printf( "nRF24L01+ TX Address : 0x%010llX\r\n", my_nrf24l01p.getTxAddress() ); |
mvanendert | 3:1f92895432d2 | 60 | pc.printf( "nRF24L01+ RX Address : 0x%010llX\r\n", my_nrf24l01p.getRxAddress(0) ); |
Owen | 0:a51a6e7da590 | 61 | |
Owen | 0:a51a6e7da590 | 62 | pc.printf( "Type keys to test transfers:\r\n (transfers are grouped into %d characters)\r\n", TRANSFER_SIZE ); |
Owen | 0:a51a6e7da590 | 63 | |
Owen | 0:a51a6e7da590 | 64 | my_nrf24l01p.setTransferSize( TRANSFER_SIZE ); |
Owen | 0:a51a6e7da590 | 65 | |
mvanendert | 3:1f92895432d2 | 66 | |
mvanendert | 3:1f92895432d2 | 67 | //Put device in transmitmode |
mvanendert | 3:1f92895432d2 | 68 | my_nrf24l01p.setTransmitMode(); |
mvanendert | 3:1f92895432d2 | 69 | |
Owen | 0:a51a6e7da590 | 70 | my_nrf24l01p.enable(); |
mvanendert | 3:1f92895432d2 | 71 | */ |
mvanendert | 3:1f92895432d2 | 72 | void ReadAvgTemp(void); |
mvanendert | 3:1f92895432d2 | 73 | void ReadAvgLight(void); |
Owen | 0:a51a6e7da590 | 74 | |
Owen | 0:a51a6e7da590 | 75 | while (1) { |
Owen | 0:a51a6e7da590 | 76 | |
mvanendert | 3:1f92895432d2 | 77 | ReadAvgTemp(); |
mvanendert | 3:1f92895432d2 | 78 | ReadAvgLight(); |
mvanendert | 3:1f92895432d2 | 79 | TemperatureC = (AvgTemp * 3.685503686 * 100); |
mvanendert | 3:1f92895432d2 | 80 | TemperatureF = (9.0 * TemperatureC) / 5.0 + 32.0; |
mvanendert | 3:1f92895432d2 | 81 | pc.printf(" Temperature "); |
mvanendert | 3:1f92895432d2 | 82 | pc.printf("%.2f C %.2f F",AvgTemp, AvgTemp); |
mvanendert | 3:1f92895432d2 | 83 | pc.printf(" Lichtintensiteit "); |
mvanendert | 3:1f92895432d2 | 84 | pc.printf("%.2f ", AvgLight); |
mvanendert | 3:1f92895432d2 | 85 | |
mvanendert | 3:1f92895432d2 | 86 | |
mvanendert | 3:1f92895432d2 | 87 | |
mvanendert | 3:1f92895432d2 | 88 | |
mvanendert | 3:1f92895432d2 | 89 | wait(1); |
mvanendert | 3:1f92895432d2 | 90 | /* |
Owen | 0:a51a6e7da590 | 91 | // If we've received anything over the host serial link... |
Owen | 0:a51a6e7da590 | 92 | if ( pc.readable() ) { |
Owen | 0:a51a6e7da590 | 93 | |
Owen | 0:a51a6e7da590 | 94 | // ...add it to the transmit buffer |
Owen | 0:a51a6e7da590 | 95 | txData[txDataCnt++] = pc.getc(); |
ninoderkinderen | 2:178e089b3ce9 | 96 | |
ninoderkinderen | 2:178e089b3ce9 | 97 | //pc.printf("lengte txData: %d \n\r", sizeof(txData)); |
ninoderkinderen | 2:178e089b3ce9 | 98 | //pc.printf("txData count: %d \n\r", txDataCnt); |
Owen | 0:a51a6e7da590 | 99 | |
Owen | 0:a51a6e7da590 | 100 | // If the transmit buffer is full |
ninoderkinderen | 2:178e089b3ce9 | 101 | if ( txDataCnt >= sizeof(txData) ) { |
ninoderkinderen | 2:178e089b3ce9 | 102 | //pc.printf("eerste if \n\r"); |
Owen | 0:a51a6e7da590 | 103 | // Send the transmitbuffer via the nRF24L01+ |
Owen | 0:a51a6e7da590 | 104 | my_nrf24l01p.write( NRF24L01P_PIPE_P0, txData, txDataCnt ); |
Owen | 0:a51a6e7da590 | 105 | |
Owen | 0:a51a6e7da590 | 106 | txDataCnt = 0; |
Owen | 0:a51a6e7da590 | 107 | } |
Owen | 0:a51a6e7da590 | 108 | |
Owen | 0:a51a6e7da590 | 109 | // Toggle LED1 (to help debug Host -> nRF24L01+ communication) |
Owen | 0:a51a6e7da590 | 110 | myled1 = !myled1; |
Owen | 0:a51a6e7da590 | 111 | } |
Owen | 0:a51a6e7da590 | 112 | |
Owen | 0:a51a6e7da590 | 113 | // If we've received anything in the nRF24L01+... |
ninoderkinderen | 2:178e089b3ce9 | 114 | if ( my_nrf24l01p.readable(NRF24L01P_PIPE_P0) ) { |
ninoderkinderen | 2:178e089b3ce9 | 115 | //pc.printf("tweede if \n\r"); |
Owen | 0:a51a6e7da590 | 116 | // ...read the data into the receive buffer |
Owen | 0:a51a6e7da590 | 117 | rxDataCnt = my_nrf24l01p.read( NRF24L01P_PIPE_P0, rxData, sizeof( rxData ) ); |
Owen | 0:a51a6e7da590 | 118 | |
Owen | 0:a51a6e7da590 | 119 | // Display the receive buffer contents via the host serial link |
Owen | 0:a51a6e7da590 | 120 | for ( int i = 0; rxDataCnt > 0; rxDataCnt--, i++ ) { |
Owen | 0:a51a6e7da590 | 121 | |
Owen | 0:a51a6e7da590 | 122 | pc.putc( rxData[i] ); |
Owen | 0:a51a6e7da590 | 123 | } |
Owen | 0:a51a6e7da590 | 124 | |
Owen | 0:a51a6e7da590 | 125 | // Toggle LED2 (to help debug nRF24L01+ -> Host communication) |
Owen | 0:a51a6e7da590 | 126 | myled2 = !myled2; |
mvanendert | 3:1f92895432d2 | 127 | }*/ |
Owen | 0:a51a6e7da590 | 128 | } |
Owen | 0:a51a6e7da590 | 129 | } |
mvanendert | 3:1f92895432d2 | 130 | |
mvanendert | 3:1f92895432d2 | 131 | |
mvanendert | 3:1f92895432d2 | 132 | |
mvanendert | 3:1f92895432d2 | 133 | |
mvanendert | 3:1f92895432d2 | 134 | |
mvanendert | 3:1f92895432d2 | 135 | void ReadAvgTemp() |
mvanendert | 3:1f92895432d2 | 136 | { |
mvanendert | 3:1f92895432d2 | 137 | for(i=0; i<10; i++) |
mvanendert | 3:1f92895432d2 | 138 | { |
mvanendert | 3:1f92895432d2 | 139 | a[i] = LM35.read(); |
mvanendert | 3:1f92895432d2 | 140 | wait(.02); |
mvanendert | 3:1f92895432d2 | 141 | } |
mvanendert | 3:1f92895432d2 | 142 | |
mvanendert | 3:1f92895432d2 | 143 | for(i=0; i<10; i++) |
mvanendert | 3:1f92895432d2 | 144 | { |
mvanendert | 3:1f92895432d2 | 145 | AvgTemp = AvgTemp+(a[i]/10); |
mvanendert | 3:1f92895432d2 | 146 | } |
mvanendert | 3:1f92895432d2 | 147 | } |
mvanendert | 3:1f92895432d2 | 148 | |
mvanendert | 3:1f92895432d2 | 149 | void ReadAvgLight() |
mvanendert | 3:1f92895432d2 | 150 | { |
mvanendert | 3:1f92895432d2 | 151 | for(i=0;i<10;i++) |
mvanendert | 3:1f92895432d2 | 152 | { |
mvanendert | 3:1f92895432d2 | 153 | a[i] = LDR.read(); |
mvanendert | 3:1f92895432d2 | 154 | wait(.02); |
mvanendert | 3:1f92895432d2 | 155 | } |
mvanendert | 3:1f92895432d2 | 156 | |
mvanendert | 3:1f92895432d2 | 157 | for(i=0;i<10;i++) |
mvanendert | 3:1f92895432d2 | 158 | { |
mvanendert | 3:1f92895432d2 | 159 | AvgLight = AvgLight+(a[i]/10); |
mvanendert | 3:1f92895432d2 | 160 | } |
mvanendert | 3:1f92895432d2 | 161 | } |
mvanendert | 3:1f92895432d2 | 162 | |
mvanendert | 3:1f92895432d2 | 163 | |
mvanendert | 3:1f92895432d2 | 164 | |
mvanendert | 3:1f92895432d2 | 165 | |
mvanendert | 3:1f92895432d2 | 166 | |
mvanendert | 3:1f92895432d2 | 167 | /* |
mvanendert | 3:1f92895432d2 | 168 | void SetAdresses () |
mvanendert | 3:1f92895432d2 | 169 | { |
mvanendert | 3:1f92895432d2 | 170 | //Note that Pipes 0 & 1 have 3, 4 or 5 byte addresses, while Pipes 2..5 only use the lowest byte (bits 7..0) of the address provided here, |
mvanendert | 3:1f92895432d2 | 171 | //and use 2, 3 or 4 bytes from Pipe 1's address. The width parameter is ignored for Pipes 2..5. |
mvanendert | 3:1f92895432d2 | 172 | |
mvanendert | 3:1f92895432d2 | 173 | my_nrf24l01p.setRxAddress(0xE7E7E7E7E7, 5, 0); //Set Device void setRxAddress ( unsigned long long address = DEFAULT_NRF24L01P_ADDRESS, |
mvanendert | 3:1f92895432d2 | 174 | // int width = DEFAULT_NRF24L01P_ADDRESS_WIDTH, |
mvanendert | 3:1f92895432d2 | 175 | // int pipe = NRF24L01P_PIPE_P0 |
mvanendert | 3:1f92895432d2 | 176 | // ) |
mvanendert | 3:1f92895432d2 | 177 | my_nrf24l01p.setRxAddress(0xB3B4B5B6F1, 10, 1); //Set Device void setRxAddress ( unsigned long long address = DEFAULT_NRF24L01P_ADDRESS, |
mvanendert | 3:1f92895432d2 | 178 | // int width = DEFAULT_NRF24L01P_ADDRESS_WIDTH, |
mvanendert | 3:1f92895432d2 | 179 | // int pipe = NRF24L01P_PIPE_P0 |
mvanendert | 3:1f92895432d2 | 180 | // ) |
mvanendert | 3:1f92895432d2 | 181 | |
mvanendert | 3:1f92895432d2 | 182 | my_nrf24l01p.setRxAddress(0xB3B4B5B6CD, 10, 2); //Set Device void setRxAddress ( unsigned long long address = DEFAULT_NRF24L01P_ADDRESS, |
mvanendert | 3:1f92895432d2 | 183 | // int width = DEFAULT_NRF24L01P_ADDRESS_WIDTH, |
mvanendert | 3:1f92895432d2 | 184 | // int pipe = NRF24L01P_PIPE_P0 |
mvanendert | 3:1f92895432d2 | 185 | // ) |
mvanendert | 3:1f92895432d2 | 186 | |
mvanendert | 3:1f92895432d2 | 187 | my_nrf24l01p.setRxAddress(0xB3B4B5B6A3, 10, 3); //Set Device void setRxAddress ( unsigned long long address = DEFAULT_NRF24L01P_ADDRESS, |
mvanendert | 3:1f92895432d2 | 188 | // int width = DEFAULT_NRF24L01P_ADDRESS_WIDTH, |
mvanendert | 3:1f92895432d2 | 189 | // int pipe = NRF24L01P_PIPE_P0 |
mvanendert | 3:1f92895432d2 | 190 | // ) |
mvanendert | 3:1f92895432d2 | 191 | |
mvanendert | 3:1f92895432d2 | 192 | my_nrf24l01p.setRxAddress(0xB3B4B5B60F, 10, 4); //Set Device void setRxAddress ( unsigned long long address = DEFAULT_NRF24L01P_ADDRESS, |
mvanendert | 3:1f92895432d2 | 193 | // int width = DEFAULT_NRF24L01P_ADDRESS_WIDTH, |
mvanendert | 3:1f92895432d2 | 194 | // int pipe = NRF24L01P_PIPE_P0 |
mvanendert | 3:1f92895432d2 | 195 | // ) |
mvanendert | 3:1f92895432d2 | 196 | |
mvanendert | 3:1f92895432d2 | 197 | my_nrf24l01p.setRxAddress(0xB3B4B5B605, 10, 5); //Set Device void setRxAddress ( unsigned long long address = DEFAULT_NRF24L01P_ADDRESS, |
mvanendert | 3:1f92895432d2 | 198 | // int width = DEFAULT_NRF24L01P_ADDRESS_WIDTH, |
mvanendert | 3:1f92895432d2 | 199 | // int pipe = NRF24L01P_PIPE_P0 |
mvanendert | 3:1f92895432d2 | 200 | // ) |
mvanendert | 3:1f92895432d2 | 201 | |
mvanendert | 3:1f92895432d2 | 202 | |
mvanendert | 3:1f92895432d2 | 203 | |
mvanendert | 3:1f92895432d2 | 204 | //Note that the address width is shared with the Receive pipes, so a change to that address width affect transmissions. |
mvanendert | 3:1f92895432d2 | 205 | |
mvanendert | 3:1f92895432d2 | 206 | my_nrf24l01p.setTxAddress(0xE7E7E7E7E7,5); //void setTxAddress ( unsigned long long address = DEFAULT_NRF24L01P_ADDRESS, |
mvanendert | 3:1f92895432d2 | 207 | // int width = DEFAULT_NRF24L01P_ADDRESS_WIDTH |
mvanendert | 3:1f92895432d2 | 208 | // ) |
mvanendert | 3:1f92895432d2 | 209 | } |
mvanendert | 3:1f92895432d2 | 210 | */ |