The Smart Home Baseboard project demonstrates successful communication using the integrated Bluetooth and RF module headers on the FRDM-K64F board as well as the serial port for UART communication to the serial terminal and Ethernet connectivity to push/pull data from the cloud. 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:   EthernetInterface M2XStreamClient jsonlite mbed-rtos mbed nRF24L01P

Fork of nRF24L01P_Hello_World by Owen Edwards

Committer:
haircules
Date:
Fri Dec 16 20:16:59 2016 +0000
Revision:
2:718f6b5cf75c
Parent:
1:5be2682710c6
This version of the Smart Home Baseboard is a basic test of each of the the following interfaces: UART (for serial terminal), Bluetooth Module, RF Module, and Ethernet.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Owen 0:a51a6e7da590 1 #include "mbed.h"
haircules 2:718f6b5cf75c 2 #include "nRF24L01P.h" /*RF Module*/
haircules 2:718f6b5cf75c 3 #include "M2XStreamClient.h" /*AT&T M2X*/
haircules 2:718f6b5cf75c 4 #include "EthernetInterface.h" /*Ethernet*/
haircules 2:718f6b5cf75c 5 #include <stdlib.h>
Owen 0:a51a6e7da590 6
haircules 2:718f6b5cf75c 7 /*************** Base Board Code **********************/
haircules 2:718f6b5cf75c 8 /* -- All revisions made by Nigel Leitzell for ECE533 final project -- */
haircules 2:718f6b5cf75c 9 /* This code allows for communication between the Mobile Board and Bluetooth App */
haircules 2:718f6b5cf75c 10 /* to the Base Board for Smart Home Automoation. Values logged from sensors will */
haircules 2:718f6b5cf75c 11 /* be collected and sent to the cloud via Ethernet from the Base Board. The Base */
haircules 2:718f6b5cf75c 12 /* Board should allow the user to control aspects of their home (i.e. lighting- */
haircules 2:718f6b5cf75c 13 /* and air temperature). However, only the Mobile Board or Bluetooth App may be- */
haircules 2:718f6b5cf75c 14 /* used at any one time. Selection of board which assumes control of the Base- */
haircules 2:718f6b5cf75c 15 /* Board is determined using buttons on the Base Board or the Bluetooth App. */
haircules 2:718f6b5cf75c 16 /*********************************************************************************/
Owen 0:a51a6e7da590 17
haircules 2:718f6b5cf75c 18 /*****************************Pin Declarations************************************/
haircules 2:718f6b5cf75c 19 Serial pc(USBTX, USBRX);// tx, rx
haircules 2:718f6b5cf75c 20 nRF24L01P my_nrf24l01p (PTD6,PTD7,PTD5,PTD4,PTB20,PTC18);// mosi, miso, sck, csn, ce, irq
Owen 0:a51a6e7da590 21 DigitalOut myled1(LED1);
Owen 0:a51a6e7da590 22 DigitalOut myled2(LED2);
haircules 2:718f6b5cf75c 23 /*********************************************************************************/
haircules 2:718f6b5cf75c 24
haircules 2:718f6b5cf75c 25 /**************************Function Prototypes************************************/
haircules 2:718f6b5cf75c 26 void unpack(int &x,int &y,int &z,char *b); /*unpacketize the buffer*/
haircules 2:718f6b5cf75c 27 void initEthernet(EthernetInterface eth); /*Initialize Ethernet*/
haircules 2:718f6b5cf75c 28 /*********************************************************************************/
haircules 2:718f6b5cf75c 29
haircules 2:718f6b5cf75c 30 /*******************************Globals*******************************************/
haircules 2:718f6b5cf75c 31 /*************** Set Sensor Stream details (AT&T M2X stuff) **********************/
haircules 2:718f6b5cf75c 32 char deviceId[] = "81c5ecb0ac94aad592ad558ead689321"; // Device you want to push to
haircules 2:718f6b5cf75c 33 char streamX[] = "acc_x"; // Stream you want to push to
haircules 2:718f6b5cf75c 34 char streamY[] ="acc_y"; // Stream you want to push to
haircules 2:718f6b5cf75c 35 char streamZ[] ="acc_z"; // Stream you want to push to
haircules 2:718f6b5cf75c 36 char m2xKey[] = "b2c3795c7022a811a3c4de49095b26ec"; // Your M2X API Key or Master API Key
haircules 2:718f6b5cf75c 37 /*********************************************************************************/
Owen 0:a51a6e7da590 38
Owen 0:a51a6e7da590 39 int main() {
Owen 0:a51a6e7da590 40
Owen 0:a51a6e7da590 41 // The nRF24L01+ supports transfers from 1 to 32 bytes, but Sparkfun's
Owen 0:a51a6e7da590 42 // "Nordic Serial Interface Board" (http://www.sparkfun.com/products/9019)
Owen 0:a51a6e7da590 43 // only handles 4 byte transfers in the ATMega code.
haircules 2:718f6b5cf75c 44 #define TRANSFER_SIZE 15
Owen 0:a51a6e7da590 45
Owen 0:a51a6e7da590 46 char txData[TRANSFER_SIZE], rxData[TRANSFER_SIZE];
Owen 0:a51a6e7da590 47 int txDataCnt = 0;
Owen 0:a51a6e7da590 48 int rxDataCnt = 0;
haircules 2:718f6b5cf75c 49 int x,y,z;
haircules 2:718f6b5cf75c 50
Owen 0:a51a6e7da590 51 my_nrf24l01p.powerUp();
Owen 0:a51a6e7da590 52
haircules 2:718f6b5cf75c 53 //my_nrf24l01p.setRxAddress(00001,5,NRF24L01P_PIPE_P0);
haircules 2:718f6b5cf75c 54 //my_nrf24l01p.setTxAddress(00001,5);
haircules 2:718f6b5cf75c 55
Owen 0:a51a6e7da590 56 // Display the (default) setup of the nRF24L01+ chip
Owen 0:a51a6e7da590 57 pc.printf( "nRF24L01+ Frequency : %d MHz\r\n", my_nrf24l01p.getRfFrequency() );
Owen 0:a51a6e7da590 58 pc.printf( "nRF24L01+ Output power : %d dBm\r\n", my_nrf24l01p.getRfOutputPower() );
Owen 0:a51a6e7da590 59 pc.printf( "nRF24L01+ Data Rate : %d kbps\r\n", my_nrf24l01p.getAirDataRate() );
Owen 0:a51a6e7da590 60 pc.printf( "nRF24L01+ TX Address : 0x%010llX\r\n", my_nrf24l01p.getTxAddress() );
Owen 0:a51a6e7da590 61 pc.printf( "nRF24L01+ RX Address : 0x%010llX\r\n", my_nrf24l01p.getRxAddress() );
Owen 0:a51a6e7da590 62
haircules 2:718f6b5cf75c 63 pc.printf( "Type keys to test transfers:\r\n (transfers are grouped into %d characters)\r\n\n\n", TRANSFER_SIZE );
Owen 0:a51a6e7da590 64
Owen 0:a51a6e7da590 65 my_nrf24l01p.setTransferSize( TRANSFER_SIZE );
Owen 0:a51a6e7da590 66
Owen 0:a51a6e7da590 67 my_nrf24l01p.setReceiveMode();
Owen 0:a51a6e7da590 68 my_nrf24l01p.enable();
haircules 2:718f6b5cf75c 69
haircules 2:718f6b5cf75c 70 /* Intialize Ethernet connection*/
haircules 2:718f6b5cf75c 71 EthernetInterface eth;
haircules 2:718f6b5cf75c 72 initEthernet(eth);
haircules 2:718f6b5cf75c 73
haircules 2:718f6b5cf75c 74 /* Initialize the M2X client */
haircules 2:718f6b5cf75c 75 Client client;
haircules 2:718f6b5cf75c 76 M2XStreamClient m2xClient(&client, m2xKey);
haircules 2:718f6b5cf75c 77
Owen 0:a51a6e7da590 78 while (1) {
Owen 0:a51a6e7da590 79
Owen 0:a51a6e7da590 80 // If we've received anything over the host serial link...
Owen 0:a51a6e7da590 81 if ( pc.readable() ) {
Owen 0:a51a6e7da590 82
Owen 0:a51a6e7da590 83 // ...add it to the transmit buffer
Owen 0:a51a6e7da590 84 txData[txDataCnt++] = pc.getc();
Owen 0:a51a6e7da590 85
haircules 2:718f6b5cf75c 86 pc.printf( "Key press recognized as key count %d\n\r", txDataCnt);
haircules 2:718f6b5cf75c 87
Owen 0:a51a6e7da590 88 // If the transmit buffer is full
Owen 0:a51a6e7da590 89 if ( txDataCnt >= sizeof( txData ) ) {
Owen 0:a51a6e7da590 90
haircules 2:718f6b5cf75c 91 pc.printf( "Final key press before TX has been recorded, count is %d..\n\n\r",txDataCnt);
haircules 2:718f6b5cf75c 92 pc.printf( "Buffer is now full, preparing to send data..\n\r");
haircules 2:718f6b5cf75c 93 pc.printf( "Contents of txData buffer: ");
haircules 2:718f6b5cf75c 94
haircules 2:718f6b5cf75c 95 /*print the contents of buffer*/
haircules 2:718f6b5cf75c 96 for(int i=0; i<txDataCnt; i++){
haircules 2:718f6b5cf75c 97
haircules 2:718f6b5cf75c 98 pc.printf("%c",txData[i]);
haircules 2:718f6b5cf75c 99
haircules 2:718f6b5cf75c 100 }/*end for*/
haircules 2:718f6b5cf75c 101
haircules 2:718f6b5cf75c 102
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;
haircules 2:718f6b5cf75c 107
haircules 2:718f6b5cf75c 108 pc.printf( "\n\rData has been sent!\n\r");
haircules 2:718f6b5cf75c 109 }/*end if*/
Owen 0:a51a6e7da590 110
Owen 0:a51a6e7da590 111 // Toggle LED1 (to help debug Host -> nRF24L01+ communication)
Owen 0:a51a6e7da590 112 myled1 = !myled1;
haircules 2:718f6b5cf75c 113
haircules 2:718f6b5cf75c 114 }/*end if*/
Owen 0:a51a6e7da590 115
Owen 0:a51a6e7da590 116 // If we've received anything in the nRF24L01+...
Owen 0:a51a6e7da590 117 if ( my_nrf24l01p.readable() ) {
Owen 0:a51a6e7da590 118
Owen 0:a51a6e7da590 119 // ...read the data into the receive buffer
haircules 2:718f6b5cf75c 120 rxDataCnt = my_nrf24l01p.read( NRF24L01P_PIPE_P0, rxData, TRANSFER_SIZE );
Owen 0:a51a6e7da590 121
haircules 2:718f6b5cf75c 122 unpack(x,y,z,rxData);//unpack data
haircules 2:718f6b5cf75c 123
haircules 2:718f6b5cf75c 124 pc.printf("Integer Representation: X-Axis Acc: %d \t",x);
haircules 2:718f6b5cf75c 125 pc.printf("Y-Axis Acc: %d\t",y);
haircules 2:718f6b5cf75c 126 pc.printf("Z-Axis Acc: %d\n\r",z);
haircules 2:718f6b5cf75c 127
haircules 2:718f6b5cf75c 128
haircules 2:718f6b5cf75c 129 //Now that data is unpacked.. What to do?
haircules 2:718f6b5cf75c 130 if(x >= 2000){
haircules 2:718f6b5cf75c 131
haircules 2:718f6b5cf75c 132 pc.printf("Woah, leaning right!\n\r");
haircules 2:718f6b5cf75c 133 }
haircules 2:718f6b5cf75c 134 else if(x <= -2000){
haircules 2:718f6b5cf75c 135
haircules 2:718f6b5cf75c 136 pc.printf("Woah, leaning left!\n\r");
haircules 2:718f6b5cf75c 137
haircules 2:718f6b5cf75c 138 }
haircules 2:718f6b5cf75c 139 else{
haircules 2:718f6b5cf75c 140
haircules 2:718f6b5cf75c 141 pc.printf("Just right.\n\r");
haircules 2:718f6b5cf75c 142
haircules 2:718f6b5cf75c 143 }
haircules 2:718f6b5cf75c 144
haircules 2:718f6b5cf75c 145 //Send data to M2X
haircules 2:718f6b5cf75c 146 m2xClient.updateStreamValue(deviceId, streamX, x);
haircules 2:718f6b5cf75c 147 m2xClient.updateStreamValue(deviceId, streamY, y);
haircules 2:718f6b5cf75c 148 m2xClient.updateStreamValue(deviceId, streamZ, z);
haircules 2:718f6b5cf75c 149
Owen 0:a51a6e7da590 150 // Display the receive buffer contents via the host serial link
haircules 2:718f6b5cf75c 151 /*for ( int i = 0; rxDataCnt > 0; rxDataCnt--, i++ ) {
haircules 2:718f6b5cf75c 152
haircules 2:718f6b5cf75c 153
haircules 2:718f6b5cf75c 154 if(i == 0){
haircules 2:718f6b5cf75c 155
haircules 2:718f6b5cf75c 156 pc.printf("String Representation: X-axis Acc: ");
haircules 2:718f6b5cf75c 157
haircules 2:718f6b5cf75c 158 }
haircules 2:718f6b5cf75c 159 else if(i == 5){
haircules 2:718f6b5cf75c 160
haircules 2:718f6b5cf75c 161 pc.printf("\tY-axis Acc: ");
haircules 2:718f6b5cf75c 162
haircules 2:718f6b5cf75c 163 }
haircules 2:718f6b5cf75c 164 else if(i == 10){
haircules 2:718f6b5cf75c 165
haircules 2:718f6b5cf75c 166 pc.printf("\tZ-axis Acc: ");
haircules 2:718f6b5cf75c 167
haircules 2:718f6b5cf75c 168 }
haircules 2:718f6b5cf75c 169
Owen 0:a51a6e7da590 170 pc.putc( rxData[i] );
haircules 2:718f6b5cf75c 171
haircules 2:718f6b5cf75c 172 }//end for*/
haircules 2:718f6b5cf75c 173
haircules 2:718f6b5cf75c 174
haircules 2:718f6b5cf75c 175
haircules 2:718f6b5cf75c 176 pc.printf("\r\n");
haircules 2:718f6b5cf75c 177
Owen 0:a51a6e7da590 178 // Toggle LED2 (to help debug nRF24L01+ -> Host communication)
Owen 0:a51a6e7da590 179 myled2 = !myled2;
haircules 2:718f6b5cf75c 180
haircules 2:718f6b5cf75c 181 }/*end if*/
haircules 2:718f6b5cf75c 182 }/*end if*/
haircules 2:718f6b5cf75c 183 }/*end while*/
haircules 2:718f6b5cf75c 184
haircules 2:718f6b5cf75c 185
haircules 2:718f6b5cf75c 186 void unpack(int &x,int &y,int &z,char *b){
haircules 2:718f6b5cf75c 187 /*This function is resposible for unpacketizing a buffer
haircules 2:718f6b5cf75c 188 that has been transmitted wirelessly by distributing data to variables
haircules 2:718f6b5cf75c 189 passed by reference appropriately.*/
haircules 2:718f6b5cf75c 190
haircules 2:718f6b5cf75c 191 char buffer[5];/*buffer used to unpack data from packet*/
haircules 2:718f6b5cf75c 192
haircules 2:718f6b5cf75c 193 for(int i = 0; i < TRANSFER_SIZE; i++){
haircules 2:718f6b5cf75c 194
haircules 2:718f6b5cf75c 195 buffer[i%5] = b[i];
haircules 2:718f6b5cf75c 196
haircules 2:718f6b5cf75c 197
haircules 2:718f6b5cf75c 198 if(i == 4){/*buffer contains x*/
haircules 2:718f6b5cf75c 199
haircules 2:718f6b5cf75c 200 x = atoi(buffer);
haircules 2:718f6b5cf75c 201
Owen 0:a51a6e7da590 202 }
haircules 2:718f6b5cf75c 203 else if(i == 9){/*buffer contains y*/
haircules 2:718f6b5cf75c 204
haircules 2:718f6b5cf75c 205 y = atoi(buffer);
haircules 2:718f6b5cf75c 206
haircules 2:718f6b5cf75c 207 }
haircules 2:718f6b5cf75c 208 else if(i == 14){/*buffer contains z*/
haircules 2:718f6b5cf75c 209
haircules 2:718f6b5cf75c 210 z = atoi(buffer);
haircules 2:718f6b5cf75c 211
haircules 2:718f6b5cf75c 212 }/*end if*/
haircules 2:718f6b5cf75c 213 }/*end for*/
haircules 2:718f6b5cf75c 214 }/*end unpack()*/
haircules 2:718f6b5cf75c 215
haircules 2:718f6b5cf75c 216 void initEthernet(EthernetInterface eth){
haircules 2:718f6b5cf75c 217 /*This function handles the ethernet initialization upon being passed a
haircules 2:718f6b5cf75c 218 pointer to an EthernetInterface object*/
haircules 2:718f6b5cf75c 219
haircules 2:718f6b5cf75c 220 eth.init();/*initialize DHCP*/
haircules 2:718f6b5cf75c 221
haircules 2:718f6b5cf75c 222 printf("Initialized DHCP! Preparing to connect...\r\n");
haircules 2:718f6b5cf75c 223
haircules 2:718f6b5cf75c 224 eth.connect();/*connect eth to internet*/
haircules 2:718f6b5cf75c 225
haircules 2:718f6b5cf75c 226 printf("Success. Connected!. Device IP Address is %s\r\n", eth.getIPAddress());
haircules 2:718f6b5cf75c 227
haircules 2:718f6b5cf75c 228 }/*end initEthernet()*/