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.
- Download or build a TCP Server on you host computer
- Please find out the TCP server code or install TCP server application in your host computer.
- Import this sample application and setup these information about AP & TCP Server
- *char* AP_SSID = "SOG";
- *char* AP_PWD = "1122334455667788";
- *char* TCP_SERVER_ADDRESS = "10.0.1.13";
- *int TCP_SERVER_PORT = 1030;
- Compiler your code and download to your mBed device.
- Control your mBed device
- 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
0x00 | 0x02 | 0xA1 |
---|---|---|
WiFi,AP&TCP Server connect | WiFi,AP, & TCP Server disconnect | Send sensor data to TCP Server |
main.cpp
- Committer:
- sog_yang
- Date:
- 2017-05-04
- Revision:
- 7:06495d54d107
- Parent:
- 6:4ca0aa131d20
- Child:
- 8:ff7fdd152f14
File content as of revision 7:06495d54d107:
#include "mbed.h" #include "BNO055.h" #include "AS7000.h" #include "ble/BLE.h" #include "ble/Gap.h" #include "ble/services/BatteryService.h" #include "DOORService.h" #include "EthernetInterface.h" #include "WIFIDevice.h" union IP { unsigned int ip; struct { unsigned char d; unsigned char c; unsigned char b; unsigned char a; } ip2; }; char ips[20]; IP ip; 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"; 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( /* event count */ 16 * /* event size */ 32 ); bool isConnect = false; bool isWiFiEnable = false; void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) { BLE::Instance().gap().startAdvertising(); } void onDataWrittenCallback(const GattWriteCallbackParams *params) { if ((params->handle == doorServicePtr->getValueHandle()) && (params->len == 1)) { { BLE_RX_CMD = *(params->data); } } } /** * This function is called when the ble initialization process has failled */ void onBleInitError(BLE &ble, ble_error_t error) { /* Initialization error handling should go here */ } /** * Callback triggered when the ble initialization process has finished */ void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) { BLE& ble = params->ble; ble_error_t error = params->error; if (error != BLE_ERROR_NONE) { /* In case of error, forward the error handling to onBleInitError */ onBleInitError(ble, error); return; } /* Ensure that it is the default instance of BLE */ if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { return; } ble.gap().onDisconnection(disconnectionCallback); ble.gattServer().onDataWritten(onDataWrittenCallback); /* Setup primary services */ doorServicePtr = new DOORService(ble, initialValueForDOORCharacteristic); /* Setup advertising */ ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, ADV_manuf, sizeof(ADV_manuf)); ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); ble.gap().setAdvertisingInterval(150); /* 1000ms */ ble.gap().startAdvertising(); } void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) { BLE &ble = BLE::Instance(); eventQueue.call(Callback<void()>(&ble, &BLE::processEvents)); } 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); ble.init(bleInitComplete); while (true) { imu.get_angles(); //query the i2c device wait_ms(5); hrm.hr_only(); wait_ms(5); /* 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); } else { isConnect = true; pc.printf("Connected to Server at %s\n",ECHO_SERVER_ADDRESS); } } } /* GATT Command 0x02 WiFI & Cloud Connection Close*/ if (BLE_RX_CMD == 0x02){ ADV_manuf[5] = 0xF2; if ( isConnect == true ) { sock_tcp.close(); isConnect = false; } if ( isWiFiEnable == true ) { eth.disconnect(); wifi.sleep(); isWiFiEnable = false; } if (isConnect == true) isConnect = false; BLE_RX_CMD = 0xFF; ADV_manuf[0] = 0x00; ADV_manuf[1] = 0x00; ADV_manuf[2] = 0x00; ADV_manuf[3] = 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()); 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); } } } } ble.waitForEvent(); } }