BLE-WiFi with BIOSensors

Dependencies:   AS7000 BNO055 NNN50_WIFI_API

DELTA NNN50 (Bluetooth LE 4.X & 802.11 b/g/n) with Bio Sensor (HRM, 9DoF motion Sensor) reference design.

Env. Setup step by step.

  1. Download or build a TCP Server on you host computer
    1. Please find out the TCP server code or install TCP server application in your host computer.
  2. Import this sample application and setup these information about AP & TCP Server
    1. *char* AP_SSID = "SOG";
    2. *char* AP_PWD = "1122334455667788";
    3. *char* TCP_SERVER_ADDRESS = "10.0.1.13";
    4. *int TCP_SERVER_PORT = 1030;
  3. Compiler your code and download to your mBed device.
  4. Control your mBed device
    1. Please use NORDSemi nRF Tool and setup and watch these information: GATT CMD: 0x00 (connect to AP, TCP Server, and create TCP socket), 0x02 disconnection TCP server, close socket, and WiFi sleep), 0xA1 (send sensor data to tcp server from out_buffer[]) and Status will be update by Bluetooth LE adv through manufacturing information
0x000x020xA1
WiFi,AP&TCP Server connectWiFi,AP, & TCP Server disconnectSend sensor data to TCP Server
Revision:
8:ff7fdd152f14
Parent:
7:06495d54d107
Child:
9:d1861b00e0fd
--- a/main.cpp	Thu May 04 02:37:17 2017 +0000
+++ b/main.cpp	Thu May 04 05:08:42 2017 +0000
@@ -8,6 +8,7 @@
 #include "EthernetInterface.h"
 #include "WIFIDevice.h"
 
+#define DEBUG_LOG 0
 
 union IP {
     unsigned int ip;
@@ -24,22 +25,19 @@
 
 DOORService *doorServicePtr;
 
-
 BNO055 imu(p0,p30);
 AS7000 hrm(p0,p30);
 Serial pc(USBTX, USBRX);
 EthernetInterface eth;
 WIFIDevice wifi;
 
-Thread thread;
-Thread network_thread;
 TCPSocketConnection sock_tcp;
 char* ECHO_SERVER_ADDRESS = "10.0.1.13";
 int ECHO_SERVER_PORT = 1030;
 
 uint8_t initialValueForDOORCharacteristic = 0xFF;
 uint8_t BLE_RX_CMD = 0xFF;
-const char DEVICE_NAME[] = "DOORCTL";
+const char DEVICE_NAME[] = "FITNCTL";
 uint8_t ADV_manuf[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
 char     out_buffer[45];
 static EventQueue eventQueue(
@@ -48,6 +46,7 @@
 
  bool isConnect = false;
  bool isWiFiEnable = false;
+ bool isCloudFiling = false;
 
 
 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
@@ -112,33 +111,12 @@
 }
 
 
-void network_send_thread (void) {
-    while(true) {
-        pc.printf("network_send_thread");
-        wait(1);
-    }
-}
-
 int main (void) {
         
     hrm.enable(); 
     imu.reset();    
     imu.setmode(OPERATION_MODE_NDOF);
     
- #if 0   
-    eth.init();  
-    wifi.setNetwork(M2M_WIFI_SEC_WPA_PSK, "SOG", "0227322327"); 
-    eth.connect(); 
-    wait_ms(20); 
-
-
-    pc.printf("Connect Success! \n");
-    pc.printf("MAC: %s\n", eth.getMACAddress());            
-    pc.printf("IP: %s\n", eth.getIPAddress());
-    pc.printf("Gateway: %s\n", eth.getGateway());
-    pc.printf("NetworkMask: %s\n", eth.getNetworkMask()); 
-    
-#endif
     
     BLE &ble = BLE::Instance();
     ble.onEventsToProcess(scheduleBleEventsProcessing);
@@ -146,23 +124,32 @@
     
     while (true) {
         imu.get_angles(); //query the i2c device
-        wait_ms(5);
         hrm.hr_only();
-        wait_ms(5);
+        pc.printf("*HR=%03d#", hrm.hrm.hreat_rate);  
         /* GATT Command 0xA1 Cloud Data Transfer*/
-        if (BLE_RX_CMD == 0xA1){                 
-            if (isConnect == true) {
-                sprintf(out_buffer,"hrm:%03d yaw:%6.2f pitch:%6.2f roll:%6.2f\n", hrm.hrm.hreat_rate, imu.euler.yaw, imu.euler.pitch, imu.euler.roll);
-                sock_tcp.send_all(out_buffer, sizeof(out_buffer) - 1);  
-                wait_ms(5);  
-            } else {
-                BLE_RX_CMD = 0xFF;
-                if (sock_tcp.connect(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT) < 0) {
-                    isConnect = false;
-                    pc.printf("Unable to connect to (%s) on port (%d)\n", ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT);
+        if (BLE_RX_CMD == 0xA1){
+            ADV_manuf[5] = 0xF1; 
+            if (isWiFiEnable == true){                 
+                if (isConnect == true) {
+                    sprintf(out_buffer,"hrm:%03d yaw:%6.2f pitch:%6.2f roll:%6.2f\n", hrm.hrm.hreat_rate, imu.euler.yaw, imu.euler.pitch, imu.euler.roll);
+                    sock_tcp.send_all(out_buffer, sizeof(out_buffer) - 1);  
+                     isCloudFiling = true;
                 } else {
-                     isConnect = true;  
-                     pc.printf("Connected to Server at %s\n",ECHO_SERVER_ADDRESS);
+                    if (sock_tcp.connect(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT) < 0) {
+                        isConnect = false;
+                        BLE_RX_CMD = 0xFF;
+#if DEBUG_LOG                        
+                        pc.printf("Unable to connect to (%s) on port (%d)\n", ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT);
+#endif                        
+                        ADV_manuf[4] = 0xCF;
+                        BLE_RX_CMD = 0xFF;
+                    } else {
+                         isConnect = true; 
+                          ADV_manuf[4] = 0xF1;
+#if DEBUG_LOG                           
+                         pc.printf("Connected to Server at %s\n",ECHO_SERVER_ADDRESS);
+#endif                         
+                    }
                 }
             }
         }
@@ -178,57 +165,70 @@
                 wifi.sleep();
                 isWiFiEnable = false;
             } 
-            if (isConnect == true) isConnect = false;           
+            if (isConnect == true) isConnect = false;   
+            isConnect = false;
+            isWiFiEnable = false;
+            isCloudFiling = false;        
             
             BLE_RX_CMD = 0xFF;    
             ADV_manuf[0] = 0x00;
             ADV_manuf[1] = 0x00;
             ADV_manuf[2] = 0x00;
-            ADV_manuf[3] = 0x00;       
+            ADV_manuf[3] = 0x00;
+            ADV_manuf[4] = 0x00;         
         }
         /* GATT Command 0x0 init WiFI & Cloud Connection*/ 
        if (BLE_RX_CMD == 0x00){
-        BLE_RX_CMD = 0xFF;
-        ADV_manuf[5] = 0xF0; 
-        
-        if (isWiFiEnable == false) {
-            eth.init();  
-            wifi.setNetwork(M2M_WIFI_SEC_WPA_PSK, "SOG", "0227322327"); 
-            eth.connect(); 
-            wait_ms(25);    
-            pc.printf("Connect Success! \n");
-            pc.printf("MAC: %s\n", eth.getMACAddress());            
-            pc.printf("IP: %s\n", eth.getIPAddress());
-            pc.printf("Gateway: %s\n", eth.getGateway());
-            pc.printf("NetworkMask: %s\n", eth.getNetworkMask()); 
+        if(isCloudFiling == false) {
+            BLE_RX_CMD = 0xFF;
+            ADV_manuf[5] = 0xF0; 
             
-            snprintf(ips, sizeof(ips), "%s",eth.getIPAddress());    
-            unsigned short a, b, c, d;
-            sscanf(ips, "%hu.%hu.%hu.%hu", &a, &b, &c, &d);    
-            sprintf(ips, "%x.%x.%x.%x", a, b, c, d);
-            ADV_manuf[0] = a;
-            ADV_manuf[1] = b;
-            ADV_manuf[2] = c;
-            ADV_manuf[3] = d; 
-            isWiFiEnable = true; 
-        } 
-        
-        if (isConnect == false) {
-            if (isWiFiEnable == true) {
-                if (sock_tcp.connect(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT) < 0) {
-                        pc.printf("Unable to connect to (%s) on port (%d)\n", ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT);
-                        sock_tcp.close();
-                        ADV_manuf[0] = 0xCF;
-                        ADV_manuf[1] = 0xCF;
-                        ADV_manuf[2] = 0xCF;
-                        ADV_manuf[3] = 0xCF;
-                    } else {
-                         isConnect = true;
-                         pc.printf("Connected to Server at %s\n",ECHO_SERVER_ADDRESS);     
-                    }
-                }  
+            if (isWiFiEnable == false) {
+                eth.init();  
+                wifi.setNetwork(M2M_WIFI_SEC_WPA_PSK, "SOG", "0227322327"); 
+                eth.connect(); 
+                while(true) {
+                    if (wifi.is_AP_connected()==1) break;
+                }
+#if DEBUG_LOG               
+                pc.printf("Connect Success! \n");
+                pc.printf("MAC: %s\n", eth.getMACAddress());            
+                pc.printf("IP: %s\n", eth.getIPAddress());
+                pc.printf("Gateway: %s\n", eth.getGateway());
+                pc.printf("NetworkMask: %s\n", eth.getNetworkMask()); 
+#endif            
+                snprintf(ips, sizeof(ips), "%s",eth.getIPAddress());    
+                unsigned short a, b, c, d;
+                sscanf(ips, "%hu.%hu.%hu.%hu", &a, &b, &c, &d);    
+                sprintf(ips, "%x.%x.%x.%x", a, b, c, d);
+                ADV_manuf[0] = a;
+                ADV_manuf[1] = b;
+                ADV_manuf[2] = c;
+                ADV_manuf[3] = d; 
+                isWiFiEnable = true; 
+            } 
+            
+            if (isConnect == false) {
+                if (isWiFiEnable == true) {
+                    if (sock_tcp.connect(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT) < 0) {
+#if DEBUG_LOG                         
+                            pc.printf("Unable to connect to (%s) on port (%d)\n", ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT);
+#endif                         
+                            sock_tcp.close();
+                            ADV_manuf[4] = 0xCF;
+                        } else {
+                             isConnect = true;
+                             isCloudFiling = false;
+                             ADV_manuf[4] = 0xF1;
+#if DEBUG_LOG                          
+                             pc.printf("Connected to Server at %s\n",ECHO_SERVER_ADDRESS);
+#endif     
+                        }
+                    }  
+                }
             }    
        }
-        ble.waitForEvent();
+       BLE::Instance(BLE::DEFAULT_INSTANCE).gap().updateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, ADV_manuf, sizeof(ADV_manuf));
+       ble.waitForEvent();    
     }
 }
\ No newline at end of file