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
Committer:
sog_yang
Date:
Thu May 04 02:37:17 2017 +0000
Revision:
7:06495d54d107
Parent:
6:4ca0aa131d20
Child:
8:ff7fdd152f14
Project init

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sog_yang 5:ffe1a4c4881e 1 #include "mbed.h"
sog_yang 7:06495d54d107 2 #include "BNO055.h"
sog_yang 7:06495d54d107 3 #include "AS7000.h"
sog_yang 5:ffe1a4c4881e 4 #include "ble/BLE.h"
sog_yang 5:ffe1a4c4881e 5 #include "ble/Gap.h"
sog_yang 5:ffe1a4c4881e 6 #include "ble/services/BatteryService.h"
sog_yang 5:ffe1a4c4881e 7 #include "DOORService.h"
sog_yang 5:ffe1a4c4881e 8 #include "EthernetInterface.h"
sog_yang 5:ffe1a4c4881e 9 #include "WIFIDevice.h"
sog_yang 5:ffe1a4c4881e 10
sog_yang 5:ffe1a4c4881e 11
sog_yang 5:ffe1a4c4881e 12 union IP {
sog_yang 5:ffe1a4c4881e 13 unsigned int ip;
sog_yang 5:ffe1a4c4881e 14 struct {
sog_yang 5:ffe1a4c4881e 15 unsigned char d;
sog_yang 5:ffe1a4c4881e 16 unsigned char c;
sog_yang 5:ffe1a4c4881e 17 unsigned char b;
sog_yang 5:ffe1a4c4881e 18 unsigned char a;
sog_yang 5:ffe1a4c4881e 19 } ip2;
sog_yang 5:ffe1a4c4881e 20 };
sog_yang 5:ffe1a4c4881e 21
sog_yang 5:ffe1a4c4881e 22 char ips[20];
sog_yang 5:ffe1a4c4881e 23 IP ip;
sog_yang 7:06495d54d107 24
sog_yang 7:06495d54d107 25 DOORService *doorServicePtr;
sog_yang 7:06495d54d107 26
sog_yang 7:06495d54d107 27
sog_yang 7:06495d54d107 28 BNO055 imu(p0,p30);
sog_yang 7:06495d54d107 29 AS7000 hrm(p0,p30);
sog_yang 7:06495d54d107 30 Serial pc(USBTX, USBRX);
sog_yang 5:ffe1a4c4881e 31 EthernetInterface eth;
sog_yang 5:ffe1a4c4881e 32 WIFIDevice wifi;
sog_yang 7:06495d54d107 33
sog_yang 7:06495d54d107 34 Thread thread;
sog_yang 7:06495d54d107 35 Thread network_thread;
sog_yang 5:ffe1a4c4881e 36 TCPSocketConnection sock_tcp;
sog_yang 7:06495d54d107 37 char* ECHO_SERVER_ADDRESS = "10.0.1.13";
sog_yang 7:06495d54d107 38 int ECHO_SERVER_PORT = 1030;
sog_yang 5:ffe1a4c4881e 39
sog_yang 7:06495d54d107 40 uint8_t initialValueForDOORCharacteristic = 0xFF;
sog_yang 7:06495d54d107 41 uint8_t BLE_RX_CMD = 0xFF;
sog_yang 7:06495d54d107 42 const char DEVICE_NAME[] = "DOORCTL";
sog_yang 7:06495d54d107 43 uint8_t ADV_manuf[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
sog_yang 7:06495d54d107 44 char out_buffer[45];
sog_yang 5:ffe1a4c4881e 45 static EventQueue eventQueue(
sog_yang 5:ffe1a4c4881e 46 /* event count */ 16 * /* event size */ 32
sog_yang 5:ffe1a4c4881e 47 );
sog_yang 7:06495d54d107 48
sog_yang 7:06495d54d107 49 bool isConnect = false;
sog_yang 7:06495d54d107 50 bool isWiFiEnable = false;
sog_yang 7:06495d54d107 51
sog_yang 5:ffe1a4c4881e 52
sog_yang 5:ffe1a4c4881e 53 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
sog_yang 5:ffe1a4c4881e 54 {
sog_yang 5:ffe1a4c4881e 55 BLE::Instance().gap().startAdvertising();
sog_yang 5:ffe1a4c4881e 56 }
sog_yang 5:ffe1a4c4881e 57
sog_yang 5:ffe1a4c4881e 58 void onDataWrittenCallback(const GattWriteCallbackParams *params) {
sog_yang 5:ffe1a4c4881e 59
sog_yang 5:ffe1a4c4881e 60
sog_yang 5:ffe1a4c4881e 61 if ((params->handle == doorServicePtr->getValueHandle()) && (params->len == 1)) {
sog_yang 5:ffe1a4c4881e 62 {
sog_yang 7:06495d54d107 63 BLE_RX_CMD = *(params->data);
sog_yang 5:ffe1a4c4881e 64 }
sog_yang 5:ffe1a4c4881e 65 }
sog_yang 5:ffe1a4c4881e 66 }
sog_yang 5:ffe1a4c4881e 67
sog_yang 5:ffe1a4c4881e 68 /**
sog_yang 5:ffe1a4c4881e 69 * This function is called when the ble initialization process has failled
sog_yang 5:ffe1a4c4881e 70 */
sog_yang 5:ffe1a4c4881e 71 void onBleInitError(BLE &ble, ble_error_t error)
sog_yang 5:ffe1a4c4881e 72 {
sog_yang 5:ffe1a4c4881e 73 /* Initialization error handling should go here */
sog_yang 5:ffe1a4c4881e 74 }
sog_yang 5:ffe1a4c4881e 75
sog_yang 5:ffe1a4c4881e 76 /**
sog_yang 5:ffe1a4c4881e 77 * Callback triggered when the ble initialization process has finished
sog_yang 5:ffe1a4c4881e 78 */
sog_yang 5:ffe1a4c4881e 79 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
sog_yang 5:ffe1a4c4881e 80 {
sog_yang 5:ffe1a4c4881e 81 BLE& ble = params->ble;
sog_yang 5:ffe1a4c4881e 82 ble_error_t error = params->error;
sog_yang 5:ffe1a4c4881e 83
sog_yang 5:ffe1a4c4881e 84 if (error != BLE_ERROR_NONE) {
sog_yang 5:ffe1a4c4881e 85 /* In case of error, forward the error handling to onBleInitError */
sog_yang 5:ffe1a4c4881e 86 onBleInitError(ble, error);
sog_yang 5:ffe1a4c4881e 87 return;
sog_yang 5:ffe1a4c4881e 88 }
sog_yang 5:ffe1a4c4881e 89
sog_yang 5:ffe1a4c4881e 90 /* Ensure that it is the default instance of BLE */
sog_yang 5:ffe1a4c4881e 91 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
sog_yang 5:ffe1a4c4881e 92 return;
sog_yang 5:ffe1a4c4881e 93 }
sog_yang 5:ffe1a4c4881e 94
sog_yang 5:ffe1a4c4881e 95 ble.gap().onDisconnection(disconnectionCallback);
sog_yang 5:ffe1a4c4881e 96 ble.gattServer().onDataWritten(onDataWrittenCallback);
sog_yang 5:ffe1a4c4881e 97
sog_yang 5:ffe1a4c4881e 98 /* Setup primary services */
sog_yang 5:ffe1a4c4881e 99 doorServicePtr = new DOORService(ble, initialValueForDOORCharacteristic);
sog_yang 5:ffe1a4c4881e 100
sog_yang 5:ffe1a4c4881e 101 /* Setup advertising */
sog_yang 5:ffe1a4c4881e 102 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
sog_yang 5:ffe1a4c4881e 103 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, ADV_manuf, sizeof(ADV_manuf));
sog_yang 5:ffe1a4c4881e 104 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
sog_yang 7:06495d54d107 105 ble.gap().setAdvertisingInterval(150); /* 1000ms */
sog_yang 5:ffe1a4c4881e 106 ble.gap().startAdvertising();
sog_yang 5:ffe1a4c4881e 107 }
sog_yang 5:ffe1a4c4881e 108
sog_yang 5:ffe1a4c4881e 109 void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) {
sog_yang 5:ffe1a4c4881e 110 BLE &ble = BLE::Instance();
sog_yang 5:ffe1a4c4881e 111 eventQueue.call(Callback<void()>(&ble, &BLE::processEvents));
sog_yang 5:ffe1a4c4881e 112 }
sog_yang 7:06495d54d107 113
sog_yang 7:06495d54d107 114
sog_yang 7:06495d54d107 115 void network_send_thread (void) {
sog_yang 7:06495d54d107 116 while(true) {
sog_yang 7:06495d54d107 117 pc.printf("network_send_thread");
sog_yang 7:06495d54d107 118 wait(1);
sog_yang 5:ffe1a4c4881e 119 }
sog_yang 5:ffe1a4c4881e 120 }
sog_yang 5:ffe1a4c4881e 121
sog_yang 5:ffe1a4c4881e 122 int main (void) {
sog_yang 7:06495d54d107 123
sog_yang 5:ffe1a4c4881e 124 hrm.enable();
sog_yang 7:06495d54d107 125 imu.reset();
sog_yang 5:ffe1a4c4881e 126 imu.setmode(OPERATION_MODE_NDOF);
sog_yang 5:ffe1a4c4881e 127
sog_yang 7:06495d54d107 128 #if 0
sog_yang 7:06495d54d107 129 eth.init();
sog_yang 7:06495d54d107 130 wifi.setNetwork(M2M_WIFI_SEC_WPA_PSK, "SOG", "0227322327");
sog_yang 7:06495d54d107 131 eth.connect();
sog_yang 7:06495d54d107 132 wait_ms(20);
sog_yang 7:06495d54d107 133
sog_yang 7:06495d54d107 134
sog_yang 7:06495d54d107 135 pc.printf("Connect Success! \n");
sog_yang 7:06495d54d107 136 pc.printf("MAC: %s\n", eth.getMACAddress());
sog_yang 7:06495d54d107 137 pc.printf("IP: %s\n", eth.getIPAddress());
sog_yang 7:06495d54d107 138 pc.printf("Gateway: %s\n", eth.getGateway());
sog_yang 7:06495d54d107 139 pc.printf("NetworkMask: %s\n", eth.getNetworkMask());
sog_yang 7:06495d54d107 140
sog_yang 7:06495d54d107 141 #endif
sog_yang 5:ffe1a4c4881e 142
sog_yang 5:ffe1a4c4881e 143 BLE &ble = BLE::Instance();
sog_yang 5:ffe1a4c4881e 144 ble.onEventsToProcess(scheduleBleEventsProcessing);
sog_yang 5:ffe1a4c4881e 145 ble.init(bleInitComplete);
sog_yang 7:06495d54d107 146
sog_yang 5:ffe1a4c4881e 147 while (true) {
sog_yang 7:06495d54d107 148 imu.get_angles(); //query the i2c device
sog_yang 7:06495d54d107 149 wait_ms(5);
sog_yang 7:06495d54d107 150 hrm.hr_only();
sog_yang 7:06495d54d107 151 wait_ms(5);
sog_yang 7:06495d54d107 152 /* GATT Command 0xA1 Cloud Data Transfer*/
sog_yang 7:06495d54d107 153 if (BLE_RX_CMD == 0xA1){
sog_yang 7:06495d54d107 154 if (isConnect == true) {
sog_yang 7:06495d54d107 155 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);
sog_yang 7:06495d54d107 156 sock_tcp.send_all(out_buffer, sizeof(out_buffer) - 1);
sog_yang 7:06495d54d107 157 wait_ms(5);
sog_yang 7:06495d54d107 158 } else {
sog_yang 7:06495d54d107 159 BLE_RX_CMD = 0xFF;
sog_yang 7:06495d54d107 160 if (sock_tcp.connect(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT) < 0) {
sog_yang 7:06495d54d107 161 isConnect = false;
sog_yang 7:06495d54d107 162 pc.printf("Unable to connect to (%s) on port (%d)\n", ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT);
sog_yang 7:06495d54d107 163 } else {
sog_yang 7:06495d54d107 164 isConnect = true;
sog_yang 7:06495d54d107 165 pc.printf("Connected to Server at %s\n",ECHO_SERVER_ADDRESS);
sog_yang 7:06495d54d107 166 }
sog_yang 7:06495d54d107 167 }
sog_yang 7:06495d54d107 168 }
sog_yang 7:06495d54d107 169 /* GATT Command 0x02 WiFI & Cloud Connection Close*/
sog_yang 7:06495d54d107 170 if (BLE_RX_CMD == 0x02){
sog_yang 7:06495d54d107 171 ADV_manuf[5] = 0xF2;
sog_yang 7:06495d54d107 172 if ( isConnect == true ) {
sog_yang 7:06495d54d107 173 sock_tcp.close();
sog_yang 7:06495d54d107 174 isConnect = false;
sog_yang 5:ffe1a4c4881e 175 }
sog_yang 7:06495d54d107 176 if ( isWiFiEnable == true ) {
sog_yang 7:06495d54d107 177 eth.disconnect();
sog_yang 7:06495d54d107 178 wifi.sleep();
sog_yang 7:06495d54d107 179 isWiFiEnable = false;
sog_yang 7:06495d54d107 180 }
sog_yang 7:06495d54d107 181 if (isConnect == true) isConnect = false;
sog_yang 7:06495d54d107 182
sog_yang 7:06495d54d107 183 BLE_RX_CMD = 0xFF;
sog_yang 7:06495d54d107 184 ADV_manuf[0] = 0x00;
sog_yang 7:06495d54d107 185 ADV_manuf[1] = 0x00;
sog_yang 7:06495d54d107 186 ADV_manuf[2] = 0x00;
sog_yang 7:06495d54d107 187 ADV_manuf[3] = 0x00;
sog_yang 7:06495d54d107 188 }
sog_yang 7:06495d54d107 189 /* GATT Command 0x0 init WiFI & Cloud Connection*/
sog_yang 7:06495d54d107 190 if (BLE_RX_CMD == 0x00){
sog_yang 7:06495d54d107 191 BLE_RX_CMD = 0xFF;
sog_yang 7:06495d54d107 192 ADV_manuf[5] = 0xF0;
sog_yang 7:06495d54d107 193
sog_yang 7:06495d54d107 194 if (isWiFiEnable == false) {
sog_yang 7:06495d54d107 195 eth.init();
sog_yang 7:06495d54d107 196 wifi.setNetwork(M2M_WIFI_SEC_WPA_PSK, "SOG", "0227322327");
sog_yang 7:06495d54d107 197 eth.connect();
sog_yang 7:06495d54d107 198 wait_ms(25);
sog_yang 7:06495d54d107 199 pc.printf("Connect Success! \n");
sog_yang 7:06495d54d107 200 pc.printf("MAC: %s\n", eth.getMACAddress());
sog_yang 7:06495d54d107 201 pc.printf("IP: %s\n", eth.getIPAddress());
sog_yang 7:06495d54d107 202 pc.printf("Gateway: %s\n", eth.getGateway());
sog_yang 7:06495d54d107 203 pc.printf("NetworkMask: %s\n", eth.getNetworkMask());
sog_yang 7:06495d54d107 204
sog_yang 7:06495d54d107 205 snprintf(ips, sizeof(ips), "%s",eth.getIPAddress());
sog_yang 7:06495d54d107 206 unsigned short a, b, c, d;
sog_yang 7:06495d54d107 207 sscanf(ips, "%hu.%hu.%hu.%hu", &a, &b, &c, &d);
sog_yang 7:06495d54d107 208 sprintf(ips, "%x.%x.%x.%x", a, b, c, d);
sog_yang 7:06495d54d107 209 ADV_manuf[0] = a;
sog_yang 7:06495d54d107 210 ADV_manuf[1] = b;
sog_yang 7:06495d54d107 211 ADV_manuf[2] = c;
sog_yang 7:06495d54d107 212 ADV_manuf[3] = d;
sog_yang 7:06495d54d107 213 isWiFiEnable = true;
sog_yang 7:06495d54d107 214 }
sog_yang 7:06495d54d107 215
sog_yang 7:06495d54d107 216 if (isConnect == false) {
sog_yang 7:06495d54d107 217 if (isWiFiEnable == true) {
sog_yang 7:06495d54d107 218 if (sock_tcp.connect(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT) < 0) {
sog_yang 7:06495d54d107 219 pc.printf("Unable to connect to (%s) on port (%d)\n", ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT);
sog_yang 7:06495d54d107 220 sock_tcp.close();
sog_yang 7:06495d54d107 221 ADV_manuf[0] = 0xCF;
sog_yang 7:06495d54d107 222 ADV_manuf[1] = 0xCF;
sog_yang 7:06495d54d107 223 ADV_manuf[2] = 0xCF;
sog_yang 7:06495d54d107 224 ADV_manuf[3] = 0xCF;
sog_yang 7:06495d54d107 225 } else {
sog_yang 7:06495d54d107 226 isConnect = true;
sog_yang 7:06495d54d107 227 pc.printf("Connected to Server at %s\n",ECHO_SERVER_ADDRESS);
sog_yang 7:06495d54d107 228 }
sog_yang 7:06495d54d107 229 }
sog_yang 7:06495d54d107 230 }
sog_yang 7:06495d54d107 231 }
sog_yang 5:ffe1a4c4881e 232 ble.waitForEvent();
sog_yang 5:ffe1a4c4881e 233 }
sog_yang 7:06495d54d107 234 }