The driver for the ESP8266 WiFi module

Dependents:   Robot Wi-fi

Fork of esp8266-driver by ESP8266

Committer:
williampeers
Date:
Thu Jul 27 03:25:51 2017 +0000
Revision:
2:77a1e2188ed9
Parent:
0:6946b0b9e323
Sender working with reconnect if AGV or control resets, but not wifi

Who changed what in which revision?

UserRevisionLine numberNew contents of line
group-ESP8266 0:6946b0b9e323 1 /* ESP8266 implementation of NetworkInterfaceAPI
group-ESP8266 0:6946b0b9e323 2 * Copyright (c) 2015 ARM Limited
group-ESP8266 0:6946b0b9e323 3 *
group-ESP8266 0:6946b0b9e323 4 * Licensed under the Apache License, Version 2.0 (the "License");
group-ESP8266 0:6946b0b9e323 5 * you may not use this file except in compliance with the License.
group-ESP8266 0:6946b0b9e323 6 * You may obtain a copy of the License at
group-ESP8266 0:6946b0b9e323 7 *
group-ESP8266 0:6946b0b9e323 8 * http://www.apache.org/licenses/LICENSE-2.0
group-ESP8266 0:6946b0b9e323 9 *
group-ESP8266 0:6946b0b9e323 10 * Unless required by applicable law or agreed to in writing, software
group-ESP8266 0:6946b0b9e323 11 * distributed under the License is distributed on an "AS IS" BASIS,
group-ESP8266 0:6946b0b9e323 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
group-ESP8266 0:6946b0b9e323 13 * See the License for the specific language governing permissions and
group-ESP8266 0:6946b0b9e323 14 * limitations under the License.
group-ESP8266 0:6946b0b9e323 15 */
group-ESP8266 0:6946b0b9e323 16
group-ESP8266 0:6946b0b9e323 17 #include <string.h>
group-ESP8266 0:6946b0b9e323 18 #include "ESP8266Interface.h"
group-ESP8266 0:6946b0b9e323 19
group-ESP8266 0:6946b0b9e323 20 // Various timeouts for different ESP8266 operations
group-ESP8266 0:6946b0b9e323 21 #define ESP8266_CONNECT_TIMEOUT 15000
group-ESP8266 0:6946b0b9e323 22 #define ESP8266_SEND_TIMEOUT 500
group-ESP8266 0:6946b0b9e323 23 #define ESP8266_RECV_TIMEOUT 0
group-ESP8266 0:6946b0b9e323 24 #define ESP8266_MISC_TIMEOUT 500
group-ESP8266 0:6946b0b9e323 25
group-ESP8266 0:6946b0b9e323 26 // ESP8266Interface implementation
group-ESP8266 0:6946b0b9e323 27 ESP8266Interface::ESP8266Interface(PinName tx, PinName rx, bool debug)
group-ESP8266 0:6946b0b9e323 28 : _esp(tx, rx, debug)
group-ESP8266 0:6946b0b9e323 29 {
group-ESP8266 0:6946b0b9e323 30 memset(_ids, 0, sizeof(_ids));
group-ESP8266 0:6946b0b9e323 31 memset(_cbs, 0, sizeof(_cbs));
group-ESP8266 0:6946b0b9e323 32
group-ESP8266 0:6946b0b9e323 33 _esp.attach(this, &ESP8266Interface::event);
group-ESP8266 0:6946b0b9e323 34 }
group-ESP8266 0:6946b0b9e323 35
williampeers 2:77a1e2188ed9 36 int ESP8266Interface::testWifi() {
williampeers 2:77a1e2188ed9 37 int ret;
williampeers 2:77a1e2188ed9 38 ret = _esp.isConnected();
williampeers 2:77a1e2188ed9 39 //ret = _esp._parser.send("AT+CWJAP?") && _esp._parser.recv("+CWJAP:\"denver\"");
williampeers 2:77a1e2188ed9 40 //_esp._parser.flush();
williampeers 2:77a1e2188ed9 41 return(ret);
williampeers 2:77a1e2188ed9 42 }
williampeers 2:77a1e2188ed9 43
williampeers 2:77a1e2188ed9 44
williampeers 2:77a1e2188ed9 45
group-ESP8266 0:6946b0b9e323 46 int ESP8266Interface::connect(const char *ssid, const char *pass, nsapi_security_t security,
group-ESP8266 0:6946b0b9e323 47 uint8_t channel)
group-ESP8266 0:6946b0b9e323 48 {
group-ESP8266 0:6946b0b9e323 49 if (channel != 0) {
group-ESP8266 0:6946b0b9e323 50 return NSAPI_ERROR_UNSUPPORTED;
group-ESP8266 0:6946b0b9e323 51 }
group-ESP8266 0:6946b0b9e323 52
group-ESP8266 0:6946b0b9e323 53 set_credentials(ssid, pass, security);
group-ESP8266 0:6946b0b9e323 54 return connect();
group-ESP8266 0:6946b0b9e323 55 }
group-ESP8266 0:6946b0b9e323 56
group-ESP8266 0:6946b0b9e323 57 int ESP8266Interface::connect()
group-ESP8266 0:6946b0b9e323 58 {
group-ESP8266 0:6946b0b9e323 59 _esp.setTimeout(ESP8266_CONNECT_TIMEOUT);
group-ESP8266 0:6946b0b9e323 60
group-ESP8266 0:6946b0b9e323 61 if (!_esp.startup(3)) {
group-ESP8266 0:6946b0b9e323 62 return NSAPI_ERROR_DEVICE_ERROR;
group-ESP8266 0:6946b0b9e323 63 }
group-ESP8266 0:6946b0b9e323 64
group-ESP8266 0:6946b0b9e323 65 if (!_esp.dhcp(true, 1)) {
group-ESP8266 0:6946b0b9e323 66 return NSAPI_ERROR_DHCP_FAILURE;
group-ESP8266 0:6946b0b9e323 67 }
group-ESP8266 0:6946b0b9e323 68
group-ESP8266 0:6946b0b9e323 69 if (!_esp.connect(ap_ssid, ap_pass)) {
group-ESP8266 0:6946b0b9e323 70 return NSAPI_ERROR_NO_CONNECTION;
group-ESP8266 0:6946b0b9e323 71 }
group-ESP8266 0:6946b0b9e323 72
group-ESP8266 0:6946b0b9e323 73 if (!_esp.getIPAddress()) {
group-ESP8266 0:6946b0b9e323 74 return NSAPI_ERROR_DHCP_FAILURE;
group-ESP8266 0:6946b0b9e323 75 }
group-ESP8266 0:6946b0b9e323 76
group-ESP8266 0:6946b0b9e323 77 return NSAPI_ERROR_OK;
group-ESP8266 0:6946b0b9e323 78 }
group-ESP8266 0:6946b0b9e323 79
group-ESP8266 0:6946b0b9e323 80 int ESP8266Interface::set_credentials(const char *ssid, const char *pass, nsapi_security_t security)
group-ESP8266 0:6946b0b9e323 81 {
group-ESP8266 0:6946b0b9e323 82 memset(ap_ssid, 0, sizeof(ap_ssid));
group-ESP8266 0:6946b0b9e323 83 strncpy(ap_ssid, ssid, sizeof(ap_ssid));
group-ESP8266 0:6946b0b9e323 84
group-ESP8266 0:6946b0b9e323 85 memset(ap_pass, 0, sizeof(ap_pass));
group-ESP8266 0:6946b0b9e323 86 strncpy(ap_pass, pass, sizeof(ap_pass));
group-ESP8266 0:6946b0b9e323 87
group-ESP8266 0:6946b0b9e323 88 ap_sec = security;
group-ESP8266 0:6946b0b9e323 89
group-ESP8266 0:6946b0b9e323 90 return 0;
group-ESP8266 0:6946b0b9e323 91 }
group-ESP8266 0:6946b0b9e323 92
group-ESP8266 0:6946b0b9e323 93 int ESP8266Interface::set_channel(uint8_t channel)
group-ESP8266 0:6946b0b9e323 94 {
group-ESP8266 0:6946b0b9e323 95 return NSAPI_ERROR_UNSUPPORTED;
group-ESP8266 0:6946b0b9e323 96 }
group-ESP8266 0:6946b0b9e323 97
group-ESP8266 0:6946b0b9e323 98
group-ESP8266 0:6946b0b9e323 99 int ESP8266Interface::disconnect()
group-ESP8266 0:6946b0b9e323 100 {
group-ESP8266 0:6946b0b9e323 101 _esp.setTimeout(ESP8266_MISC_TIMEOUT);
group-ESP8266 0:6946b0b9e323 102
group-ESP8266 0:6946b0b9e323 103 if (!_esp.disconnect()) {
group-ESP8266 0:6946b0b9e323 104 return NSAPI_ERROR_DEVICE_ERROR;
group-ESP8266 0:6946b0b9e323 105 }
group-ESP8266 0:6946b0b9e323 106
group-ESP8266 0:6946b0b9e323 107 return NSAPI_ERROR_OK;
group-ESP8266 0:6946b0b9e323 108 }
group-ESP8266 0:6946b0b9e323 109
group-ESP8266 0:6946b0b9e323 110 const char *ESP8266Interface::get_ip_address()
group-ESP8266 0:6946b0b9e323 111 {
group-ESP8266 0:6946b0b9e323 112 return _esp.getIPAddress();
group-ESP8266 0:6946b0b9e323 113 }
group-ESP8266 0:6946b0b9e323 114
group-ESP8266 0:6946b0b9e323 115 const char *ESP8266Interface::get_mac_address()
group-ESP8266 0:6946b0b9e323 116 {
group-ESP8266 0:6946b0b9e323 117 return _esp.getMACAddress();
group-ESP8266 0:6946b0b9e323 118 }
group-ESP8266 0:6946b0b9e323 119
group-ESP8266 0:6946b0b9e323 120 const char *ESP8266Interface::get_gateway()
group-ESP8266 0:6946b0b9e323 121 {
group-ESP8266 0:6946b0b9e323 122 return _esp.getGateway();
group-ESP8266 0:6946b0b9e323 123 }
group-ESP8266 0:6946b0b9e323 124
group-ESP8266 0:6946b0b9e323 125 const char *ESP8266Interface::get_netmask()
group-ESP8266 0:6946b0b9e323 126 {
group-ESP8266 0:6946b0b9e323 127 return _esp.getNetmask();
group-ESP8266 0:6946b0b9e323 128 }
group-ESP8266 0:6946b0b9e323 129
group-ESP8266 0:6946b0b9e323 130 int8_t ESP8266Interface::get_rssi()
group-ESP8266 0:6946b0b9e323 131 {
group-ESP8266 0:6946b0b9e323 132 return _esp.getRSSI();
group-ESP8266 0:6946b0b9e323 133 }
group-ESP8266 0:6946b0b9e323 134
group-ESP8266 0:6946b0b9e323 135 int ESP8266Interface::scan(WiFiAccessPoint *res, unsigned count)
group-ESP8266 0:6946b0b9e323 136 {
group-ESP8266 0:6946b0b9e323 137 return _esp.scan(res, count);
group-ESP8266 0:6946b0b9e323 138 }
group-ESP8266 0:6946b0b9e323 139
group-ESP8266 0:6946b0b9e323 140 struct esp8266_socket {
group-ESP8266 0:6946b0b9e323 141 int id;
group-ESP8266 0:6946b0b9e323 142 nsapi_protocol_t proto;
group-ESP8266 0:6946b0b9e323 143 bool connected;
group-ESP8266 0:6946b0b9e323 144 SocketAddress addr;
group-ESP8266 0:6946b0b9e323 145 };
group-ESP8266 0:6946b0b9e323 146
group-ESP8266 0:6946b0b9e323 147 int ESP8266Interface::socket_open(void **handle, nsapi_protocol_t proto)
group-ESP8266 0:6946b0b9e323 148 {
group-ESP8266 0:6946b0b9e323 149 // Look for an unused socket
group-ESP8266 0:6946b0b9e323 150 int id = -1;
group-ESP8266 0:6946b0b9e323 151
group-ESP8266 0:6946b0b9e323 152 for (int i = 0; i < ESP8266_SOCKET_COUNT; i++) {
group-ESP8266 0:6946b0b9e323 153 if (!_ids[i]) {
group-ESP8266 0:6946b0b9e323 154 id = i;
group-ESP8266 0:6946b0b9e323 155 _ids[i] = true;
group-ESP8266 0:6946b0b9e323 156 break;
group-ESP8266 0:6946b0b9e323 157 }
group-ESP8266 0:6946b0b9e323 158 }
group-ESP8266 0:6946b0b9e323 159
group-ESP8266 0:6946b0b9e323 160 if (id == -1) {
group-ESP8266 0:6946b0b9e323 161 return NSAPI_ERROR_NO_SOCKET;
group-ESP8266 0:6946b0b9e323 162 }
group-ESP8266 0:6946b0b9e323 163
group-ESP8266 0:6946b0b9e323 164 struct esp8266_socket *socket = new struct esp8266_socket;
group-ESP8266 0:6946b0b9e323 165 if (!socket) {
group-ESP8266 0:6946b0b9e323 166 return NSAPI_ERROR_NO_SOCKET;
group-ESP8266 0:6946b0b9e323 167 }
group-ESP8266 0:6946b0b9e323 168
group-ESP8266 0:6946b0b9e323 169 socket->id = id;
group-ESP8266 0:6946b0b9e323 170 socket->proto = proto;
group-ESP8266 0:6946b0b9e323 171 socket->connected = false;
group-ESP8266 0:6946b0b9e323 172 *handle = socket;
group-ESP8266 0:6946b0b9e323 173 return 0;
group-ESP8266 0:6946b0b9e323 174 }
group-ESP8266 0:6946b0b9e323 175
group-ESP8266 0:6946b0b9e323 176 int ESP8266Interface::socket_close(void *handle)
group-ESP8266 0:6946b0b9e323 177 {
group-ESP8266 0:6946b0b9e323 178 struct esp8266_socket *socket = (struct esp8266_socket *)handle;
group-ESP8266 0:6946b0b9e323 179 int err = 0;
group-ESP8266 0:6946b0b9e323 180 _esp.setTimeout(ESP8266_MISC_TIMEOUT);
group-ESP8266 0:6946b0b9e323 181
group-ESP8266 0:6946b0b9e323 182 if (!_esp.close(socket->id)) {
group-ESP8266 0:6946b0b9e323 183 err = NSAPI_ERROR_DEVICE_ERROR;
group-ESP8266 0:6946b0b9e323 184 }
group-ESP8266 0:6946b0b9e323 185
group-ESP8266 0:6946b0b9e323 186 _ids[socket->id] = false;
group-ESP8266 0:6946b0b9e323 187 delete socket;
group-ESP8266 0:6946b0b9e323 188 return err;
group-ESP8266 0:6946b0b9e323 189 }
group-ESP8266 0:6946b0b9e323 190
group-ESP8266 0:6946b0b9e323 191 int ESP8266Interface::socket_bind(void *handle, const SocketAddress &address)
group-ESP8266 0:6946b0b9e323 192 {
group-ESP8266 0:6946b0b9e323 193 return NSAPI_ERROR_UNSUPPORTED;
group-ESP8266 0:6946b0b9e323 194 }
group-ESP8266 0:6946b0b9e323 195
group-ESP8266 0:6946b0b9e323 196 int ESP8266Interface::socket_listen(void *handle, int backlog)
group-ESP8266 0:6946b0b9e323 197 {
group-ESP8266 0:6946b0b9e323 198 return NSAPI_ERROR_UNSUPPORTED;
group-ESP8266 0:6946b0b9e323 199 }
group-ESP8266 0:6946b0b9e323 200
group-ESP8266 0:6946b0b9e323 201 int ESP8266Interface::socket_connect(void *handle, const SocketAddress &addr)
group-ESP8266 0:6946b0b9e323 202 {
group-ESP8266 0:6946b0b9e323 203 struct esp8266_socket *socket = (struct esp8266_socket *)handle;
group-ESP8266 0:6946b0b9e323 204 _esp.setTimeout(ESP8266_MISC_TIMEOUT);
group-ESP8266 0:6946b0b9e323 205
group-ESP8266 0:6946b0b9e323 206 const char *proto = (socket->proto == NSAPI_UDP) ? "UDP" : "TCP";
group-ESP8266 0:6946b0b9e323 207 if (!_esp.open(proto, socket->id, addr.get_ip_address(), addr.get_port())) {
group-ESP8266 0:6946b0b9e323 208 return NSAPI_ERROR_DEVICE_ERROR;
group-ESP8266 0:6946b0b9e323 209 }
group-ESP8266 0:6946b0b9e323 210
group-ESP8266 0:6946b0b9e323 211 socket->connected = true;
group-ESP8266 0:6946b0b9e323 212 return 0;
group-ESP8266 0:6946b0b9e323 213 }
group-ESP8266 0:6946b0b9e323 214
group-ESP8266 0:6946b0b9e323 215 int ESP8266Interface::socket_accept(void *server, void **socket, SocketAddress *addr)
group-ESP8266 0:6946b0b9e323 216 {
group-ESP8266 0:6946b0b9e323 217 return NSAPI_ERROR_UNSUPPORTED;
group-ESP8266 0:6946b0b9e323 218 }
group-ESP8266 0:6946b0b9e323 219
group-ESP8266 0:6946b0b9e323 220 int ESP8266Interface::socket_send(void *handle, const void *data, unsigned size)
group-ESP8266 0:6946b0b9e323 221 {
group-ESP8266 0:6946b0b9e323 222 struct esp8266_socket *socket = (struct esp8266_socket *)handle;
group-ESP8266 0:6946b0b9e323 223 _esp.setTimeout(ESP8266_SEND_TIMEOUT);
group-ESP8266 0:6946b0b9e323 224
group-ESP8266 0:6946b0b9e323 225 if (!_esp.send(socket->id, data, size)) {
group-ESP8266 0:6946b0b9e323 226 return NSAPI_ERROR_DEVICE_ERROR;
group-ESP8266 0:6946b0b9e323 227 }
group-ESP8266 0:6946b0b9e323 228
group-ESP8266 0:6946b0b9e323 229 return size;
group-ESP8266 0:6946b0b9e323 230 }
group-ESP8266 0:6946b0b9e323 231
group-ESP8266 0:6946b0b9e323 232 int ESP8266Interface::socket_recv(void *handle, void *data, unsigned size)
group-ESP8266 0:6946b0b9e323 233 {
group-ESP8266 0:6946b0b9e323 234 struct esp8266_socket *socket = (struct esp8266_socket *)handle;
group-ESP8266 0:6946b0b9e323 235 _esp.setTimeout(ESP8266_RECV_TIMEOUT);
group-ESP8266 0:6946b0b9e323 236
group-ESP8266 0:6946b0b9e323 237 int32_t recv = _esp.recv(socket->id, data, size);
group-ESP8266 0:6946b0b9e323 238 if (recv < 0) {
group-ESP8266 0:6946b0b9e323 239 return NSAPI_ERROR_WOULD_BLOCK;
group-ESP8266 0:6946b0b9e323 240 }
group-ESP8266 0:6946b0b9e323 241
group-ESP8266 0:6946b0b9e323 242 return recv;
group-ESP8266 0:6946b0b9e323 243 }
group-ESP8266 0:6946b0b9e323 244
group-ESP8266 0:6946b0b9e323 245 int ESP8266Interface::socket_sendto(void *handle, const SocketAddress &addr, const void *data, unsigned size)
group-ESP8266 0:6946b0b9e323 246 {
group-ESP8266 0:6946b0b9e323 247 struct esp8266_socket *socket = (struct esp8266_socket *)handle;
group-ESP8266 0:6946b0b9e323 248
group-ESP8266 0:6946b0b9e323 249 if (socket->connected && socket->addr != addr) {
group-ESP8266 0:6946b0b9e323 250 _esp.setTimeout(ESP8266_MISC_TIMEOUT);
group-ESP8266 0:6946b0b9e323 251 if (!_esp.close(socket->id)) {
group-ESP8266 0:6946b0b9e323 252 return NSAPI_ERROR_DEVICE_ERROR;
group-ESP8266 0:6946b0b9e323 253 }
group-ESP8266 0:6946b0b9e323 254 socket->connected = false;
group-ESP8266 0:6946b0b9e323 255 }
group-ESP8266 0:6946b0b9e323 256
group-ESP8266 0:6946b0b9e323 257 if (!socket->connected) {
group-ESP8266 0:6946b0b9e323 258 int err = socket_connect(socket, addr);
group-ESP8266 0:6946b0b9e323 259 if (err < 0) {
group-ESP8266 0:6946b0b9e323 260 return err;
group-ESP8266 0:6946b0b9e323 261 }
group-ESP8266 0:6946b0b9e323 262 socket->addr = addr;
group-ESP8266 0:6946b0b9e323 263 }
group-ESP8266 0:6946b0b9e323 264
group-ESP8266 0:6946b0b9e323 265 return socket_send(socket, data, size);
group-ESP8266 0:6946b0b9e323 266 }
group-ESP8266 0:6946b0b9e323 267
group-ESP8266 0:6946b0b9e323 268 int ESP8266Interface::socket_recvfrom(void *handle, SocketAddress *addr, void *data, unsigned size)
group-ESP8266 0:6946b0b9e323 269 {
group-ESP8266 0:6946b0b9e323 270 struct esp8266_socket *socket = (struct esp8266_socket *)handle;
group-ESP8266 0:6946b0b9e323 271 int ret = socket_recv(socket, data, size);
group-ESP8266 0:6946b0b9e323 272 if (ret >= 0 && addr) {
group-ESP8266 0:6946b0b9e323 273 *addr = socket->addr;
group-ESP8266 0:6946b0b9e323 274 }
group-ESP8266 0:6946b0b9e323 275
group-ESP8266 0:6946b0b9e323 276 return ret;
group-ESP8266 0:6946b0b9e323 277 }
group-ESP8266 0:6946b0b9e323 278
group-ESP8266 0:6946b0b9e323 279 void ESP8266Interface::socket_attach(void *handle, void (*callback)(void *), void *data)
group-ESP8266 0:6946b0b9e323 280 {
group-ESP8266 0:6946b0b9e323 281 struct esp8266_socket *socket = (struct esp8266_socket *)handle;
group-ESP8266 0:6946b0b9e323 282 _cbs[socket->id].callback = callback;
group-ESP8266 0:6946b0b9e323 283 _cbs[socket->id].data = data;
group-ESP8266 0:6946b0b9e323 284 }
group-ESP8266 0:6946b0b9e323 285
group-ESP8266 0:6946b0b9e323 286 void ESP8266Interface::event() {
group-ESP8266 0:6946b0b9e323 287 for (int i = 0; i < ESP8266_SOCKET_COUNT; i++) {
group-ESP8266 0:6946b0b9e323 288 if (_cbs[i].callback) {
group-ESP8266 0:6946b0b9e323 289 _cbs[i].callback(_cbs[i].data);
group-ESP8266 0:6946b0b9e323 290 }
group-ESP8266 0:6946b0b9e323 291 }
group-ESP8266 0:6946b0b9e323 292 }