Nino Der Kinderen
/
nRF24L01P_ProjectSensor
se
Fork of nRF24L01P_Project by
main.cpp
- Committer:
- mvanendert
- Date:
- 2015-05-13
- Revision:
- 3:1f92895432d2
- Parent:
- 2:178e089b3ce9
- Child:
- 4:25a500ef61fc
File content as of revision 3:1f92895432d2:
#include "mbed.h" #include "nRF24L01P.h" Serial pc(USBTX, USBRX); // tx, rx nRF24L01P my_nrf24l01p(p5, p6, p7, p8, p9, p10); // mosi, miso, sck, csn, ce, irq DigitalOut myled1(LED1); DigitalOut myled2(LED2); AnalogIn LM35(p15); AnalogIn LDR(p16); #define TRANSFER_SIZE 32 char txData[TRANSFER_SIZE], rxData[TRANSFER_SIZE]; int txDataCnt = 0; int rxDataCnt = 0; float AvgTemp = 0; float AvgLight = 0; float TemperatureC = 0; float TemperatureF = 0; float a[10]; int i = 0; int main() { // The nRF24L01+ supports transfers from 1 to 32 bytes, but Sparkfun's // "Nordic Serial Interface Board" (http://www.sparkfun.com/products/9019) // Only handles 4 byte transfers in the ATMega code. /* my_nrf24l01p.powerUp(); //my_nrf24l01p.setRxAddress(0xE7E7E7E7E7,5); my_nrf24l01p.setRfFrequency(2450); my_nrf24l01p.setRfOutputPower(0); //my_nrf24l01p.enableAutoRetransmit(1000, 5); //enableAutoRetransmit ( int delay, int count); // delay the delay between restransmits, in uS (250uS..4000uS) // count number of retransmits before generating an error (1..15) //Enable autoAcknowledge my_nrf24l01p.enableAutoAcknowledge(0); my_nrf24l01p.enableAutoAcknowledge(1); my_nrf24l01p.enableAutoAcknowledge(2); my_nrf24l01p.enableAutoAcknowledge(3); my_nrf24l01p.enableAutoAcknowledge(4); my_nrf24l01p.enableAutoAcknowledge(5); // Display the (default) setup of the nRF24L01+ chip pc.printf( "nRF24L01+ Frequency : %d MHz\r\n", my_nrf24l01p.getRfFrequency() ); pc.printf( "nRF24L01+ Output power : %d dBm\r\n", my_nrf24l01p.getRfOutputPower() ); pc.printf( "nRF24L01+ Data Rate : %d kbps\r\n", my_nrf24l01p.getAirDataRate() ); pc.printf( "nRF24L01+ TX Address : 0x%010llX\r\n", my_nrf24l01p.getTxAddress() ); pc.printf( "nRF24L01+ RX Address : 0x%010llX\r\n", my_nrf24l01p.getRxAddress(0) ); pc.printf( "Type keys to test transfers:\r\n (transfers are grouped into %d characters)\r\n", TRANSFER_SIZE ); my_nrf24l01p.setTransferSize( TRANSFER_SIZE ); //Put device in transmitmode my_nrf24l01p.setTransmitMode(); my_nrf24l01p.enable(); */ void ReadAvgTemp(void); void ReadAvgLight(void); while (1) { ReadAvgTemp(); ReadAvgLight(); TemperatureC = (AvgTemp * 3.685503686 * 100); TemperatureF = (9.0 * TemperatureC) / 5.0 + 32.0; pc.printf(" Temperature "); pc.printf("%.2f C %.2f F",AvgTemp, AvgTemp); pc.printf(" Lichtintensiteit "); pc.printf("%.2f ", AvgLight); wait(1); /* // If we've received anything over the host serial link... if ( pc.readable() ) { // ...add it to the transmit buffer txData[txDataCnt++] = pc.getc(); //pc.printf("lengte txData: %d \n\r", sizeof(txData)); //pc.printf("txData count: %d \n\r", txDataCnt); // If the transmit buffer is full if ( txDataCnt >= sizeof(txData) ) { //pc.printf("eerste if \n\r"); // Send the transmitbuffer via the nRF24L01+ my_nrf24l01p.write( NRF24L01P_PIPE_P0, txData, txDataCnt ); txDataCnt = 0; } // Toggle LED1 (to help debug Host -> nRF24L01+ communication) myled1 = !myled1; } // If we've received anything in the nRF24L01+... if ( my_nrf24l01p.readable(NRF24L01P_PIPE_P0) ) { //pc.printf("tweede if \n\r"); // ...read the data into the receive buffer rxDataCnt = my_nrf24l01p.read( NRF24L01P_PIPE_P0, rxData, sizeof( rxData ) ); // Display the receive buffer contents via the host serial link for ( int i = 0; rxDataCnt > 0; rxDataCnt--, i++ ) { pc.putc( rxData[i] ); } // Toggle LED2 (to help debug nRF24L01+ -> Host communication) myled2 = !myled2; }*/ } } void ReadAvgTemp() { for(i=0; i<10; i++) { a[i] = LM35.read(); wait(.02); } for(i=0; i<10; i++) { AvgTemp = AvgTemp+(a[i]/10); } } void ReadAvgLight() { for(i=0;i<10;i++) { a[i] = LDR.read(); wait(.02); } for(i=0;i<10;i++) { AvgLight = AvgLight+(a[i]/10); } } /* void SetAdresses () { //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, //and use 2, 3 or 4 bytes from Pipe 1's address. The width parameter is ignored for Pipes 2..5. my_nrf24l01p.setRxAddress(0xE7E7E7E7E7, 5, 0); //Set Device void setRxAddress ( unsigned long long address = DEFAULT_NRF24L01P_ADDRESS, // int width = DEFAULT_NRF24L01P_ADDRESS_WIDTH, // int pipe = NRF24L01P_PIPE_P0 // ) my_nrf24l01p.setRxAddress(0xB3B4B5B6F1, 10, 1); //Set Device void setRxAddress ( unsigned long long address = DEFAULT_NRF24L01P_ADDRESS, // int width = DEFAULT_NRF24L01P_ADDRESS_WIDTH, // int pipe = NRF24L01P_PIPE_P0 // ) my_nrf24l01p.setRxAddress(0xB3B4B5B6CD, 10, 2); //Set Device void setRxAddress ( unsigned long long address = DEFAULT_NRF24L01P_ADDRESS, // int width = DEFAULT_NRF24L01P_ADDRESS_WIDTH, // int pipe = NRF24L01P_PIPE_P0 // ) my_nrf24l01p.setRxAddress(0xB3B4B5B6A3, 10, 3); //Set Device void setRxAddress ( unsigned long long address = DEFAULT_NRF24L01P_ADDRESS, // int width = DEFAULT_NRF24L01P_ADDRESS_WIDTH, // int pipe = NRF24L01P_PIPE_P0 // ) my_nrf24l01p.setRxAddress(0xB3B4B5B60F, 10, 4); //Set Device void setRxAddress ( unsigned long long address = DEFAULT_NRF24L01P_ADDRESS, // int width = DEFAULT_NRF24L01P_ADDRESS_WIDTH, // int pipe = NRF24L01P_PIPE_P0 // ) my_nrf24l01p.setRxAddress(0xB3B4B5B605, 10, 5); //Set Device void setRxAddress ( unsigned long long address = DEFAULT_NRF24L01P_ADDRESS, // int width = DEFAULT_NRF24L01P_ADDRESS_WIDTH, // int pipe = NRF24L01P_PIPE_P0 // ) //Note that the address width is shared with the Receive pipes, so a change to that address width affect transmissions. my_nrf24l01p.setTxAddress(0xE7E7E7E7E7,5); //void setTxAddress ( unsigned long long address = DEFAULT_NRF24L01P_ADDRESS, // int width = DEFAULT_NRF24L01P_ADDRESS_WIDTH // ) } */