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 /* ESP8266 implementation of NetworkInterfaceAPI
edamame22 0:29983394c6b6 2 * Copyright (c) 2015 ARM Limited
edamame22 0:29983394c6b6 3 *
edamame22 0:29983394c6b6 4 * Licensed under the Apache License, Version 2.0 (the "License");
edamame22 0:29983394c6b6 5 * you may not use this file except in compliance with the License.
edamame22 0:29983394c6b6 6 * You may obtain a copy of the License at
edamame22 0:29983394c6b6 7 *
edamame22 0:29983394c6b6 8 * http://www.apache.org/licenses/LICENSE-2.0
edamame22 0:29983394c6b6 9 *
edamame22 0:29983394c6b6 10 * Unless required by applicable law or agreed to in writing, software
edamame22 0:29983394c6b6 11 * distributed under the License is distributed on an "AS IS" BASIS,
edamame22 0:29983394c6b6 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
edamame22 0:29983394c6b6 13 * See the License for the specific language governing permissions and
edamame22 0:29983394c6b6 14 * limitations under the License.
edamame22 0:29983394c6b6 15 */
edamame22 0:29983394c6b6 16
edamame22 0:29983394c6b6 17 #ifndef ESP8266_INTERFACE_H
edamame22 0:29983394c6b6 18 #define ESP8266_INTERFACE_H
edamame22 0:29983394c6b6 19
edamame22 0:29983394c6b6 20 #include "mbed.h"
edamame22 0:29983394c6b6 21 #include "ESP8266.h"
edamame22 0:29983394c6b6 22
edamame22 0:29983394c6b6 23
edamame22 0:29983394c6b6 24 #define ESP8266_SOCKET_COUNT 5
edamame22 0:29983394c6b6 25
edamame22 0:29983394c6b6 26 /** ESP8266Interface class
edamame22 0:29983394c6b6 27 * Implementation of the NetworkStack for the ESP8266
edamame22 0:29983394c6b6 28 */
edamame22 0:29983394c6b6 29 class ESP8266Interface : public NetworkStack, public WiFiInterface
edamame22 0:29983394c6b6 30 {
edamame22 0:29983394c6b6 31 public:
edamame22 0:29983394c6b6 32 /** ESP8266Interface lifetime
edamame22 0:29983394c6b6 33 * @param tx TX pin
edamame22 0:29983394c6b6 34 * @param rx RX pin
edamame22 0:29983394c6b6 35 * @param debug Enable debugging
edamame22 0:29983394c6b6 36 */
edamame22 0:29983394c6b6 37 ESP8266Interface(PinName tx, PinName rx, bool debug = false);
edamame22 0:29983394c6b6 38
edamame22 0:29983394c6b6 39 /** Start the interface
edamame22 0:29983394c6b6 40 *
edamame22 0:29983394c6b6 41 * Attempts to connect to a WiFi network. Requires ssid and passphrase to be set.
edamame22 0:29983394c6b6 42 * If passphrase is invalid, NSAPI_ERROR_AUTH_ERROR is returned.
edamame22 0:29983394c6b6 43 *
edamame22 0:29983394c6b6 44 * @return 0 on success, negative error code on failure
edamame22 0:29983394c6b6 45 */
edamame22 0:29983394c6b6 46 virtual int connect();
edamame22 0:29983394c6b6 47
edamame22 0:29983394c6b6 48 /** Start the interface
edamame22 0:29983394c6b6 49 *
edamame22 0:29983394c6b6 50 * Attempts to connect to a WiFi network.
edamame22 0:29983394c6b6 51 *
edamame22 0:29983394c6b6 52 * @param ssid Name of the network to connect to
edamame22 0:29983394c6b6 53 * @param pass Security passphrase to connect to the network
edamame22 0:29983394c6b6 54 * @param security Type of encryption for connection (Default: NSAPI_SECURITY_NONE)
edamame22 0:29983394c6b6 55 * @param channel This parameter is not supported, setting it to anything else than 0 will result in NSAPI_ERROR_UNSUPPORTED
edamame22 0:29983394c6b6 56 * @return 0 on success, or error code on failure
edamame22 0:29983394c6b6 57 */
edamame22 0:29983394c6b6 58 virtual int connect(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE,
edamame22 0:29983394c6b6 59 uint8_t channel = 0);
edamame22 0:29983394c6b6 60
edamame22 0:29983394c6b6 61 /** Set the WiFi network credentials
edamame22 0:29983394c6b6 62 *
edamame22 0:29983394c6b6 63 * @param ssid Name of the network to connect to
edamame22 0:29983394c6b6 64 * @param pass Security passphrase to connect to the network
edamame22 0:29983394c6b6 65 * @param security Type of encryption for connection
edamame22 0:29983394c6b6 66 * (defaults to NSAPI_SECURITY_NONE)
edamame22 0:29983394c6b6 67 * @return 0 on success, or error code on failure
edamame22 0:29983394c6b6 68 */
edamame22 0:29983394c6b6 69 virtual int set_credentials(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE);
edamame22 0:29983394c6b6 70
edamame22 0:29983394c6b6 71 /** Set the WiFi network channel - NOT SUPPORTED
edamame22 0:29983394c6b6 72 *
edamame22 0:29983394c6b6 73 * This function is not supported and will return NSAPI_ERROR_UNSUPPORTED
edamame22 0:29983394c6b6 74 *
edamame22 0:29983394c6b6 75 * @param channel Channel on which the connection is to be made, or 0 for any (Default: 0)
edamame22 0:29983394c6b6 76 * @return Not supported, returns NSAPI_ERROR_UNSUPPORTED
edamame22 0:29983394c6b6 77 */
edamame22 0:29983394c6b6 78 virtual int set_channel(uint8_t channel);
edamame22 0:29983394c6b6 79
edamame22 0:29983394c6b6 80 /** Stop the interface
edamame22 0:29983394c6b6 81 * @return 0 on success, negative on failure
edamame22 0:29983394c6b6 82 */
edamame22 0:29983394c6b6 83 virtual int disconnect();
edamame22 0:29983394c6b6 84
edamame22 0:29983394c6b6 85 /** Get the internally stored IP address
edamame22 0:29983394c6b6 86 * @return IP address of the interface or null if not yet connected
edamame22 0:29983394c6b6 87 */
edamame22 0:29983394c6b6 88 virtual const char *get_ip_address();
edamame22 0:29983394c6b6 89
edamame22 0:29983394c6b6 90 /** Get the internally stored MAC address
edamame22 0:29983394c6b6 91 * @return MAC address of the interface
edamame22 0:29983394c6b6 92 */
edamame22 0:29983394c6b6 93 virtual const char *get_mac_address();
edamame22 0:29983394c6b6 94
edamame22 0:29983394c6b6 95 /** Get the local gateway
edamame22 0:29983394c6b6 96 *
edamame22 0:29983394c6b6 97 * @return Null-terminated representation of the local gateway
edamame22 0:29983394c6b6 98 * or null if no network mask has been recieved
edamame22 0:29983394c6b6 99 */
edamame22 0:29983394c6b6 100 virtual const char *get_gateway();
edamame22 0:29983394c6b6 101
edamame22 0:29983394c6b6 102 /** Get the local network mask
edamame22 0:29983394c6b6 103 *
edamame22 0:29983394c6b6 104 * @return Null-terminated representation of the local network mask
edamame22 0:29983394c6b6 105 * or null if no network mask has been recieved
edamame22 0:29983394c6b6 106 */
edamame22 0:29983394c6b6 107 virtual const char *get_netmask();
edamame22 0:29983394c6b6 108
edamame22 0:29983394c6b6 109 /** Gets the current radio signal strength for active connection
edamame22 0:29983394c6b6 110 *
edamame22 0:29983394c6b6 111 * @return Connection strength in dBm (negative value)
edamame22 0:29983394c6b6 112 */
edamame22 0:29983394c6b6 113 virtual int8_t get_rssi();
edamame22 0:29983394c6b6 114
edamame22 0:29983394c6b6 115 /** Scan for available networks
edamame22 0:29983394c6b6 116 *
edamame22 0:29983394c6b6 117 * This function will block.
edamame22 0:29983394c6b6 118 *
edamame22 0:29983394c6b6 119 * @param ap Pointer to allocated array to store discovered AP
edamame22 0:29983394c6b6 120 * @param count Size of allocated @a res array, or 0 to only count available AP
edamame22 0:29983394c6b6 121 * @param timeout Timeout in milliseconds; 0 for no timeout (Default: 0)
edamame22 0:29983394c6b6 122 * @return Number of entries in @a, or if @a count was 0 number of available networks, negative on error
edamame22 0:29983394c6b6 123 * see @a nsapi_error
edamame22 0:29983394c6b6 124 */
edamame22 0:29983394c6b6 125 virtual int scan(WiFiAccessPoint *res, unsigned count);
edamame22 0:29983394c6b6 126
edamame22 0:29983394c6b6 127 protected:
edamame22 0:29983394c6b6 128 /** Open a socket
edamame22 0:29983394c6b6 129 * @param handle Handle in which to store new socket
edamame22 0:29983394c6b6 130 * @param proto Type of socket to open, NSAPI_TCP or NSAPI_UDP
edamame22 0:29983394c6b6 131 * @return 0 on success, negative on failure
edamame22 0:29983394c6b6 132 */
edamame22 0:29983394c6b6 133 virtual int socket_open(void **handle, nsapi_protocol_t proto);
edamame22 0:29983394c6b6 134
edamame22 0:29983394c6b6 135 /** Close the socket
edamame22 0:29983394c6b6 136 * @param handle Socket handle
edamame22 0:29983394c6b6 137 * @return 0 on success, negative on failure
edamame22 0:29983394c6b6 138 * @note On failure, any memory associated with the socket must still
edamame22 0:29983394c6b6 139 * be cleaned up
edamame22 0:29983394c6b6 140 */
edamame22 0:29983394c6b6 141 virtual int socket_close(void *handle);
edamame22 0:29983394c6b6 142
edamame22 0:29983394c6b6 143 /** Bind a server socket to a specific port
edamame22 0:29983394c6b6 144 * @param handle Socket handle
edamame22 0:29983394c6b6 145 * @param address Local address to listen for incoming connections on
edamame22 0:29983394c6b6 146 * @return 0 on success, negative on failure.
edamame22 0:29983394c6b6 147 */
edamame22 0:29983394c6b6 148 virtual int socket_bind(void *handle, const SocketAddress &address);
edamame22 0:29983394c6b6 149
edamame22 0:29983394c6b6 150 /** Start listening for incoming connections
edamame22 0:29983394c6b6 151 * @param handle Socket handle
edamame22 0:29983394c6b6 152 * @param backlog Number of pending connections that can be queued up at any
edamame22 0:29983394c6b6 153 * one time [Default: 1]
edamame22 0:29983394c6b6 154 * @return 0 on success, negative on failure
edamame22 0:29983394c6b6 155 */
edamame22 0:29983394c6b6 156 virtual int socket_listen(void *handle, int backlog);
edamame22 0:29983394c6b6 157
edamame22 0:29983394c6b6 158 /** Connects this TCP socket to the server
edamame22 0:29983394c6b6 159 * @param handle Socket handle
edamame22 0:29983394c6b6 160 * @param address SocketAddress to connect to
edamame22 0:29983394c6b6 161 * @return 0 on success, negative on failure
edamame22 0:29983394c6b6 162 */
edamame22 0:29983394c6b6 163 virtual int socket_connect(void *handle, const SocketAddress &address);
edamame22 0:29983394c6b6 164
edamame22 0:29983394c6b6 165 /** Accept a new connection.
edamame22 0:29983394c6b6 166 * @param handle Handle in which to store new socket
edamame22 0:29983394c6b6 167 * @param server Socket handle to server to accept from
edamame22 0:29983394c6b6 168 * @return 0 on success, negative on failure
edamame22 0:29983394c6b6 169 * @note This call is not-blocking, if this call would block, must
edamame22 0:29983394c6b6 170 * immediately return NSAPI_ERROR_WOULD_WAIT
edamame22 0:29983394c6b6 171 */
edamame22 0:29983394c6b6 172 virtual int socket_accept(void *handle, void **socket, SocketAddress *address);
edamame22 0:29983394c6b6 173
edamame22 0:29983394c6b6 174 /** Send data to the remote host
edamame22 0:29983394c6b6 175 * @param handle Socket handle
edamame22 0:29983394c6b6 176 * @param data The buffer to send to the host
edamame22 0:29983394c6b6 177 * @param size The length of the buffer to send
edamame22 0:29983394c6b6 178 * @return Number of written bytes on success, negative on failure
edamame22 0:29983394c6b6 179 * @note This call is not-blocking, if this call would block, must
edamame22 0:29983394c6b6 180 * immediately return NSAPI_ERROR_WOULD_WAIT
edamame22 0:29983394c6b6 181 */
edamame22 0:29983394c6b6 182 virtual int socket_send(void *handle, const void *data, unsigned size);
edamame22 0:29983394c6b6 183
edamame22 0:29983394c6b6 184 /** Receive data from the remote host
edamame22 0:29983394c6b6 185 * @param handle Socket handle
edamame22 0:29983394c6b6 186 * @param data The buffer in which to store the data received from the host
edamame22 0:29983394c6b6 187 * @param size The maximum length of the buffer
edamame22 0:29983394c6b6 188 * @return Number of received bytes on success, negative on failure
edamame22 0:29983394c6b6 189 * @note This call is not-blocking, if this call would block, must
edamame22 0:29983394c6b6 190 * immediately return NSAPI_ERROR_WOULD_WAIT
edamame22 0:29983394c6b6 191 */
edamame22 0:29983394c6b6 192 virtual int socket_recv(void *handle, void *data, unsigned size);
edamame22 0:29983394c6b6 193
edamame22 0:29983394c6b6 194 /** Send a packet to a remote endpoint
edamame22 0:29983394c6b6 195 * @param handle Socket handle
edamame22 0:29983394c6b6 196 * @param address The remote SocketAddress
edamame22 0:29983394c6b6 197 * @param data The packet to be sent
edamame22 0:29983394c6b6 198 * @param size The length of the packet to be sent
edamame22 0:29983394c6b6 199 * @return The number of written bytes on success, negative on failure
edamame22 0:29983394c6b6 200 * @note This call is not-blocking, if this call would block, must
edamame22 0:29983394c6b6 201 * immediately return NSAPI_ERROR_WOULD_WAIT
edamame22 0:29983394c6b6 202 */
edamame22 0:29983394c6b6 203 virtual int socket_sendto(void *handle, const SocketAddress &address, const void *data, unsigned size);
edamame22 0:29983394c6b6 204
edamame22 0:29983394c6b6 205 /** Receive a packet from a remote endpoint
edamame22 0:29983394c6b6 206 * @param handle Socket handle
edamame22 0:29983394c6b6 207 * @param address Destination for the remote SocketAddress or null
edamame22 0:29983394c6b6 208 * @param buffer The buffer for storing the incoming packet data
edamame22 0:29983394c6b6 209 * If a packet is too long to fit in the supplied buffer,
edamame22 0:29983394c6b6 210 * excess bytes are discarded
edamame22 0:29983394c6b6 211 * @param size The length of the buffer
edamame22 0:29983394c6b6 212 * @return The number of received bytes on success, negative on failure
edamame22 0:29983394c6b6 213 * @note This call is not-blocking, if this call would block, must
edamame22 0:29983394c6b6 214 * immediately return NSAPI_ERROR_WOULD_WAIT
edamame22 0:29983394c6b6 215 */
edamame22 0:29983394c6b6 216 virtual int socket_recvfrom(void *handle, SocketAddress *address, void *buffer, unsigned size);
edamame22 0:29983394c6b6 217
edamame22 0:29983394c6b6 218 /** Register a callback on state change of the socket
edamame22 0:29983394c6b6 219 * @param handle Socket handle
edamame22 0:29983394c6b6 220 * @param callback Function to call on state change
edamame22 0:29983394c6b6 221 * @param data Argument to pass to callback
edamame22 0:29983394c6b6 222 * @note Callback may be called in an interrupt context.
edamame22 0:29983394c6b6 223 */
edamame22 0:29983394c6b6 224 virtual void socket_attach(void *handle, void (*callback)(void *), void *data);
edamame22 0:29983394c6b6 225
edamame22 0:29983394c6b6 226 /** Provide access to the NetworkStack object
edamame22 0:29983394c6b6 227 *
edamame22 0:29983394c6b6 228 * @return The underlying NetworkStack object
edamame22 0:29983394c6b6 229 */
edamame22 0:29983394c6b6 230 virtual NetworkStack *get_stack()
edamame22 0:29983394c6b6 231 {
edamame22 0:29983394c6b6 232 return this;
edamame22 0:29983394c6b6 233 }
edamame22 0:29983394c6b6 234
edamame22 0:29983394c6b6 235 private:
edamame22 0:29983394c6b6 236 ESP8266 _esp;
edamame22 0:29983394c6b6 237 bool _ids[ESP8266_SOCKET_COUNT];
edamame22 0:29983394c6b6 238
edamame22 0:29983394c6b6 239 char ap_ssid[33]; /* 32 is what 802.11 defines as longest possible name; +1 for the \0 */
edamame22 0:29983394c6b6 240 nsapi_security_t ap_sec;
edamame22 0:29983394c6b6 241 uint8_t ap_ch;
edamame22 0:29983394c6b6 242 char ap_pass[64]; /* The longest allowed passphrase */
edamame22 0:29983394c6b6 243
edamame22 0:29983394c6b6 244 void event();
edamame22 0:29983394c6b6 245
edamame22 0:29983394c6b6 246 struct {
edamame22 0:29983394c6b6 247 void (*callback)(void *);
edamame22 0:29983394c6b6 248 void *data;
edamame22 0:29983394c6b6 249 } _cbs[ESP8266_SOCKET_COUNT];
edamame22 0:29983394c6b6 250 };
edamame22 0:29983394c6b6 251
edamame22 0:29983394c6b6 252 #endif