Xbee comms for sensor box

Dependencies:   MMA8451Q mbed nRF24L01P

Fork of Sensor_Box by Sensor CDT

Committer:
jaehughes
Date:
Wed Jul 01 21:16:03 2015 +0000
Revision:
5:7c53f5f2af8d
Parent:
4:7d3a1dfe5454
Child:
6:fb76109304df
analog read and store in vector

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 5:7c53f5f2af8d 4 #include <vector>
jaehughes 5:7c53f5f2af8d 5 #include <math.h>
jaehughes 5:7c53f5f2af8d 6
trisjph 3:5c7f4e5a7605 7 #define MMA8451_I2C_ADDRESS (0x1d<<1)
jaehughes 5:7c53f5f2af8d 8 #define TRANSFER_SIZE 24
jaehughes 5:7c53f5f2af8d 9
Owen 0:a51a6e7da590 10 Serial pc(USBTX, USBRX); // tx, rx
jaehughes 5:7c53f5f2af8d 11
trisjph 3:5c7f4e5a7605 12 PinName const SDA = PTE25;
trisjph 3:5c7f4e5a7605 13 PinName const SCL = PTE24;
jaehughes 5:7c53f5f2af8d 14
jaehughes 5:7c53f5f2af8d 15 AnalogIn light_ain(A0);
jaehughes 5:7c53f5f2af8d 16 AnalogIn temp_ain(A1);
jaehughes 5:7c53f5f2af8d 17 AnalogIn pir_ain(A3);
jaehughes 5:7c53f5f2af8d 18
jaehughes 4:7d3a1dfe5454 19 nRF24L01P my_nrf24l01p(PTD2, PTD3, PTD1, PTE1, PTE0, PTD0); // mosi, miso, sck, csn, ce, irq
jaehughes 5:7c53f5f2af8d 20
Owen 0:a51a6e7da590 21 DigitalOut myled1(LED1);
Owen 0:a51a6e7da590 22 DigitalOut myled2(LED2);
jaehughes 5:7c53f5f2af8d 23
jaehughes 5:7c53f5f2af8d 24 uint16_t light;
jaehughes 5:7c53f5f2af8d 25 uint16_t temp;
jaehughes 5:7c53f5f2af8d 26 uint16_t pir;
jaehughes 5:7c53f5f2af8d 27
jaehughes 5:7c53f5f2af8d 28 std::vector<uint16_t> light_data;
jaehughes 5:7c53f5f2af8d 29 std::vector<uint16_t> temp_data;
jaehughes 5:7c53f5f2af8d 30 std::vector<uint16_t> pir_data;
jaehughes 5:7c53f5f2af8d 31
jaehughes 5:7c53f5f2af8d 32 void getdata()
jaehughes 5:7c53f5f2af8d 33 {
jaehughes 5:7c53f5f2af8d 34 light = light_ain.read_u16();
jaehughes 5:7c53f5f2af8d 35 temp = temp_ain.read_u16();
jaehughes 5:7c53f5f2af8d 36 pir = pir_ain.read_u16();
jaehughes 5:7c53f5f2af8d 37
jaehughes 5:7c53f5f2af8d 38 light_data.push_back (light);
jaehughes 5:7c53f5f2af8d 39 temp_data.push_back (temp);
jaehughes 5:7c53f5f2af8d 40 pir_data.push_back (pir);
jaehughes 5:7c53f5f2af8d 41
jaehughes 5:7c53f5f2af8d 42 light_data.pop_back();
jaehughes 5:7c53f5f2af8d 43 temp_data.pop_back();
jaehughes 5:7c53f5f2af8d 44 pir_data.pop_back();
jaehughes 5:7c53f5f2af8d 45
jaehughes 5:7c53f5f2af8d 46
jaehughes 5:7c53f5f2af8d 47 printf("%f\n", light_data);
jaehughes 5:7c53f5f2af8d 48
jaehughes 5:7c53f5f2af8d 49 }
jaehughes 5:7c53f5f2af8d 50
jaehughes 5:7c53f5f2af8d 51
trisjph 3:5c7f4e5a7605 52 int main()
trisjph 3:5c7f4e5a7605 53 {
jaehughes 5:7c53f5f2af8d 54
trisjph 3:5c7f4e5a7605 55 MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS);
jaehughes 5:7c53f5f2af8d 56
jaehughes 5:7c53f5f2af8d 57 light_data.assign (20,0);
jaehughes 5:7c53f5f2af8d 58 temp_data.assign (20,0);
jaehughes 5:7c53f5f2af8d 59 pir_data.assign (20,0);
jaehughes 5:7c53f5f2af8d 60
jaehughes 5:7c53f5f2af8d 61
Owen 0:a51a6e7da590 62 char txData[TRANSFER_SIZE], rxData[TRANSFER_SIZE];
Owen 0:a51a6e7da590 63 int txDataCnt = 0;
Owen 0:a51a6e7da590 64 int rxDataCnt = 0;
jaehughes 5:7c53f5f2af8d 65
Owen 0:a51a6e7da590 66 my_nrf24l01p.powerUp();
jaehughes 5:7c53f5f2af8d 67
Owen 0:a51a6e7da590 68 // Display the (default) setup of the nRF24L01+ chip
jaehughes 4:7d3a1dfe5454 69 //pc.printf( "nRF24L01+ Frequency : %d MHz\r\n", my_nrf24l01p.getRfFrequency() );
jaehughes 4:7d3a1dfe5454 70 //pc.printf( "nRF24L01+ Output power : %d dBm\r\n", my_nrf24l01p.getRfOutputPower() );
jaehughes 4:7d3a1dfe5454 71 //pc.printf( "nRF24L01+ Data Rate : %d kbps\r\n", my_nrf24l01p.getAirDataRate() );
jaehughes 4:7d3a1dfe5454 72 //pc.printf( "nRF24L01+ TX Address : 0x%010llX\r\n", my_nrf24l01p.getTxAddress() );
jaehughes 4:7d3a1dfe5454 73 //pc.printf( "nRF24L01+ RX Address : 0x%010llX\r\n", my_nrf24l01p.getRxAddress() );
jaehughes 5:7c53f5f2af8d 74
jaehughes 4:7d3a1dfe5454 75 //pc.printf( "Type keys to test transfers:\r\n (transfers are grouped into %d characters)\r\n", TRANSFER_SIZE );
jaehughes 5:7c53f5f2af8d 76
Owen 0:a51a6e7da590 77 my_nrf24l01p.setTransferSize( TRANSFER_SIZE );
jaehughes 5:7c53f5f2af8d 78
Owen 0:a51a6e7da590 79 my_nrf24l01p.setReceiveMode();
Owen 0:a51a6e7da590 80 my_nrf24l01p.enable();
jaehughes 5:7c53f5f2af8d 81
trisjph 3:5c7f4e5a7605 82 printf("MMA8451 ID: %d\n", acc.getWhoAmI());
jaehughes 5:7c53f5f2af8d 83
Owen 0:a51a6e7da590 84 while (1) {
jaehughes 5:7c53f5f2af8d 85 getdata();
jaehughes 5:7c53f5f2af8d 86
jaehughes 5:7c53f5f2af8d 87 // txDataCnt = sprintf(txData, " %1.3f %1.3f %1.3f\n", x,y,z);
jaehughes 5:7c53f5f2af8d 88 //my_nrf24l01p.write( NRF24L01P_PIPE_P0, txData, txDataCnt );
trisjph 3:5c7f4e5a7605 89 myled1 = !myled1;
jaehughes 5:7c53f5f2af8d 90 wait(0.5);
Owen 0:a51a6e7da590 91 }
jaehughes 5:7c53f5f2af8d 92
jaehughes 5:7c53f5f2af8d 93 }
jaehughes 5:7c53f5f2af8d 94
jaehughes 5:7c53f5f2af8d 95