This is an example of BLE GATT Client, which receives broadcast data from BLE_Server_BME280 ( a GATT server) , then transfers values up to mbed Device Connector (cloud).

Please refer details about BLEClient_mbedDevConn below. https://github.com/soramame21/BLEClient_mbedDevConn

The location of required BLE GATT server, BLE_Server_BME280, is at here. https://developer.mbed.org/users/edamame22/code/BLE_Server_BME280/

Committer:
Ren Boting
Date:
Tue Sep 05 11:56:13 2017 +0900
Revision:
2:b894b3508057
Parent:
0:29983394c6b6
Update all libraries and reform main.cpp

Who changed what in which revision?

UserRevisionLine numberNew contents of line
edamame22 0:29983394c6b6 1 # BLEClient_mbedDevConn
edamame22 0:29983394c6b6 2
edamame22 0:29983394c6b6 3 This example includes 2 main features.
edamame22 0:29983394c6b6 4 * [**BLE GATT Client** - receiving sensor data on field](#ble-gatt-client-for-bme280)
edamame22 0:29983394c6b6 5 * [**mbed Client** - transferring sensor data up on cloud](#mbed-client-on-mbed-os)
edamame22 0:29983394c6b6 6
edamame22 0:29983394c6b6 7
edamame22 0:29983394c6b6 8 # BLE GATT Client for BME280
edamame22 0:29983394c6b6 9
edamame22 0:29983394c6b6 10 The ``GattClient`` APIs are used to implement BLE GATT client. this feature communicates with another GATT server device.
edamame22 0:29983394c6b6 11
edamame22 0:29983394c6b6 12 1. The GATT Server device - runs the application ``BLE_Server_BME280`` from [ here ](https://github.com/soramame21/BLE_Server_BME280), which broadcasts measured values of BME280 over BLE.
edamame22 0:29983394c6b6 13 1. The GATT Client device - runs the application ``BLEClient_mbedDevConn`` to receive broadcast data over BLE.
edamame22 0:29983394c6b6 14
edamame22 0:29983394c6b6 15 Please note: The application ``BLEClient_mbedDevConn`` initiates a connection to all ble devices which advertise "BME280" as complete local name. By default, the application `BLE_Server_BME280` advertise "BME280" as complete local name. If you change the local name advertised by the application `BLE_Sever_BME280` you should reflect your change in this (GATT Client) application by changing the value of the constant `PEER_NAME` in `main.cpp`.
edamame22 0:29983394c6b6 16
edamame22 0:29983394c6b6 17 # mbed Client on mbed OS
edamame22 0:29983394c6b6 18
edamame22 0:29983394c6b6 19 The mbed Client working on mbed OS demonstrates how to register a device with mbed Device Connector, how to read a device/resource and how to deregister. If you are unfamiliar with mbed Device Connector, it is recommended that you read [the introduction to the data model](https://docs.mbed.com/docs/mbed-device-connector-web-interfaces/en/latest/#the-mbed-device-connector-data-model) first.
edamame22 0:29983394c6b6 20
edamame22 0:29983394c6b6 21 The application:
edamame22 0:29983394c6b6 22
edamame22 0:29983394c6b6 23 * Connects to network via Ethernet connection.
edamame22 0:29983394c6b6 24 * Registers with mbed Device Connector.
edamame22 0:29983394c6b6 25 * Gives mbed Device Connector access to its resources (read only).
edamame22 0:29983394c6b6 26 * Reads value from the sensor (BME280) & display the same using HTTP GET request.
edamame22 0:29983394c6b6 27
edamame22 0:29983394c6b6 28 ## Required hardware
edamame22 0:29983394c6b6 29
edamame22 0:29983394c6b6 30 * K64F target platform .
edamame22 0:29983394c6b6 31 * ST BLE shield [X-NUCLEO-IDB05A1](http://www.st.com/ja/ecosystems/x-nucleo-idb05a1.html) running v7.2 or later firmware. [The latest firmware is at here]( https://developer.mbed.org/teams/ST/code/BlueNRG-MS-Stack-Updater/)
edamame22 0:29983394c6b6 32 * 1x micro-USB cable.
edamame22 0:29983394c6b6 33 * Ethernet cable and connection to the internet.
edamame22 0:29983394c6b6 34
edamame22 0:29983394c6b6 35 ## Required hardware patch on [X-NUCLEO-IDB05A1](http://www.st.com/ja/ecosystems/x-nucleo-idb05a1.html)
edamame22 0:29983394c6b6 36 Following hardware patch, which let it becomes fully Arduino compatible, is required to work with K64F. it is called "D13 patch" for enabling pin D13 to drive SPI clock. please read further [details at here](https://developer.mbed.org/teams/ST/code/X_NUCLEO_IDB0XA1/)
edamame22 0:29983394c6b6 37
edamame22 0:29983394c6b6 38 * Removing zero resistor R4
edamame22 0:29983394c6b6 39 * Soldering zero resistor R6
edamame22 0:29983394c6b6 40
edamame22 0:29983394c6b6 41 ## Tested Target hardware configurations
edamame22 0:29983394c6b6 42
edamame22 0:29983394c6b6 43 * K64F (Ethernet)
edamame22 0:29983394c6b6 44
edamame22 0:29983394c6b6 45 ## Requirements for non-K64F boards
edamame22 0:29983394c6b6 46
edamame22 0:29983394c6b6 47 * This example requires TLS functionality to be enabled on mbed TLS.
edamame22 0:29983394c6b6 48 On devices where hardware entropy is not present, TLS is disabled by default.
edamame22 0:29983394c6b6 49 This would result in compile time failures or linking failures.
edamame22 0:29983394c6b6 50
edamame22 0:29983394c6b6 51 To learn why entropy is required, read the
edamame22 0:29983394c6b6 52 [TLS Porting guide](https://docs.mbed.com/docs/mbed-os-handbook/en/5.2/advanced/tls_porting/).
edamame22 0:29983394c6b6 53
edamame22 0:29983394c6b6 54 ## Required software
edamame22 0:29983394c6b6 55
edamame22 0:29983394c6b6 56 * [ARM mbed account](https://developer.mbed.org/account/login/?next=/).
edamame22 0:29983394c6b6 57 * [mbed-cli](https://github.com/ARMmbed/mbed-cli) - to build the example programs. To learn how to build mbed OS applications with mbed-cli, see [the user guide](https://github.com/ARMmbed/mbed-cli/blob/master/README.md).
edamame22 0:29983394c6b6 58 * [Serial port monitor](https://developer.mbed.org/handbook/SerialPC#host-interface-and-terminal-applications).
edamame22 0:29983394c6b6 59 * [mbed Device Connector account](https://connector.mbed.com/)
edamame22 0:29983394c6b6 60
edamame22 0:29983394c6b6 61 ## Application setup
edamame22 0:29983394c6b6 62
edamame22 0:29983394c6b6 63 To configure the example application:
edamame22 0:29983394c6b6 64
edamame22 0:29983394c6b6 65 1. [Select the connection type](#connection-type).
edamame22 0:29983394c6b6 66 1. [Set the client credentials](#client-credentials).
edamame22 0:29983394c6b6 67 1. [Change Ethernet settings](#ethernet-settings).
edamame22 0:29983394c6b6 68 1. [Set up an IP address](#ip-address-setup). This step is optional.
edamame22 0:29983394c6b6 69 1. [Change the socket type](#changing-socket-type). This step is optional.
edamame22 0:29983394c6b6 70
edamame22 0:29983394c6b6 71 ### Connection type
edamame22 0:29983394c6b6 72
edamame22 0:29983394c6b6 73 The application uses Ethernet as the default connection type. To change the connection type, set one of them in `mbed_app.json`. For example to select ETHERNET.
edamame22 0:29983394c6b6 74
edamame22 0:29983394c6b6 75 ```json
edamame22 0:29983394c6b6 76 "network-interface": {
edamame22 0:29983394c6b6 77 "help": "options are ETHERNET,WIFI,MESH_LOWPAN_ND,MESH_THREAD.",
edamame22 0:29983394c6b6 78 "value": "ETHERNET"
edamame22 0:29983394c6b6 79 }
edamame22 0:29983394c6b6 80 ```
edamame22 0:29983394c6b6 81
edamame22 0:29983394c6b6 82 ### Client credentials
edamame22 0:29983394c6b6 83
edamame22 0:29983394c6b6 84 To register the application with the Connector service, you need to create and set the client side certificate.
edamame22 0:29983394c6b6 85
edamame22 0:29983394c6b6 86 1. Go to [mbed Device Connector](https://connector.mbed.com) and log in with your mbed account.
edamame22 0:29983394c6b6 87 1. On mbed Device Connector, go to [My Devices > Security credentials](https://connector.mbed.com/#credentials) and click the **Get my device security credentials** to get new credentials for your device.
edamame22 0:29983394c6b6 88 1. Replace the contents in the `security.h` file of this project's directory with the content copied above.
edamame22 0:29983394c6b6 89
edamame22 0:29983394c6b6 90 ### Ethernet settings
edamame22 0:29983394c6b6 91
edamame22 0:29983394c6b6 92 For running the example application using Ethernet, you need:
edamame22 0:29983394c6b6 93
edamame22 0:29983394c6b6 94 - An Ethernet cable.
edamame22 0:29983394c6b6 95 - An Ethernet connection to the internet.
edamame22 0:29983394c6b6 96
edamame22 0:29983394c6b6 97 ### IP address setup
edamame22 0:29983394c6b6 98
edamame22 0:29983394c6b6 99 This example uses IPv4 to communicate with the [mbed Device Connector Server](https://api.connector.mbed.com). The example program should automatically get an IPv4 address from the router when connected over Ethernet.
edamame22 0:29983394c6b6 100
edamame22 0:29983394c6b6 101 If your network does not have DHCP enabled, you have to manually assign a static IP address to the board. We recommend having DHCP enabled to make everything run smoothly.
edamame22 0:29983394c6b6 102
edamame22 0:29983394c6b6 103 ### Changing socket type
edamame22 0:29983394c6b6 104
edamame22 0:29983394c6b6 105 Your device can connect to mbed Device Connector via UDP or TCP binding mode. The default is UDP.
edamame22 0:29983394c6b6 106
edamame22 0:29983394c6b6 107 To change the binding mode:
edamame22 0:29983394c6b6 108
edamame22 0:29983394c6b6 109 1. In the `simpleclient.h` file, find the parameter `SOCKET_MODE`. The default is `M2MInterface::UDP`.
edamame22 0:29983394c6b6 110 1. To switch to TCP, change it to `M2MInterface::TCP`.
edamame22 0:29983394c6b6 111 1. Rebuild and flash the application.
edamame22 0:29983394c6b6 112
edamame22 0:29983394c6b6 113 <span class="tips">**Tip:** The instructions in this document remain the same, irrespective of the socket mode you select.</span>
edamame22 0:29983394c6b6 114
edamame22 0:29983394c6b6 115 ## Building and running the example
edamame22 0:29983394c6b6 116
edamame22 0:29983394c6b6 117 To build the example using mbed CLI:
edamame22 0:29983394c6b6 118
edamame22 0:29983394c6b6 119 1. Open a command line tool and navigate to the project’s directory.
edamame22 0:29983394c6b6 120
edamame22 0:29983394c6b6 121 2. Clone this repo.
edamame22 0:29983394c6b6 122
edamame22 0:29983394c6b6 123 3. [Configure](#application-setup) the client application.
edamame22 0:29983394c6b6 124
edamame22 0:29983394c6b6 125 4. To build the application, select the hardware board and build the toolchain using the command:
edamame22 0:29983394c6b6 126
edamame22 0:29983394c6b6 127 ```
edamame22 0:29983394c6b6 128 mbed compile -m K64F -t ARM -c
edamame22 0:29983394c6b6 129 ```
edamame22 0:29983394c6b6 130
edamame22 0:29983394c6b6 131 mbed CLI builds a binary file under the project’s `BUILD/` directory.
edamame22 0:29983394c6b6 132
edamame22 0:29983394c6b6 133 5. Plug the Ethernet cable into the board if you are using Ethernet mode.
edamame22 0:29983394c6b6 134
edamame22 0:29983394c6b6 135 6. Plug the micro-USB cable into the board. The board is listed as a mass-storage device.
edamame22 0:29983394c6b6 136
edamame22 0:29983394c6b6 137 7. Drag the binary `BUILD/K64F/ARM/*.bin` to the board to flash the application.
edamame22 0:29983394c6b6 138
edamame22 0:29983394c6b6 139 8. The board is automatically programmed with the new binary. A flashing LED on it indicates that it is still working. When the LED stops blinking, the board is ready to work.
edamame22 0:29983394c6b6 140
edamame22 0:29983394c6b6 141 9. Build and install ``BLE_Server_BME280`` according to [instructions at here](https://github.com/soramame21/BLE_Server_BME280).
edamame22 0:29983394c6b6 142
edamame22 0:29983394c6b6 143 10. Press the **Reset** button on K64F board flashed at Step 8 to run the program ``BLEClient_mbedDevConn``.
edamame22 0:29983394c6b6 144
edamame22 0:29983394c6b6 145 11. For verification, continue to the [Monitoring the application](#monitoring-the-application) chapter.
edamame22 0:29983394c6b6 146
edamame22 0:29983394c6b6 147 ## Monitoring the application
edamame22 0:29983394c6b6 148
edamame22 0:29983394c6b6 149 You need a terminal program to listen to the output through a serial port. You can download one, for example:
edamame22 0:29983394c6b6 150
edamame22 0:29983394c6b6 151 * Tera Term / PuTTY for Windows.
edamame22 0:29983394c6b6 152 * CoolTerm for Mac OS X.
edamame22 0:29983394c6b6 153 * GNU Screen for Linux.
edamame22 0:29983394c6b6 154
edamame22 0:29983394c6b6 155 To see the application's output:
edamame22 0:29983394c6b6 156
edamame22 0:29983394c6b6 157 1. Check which serial port your device is connected to.
edamame22 0:29983394c6b6 158 1. Run a terminal program with the correct serial port and set the baud rate to 9600. For example, to use GNU Screen, run: ``screen /dev/tty.usbmodem1412 9600``.
edamame22 0:29983394c6b6 159 1. The application should start printing the measured value to the terminal.
edamame22 0:29983394c6b6 160
edamame22 0:29983394c6b6 161 **Note:** ``BLEClient_mbedDevConn`` will not run properly if the ``BLE_Server_BME280`` application is not running on a second device. The terminal will show a few print statements, but you will not be able to see received data.
edamame22 0:29983394c6b6 162
edamame22 0:29983394c6b6 163
edamame22 0:29983394c6b6 164 After connecting, you should see messages about connecting to mbed Device Connector:
edamame22 0:29983394c6b6 165
edamame22 0:29983394c6b6 166 ```
edamame22 0:29983394c6b6 167 Connected to Network successfully
edamame22 0:29983394c6b6 168 IP address 10.128.4.46
edamame22 0:29983394c6b6 169
edamame22 0:29983394c6b6 170 SOCKET_MODE : TCP
edamame22 0:29983394c6b6 171 Connecting to coap://api.connector.mbed.com:5684
edamame22 0:29983394c6b6 172 created bme280 instance now!!
edamame22 0:29983394c6b6 173 I'm inside BLE thread_init.....
edamame22 0:29983394c6b6 174 I'm inside BLE init Complete
edamame22 0:29983394c6b6 175 BLE Error startScan = 0
edamame22 0:29983394c6b6 176 inside main for client
edamame22 0:29983394c6b6 177
edamame22 0:29983394c6b6 178 Registered object successfully!
edamame22 0:29983394c6b6 179 adv peerAddr[df 28 cb 9b 5a b8] rssi -60, isScanResponse 0, AdvertisementType 0
edamame22 0:29983394c6b6 180 Connected to BME280 now...
edamame22 0:29983394c6b6 181 ble.gattClient().launchServiceDiscovery = 0
edamame22 0:29983394c6b6 182 S type short UUID-181a attrs[12 255]
edamame22 0:29983394c6b6 183 C UUID-2a6f valueAttr[14] props[0]
edamame22 0:29983394c6b6 184 is_active[HUMIDITY] = true
edamame22 0:29983394c6b6 185 C UUID-2a6d valueAttr[16] props[0]
edamame22 0:29983394c6b6 186 is_active[PRESSURE] = true
edamame22 0:29983394c6b6 187 C UUID-2a6e valueAttr[18] props[0]
edamame22 0:29983394c6b6 188 is_active[TEMPERATURE] = true
edamame22 0:29983394c6b6 189 terminated SD for handle 2049
edamame22 0:29983394c6b6 190 Humidity = 99.50%
edamame22 0:29983394c6b6 191 Pressure = 995.0 hPa
edamame22 0:29983394c6b6 192 inside main for client
edamame22 0:29983394c6b6 193 Temperature = 25.69 degC
edamame22 0:29983394c6b6 194 Humidity = 36.00%
edamame22 0:29983394c6b6 195 Pressure = 995.0 hPa
edamame22 0:29983394c6b6 196 Temperature = 25.70 degC
edamame22 0:29983394c6b6 197 ```
edamame22 0:29983394c6b6 198 <span class="notes">**Note:** Device name is the endpoint name you will need later on when [testing the application](https://github.com/ARMmbed/mbed-os-example-client#testing-the-application).</span>
edamame22 0:29983394c6b6 199
edamame22 0:29983394c6b6 200 ## Testing the application
edamame22 0:29983394c6b6 201
edamame22 0:29983394c6b6 202 Please refer the [details at here](/docs/testing.md)
edamame22 0:29983394c6b6 203
edamame22 0:29983394c6b6 204
edamame22 0:29983394c6b6 205 ## Overview
edamame22 0:29983394c6b6 206 ![Overview of Demo](./docs/img/myImageBME280_Client.png)
edamame22 0:29983394c6b6 207
edamame22 0:29983394c6b6 208
edamame22 0:29983394c6b6 209
edamame22 0:29983394c6b6 210 Please find the BLE GATT Server application under https://github.com/soramame21/BLE_Server_BME280 repo.