Nigel Leitzell / Mbed 2 deprecated SmartHomeBaseBoard

Dependencies:   EthernetInterface M2XStreamClient jsonlite mbed-rtos mbed nRF24L01P

Fork of nRF24L01P_Hello_World by Owen Edwards

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "nRF24L01P.h" /*RF Module*/
00003 #include "M2XStreamClient.h" /*AT&T M2X*/
00004 #include "EthernetInterface.h" /*Ethernet*/
00005 #include <stdlib.h>
00006 
00007 /***************              Base Board Code               **********************/
00008 /*     -- All revisions made by Nigel Leitzell for ECE533 final project --       */
00009 /* This code allows for communication between the Mobile Board and Bluetooth App */
00010 /* to the Base Board for Smart Home Automoation. Values logged from sensors will */
00011 /* be collected and sent to the cloud via Ethernet from the Base Board. The Base */
00012 /* Board should allow the user to control aspects of their home (i.e. lighting-  */
00013 /* and air temperature). However, only the Mobile Board or Bluetooth App may be- */
00014 /* used at any one time. Selection of board which assumes control of the Base-   */
00015 /* Board is determined using buttons on the Base Board or the Bluetooth App.     */
00016 /*********************************************************************************/
00017 
00018 /*****************************Pin Declarations************************************/
00019 Serial pc(USBTX, USBRX);// tx, rx
00020 nRF24L01P my_nrf24l01p (PTD6,PTD7,PTD5,PTD4,PTB20,PTC18);// mosi, miso, sck, csn, ce, irq
00021 DigitalOut myled1(LED1);
00022 DigitalOut myled2(LED2);
00023 /*********************************************************************************/
00024 
00025 /**************************Function Prototypes************************************/
00026 void unpack(int &x,int &y,int &z,char *b); /*unpacketize the buffer*/
00027 void initEthernet(EthernetInterface eth); /*Initialize Ethernet*/
00028 /*********************************************************************************/
00029 
00030 /*******************************Globals*******************************************/
00031 /*************** Set Sensor Stream details (AT&T M2X stuff) **********************/
00032 char deviceId[] = "81c5ecb0ac94aad592ad558ead689321"; // Device you want to push to
00033 char streamX[] = "acc_x"; // Stream you want to push to
00034 char streamY[] ="acc_y"; // Stream you want to push to
00035 char streamZ[] ="acc_z"; // Stream you want to push to
00036 char m2xKey[] = "b2c3795c7022a811a3c4de49095b26ec"; // Your M2X API Key or Master API Key
00037 /*********************************************************************************/
00038 
00039 int main() {
00040 
00041 // The nRF24L01+ supports transfers from 1 to 32 bytes, but Sparkfun's
00042 //  "Nordic Serial Interface Board" (http://www.sparkfun.com/products/9019)
00043 //  only handles 4 byte transfers in the ATMega code.
00044 #define TRANSFER_SIZE   15
00045 
00046     char txData[TRANSFER_SIZE], rxData[TRANSFER_SIZE];
00047     int txDataCnt = 0;
00048     int rxDataCnt = 0;
00049     int x,y,z;
00050     
00051     my_nrf24l01p.powerUp();
00052 
00053     //my_nrf24l01p.setRxAddress(00001,5,NRF24L01P_PIPE_P0);
00054     //my_nrf24l01p.setTxAddress(00001,5);
00055      
00056     // Display the (default) setup of the nRF24L01+ chip
00057     pc.printf( "nRF24L01+ Frequency    : %d MHz\r\n",  my_nrf24l01p.getRfFrequency() );
00058     pc.printf( "nRF24L01+ Output power : %d dBm\r\n",  my_nrf24l01p.getRfOutputPower() );
00059     pc.printf( "nRF24L01+ Data Rate    : %d kbps\r\n", my_nrf24l01p.getAirDataRate() );
00060     pc.printf( "nRF24L01+ TX Address   : 0x%010llX\r\n", my_nrf24l01p.getTxAddress() );
00061     pc.printf( "nRF24L01+ RX Address   : 0x%010llX\r\n", my_nrf24l01p.getRxAddress() );
00062 
00063     pc.printf( "Type keys to test transfers:\r\n  (transfers are grouped into %d characters)\r\n\n\n", TRANSFER_SIZE );
00064 
00065     my_nrf24l01p.setTransferSize( TRANSFER_SIZE );
00066 
00067     my_nrf24l01p.setReceiveMode();
00068     my_nrf24l01p.enable();
00069     
00070     /* Intialize Ethernet connection*/
00071     EthernetInterface eth; 
00072     initEthernet(eth); 
00073     
00074     /* Initialize the M2X client */
00075     Client client;
00076     M2XStreamClient m2xClient(&client, m2xKey);
00077     
00078     while (1) {
00079 
00080         // If we've received anything over the host serial link...
00081         if ( pc.readable() ) {
00082 
00083             // ...add it to the transmit buffer
00084             txData[txDataCnt++] = pc.getc();
00085 
00086             pc.printf( "Key press recognized as key count %d\n\r", txDataCnt);
00087             
00088             // If the transmit buffer is full
00089             if ( txDataCnt >= sizeof( txData ) ) {
00090 
00091                 pc.printf( "Final key press before TX has been recorded, count is %d..\n\n\r",txDataCnt);
00092                 pc.printf( "Buffer is now full, preparing to send data..\n\r");
00093                 pc.printf( "Contents of txData buffer: ");
00094                 
00095                 /*print the contents of buffer*/
00096                 for(int i=0; i<txDataCnt; i++){ 
00097                     
00098                     pc.printf("%c",txData[i]);
00099                     
00100                 }/*end for*/
00101                 
00102                 
00103                 // Send the transmitbuffer via the nRF24L01+
00104                 my_nrf24l01p.write( NRF24L01P_PIPE_P0, txData, txDataCnt );
00105 
00106                 txDataCnt = 0;
00107                 
00108                 pc.printf( "\n\rData has been sent!\n\r");
00109             }/*end if*/
00110 
00111             // Toggle LED1 (to help debug Host -> nRF24L01+ communication)
00112             myled1 = !myled1;
00113             
00114         }/*end if*/
00115 
00116         // If we've received anything in the nRF24L01+...
00117         if ( my_nrf24l01p.readable() ) {
00118 
00119             // ...read the data into the receive buffer
00120             rxDataCnt = my_nrf24l01p.read( NRF24L01P_PIPE_P0, rxData, TRANSFER_SIZE );
00121 
00122             unpack(x,y,z,rxData);//unpack data 
00123                         
00124             pc.printf("Integer Representation: X-Axis Acc: %d \t",x);
00125             pc.printf("Y-Axis Acc: %d\t",y);
00126             pc.printf("Z-Axis Acc: %d\n\r",z);
00127             
00128             
00129             //Now that data is unpacked.. What to do?
00130             if(x >= 2000){
00131                 
00132                 pc.printf("Woah, leaning right!\n\r");
00133             }
00134             else if(x <= -2000){
00135                 
00136                 pc.printf("Woah, leaning left!\n\r");
00137                 
00138             }
00139             else{
00140                 
00141                 pc.printf("Just right.\n\r");
00142                 
00143             }
00144             
00145             //Send data to M2X
00146             m2xClient.updateStreamValue(deviceId, streamX, x);
00147             m2xClient.updateStreamValue(deviceId, streamY, y);
00148             m2xClient.updateStreamValue(deviceId, streamZ, z);
00149             
00150             // Display the receive buffer contents via the host serial link
00151             /*for ( int i = 0; rxDataCnt > 0; rxDataCnt--, i++ ) {
00152             
00153                 
00154                 if(i == 0){
00155                     
00156                     pc.printf("String  Representation: X-axis Acc: ");
00157                     
00158                 }
00159                 else if(i == 5){
00160                     
00161                     pc.printf("\tY-axis Acc: ");
00162                 
00163                 }
00164                 else if(i == 10){
00165                     
00166                     pc.printf("\tZ-axis Acc: ");
00167                     
00168                 }
00169                     
00170                 pc.putc( rxData[i] );
00171                 
00172             }//end for*/
00173             
00174             
00175             
00176             pc.printf("\r\n");
00177             
00178             // Toggle LED2 (to help debug nRF24L01+ -> Host communication)
00179             myled2 = !myled2;
00180         
00181         }/*end if*/
00182     }/*end if*/
00183 }/*end while*/
00184 
00185 
00186 void unpack(int &x,int &y,int &z,char *b){
00187 /*This function is resposible for unpacketizing a buffer 
00188 that has been transmitted wirelessly by distributing data to variables 
00189 passed by reference appropriately.*/
00190     
00191     char buffer[5];/*buffer used to unpack data from packet*/
00192     
00193     for(int i = 0; i < TRANSFER_SIZE; i++){
00194                 
00195         buffer[i%5] = b[i];
00196                 
00197             
00198         if(i == 4){/*buffer contains x*/
00199                  
00200             x = atoi(buffer);
00201                 
00202         }
00203         else if(i == 9){/*buffer contains y*/
00204                     
00205             y = atoi(buffer);
00206                     
00207         }
00208         else if(i == 14){/*buffer contains z*/
00209                     
00210             z = atoi(buffer);
00211                     
00212         }/*end if*/
00213     }/*end for*/
00214 }/*end unpack()*/
00215 
00216 void initEthernet(EthernetInterface eth){
00217 /*This function handles the ethernet initialization upon being passed a
00218 pointer to an EthernetInterface object*/
00219         
00220         eth.init();/*initialize DHCP*/
00221         
00222         printf("Initialized DHCP! Preparing to connect...\r\n");
00223         
00224         eth.connect();/*connect eth to internet*/
00225         
00226         printf("Success. Connected!. Device IP Address is %s\r\n", eth.getIPAddress());
00227     
00228 }/*end initEthernet()*/