Tsungta Wu / Mbed OS NNN50_BLEWIFISensor

Dependencies:   BNO055_AS7000 NNN50_WIFI_API

Fork of NNN50_BLEWIFISensor by Sog Yang

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "BNO055.h"
00003 //#include "AS7000.h"
00004 #include "ble/BLE.h"
00005 #include "ble/Gap.h"
00006 #include "ble/services/BatteryService.h"
00007 #include "DOORService.h"
00008 #include "EthernetInterface.h"
00009 #include "WIFIDevice.h"
00010 
00011 #define DEBUG_LOG 1
00012 
00013 union IP {
00014     unsigned int ip;
00015     struct {
00016       unsigned char d;
00017       unsigned char c;
00018       unsigned char b;
00019       unsigned char a;
00020     } ip2;
00021 };
00022 
00023 char  ips[20];
00024 IP ip;
00025 
00026 DOORService *doorServicePtr;
00027 
00028 BNO055 imu(p0,p30);
00029 //AS7000 hrm(p0,p30);
00030 Serial pc(USBTX, USBRX);
00031 DigitalInOut myOutputPin(USBTX);
00032 EthernetInterface eth;
00033 WIFIDevice wifi;
00034 
00035 TCPSocketConnection sock_tcp;
00036 char* AP_SSID = "TWCYNPC0209_Mac mini";
00037 char* AP_PWD = "mayday55555";
00038 char* TCP_SERVER_ADDRESS = "192.168.2.7";
00039 int TCP_SERVER_PORT = 1030;
00040 
00041 uint8_t initialValueForDOORCharacteristic = 0xFF;
00042 uint8_t BLE_RX_CMD = 0xFF;
00043 const char DEVICE_NAME[] = "FITNCTL";
00044 uint8_t ADV_manuf[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
00045 char    out_buffer[45];
00046 static EventQueue eventQueue(
00047     /* event count */ 16 * /* event size */ 32
00048 );
00049 
00050  bool isConnect = false;
00051  bool isWiFiEnable = false;
00052  bool isCloudFiling = false;
00053 
00054 
00055 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
00056 {
00057     BLE::Instance().gap().startAdvertising();
00058 }
00059 
00060 void onDataWrittenCallback(const GattWriteCallbackParams *params) {
00061     
00062     
00063     if ((params->handle == doorServicePtr->getValueHandle()) && (params->len == 1)) {
00064         {
00065             BLE_RX_CMD = *(params->data);                   
00066         }
00067     }
00068 }
00069 
00070 /**
00071  * This function is called when the ble initialization process has failled
00072  */
00073 void onBleInitError(BLE &ble, ble_error_t error)
00074 {
00075     /* Initialization error handling should go here */
00076 }
00077 
00078 /**
00079  * Callback triggered when the ble initialization process has finished
00080  */
00081 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
00082 {
00083     BLE&        ble   = params->ble;
00084     ble_error_t error = params->error;
00085 
00086     if (error != BLE_ERROR_NONE) {
00087         /* In case of error, forward the error handling to onBleInitError */
00088         onBleInitError(ble, error);
00089         return;
00090     }
00091 
00092     /* Ensure that it is the default instance of BLE */
00093     if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
00094         return;
00095     }
00096 
00097     ble.gap().onDisconnection(disconnectionCallback);
00098     ble.gattServer().onDataWritten(onDataWrittenCallback);
00099     
00100     /* Setup primary services */  
00101     doorServicePtr = new DOORService(ble, initialValueForDOORCharacteristic);
00102 
00103     /* Setup advertising */
00104     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
00105     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, ADV_manuf, sizeof(ADV_manuf));
00106     ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
00107     ble.gap().setAdvertisingInterval(150); /* 1000ms */
00108     ble.gap().startAdvertising();
00109 }
00110 
00111 void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) {
00112     BLE &ble = BLE::Instance();
00113     eventQueue.call(Callback<void()>(&ble, &BLE::processEvents));
00114 }
00115 
00116 
00117 int main (void) {
00118     myOutputPin.mode(PullUp); 
00119     pc.baud(38400);    
00120     imu.enable(); 
00121     imu.reset();    
00122     imu.setmode(OPERATION_MODE_NDOF);
00123     
00124     
00125     BLE &ble = BLE::Instance();
00126     ble.onEventsToProcess(scheduleBleEventsProcessing);
00127     ble.init(bleInitComplete);
00128     
00129     int i = 0;
00130     int j = 0;
00131     BLE_RX_CMD = 0xFF;
00132     wait_ms(10);
00133     sprintf(out_buffer,"\r\n");
00134     
00135     
00136     while (true) {
00137         wait_ms(8);
00138         imu.hr_only();
00139         wait_ms(2);
00140         imu.get_angles(); //query the i2c device
00141         wait_ms(8);
00142         if (BLE_RX_CMD != 0xA1) {
00143             if (i > 40){
00144                 pc.printf("*HR=%03d# yaw:%6.2f#", imu.hrm.hreat_rate, imu.euler.yaw);
00145                 i=0;
00146             } i++;
00147         }
00148         /* GATT Command 0xA1 Cloud Data Transfer*/
00149         if (BLE_RX_CMD == 0xA1){
00150             ADV_manuf[5] = 0xF1; 
00151             if (isWiFiEnable == true){                 
00152                 if (isConnect == true) {                   
00153                     if (j > 2){
00154                         pc.printf("*HR=%03d#", imu.hrm.hreat_rate);
00155                         j = 0;
00156                     }j++;                 
00157                     sprintf(out_buffer,"hrm:%03d yaw:%6.2f pitch:%6.2f roll:%6.2f\n", imu.hrm.hreat_rate, imu.euler.yaw, imu.euler.pitch, imu.euler.roll);
00158                     //sock_tcp.set_blocking(false, 1500);// Timeout after 0.2s
00159                     sock_tcp.send_all(out_buffer, sizeof(out_buffer) - 1);  
00160                      isCloudFiling = true;
00161                 } else {
00162                     if (sock_tcp.connect(TCP_SERVER_ADDRESS, TCP_SERVER_PORT) < 0) {
00163                         isConnect = false;
00164                         BLE_RX_CMD = 0xFF;
00165 #if DEBUG_LOG                        
00166                         pc.printf("Unable to connect to (%s) on port (%d)\n", TCP_SERVER_ADDRESS, TCP_SERVER_PORT);
00167 #endif                        
00168                         ADV_manuf[4] = 0xCF;
00169                         BLE_RX_CMD = 0xFF;
00170                     } else {
00171                          isConnect = true; 
00172                           ADV_manuf[4] = 0xF1;
00173 #if DEBUG_LOG                           
00174                          pc.printf("Connected to Server at %s\n",TCP_SERVER_ADDRESS);
00175 #endif                         
00176                     }
00177                 }
00178             }
00179         }
00180         /* GATT Command 0x02 WiFI & Cloud Connection Close*/
00181          if (BLE_RX_CMD == 0x02){
00182             BLE_RX_CMD = 0xFF;
00183             ADV_manuf[5] = 0xF2;  
00184             if ( isConnect == true ) {
00185                 sock_tcp.close(); 
00186                 isConnect = false;                                
00187             }
00188             if ( isWiFiEnable == true ) {
00189                 eth.disconnect();                              
00190                 wifi.sleep();
00191                 isWiFiEnable = false;
00192             } 
00193             while(true) {
00194                     if (wifi.is_AP_connected()==0) break; //make sure wifi disconnect
00195             }
00196             if (isConnect == true) isConnect = false;   
00197             isConnect = false;
00198             isWiFiEnable = false;
00199             isCloudFiling = false;      
00200              
00201             ADV_manuf[0] = 0x00;
00202             ADV_manuf[1] = 0x00;
00203             ADV_manuf[2] = 0x00;
00204             ADV_manuf[3] = 0x00;
00205             ADV_manuf[4] = 0x00;
00206             sprintf(out_buffer,"\n");         
00207         }
00208         /* GATT Command 0x0 init WiFI & Cloud Connection*/ 
00209        if (BLE_RX_CMD == 0x00){
00210         if(isCloudFiling == false) {
00211             BLE_RX_CMD = 0xFF;
00212             ADV_manuf[5] = 0xF0; 
00213             
00214             if (isWiFiEnable == false) {
00215                 eth.init();  
00216                 wifi.setNetwork(M2M_WIFI_SEC_WPA_PSK, AP_SSID, AP_PWD);
00217                 eth.connect(); 
00218                 while(true) {
00219                     if (wifi.is_AP_connected()==1) break;
00220                 }
00221                 //sock_tcp.set_blocking(false, 1200);// Timeout after 1.2s 
00222 #if DEBUG_LOG               
00223                 pc.printf("Connect Success! \n");
00224                 pc.printf("MAC: %s\n", eth.getMACAddress());            
00225                 pc.printf("IP: %s\n", eth.getIPAddress());
00226                 pc.printf("Gateway: %s\n", eth.getGateway());
00227                 pc.printf("NetworkMask: %s\n", eth.getNetworkMask()); 
00228 #endif            
00229                 snprintf(ips, sizeof(ips), "%s",eth.getIPAddress());    
00230                 unsigned short a, b, c, d;
00231                 sscanf(ips, "%hu.%hu.%hu.%hu", &a, &b, &c, &d);    
00232                 sprintf(ips, "%x.%x.%x.%x", a, b, c, d);
00233                 ADV_manuf[0] = a;
00234                 ADV_manuf[1] = b;
00235                 ADV_manuf[2] = c;
00236                 ADV_manuf[3] = d; 
00237                 isWiFiEnable = true; 
00238             } 
00239             
00240             if (isConnect == false) {
00241                 if (isWiFiEnable == true) {
00242                     if (sock_tcp.connect(TCP_SERVER_ADDRESS, TCP_SERVER_PORT) < 0) {
00243 #if DEBUG_LOG                         
00244                             pc.printf("Unable to connect to (%s) on port (%d)\n", TCP_SERVER_ADDRESS, TCP_SERVER_PORT);
00245 #endif                         
00246                             sock_tcp.close();
00247                             ADV_manuf[4] = 0xCF;
00248                         } else {
00249                              isConnect = true;
00250                              isCloudFiling = false;
00251                              ADV_manuf[4] = 0xF1;
00252 #if DEBUG_LOG                          
00253                              pc.printf("Connected to Server at %s\n",TCP_SERVER_ADDRESS);
00254 #endif     
00255                         }
00256                     }  
00257                 }
00258             }    
00259        }
00260        BLE::Instance(BLE::DEFAULT_INSTANCE).gap().updateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, ADV_manuf, sizeof(ADV_manuf));
00261        ble.waitForEvent();    
00262     }
00263 }