Xbee comms for sensor box

Dependencies:   MMA8451Q mbed nRF24L01P

Fork of Sensor_Box by Sensor CDT

Committer:
jaehughes
Date:
Mon Jun 29 12:29:05 2015 +0000
Revision:
4:7d3a1dfe5454
Parent:
3:5c7f4e5a7605
Child:
5:7c53f5f2af8d
NRF + Accelerometer

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Owen 0:a51a6e7da590 1 #include "mbed.h"
Owen 0:a51a6e7da590 2 #include "nRF24L01P.h"
trisjph 3:5c7f4e5a7605 3 #include "MMA8451Q.h"
jaehughes 4:7d3a1dfe5454 4
trisjph 3:5c7f4e5a7605 5 #define MMA8451_I2C_ADDRESS (0x1d<<1)
jaehughes 4:7d3a1dfe5454 6
Owen 0:a51a6e7da590 7 Serial pc(USBTX, USBRX); // tx, rx
jaehughes 4:7d3a1dfe5454 8
trisjph 3:5c7f4e5a7605 9 PinName const SDA = PTE25;
trisjph 3:5c7f4e5a7605 10 PinName const SCL = PTE24;
jaehughes 4:7d3a1dfe5454 11
jaehughes 4:7d3a1dfe5454 12 nRF24L01P my_nrf24l01p(PTD2, PTD3, PTD1, PTE1, PTE0, PTD0); // mosi, miso, sck, csn, ce, irq
jaehughes 4:7d3a1dfe5454 13
Owen 0:a51a6e7da590 14 DigitalOut myled1(LED1);
Owen 0:a51a6e7da590 15 DigitalOut myled2(LED2);
jaehughes 4:7d3a1dfe5454 16
trisjph 3:5c7f4e5a7605 17 int main()
trisjph 3:5c7f4e5a7605 18 {
jaehughes 4:7d3a1dfe5454 19
trisjph 3:5c7f4e5a7605 20 MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS);
trisjph 3:5c7f4e5a7605 21 //PwmOut rled(LED1);
trisjph 3:5c7f4e5a7605 22 //PwmOut gled(LED2);
trisjph 3:5c7f4e5a7605 23 //PwmOut bled(LED3);
jaehughes 4:7d3a1dfe5454 24
Owen 0:a51a6e7da590 25 // The nRF24L01+ supports transfers from 1 to 32 bytes, but Sparkfun's
Owen 0:a51a6e7da590 26 // "Nordic Serial Interface Board" (http://www.sparkfun.com/products/9019)
Owen 0:a51a6e7da590 27 // only handles 4 byte transfers in the ATMega code.
trisjph 3:5c7f4e5a7605 28 #define TRANSFER_SIZE 24
jaehughes 4:7d3a1dfe5454 29
Owen 0:a51a6e7da590 30 char txData[TRANSFER_SIZE], rxData[TRANSFER_SIZE];
Owen 0:a51a6e7da590 31 int txDataCnt = 0;
Owen 0:a51a6e7da590 32 int rxDataCnt = 0;
jaehughes 4:7d3a1dfe5454 33
Owen 0:a51a6e7da590 34 my_nrf24l01p.powerUp();
jaehughes 4:7d3a1dfe5454 35
Owen 0:a51a6e7da590 36 // Display the (default) setup of the nRF24L01+ chip
jaehughes 4:7d3a1dfe5454 37 //pc.printf( "nRF24L01+ Frequency : %d MHz\r\n", my_nrf24l01p.getRfFrequency() );
jaehughes 4:7d3a1dfe5454 38 //pc.printf( "nRF24L01+ Output power : %d dBm\r\n", my_nrf24l01p.getRfOutputPower() );
jaehughes 4:7d3a1dfe5454 39 //pc.printf( "nRF24L01+ Data Rate : %d kbps\r\n", my_nrf24l01p.getAirDataRate() );
jaehughes 4:7d3a1dfe5454 40 //pc.printf( "nRF24L01+ TX Address : 0x%010llX\r\n", my_nrf24l01p.getTxAddress() );
jaehughes 4:7d3a1dfe5454 41 //pc.printf( "nRF24L01+ RX Address : 0x%010llX\r\n", my_nrf24l01p.getRxAddress() );
jaehughes 4:7d3a1dfe5454 42
jaehughes 4:7d3a1dfe5454 43 //pc.printf( "Type keys to test transfers:\r\n (transfers are grouped into %d characters)\r\n", TRANSFER_SIZE );
jaehughes 4:7d3a1dfe5454 44
Owen 0:a51a6e7da590 45 my_nrf24l01p.setTransferSize( TRANSFER_SIZE );
jaehughes 4:7d3a1dfe5454 46
Owen 0:a51a6e7da590 47 my_nrf24l01p.setReceiveMode();
Owen 0:a51a6e7da590 48 my_nrf24l01p.enable();
jaehughes 4:7d3a1dfe5454 49
jaehughes 4:7d3a1dfe5454 50
jaehughes 4:7d3a1dfe5454 51
trisjph 3:5c7f4e5a7605 52 printf("MMA8451 ID: %d\n", acc.getWhoAmI());
jaehughes 4:7d3a1dfe5454 53
Owen 0:a51a6e7da590 54 while (1) {
jaehughes 4:7d3a1dfe5454 55
trisjph 3:5c7f4e5a7605 56 float x, y, z;
trisjph 3:5c7f4e5a7605 57 x = abs(acc.getAccX());
trisjph 3:5c7f4e5a7605 58 y = abs(acc.getAccY());
trisjph 3:5c7f4e5a7605 59 z = abs(acc.getAccZ());
trisjph 3:5c7f4e5a7605 60 //rled = 1.0f - x;
trisjph 3:5c7f4e5a7605 61 //gled = 1.0f - y;
trisjph 3:5c7f4e5a7605 62 //bled = 1.0f - z;
jaehughes 4:7d3a1dfe5454 63 //wait(0.01);
jaehughes 4:7d3a1dfe5454 64 //txDataCnt = sprintf(txData, "X:%1.3f,Y:%1.3f,Z:%1.3f\n", x,y,z);
jaehughes 4:7d3a1dfe5454 65 txDataCnt = sprintf(txData, " %1.3f %1.3f %1.3f\n", x,y,z);
trisjph 3:5c7f4e5a7605 66 printf("X:%f,Y:%f,Z:%f size %i %s\n", x, y, z,txDataCnt, txData);
jaehughes 4:7d3a1dfe5454 67
Owen 0:a51a6e7da590 68 // If we've received anything over the host serial link...
jaehughes 4:7d3a1dfe5454 69
trisjph 3:5c7f4e5a7605 70 // ...add it to the transmit buffer
trisjph 3:5c7f4e5a7605 71 //txData[txDataCnt++] = pc.getc();
jaehughes 4:7d3a1dfe5454 72
trisjph 3:5c7f4e5a7605 73 // If the transmit buffer is full
jaehughes 4:7d3a1dfe5454 74
trisjph 3:5c7f4e5a7605 75 // Send the transmitbuffer via the nRF24L01+
trisjph 3:5c7f4e5a7605 76 my_nrf24l01p.write( NRF24L01P_PIPE_P0, txData, txDataCnt );
trisjph 3:5c7f4e5a7605 77
trisjph 3:5c7f4e5a7605 78 printf("sent");
jaehughes 4:7d3a1dfe5454 79
trisjph 3:5c7f4e5a7605 80 // Toggle LED1 (to help debug Host -> nRF24L01+ communication)
trisjph 3:5c7f4e5a7605 81 myled1 = !myled1;
Owen 0:a51a6e7da590 82 }
jaehughes 4:7d3a1dfe5454 83 }