The Smart Home Mobileboard project demonstrates successful communication using the integrated RF module header on the FRDM-K64F board as well as the serial port for UART communication to the serial terminal. The Smart Home project ultimately is based on creating an automated home controlled either by the mobile board (i.e. wearable device) or the Smart Home Bluetooth App designed in MIT App Inventor 2. This project was completed for ECE533 class within the IUPUI Purdue School of Engineering.
Dependencies: FXOS8700Q mbed nRF24L01P
main.cpp@0:b709611436a1, 2016-12-16 (annotated)
- Committer:
- haircules
- Date:
- Fri Dec 16 20:37:06 2016 +0000
- Revision:
- 0:b709611436a1
This version of the Smart Home Mobile Board allows the user to send accelerometer data to the Base Board via RF Module when button is pressed.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
haircules | 0:b709611436a1 | 1 | /* FXOS8700Q Example Program |
haircules | 0:b709611436a1 | 2 | * Copyright (c) 2014-2015 ARM Limited |
haircules | 0:b709611436a1 | 3 | * |
haircules | 0:b709611436a1 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
haircules | 0:b709611436a1 | 5 | * you may not use this file except in compliance with the License. |
haircules | 0:b709611436a1 | 6 | * You may obtain a copy of the License at |
haircules | 0:b709611436a1 | 7 | * |
haircules | 0:b709611436a1 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
haircules | 0:b709611436a1 | 9 | * |
haircules | 0:b709611436a1 | 10 | * Unless required by applicable law or agreed to in writing, software |
haircules | 0:b709611436a1 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
haircules | 0:b709611436a1 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
haircules | 0:b709611436a1 | 13 | * See the License for the specific language governing permissions and |
haircules | 0:b709611436a1 | 14 | * limitations under the License. |
haircules | 0:b709611436a1 | 15 | */ |
haircules | 0:b709611436a1 | 16 | |
haircules | 0:b709611436a1 | 17 | #include "mbed.h" |
haircules | 0:b709611436a1 | 18 | #include "FXOS8700Q.h" |
haircules | 0:b709611436a1 | 19 | #include "nRF24L01P.h" |
haircules | 0:b709611436a1 | 20 | #include <string.h> |
haircules | 0:b709611436a1 | 21 | #include <stdlib.h> |
haircules | 0:b709611436a1 | 22 | #include <stdio.h> |
haircules | 0:b709611436a1 | 23 | |
haircules | 0:b709611436a1 | 24 | /*Pin Declarations*/ |
haircules | 0:b709611436a1 | 25 | Serial pc(USBTX, USBRX); |
haircules | 0:b709611436a1 | 26 | I2C i2c(PTE25, PTE24); |
haircules | 0:b709611436a1 | 27 | FXOS8700QAccelerometer acc(i2c, FXOS8700CQ_SLAVE_ADDR1); // Configured for the FRDM-K64F with onboard sensors |
haircules | 0:b709611436a1 | 28 | DigitalIn sw3(SW3); //Enable button for sending data |
haircules | 0:b709611436a1 | 29 | |
haircules | 0:b709611436a1 | 30 | /*RF Module Instantiations*/ |
haircules | 0:b709611436a1 | 31 | nRF24L01P my_nrf24l01p (PTD6,PTD7,PTD5,PTD4,PTB20,PTC18); // mosi, miso, sck, csn, ce, irq |
haircules | 0:b709611436a1 | 32 | |
haircules | 0:b709611436a1 | 33 | DigitalOut myled1(LED1); |
haircules | 0:b709611436a1 | 34 | DigitalOut myled2(LED2); |
haircules | 0:b709611436a1 | 35 | |
haircules | 0:b709611436a1 | 36 | /*Function Prototypes*/ |
haircules | 0:b709611436a1 | 37 | void packetize(int16_t raX, int16_t raY, int16_t raZ, char* buffer); |
haircules | 0:b709611436a1 | 38 | |
haircules | 0:b709611436a1 | 39 | int main(void) |
haircules | 0:b709611436a1 | 40 | { |
haircules | 0:b709611436a1 | 41 | |
haircules | 0:b709611436a1 | 42 | #define TRANSFER_SIZE 15 |
haircules | 0:b709611436a1 | 43 | |
haircules | 0:b709611436a1 | 44 | int16_t raX, raY, raZ; |
haircules | 0:b709611436a1 | 45 | |
haircules | 0:b709611436a1 | 46 | char buffer[TRANSFER_SIZE];/*first char is sign, the rest are ints*/ |
haircules | 0:b709611436a1 | 47 | |
haircules | 0:b709611436a1 | 48 | my_nrf24l01p.powerUp(); |
haircules | 0:b709611436a1 | 49 | |
haircules | 0:b709611436a1 | 50 | // Display the (default) setup of the nRF24L01+ chip |
haircules | 0:b709611436a1 | 51 | pc.printf( "nRF24L01+ Frequency : %d MHz\r\n", my_nrf24l01p.getRfFrequency() ); |
haircules | 0:b709611436a1 | 52 | pc.printf( "nRF24L01+ Output power : %d dBm\r\n", my_nrf24l01p.getRfOutputPower() ); |
haircules | 0:b709611436a1 | 53 | pc.printf( "nRF24L01+ Data Rate : %d kbps\r\n", my_nrf24l01p.getAirDataRate() ); |
haircules | 0:b709611436a1 | 54 | pc.printf( "nRF24L01+ TX Address : 0x%010llX\r\n", my_nrf24l01p.getTxAddress() ); |
haircules | 0:b709611436a1 | 55 | pc.printf( "nRF24L01+ RX Address : 0x%010llX\r\n", my_nrf24l01p.getRxAddress() ); |
haircules | 0:b709611436a1 | 56 | |
haircules | 0:b709611436a1 | 57 | /*setting intializations and enable of RF module*/ |
haircules | 0:b709611436a1 | 58 | my_nrf24l01p.setTransferSize( TRANSFER_SIZE ); |
haircules | 0:b709611436a1 | 59 | my_nrf24l01p.setReceiveMode(); |
haircules | 0:b709611436a1 | 60 | my_nrf24l01p.enable(); |
haircules | 0:b709611436a1 | 61 | |
haircules | 0:b709611436a1 | 62 | /*enable accelerometer*/ |
haircules | 0:b709611436a1 | 63 | acc.enable(); |
haircules | 0:b709611436a1 | 64 | |
haircules | 0:b709611436a1 | 65 | |
haircules | 0:b709611436a1 | 66 | while (true) { |
haircules | 0:b709611436a1 | 67 | |
haircules | 0:b709611436a1 | 68 | /* get accelerometer data*/ |
haircules | 0:b709611436a1 | 69 | acc.getX(raX); |
haircules | 0:b709611436a1 | 70 | acc.getY(raY); |
haircules | 0:b709611436a1 | 71 | acc.getZ(raZ); |
haircules | 0:b709611436a1 | 72 | |
haircules | 0:b709611436a1 | 73 | /*print data to terminal*/ |
haircules | 0:b709611436a1 | 74 | printf("ACC: X=%d Y=%d Z=%d \r\n", raX, raY, raZ); |
haircules | 0:b709611436a1 | 75 | |
haircules | 0:b709611436a1 | 76 | /*packetize data to be sent*/ |
haircules | 0:b709611436a1 | 77 | packetize(raX,raY,raZ,buffer); |
haircules | 0:b709611436a1 | 78 | |
haircules | 0:b709611436a1 | 79 | |
haircules | 0:b709611436a1 | 80 | if(!sw3){/*only send data via RF module when sw2 button is held*/ |
haircules | 0:b709611436a1 | 81 | |
haircules | 0:b709611436a1 | 82 | /* Send the transmit buffer via the RF module*/ |
haircules | 0:b709611436a1 | 83 | my_nrf24l01p.write( NRF24L01P_PIPE_P0, buffer, TRANSFER_SIZE ); |
haircules | 0:b709611436a1 | 84 | |
haircules | 0:b709611436a1 | 85 | pc.printf( "\n\rData has been sent!\n\r"); |
haircules | 0:b709611436a1 | 86 | |
haircules | 0:b709611436a1 | 87 | }/*end if*/ |
haircules | 0:b709611436a1 | 88 | |
haircules | 0:b709611436a1 | 89 | // Toggle LED1 (to help debug Host -> nRF24L01+ communication) |
haircules | 0:b709611436a1 | 90 | myled1 = !myled1; |
haircules | 0:b709611436a1 | 91 | |
haircules | 0:b709611436a1 | 92 | wait(1); |
haircules | 0:b709611436a1 | 93 | |
haircules | 0:b709611436a1 | 94 | }/*end while*/ |
haircules | 0:b709611436a1 | 95 | }/*end main*/ |
haircules | 0:b709611436a1 | 96 | |
haircules | 0:b709611436a1 | 97 | void packetize(int16_t raX, int16_t raY, int16_t raZ, char* buffer){ |
haircules | 0:b709611436a1 | 98 | /*Packetize is called upon when a char buffer should be loaded to send |
haircules | 0:b709611436a1 | 99 | data via RF module. In this particular instance the char buffer "buffer" |
haircules | 0:b709611436a1 | 100 | holds three signed - four digit numbers, therefore the sign of each of the |
haircules | 0:b709611436a1 | 101 | three numbers are determined first and loaded to it's corresponding index |
haircules | 0:b709611436a1 | 102 | in buffer. Then, the Thousands, Hundreds, Tens and Ones digits are converted |
haircules | 0:b709611436a1 | 103 | to ASCII chars by adding the hex ASCII value of 0 to the corresponding digit.*/ |
haircules | 0:b709611436a1 | 104 | |
haircules | 0:b709611436a1 | 105 | |
haircules | 0:b709611436a1 | 106 | /*determine sign char for raX,raY,raZ*/ |
haircules | 0:b709611436a1 | 107 | if(raX >= 0){ |
haircules | 0:b709611436a1 | 108 | |
haircules | 0:b709611436a1 | 109 | buffer[0] = ' '; |
haircules | 0:b709611436a1 | 110 | } |
haircules | 0:b709611436a1 | 111 | else{/*raX is negative*/ |
haircules | 0:b709611436a1 | 112 | |
haircules | 0:b709611436a1 | 113 | buffer[0] = '-'; |
haircules | 0:b709611436a1 | 114 | |
haircules | 0:b709611436a1 | 115 | raX = raX * (-1);/*get the absolute value of raX*/ |
haircules | 0:b709611436a1 | 116 | |
haircules | 0:b709611436a1 | 117 | } |
haircules | 0:b709611436a1 | 118 | if(raY >= 0){ /*determine sign char*/ |
haircules | 0:b709611436a1 | 119 | |
haircules | 0:b709611436a1 | 120 | buffer[5] = ' '; |
haircules | 0:b709611436a1 | 121 | } |
haircules | 0:b709611436a1 | 122 | else{/*raY is negative*/ |
haircules | 0:b709611436a1 | 123 | |
haircules | 0:b709611436a1 | 124 | buffer[5] = '-'; |
haircules | 0:b709611436a1 | 125 | |
haircules | 0:b709611436a1 | 126 | raY = raY * (-1);/*get the absolute value of raY*/ |
haircules | 0:b709611436a1 | 127 | |
haircules | 0:b709611436a1 | 128 | } |
haircules | 0:b709611436a1 | 129 | if(raZ >= 0){ /*determine sign char*/ |
haircules | 0:b709611436a1 | 130 | |
haircules | 0:b709611436a1 | 131 | buffer[10] = ' '; |
haircules | 0:b709611436a1 | 132 | } |
haircules | 0:b709611436a1 | 133 | else{/*raZ is negative*/ |
haircules | 0:b709611436a1 | 134 | |
haircules | 0:b709611436a1 | 135 | buffer[10] = '-'; |
haircules | 0:b709611436a1 | 136 | |
haircules | 0:b709611436a1 | 137 | raZ = raZ * (-1);/*get the absolute value of raZ*/ |
haircules | 0:b709611436a1 | 138 | |
haircules | 0:b709611436a1 | 139 | } |
haircules | 0:b709611436a1 | 140 | |
haircules | 0:b709611436a1 | 141 | /*int to string for raX,raY,raZ*/ |
haircules | 0:b709611436a1 | 142 | /*raX*/ |
haircules | 0:b709611436a1 | 143 | buffer[1] = (raX/1000)+'0'; /*thousands char*/ |
haircules | 0:b709611436a1 | 144 | raX = raX % 1000; |
haircules | 0:b709611436a1 | 145 | buffer[2] = (raX/100) + '0';/*hundreds char*/ |
haircules | 0:b709611436a1 | 146 | raX = raX % 100; |
haircules | 0:b709611436a1 | 147 | buffer[3] = (raX/10) + '0'; /*tens char*/ |
haircules | 0:b709611436a1 | 148 | raX = raX % 10; |
haircules | 0:b709611436a1 | 149 | buffer[4] = raX + '0'; /*ones char*/ |
haircules | 0:b709611436a1 | 150 | |
haircules | 0:b709611436a1 | 151 | /*raY*/ |
haircules | 0:b709611436a1 | 152 | buffer[6] = (raY/1000)+'0'; /*thousands char*/ |
haircules | 0:b709611436a1 | 153 | raY = raY % 1000; |
haircules | 0:b709611436a1 | 154 | buffer[7] = (raY/100) + '0';/*hundreds char*/ |
haircules | 0:b709611436a1 | 155 | raY = raY % 100; |
haircules | 0:b709611436a1 | 156 | buffer[8] = (raY/10) + '0'; /*tens char*/ |
haircules | 0:b709611436a1 | 157 | raY = raY % 10; |
haircules | 0:b709611436a1 | 158 | buffer[9] = raY + '0'; /*ones char*/ |
haircules | 0:b709611436a1 | 159 | |
haircules | 0:b709611436a1 | 160 | /*raZ*/ |
haircules | 0:b709611436a1 | 161 | buffer[11] = (raZ/1000)+'0'; /*thousands char*/ |
haircules | 0:b709611436a1 | 162 | raZ = raZ % 1000; |
haircules | 0:b709611436a1 | 163 | buffer[12] = (raZ/100) + '0';/*hundreds char*/ |
haircules | 0:b709611436a1 | 164 | raZ = raZ % 100; |
haircules | 0:b709611436a1 | 165 | buffer[13] = (raZ/10) + '0'; /*tens char*/ |
haircules | 0:b709611436a1 | 166 | raZ = raZ % 10; |
haircules | 0:b709611436a1 | 167 | buffer[14] = raZ + '0'; /*ones char*/ |
haircules | 0:b709611436a1 | 168 | |
haircules | 0:b709611436a1 | 169 | }/*end packetize()*/ |