Socket interface for ESP8266. Implements the NetworkSocketAPI. Requires device to use the Espressif Firmware.

Dependencies:   ESP8266

Dependents:   ESP8266InterfaceTests HelloESP8266Interface

Fork of ESP8266Interface by NetworkSocketAPI

Note

This library assumes your ESP8266 is running the Espressif Firmware. For instructions on how to update your ESP8266 to use the correct firmware see the Firmware Update Wiki Page.

Currently the ESP8266Interface LIbrary has the following Abilities:

Working

  • TCP Client
  • UDP Client
  • Transparent mode (single connection of 1 type at a time)
  • Station Mode (connects to AP)

To be implimented

  • TCP Server
  • UDP Server
  • Multi Connection Mode (able to have up to 5 sockets at a time)
  • AP Mode (Make ESP Chip act like access point)
  • DNS Support (currently websites must be looked up by IP)
  • Error Recovery

Nice but not necessary

  • colorized text for ESP AT Commands in Command line (easier to differentiate from other text)
Committer:
Christopher Haster
Date:
Thu Apr 21 06:11:08 2016 -0500
Revision:
56:34829ec3a3da
Parent:
54:e78fad32cfff
Child:
57:2ad35ade7a83
Match changes to the NSAPI

Who changed what in which revision?

UserRevisionLine numberNew contents of line
geky 49:750ed1b67483 1 /* ESP8266 implementation of NetworkInterfaceAPI
sam_grove 11:288c15b80a26 2 * Copyright (c) 2015 ARM Limited
sam_grove 11:288c15b80a26 3 *
sam_grove 11:288c15b80a26 4 * Licensed under the Apache License, Version 2.0 (the "License");
sam_grove 11:288c15b80a26 5 * you may not use this file except in compliance with the License.
sam_grove 11:288c15b80a26 6 * You may obtain a copy of the License at
sam_grove 11:288c15b80a26 7 *
sam_grove 11:288c15b80a26 8 * http://www.apache.org/licenses/LICENSE-2.0
sam_grove 11:288c15b80a26 9 *
sam_grove 11:288c15b80a26 10 * Unless required by applicable law or agreed to in writing, software
sam_grove 11:288c15b80a26 11 * distributed under the License is distributed on an "AS IS" BASIS,
sam_grove 11:288c15b80a26 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
sam_grove 11:288c15b80a26 13 * See the License for the specific language governing permissions and
sam_grove 11:288c15b80a26 14 * limitations under the License.
sam_grove 11:288c15b80a26 15 */
sam_grove 24:37504440f296 16
sarahmarshy 18:9fc7976c7b27 17 #include "ESP8266Interface.h"
sam_grove 11:288c15b80a26 18
Christopher Haster 45:538e5ce2f0d3 19 // Various timeouts for different ESP8266 operations
Christopher Haster 45:538e5ce2f0d3 20 #define ESP8266_CONNECT_TIMEOUT 15000
Christopher Haster 45:538e5ce2f0d3 21 #define ESP8266_SEND_TIMEOUT 500
Christopher Haster 45:538e5ce2f0d3 22 #define ESP8266_RECV_TIMEOUT 0
Christopher Haster 47:60a4b49e8b83 23 #define ESP8266_MISC_TIMEOUT 500
Christopher Haster 45:538e5ce2f0d3 24
Christopher Haster 42:4bf09cadf328 25
Christopher Haster 40:83c6b4129468 26 // ESP8266Interface implementation
Christopher Haster 34:9c26a3dcdc1f 27 ESP8266Interface::ESP8266Interface(PinName tx, PinName rx, bool debug)
Christopher Haster 34:9c26a3dcdc1f 28 : _esp(tx, rx, debug)
sam_grove 24:37504440f296 29 {
Christopher Haster 34:9c26a3dcdc1f 30 memset(_ids, 0, sizeof(_ids));
sam_grove 11:288c15b80a26 31 }
sam_grove 11:288c15b80a26 32
Christopher Haster 56:34829ec3a3da 33 int ESP8266Interface::connect(
Christopher Haster 34:9c26a3dcdc1f 34 const char *ap,
Christopher Haster 34:9c26a3dcdc1f 35 const char *pass_phrase,
Christopher Haster 56:34829ec3a3da 36 nsapi_security_t)
sam_grove 11:288c15b80a26 37 {
Christopher Haster 45:538e5ce2f0d3 38 _esp.setTimeout(ESP8266_CONNECT_TIMEOUT);
Christopher Haster 45:538e5ce2f0d3 39
Christopher Haster 41:3f4d5f4862d2 40 if (!_esp.startup(3)) {
Christopher Haster 56:34829ec3a3da 41 return NSAPI_ERROR_DEVICE_ERROR;
Christopher Haster 41:3f4d5f4862d2 42 }
Christopher Haster 41:3f4d5f4862d2 43
Christopher Haster 41:3f4d5f4862d2 44 if (!_esp.dhcp(true, 1)) {
Christopher Haster 56:34829ec3a3da 45 return NSAPI_ERROR_DHCP_FAILURE;
Christopher Haster 41:3f4d5f4862d2 46 }
Christopher Haster 41:3f4d5f4862d2 47
Christopher Haster 41:3f4d5f4862d2 48 if (!_esp.connect(ap, pass_phrase)) {
Christopher Haster 56:34829ec3a3da 49 return NSAPI_ERROR_NO_CONNECTION;
Christopher Haster 41:3f4d5f4862d2 50 }
Christopher Haster 34:9c26a3dcdc1f 51
Christopher Haster 46:6b1bd1268074 52 if (!_esp.getIPAddress()) {
Christopher Haster 56:34829ec3a3da 53 return NSAPI_ERROR_DHCP_FAILURE;
Christopher Haster 41:3f4d5f4862d2 54 }
Christopher Haster 34:9c26a3dcdc1f 55
sam_grove 24:37504440f296 56 return 0;
sam_grove 11:288c15b80a26 57 }
sam_grove 11:288c15b80a26 58
Christopher Haster 56:34829ec3a3da 59 int ESP8266Interface::disconnect()
sam_grove 11:288c15b80a26 60 {
Christopher Haster 45:538e5ce2f0d3 61 _esp.setTimeout(ESP8266_MISC_TIMEOUT);
Christopher Haster 45:538e5ce2f0d3 62
Christopher Haster 41:3f4d5f4862d2 63 if (!_esp.disconnect()) {
Christopher Haster 56:34829ec3a3da 64 return NSAPI_ERROR_DEVICE_ERROR;
Christopher Haster 41:3f4d5f4862d2 65 }
Christopher Haster 34:9c26a3dcdc1f 66
sarahmarshy 22:312453862371 67 return 0;
sam_grove 11:288c15b80a26 68 }
sam_grove 11:288c15b80a26 69
Christopher Haster 56:34829ec3a3da 70 const char *ESP8266Interface::get_ip_address()
sarahmarshy 18:9fc7976c7b27 71 {
Christopher Haster 46:6b1bd1268074 72 return _esp.getIPAddress();
sarahmarshy 18:9fc7976c7b27 73 }
sarahmarshy 18:9fc7976c7b27 74
Christopher Haster 56:34829ec3a3da 75 const char *ESP8266Interface::get_mac_address()
sam_grove 11:288c15b80a26 76 {
Christopher Haster 46:6b1bd1268074 77 return _esp.getMACAddress();
sam_grove 11:288c15b80a26 78 }
sam_grove 11:288c15b80a26 79
Christopher Haster 56:34829ec3a3da 80 struct esp8266_socket {
Christopher Haster 56:34829ec3a3da 81 int id;
Christopher Haster 56:34829ec3a3da 82 nsapi_protocol_t proto;
Christopher Haster 56:34829ec3a3da 83 bool connected;
Christopher Haster 56:34829ec3a3da 84 };
Christopher Haster 56:34829ec3a3da 85
Christopher Haster 56:34829ec3a3da 86 int ESP8266Interface::socket_open(void **handle, nsapi_protocol_t proto)
sam_grove 11:288c15b80a26 87 {
Christopher Haster 34:9c26a3dcdc1f 88 // Look for an unused socket
sarahmarshy 18:9fc7976c7b27 89 int id = -1;
Christopher Haster 34:9c26a3dcdc1f 90
Christopher Haster 34:9c26a3dcdc1f 91 for (int i = 0; i < ESP8266_SOCKET_COUNT; i++) {
Christopher Haster 34:9c26a3dcdc1f 92 if (!_ids[i]) {
sam_grove 24:37504440f296 93 id = i;
Christopher Haster 34:9c26a3dcdc1f 94 _ids[i] = true;
sarahmarshy 18:9fc7976c7b27 95 break;
sarahmarshy 18:9fc7976c7b27 96 }
sarahmarshy 18:9fc7976c7b27 97 }
Christopher Haster 34:9c26a3dcdc1f 98
sam_grove 24:37504440f296 99 if (id == -1) {
Christopher Haster 56:34829ec3a3da 100 return NSAPI_ERROR_NO_SOCKET;
sarahmarshy 22:312453862371 101 }
bridadan 16:b2f781416464 102
Christopher Haster 56:34829ec3a3da 103 struct esp8266_socket *socket = new struct esp8266_socket;
Christopher Haster 56:34829ec3a3da 104 if (!socket) {
Christopher Haster 56:34829ec3a3da 105 return NSAPI_ERROR_NO_SOCKET;
Christopher Haster 40:83c6b4129468 106 }
Christopher Haster 40:83c6b4129468 107
Christopher Haster 56:34829ec3a3da 108 socket->id = id;
Christopher Haster 56:34829ec3a3da 109 socket->proto = proto;
Christopher Haster 56:34829ec3a3da 110 socket->connected = false;
Christopher Haster 56:34829ec3a3da 111 *handle = socket;
Christopher Haster 40:83c6b4129468 112 return 0;
Christopher Haster 40:83c6b4129468 113 }
Christopher Haster 40:83c6b4129468 114
Christopher Haster 56:34829ec3a3da 115 int ESP8266Interface::socket_close(void *handle)
Christopher Haster 40:83c6b4129468 116 {
Christopher Haster 56:34829ec3a3da 117 struct esp8266_socket *socket = (struct esp8266_socket *)handle;
Christopher Haster 56:34829ec3a3da 118 int err = 0;
Christopher Haster 56:34829ec3a3da 119 _esp.setTimeout(ESP8266_MISC_TIMEOUT);
Christopher Haster 56:34829ec3a3da 120
Christopher Haster 56:34829ec3a3da 121 if (!_esp.close(socket->id)) {
Christopher Haster 56:34829ec3a3da 122 err = NSAPI_ERROR_DEVICE_ERROR;
Christopher Haster 40:83c6b4129468 123 }
Christopher Haster 40:83c6b4129468 124
Christopher Haster 56:34829ec3a3da 125 _ids[socket->id] = false;
Christopher Haster 56:34829ec3a3da 126 delete socket;
Christopher Haster 56:34829ec3a3da 127 return err;
Christopher Haster 56:34829ec3a3da 128 }
Christopher Haster 56:34829ec3a3da 129
Christopher Haster 56:34829ec3a3da 130 int ESP8266Interface::socket_bind(void *handle, const SocketAddress &address)
Christopher Haster 56:34829ec3a3da 131 {
Christopher Haster 56:34829ec3a3da 132 return NSAPI_ERROR_UNSUPPORTED;
Christopher Haster 56:34829ec3a3da 133 }
Christopher Haster 56:34829ec3a3da 134
Christopher Haster 56:34829ec3a3da 135 int ESP8266Interface::socket_listen(void *handle, int backlog)
Christopher Haster 56:34829ec3a3da 136 {
Christopher Haster 56:34829ec3a3da 137 return NSAPI_ERROR_UNSUPPORTED;
Christopher Haster 56:34829ec3a3da 138 }
Christopher Haster 56:34829ec3a3da 139
Christopher Haster 56:34829ec3a3da 140 int ESP8266Interface::socket_connect(void *handle, const SocketAddress &addr)
Christopher Haster 56:34829ec3a3da 141 {
Christopher Haster 56:34829ec3a3da 142 struct esp8266_socket *socket = (struct esp8266_socket *)handle;
Christopher Haster 56:34829ec3a3da 143 _esp.setTimeout(ESP8266_MISC_TIMEOUT);
Christopher Haster 56:34829ec3a3da 144
Christopher Haster 56:34829ec3a3da 145 const char *proto = (socket->proto == NSAPI_UDP) ? "UDP" : "TCP";
Christopher Haster 56:34829ec3a3da 146 if (!_esp.open(proto, socket->id, addr.get_ip_address(), addr.get_port())) {
Christopher Haster 56:34829ec3a3da 147 return NSAPI_ERROR_DEVICE_ERROR;
Christopher Haster 56:34829ec3a3da 148 }
Christopher Haster 56:34829ec3a3da 149
Christopher Haster 56:34829ec3a3da 150 socket->connected = true;
Christopher Haster 40:83c6b4129468 151 return 0;
Christopher Haster 40:83c6b4129468 152 }
Christopher Haster 56:34829ec3a3da 153
Christopher Haster 56:34829ec3a3da 154 int ESP8266Interface::socket_accept(void **handle, void *server)
Christopher Haster 40:83c6b4129468 155 {
Christopher Haster 56:34829ec3a3da 156 return NSAPI_ERROR_UNSUPPORTED;
Christopher Haster 56:34829ec3a3da 157 }
Christopher Haster 45:538e5ce2f0d3 158
Christopher Haster 56:34829ec3a3da 159 int ESP8266Interface::socket_send(void *handle, const void *data, unsigned size)
Christopher Haster 56:34829ec3a3da 160 {
Christopher Haster 56:34829ec3a3da 161 struct esp8266_socket *socket = (struct esp8266_socket *)handle;
Christopher Haster 56:34829ec3a3da 162 _esp.setTimeout(ESP8266_SEND_TIMEOUT);
Christopher Haster 56:34829ec3a3da 163
Christopher Haster 56:34829ec3a3da 164 if (!_esp.send(socket->id, data, size)) {
Christopher Haster 56:34829ec3a3da 165 return NSAPI_ERROR_DEVICE_ERROR;
Christopher Haster 40:83c6b4129468 166 }
Christopher Haster 56:34829ec3a3da 167
geky 54:e78fad32cfff 168 return size;
Christopher Haster 40:83c6b4129468 169 }
Christopher Haster 40:83c6b4129468 170
Christopher Haster 56:34829ec3a3da 171 int ESP8266Interface::socket_recv(void *handle, void *data, unsigned size)
Christopher Haster 40:83c6b4129468 172 {
Christopher Haster 56:34829ec3a3da 173 struct esp8266_socket *socket = (struct esp8266_socket *)handle;
Christopher Haster 56:34829ec3a3da 174 _esp.setTimeout(ESP8266_RECV_TIMEOUT);
Christopher Haster 56:34829ec3a3da 175
Christopher Haster 56:34829ec3a3da 176 int32_t recv = _esp.recv(socket->id, data, size);
Christopher Haster 44:7ac7eb406603 177 if (recv < 0) {
Christopher Haster 56:34829ec3a3da 178 return NSAPI_ERROR_WOULD_BLOCK;
Christopher Haster 43:7c010b1db697 179 }
Christopher Haster 56:34829ec3a3da 180
Christopher Haster 44:7ac7eb406603 181 return recv;
Christopher Haster 40:83c6b4129468 182 }
Christopher Haster 40:83c6b4129468 183
Christopher Haster 56:34829ec3a3da 184 int ESP8266Interface::socket_sendto(void *handle, const SocketAddress &addr, const void *data, unsigned size)
Christopher Haster 56:34829ec3a3da 185 {
Christopher Haster 56:34829ec3a3da 186 struct esp8266_socket *socket = (struct esp8266_socket *)handle;
Christopher Haster 56:34829ec3a3da 187 if (!socket->connected) {
Christopher Haster 56:34829ec3a3da 188 int err = socket_connect(socket, addr);
Christopher Haster 56:34829ec3a3da 189 if (err < 0) {
Christopher Haster 56:34829ec3a3da 190 return err;
Christopher Haster 56:34829ec3a3da 191 }
Christopher Haster 56:34829ec3a3da 192 }
Christopher Haster 56:34829ec3a3da 193
Christopher Haster 56:34829ec3a3da 194 return socket_send(socket, data, size);
Christopher Haster 56:34829ec3a3da 195 }
Christopher Haster 56:34829ec3a3da 196
Christopher Haster 56:34829ec3a3da 197 int ESP8266Interface::socket_recvfrom(void *handle, SocketAddress *addr, void *data, unsigned size)
Christopher Haster 56:34829ec3a3da 198 {
Christopher Haster 56:34829ec3a3da 199 struct esp8266_socket *socket = (struct esp8266_socket *)handle;
Christopher Haster 56:34829ec3a3da 200 return socket_recv(socket, data, size);
Christopher Haster 56:34829ec3a3da 201 }
Christopher Haster 56:34829ec3a3da 202
Christopher Haster 56:34829ec3a3da 203 void ESP8266Interface::socket_attach(void *handle, void (*callback)(void *), void *data)
Christopher Haster 56:34829ec3a3da 204 {
Christopher Haster 56:34829ec3a3da 205 }
Christopher Haster 56:34829ec3a3da 206