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/
esp8266-driver/ESP8266Interface.cpp@2:b894b3508057, 2017-09-05 (annotated)
- 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?
User | Revision | Line number | New 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 | #include <string.h> |
edamame22 | 0:29983394c6b6 | 18 | #include "ESP8266Interface.h" |
edamame22 | 0:29983394c6b6 | 19 | |
edamame22 | 0:29983394c6b6 | 20 | // Various timeouts for different ESP8266 operations |
edamame22 | 0:29983394c6b6 | 21 | #define ESP8266_CONNECT_TIMEOUT 15000 |
edamame22 | 0:29983394c6b6 | 22 | #define ESP8266_SEND_TIMEOUT 500 |
edamame22 | 0:29983394c6b6 | 23 | #define ESP8266_RECV_TIMEOUT 0 |
edamame22 | 0:29983394c6b6 | 24 | #define ESP8266_MISC_TIMEOUT 500 |
edamame22 | 0:29983394c6b6 | 25 | |
edamame22 | 0:29983394c6b6 | 26 | // ESP8266Interface implementation |
edamame22 | 0:29983394c6b6 | 27 | ESP8266Interface::ESP8266Interface(PinName tx, PinName rx, bool debug) |
edamame22 | 0:29983394c6b6 | 28 | : _esp(tx, rx, debug) |
edamame22 | 0:29983394c6b6 | 29 | { |
edamame22 | 0:29983394c6b6 | 30 | memset(_ids, 0, sizeof(_ids)); |
edamame22 | 0:29983394c6b6 | 31 | memset(_cbs, 0, sizeof(_cbs)); |
edamame22 | 0:29983394c6b6 | 32 | |
edamame22 | 0:29983394c6b6 | 33 | _esp.attach(this, &ESP8266Interface::event); |
edamame22 | 0:29983394c6b6 | 34 | } |
edamame22 | 0:29983394c6b6 | 35 | |
edamame22 | 0:29983394c6b6 | 36 | int ESP8266Interface::connect(const char *ssid, const char *pass, nsapi_security_t security, |
edamame22 | 0:29983394c6b6 | 37 | uint8_t channel) |
edamame22 | 0:29983394c6b6 | 38 | { |
edamame22 | 0:29983394c6b6 | 39 | if (channel != 0) { |
edamame22 | 0:29983394c6b6 | 40 | return NSAPI_ERROR_UNSUPPORTED; |
edamame22 | 0:29983394c6b6 | 41 | } |
edamame22 | 0:29983394c6b6 | 42 | |
edamame22 | 0:29983394c6b6 | 43 | set_credentials(ssid, pass, security); |
edamame22 | 0:29983394c6b6 | 44 | return connect(); |
edamame22 | 0:29983394c6b6 | 45 | } |
edamame22 | 0:29983394c6b6 | 46 | |
edamame22 | 0:29983394c6b6 | 47 | int ESP8266Interface::connect() |
edamame22 | 0:29983394c6b6 | 48 | { |
edamame22 | 0:29983394c6b6 | 49 | _esp.setTimeout(ESP8266_CONNECT_TIMEOUT); |
edamame22 | 0:29983394c6b6 | 50 | |
edamame22 | 0:29983394c6b6 | 51 | if (!_esp.startup(3)) { |
edamame22 | 0:29983394c6b6 | 52 | return NSAPI_ERROR_DEVICE_ERROR; |
edamame22 | 0:29983394c6b6 | 53 | } |
edamame22 | 0:29983394c6b6 | 54 | |
edamame22 | 0:29983394c6b6 | 55 | if (!_esp.dhcp(true, 1)) { |
edamame22 | 0:29983394c6b6 | 56 | return NSAPI_ERROR_DHCP_FAILURE; |
edamame22 | 0:29983394c6b6 | 57 | } |
edamame22 | 0:29983394c6b6 | 58 | |
edamame22 | 0:29983394c6b6 | 59 | if (!_esp.connect(ap_ssid, ap_pass)) { |
edamame22 | 0:29983394c6b6 | 60 | return NSAPI_ERROR_NO_CONNECTION; |
edamame22 | 0:29983394c6b6 | 61 | } |
edamame22 | 0:29983394c6b6 | 62 | |
edamame22 | 0:29983394c6b6 | 63 | if (!_esp.getIPAddress()) { |
edamame22 | 0:29983394c6b6 | 64 | return NSAPI_ERROR_DHCP_FAILURE; |
edamame22 | 0:29983394c6b6 | 65 | } |
edamame22 | 0:29983394c6b6 | 66 | |
edamame22 | 0:29983394c6b6 | 67 | return NSAPI_ERROR_OK; |
edamame22 | 0:29983394c6b6 | 68 | } |
edamame22 | 0:29983394c6b6 | 69 | |
edamame22 | 0:29983394c6b6 | 70 | int ESP8266Interface::set_credentials(const char *ssid, const char *pass, nsapi_security_t security) |
edamame22 | 0:29983394c6b6 | 71 | { |
edamame22 | 0:29983394c6b6 | 72 | memset(ap_ssid, 0, sizeof(ap_ssid)); |
edamame22 | 0:29983394c6b6 | 73 | strncpy(ap_ssid, ssid, sizeof(ap_ssid)); |
edamame22 | 0:29983394c6b6 | 74 | |
edamame22 | 0:29983394c6b6 | 75 | memset(ap_pass, 0, sizeof(ap_pass)); |
edamame22 | 0:29983394c6b6 | 76 | strncpy(ap_pass, pass, sizeof(ap_pass)); |
edamame22 | 0:29983394c6b6 | 77 | |
edamame22 | 0:29983394c6b6 | 78 | ap_sec = security; |
edamame22 | 0:29983394c6b6 | 79 | |
edamame22 | 0:29983394c6b6 | 80 | return 0; |
edamame22 | 0:29983394c6b6 | 81 | } |
edamame22 | 0:29983394c6b6 | 82 | |
edamame22 | 0:29983394c6b6 | 83 | int ESP8266Interface::set_channel(uint8_t channel) |
edamame22 | 0:29983394c6b6 | 84 | { |
edamame22 | 0:29983394c6b6 | 85 | return NSAPI_ERROR_UNSUPPORTED; |
edamame22 | 0:29983394c6b6 | 86 | } |
edamame22 | 0:29983394c6b6 | 87 | |
edamame22 | 0:29983394c6b6 | 88 | |
edamame22 | 0:29983394c6b6 | 89 | int ESP8266Interface::disconnect() |
edamame22 | 0:29983394c6b6 | 90 | { |
edamame22 | 0:29983394c6b6 | 91 | _esp.setTimeout(ESP8266_MISC_TIMEOUT); |
edamame22 | 0:29983394c6b6 | 92 | |
edamame22 | 0:29983394c6b6 | 93 | if (!_esp.disconnect()) { |
edamame22 | 0:29983394c6b6 | 94 | return NSAPI_ERROR_DEVICE_ERROR; |
edamame22 | 0:29983394c6b6 | 95 | } |
edamame22 | 0:29983394c6b6 | 96 | |
edamame22 | 0:29983394c6b6 | 97 | return NSAPI_ERROR_OK; |
edamame22 | 0:29983394c6b6 | 98 | } |
edamame22 | 0:29983394c6b6 | 99 | |
edamame22 | 0:29983394c6b6 | 100 | const char *ESP8266Interface::get_ip_address() |
edamame22 | 0:29983394c6b6 | 101 | { |
edamame22 | 0:29983394c6b6 | 102 | return _esp.getIPAddress(); |
edamame22 | 0:29983394c6b6 | 103 | } |
edamame22 | 0:29983394c6b6 | 104 | |
edamame22 | 0:29983394c6b6 | 105 | const char *ESP8266Interface::get_mac_address() |
edamame22 | 0:29983394c6b6 | 106 | { |
edamame22 | 0:29983394c6b6 | 107 | return _esp.getMACAddress(); |
edamame22 | 0:29983394c6b6 | 108 | } |
edamame22 | 0:29983394c6b6 | 109 | |
edamame22 | 0:29983394c6b6 | 110 | const char *ESP8266Interface::get_gateway() |
edamame22 | 0:29983394c6b6 | 111 | { |
edamame22 | 0:29983394c6b6 | 112 | return _esp.getGateway(); |
edamame22 | 0:29983394c6b6 | 113 | } |
edamame22 | 0:29983394c6b6 | 114 | |
edamame22 | 0:29983394c6b6 | 115 | const char *ESP8266Interface::get_netmask() |
edamame22 | 0:29983394c6b6 | 116 | { |
edamame22 | 0:29983394c6b6 | 117 | return _esp.getNetmask(); |
edamame22 | 0:29983394c6b6 | 118 | } |
edamame22 | 0:29983394c6b6 | 119 | |
edamame22 | 0:29983394c6b6 | 120 | int8_t ESP8266Interface::get_rssi() |
edamame22 | 0:29983394c6b6 | 121 | { |
edamame22 | 0:29983394c6b6 | 122 | return _esp.getRSSI(); |
edamame22 | 0:29983394c6b6 | 123 | } |
edamame22 | 0:29983394c6b6 | 124 | |
edamame22 | 0:29983394c6b6 | 125 | int ESP8266Interface::scan(WiFiAccessPoint *res, unsigned count) |
edamame22 | 0:29983394c6b6 | 126 | { |
edamame22 | 0:29983394c6b6 | 127 | return _esp.scan(res, count); |
edamame22 | 0:29983394c6b6 | 128 | } |
edamame22 | 0:29983394c6b6 | 129 | |
edamame22 | 0:29983394c6b6 | 130 | struct esp8266_socket { |
edamame22 | 0:29983394c6b6 | 131 | int id; |
edamame22 | 0:29983394c6b6 | 132 | nsapi_protocol_t proto; |
edamame22 | 0:29983394c6b6 | 133 | bool connected; |
edamame22 | 0:29983394c6b6 | 134 | }; |
edamame22 | 0:29983394c6b6 | 135 | |
edamame22 | 0:29983394c6b6 | 136 | int ESP8266Interface::socket_open(void **handle, nsapi_protocol_t proto) |
edamame22 | 0:29983394c6b6 | 137 | { |
edamame22 | 0:29983394c6b6 | 138 | // Look for an unused socket |
edamame22 | 0:29983394c6b6 | 139 | int id = -1; |
edamame22 | 0:29983394c6b6 | 140 | |
edamame22 | 0:29983394c6b6 | 141 | for (int i = 0; i < ESP8266_SOCKET_COUNT; i++) { |
edamame22 | 0:29983394c6b6 | 142 | if (!_ids[i]) { |
edamame22 | 0:29983394c6b6 | 143 | id = i; |
edamame22 | 0:29983394c6b6 | 144 | _ids[i] = true; |
edamame22 | 0:29983394c6b6 | 145 | break; |
edamame22 | 0:29983394c6b6 | 146 | } |
edamame22 | 0:29983394c6b6 | 147 | } |
edamame22 | 0:29983394c6b6 | 148 | |
edamame22 | 0:29983394c6b6 | 149 | if (id == -1) { |
edamame22 | 0:29983394c6b6 | 150 | return NSAPI_ERROR_NO_SOCKET; |
edamame22 | 0:29983394c6b6 | 151 | } |
edamame22 | 0:29983394c6b6 | 152 | |
edamame22 | 0:29983394c6b6 | 153 | struct esp8266_socket *socket = new struct esp8266_socket; |
edamame22 | 0:29983394c6b6 | 154 | if (!socket) { |
edamame22 | 0:29983394c6b6 | 155 | return NSAPI_ERROR_NO_SOCKET; |
edamame22 | 0:29983394c6b6 | 156 | } |
edamame22 | 0:29983394c6b6 | 157 | |
edamame22 | 0:29983394c6b6 | 158 | socket->id = id; |
edamame22 | 0:29983394c6b6 | 159 | socket->proto = proto; |
edamame22 | 0:29983394c6b6 | 160 | socket->connected = false; |
edamame22 | 0:29983394c6b6 | 161 | *handle = socket; |
edamame22 | 0:29983394c6b6 | 162 | return 0; |
edamame22 | 0:29983394c6b6 | 163 | } |
edamame22 | 0:29983394c6b6 | 164 | |
edamame22 | 0:29983394c6b6 | 165 | int ESP8266Interface::socket_close(void *handle) |
edamame22 | 0:29983394c6b6 | 166 | { |
edamame22 | 0:29983394c6b6 | 167 | struct esp8266_socket *socket = (struct esp8266_socket *)handle; |
edamame22 | 0:29983394c6b6 | 168 | int err = 0; |
edamame22 | 0:29983394c6b6 | 169 | _esp.setTimeout(ESP8266_MISC_TIMEOUT); |
edamame22 | 0:29983394c6b6 | 170 | |
edamame22 | 0:29983394c6b6 | 171 | if (!_esp.close(socket->id)) { |
edamame22 | 0:29983394c6b6 | 172 | err = NSAPI_ERROR_DEVICE_ERROR; |
edamame22 | 0:29983394c6b6 | 173 | } |
edamame22 | 0:29983394c6b6 | 174 | |
edamame22 | 0:29983394c6b6 | 175 | _ids[socket->id] = false; |
edamame22 | 0:29983394c6b6 | 176 | delete socket; |
edamame22 | 0:29983394c6b6 | 177 | return err; |
edamame22 | 0:29983394c6b6 | 178 | } |
edamame22 | 0:29983394c6b6 | 179 | |
edamame22 | 0:29983394c6b6 | 180 | int ESP8266Interface::socket_bind(void *handle, const SocketAddress &address) |
edamame22 | 0:29983394c6b6 | 181 | { |
edamame22 | 0:29983394c6b6 | 182 | return NSAPI_ERROR_UNSUPPORTED; |
edamame22 | 0:29983394c6b6 | 183 | } |
edamame22 | 0:29983394c6b6 | 184 | |
edamame22 | 0:29983394c6b6 | 185 | int ESP8266Interface::socket_listen(void *handle, int backlog) |
edamame22 | 0:29983394c6b6 | 186 | { |
edamame22 | 0:29983394c6b6 | 187 | return NSAPI_ERROR_UNSUPPORTED; |
edamame22 | 0:29983394c6b6 | 188 | } |
edamame22 | 0:29983394c6b6 | 189 | |
edamame22 | 0:29983394c6b6 | 190 | int ESP8266Interface::socket_connect(void *handle, const SocketAddress &addr) |
edamame22 | 0:29983394c6b6 | 191 | { |
edamame22 | 0:29983394c6b6 | 192 | struct esp8266_socket *socket = (struct esp8266_socket *)handle; |
edamame22 | 0:29983394c6b6 | 193 | _esp.setTimeout(ESP8266_MISC_TIMEOUT); |
edamame22 | 0:29983394c6b6 | 194 | |
edamame22 | 0:29983394c6b6 | 195 | const char *proto = (socket->proto == NSAPI_UDP) ? "UDP" : "TCP"; |
edamame22 | 0:29983394c6b6 | 196 | if (!_esp.open(proto, socket->id, addr.get_ip_address(), addr.get_port())) { |
edamame22 | 0:29983394c6b6 | 197 | return NSAPI_ERROR_DEVICE_ERROR; |
edamame22 | 0:29983394c6b6 | 198 | } |
edamame22 | 0:29983394c6b6 | 199 | |
edamame22 | 0:29983394c6b6 | 200 | socket->connected = true; |
edamame22 | 0:29983394c6b6 | 201 | return 0; |
edamame22 | 0:29983394c6b6 | 202 | } |
edamame22 | 0:29983394c6b6 | 203 | |
edamame22 | 0:29983394c6b6 | 204 | int ESP8266Interface::socket_accept(void *server, void **socket, SocketAddress *addr) |
edamame22 | 0:29983394c6b6 | 205 | { |
edamame22 | 0:29983394c6b6 | 206 | return NSAPI_ERROR_UNSUPPORTED; |
edamame22 | 0:29983394c6b6 | 207 | } |
edamame22 | 0:29983394c6b6 | 208 | |
edamame22 | 0:29983394c6b6 | 209 | int ESP8266Interface::socket_send(void *handle, const void *data, unsigned size) |
edamame22 | 0:29983394c6b6 | 210 | { |
edamame22 | 0:29983394c6b6 | 211 | struct esp8266_socket *socket = (struct esp8266_socket *)handle; |
edamame22 | 0:29983394c6b6 | 212 | _esp.setTimeout(ESP8266_SEND_TIMEOUT); |
edamame22 | 0:29983394c6b6 | 213 | |
edamame22 | 0:29983394c6b6 | 214 | if (!_esp.send(socket->id, data, size)) { |
edamame22 | 0:29983394c6b6 | 215 | return NSAPI_ERROR_DEVICE_ERROR; |
edamame22 | 0:29983394c6b6 | 216 | } |
edamame22 | 0:29983394c6b6 | 217 | |
edamame22 | 0:29983394c6b6 | 218 | return size; |
edamame22 | 0:29983394c6b6 | 219 | } |
edamame22 | 0:29983394c6b6 | 220 | |
edamame22 | 0:29983394c6b6 | 221 | int ESP8266Interface::socket_recv(void *handle, void *data, unsigned size) |
edamame22 | 0:29983394c6b6 | 222 | { |
edamame22 | 0:29983394c6b6 | 223 | struct esp8266_socket *socket = (struct esp8266_socket *)handle; |
edamame22 | 0:29983394c6b6 | 224 | _esp.setTimeout(ESP8266_RECV_TIMEOUT); |
edamame22 | 0:29983394c6b6 | 225 | |
edamame22 | 0:29983394c6b6 | 226 | int32_t recv = _esp.recv(socket->id, data, size); |
edamame22 | 0:29983394c6b6 | 227 | if (recv < 0) { |
edamame22 | 0:29983394c6b6 | 228 | return NSAPI_ERROR_WOULD_BLOCK; |
edamame22 | 0:29983394c6b6 | 229 | } |
edamame22 | 0:29983394c6b6 | 230 | |
edamame22 | 0:29983394c6b6 | 231 | return recv; |
edamame22 | 0:29983394c6b6 | 232 | } |
edamame22 | 0:29983394c6b6 | 233 | |
edamame22 | 0:29983394c6b6 | 234 | int ESP8266Interface::socket_sendto(void *handle, const SocketAddress &addr, const void *data, unsigned size) |
edamame22 | 0:29983394c6b6 | 235 | { |
edamame22 | 0:29983394c6b6 | 236 | struct esp8266_socket *socket = (struct esp8266_socket *)handle; |
edamame22 | 0:29983394c6b6 | 237 | if (!socket->connected) { |
edamame22 | 0:29983394c6b6 | 238 | int err = socket_connect(socket, addr); |
edamame22 | 0:29983394c6b6 | 239 | if (err < 0) { |
edamame22 | 0:29983394c6b6 | 240 | return err; |
edamame22 | 0:29983394c6b6 | 241 | } |
edamame22 | 0:29983394c6b6 | 242 | } |
edamame22 | 0:29983394c6b6 | 243 | |
edamame22 | 0:29983394c6b6 | 244 | return socket_send(socket, data, size); |
edamame22 | 0:29983394c6b6 | 245 | } |
edamame22 | 0:29983394c6b6 | 246 | |
edamame22 | 0:29983394c6b6 | 247 | int ESP8266Interface::socket_recvfrom(void *handle, SocketAddress *addr, void *data, unsigned size) |
edamame22 | 0:29983394c6b6 | 248 | { |
edamame22 | 0:29983394c6b6 | 249 | struct esp8266_socket *socket = (struct esp8266_socket *)handle; |
edamame22 | 0:29983394c6b6 | 250 | return socket_recv(socket, data, size); |
edamame22 | 0:29983394c6b6 | 251 | } |
edamame22 | 0:29983394c6b6 | 252 | |
edamame22 | 0:29983394c6b6 | 253 | void ESP8266Interface::socket_attach(void *handle, void (*callback)(void *), void *data) |
edamame22 | 0:29983394c6b6 | 254 | { |
edamame22 | 0:29983394c6b6 | 255 | struct esp8266_socket *socket = (struct esp8266_socket *)handle; |
edamame22 | 0:29983394c6b6 | 256 | _cbs[socket->id].callback = callback; |
edamame22 | 0:29983394c6b6 | 257 | _cbs[socket->id].data = data; |
edamame22 | 0:29983394c6b6 | 258 | } |
edamame22 | 0:29983394c6b6 | 259 | |
edamame22 | 0:29983394c6b6 | 260 | void ESP8266Interface::event() { |
edamame22 | 0:29983394c6b6 | 261 | for (int i = 0; i < ESP8266_SOCKET_COUNT; i++) { |
edamame22 | 0:29983394c6b6 | 262 | if (_cbs[i].callback) { |
edamame22 | 0:29983394c6b6 | 263 | _cbs[i].callback(_cbs[i].data); |
edamame22 | 0:29983394c6b6 | 264 | } |
edamame22 | 0:29983394c6b6 | 265 | } |
edamame22 | 0:29983394c6b6 | 266 | } |