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:
geky
Date:
Mon Feb 29 23:05:00 2016 +0000
Revision:
50:2d7f6b97234e
Parent:
49:750ed1b67483
Child:
51:eb8c3577e22d
Matched changes to NetworkSocketAPI

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 42:4bf09cadf328 26 /** ESP8266Socket class
Christopher Haster 42:4bf09cadf328 27 * Implementation of the SocketInterface for the ESP8266
Christopher Haster 42:4bf09cadf328 28 */
Christopher Haster 42:4bf09cadf328 29 class ESP8266Socket : public SocketInterface
Christopher Haster 42:4bf09cadf328 30 {
Christopher Haster 42:4bf09cadf328 31 public:
Christopher Haster 42:4bf09cadf328 32 // ESP8266 specific methods
Christopher Haster 42:4bf09cadf328 33 ESP8266Socket(
Christopher Haster 42:4bf09cadf328 34 ESP8266 *esp,
geky 50:2d7f6b97234e 35 ns_protocol_t proto,
Christopher Haster 42:4bf09cadf328 36 int id);
Christopher Haster 42:4bf09cadf328 37
Christopher Haster 42:4bf09cadf328 38 virtual ~ESP8266Socket();
Christopher Haster 42:4bf09cadf328 39
Christopher Haster 42:4bf09cadf328 40 int getID() const;
Christopher Haster 42:4bf09cadf328 41
Christopher Haster 42:4bf09cadf328 42 // Implementation of SocketInterface
Christopher Haster 42:4bf09cadf328 43 virtual int32_t open(const char *ip, uint16_t port);
Christopher Haster 42:4bf09cadf328 44 virtual int32_t close();
Christopher Haster 42:4bf09cadf328 45
Christopher Haster 44:7ac7eb406603 46 virtual int32_t send(const void *data, uint32_t size);
Christopher Haster 44:7ac7eb406603 47 virtual int32_t recv(void *data, uint32_t size);
Christopher Haster 42:4bf09cadf328 48
Christopher Haster 42:4bf09cadf328 49 private:
Christopher Haster 42:4bf09cadf328 50 ESP8266 *_esp;
geky 50:2d7f6b97234e 51 ns_protocol_t _proto;
Christopher Haster 42:4bf09cadf328 52 int _id;
Christopher Haster 42:4bf09cadf328 53 };
Christopher Haster 42:4bf09cadf328 54
Christopher Haster 42:4bf09cadf328 55
Christopher Haster 40:83c6b4129468 56 // ESP8266Interface implementation
Christopher Haster 34:9c26a3dcdc1f 57 ESP8266Interface::ESP8266Interface(PinName tx, PinName rx, bool debug)
Christopher Haster 34:9c26a3dcdc1f 58 : _esp(tx, rx, debug)
sam_grove 24:37504440f296 59 {
Christopher Haster 34:9c26a3dcdc1f 60 memset(_ids, 0, sizeof(_ids));
sam_grove 11:288c15b80a26 61 }
sam_grove 11:288c15b80a26 62
Christopher Haster 40:83c6b4129468 63 ESP8266Interface::~ESP8266Interface()
Christopher Haster 40:83c6b4129468 64 {
Christopher Haster 40:83c6b4129468 65 }
Christopher Haster 40:83c6b4129468 66
Christopher Haster 34:9c26a3dcdc1f 67 int32_t ESP8266Interface::connect(
Christopher Haster 34:9c26a3dcdc1f 68 const char *ap,
Christopher Haster 34:9c26a3dcdc1f 69 const char *pass_phrase,
geky 50:2d7f6b97234e 70 ns_security_t)
sam_grove 11:288c15b80a26 71 {
Christopher Haster 45:538e5ce2f0d3 72 _esp.setTimeout(ESP8266_CONNECT_TIMEOUT);
Christopher Haster 45:538e5ce2f0d3 73
Christopher Haster 41:3f4d5f4862d2 74 if (!_esp.startup(3)) {
Christopher Haster 43:7c010b1db697 75 return NS_ERROR_DEVICE_ERROR;
Christopher Haster 41:3f4d5f4862d2 76 }
Christopher Haster 41:3f4d5f4862d2 77
Christopher Haster 41:3f4d5f4862d2 78 if (!_esp.dhcp(true, 1)) {
Christopher Haster 43:7c010b1db697 79 return NS_ERROR_DHCP_FAILURE;
Christopher Haster 41:3f4d5f4862d2 80 }
Christopher Haster 41:3f4d5f4862d2 81
Christopher Haster 41:3f4d5f4862d2 82 if (!_esp.connect(ap, pass_phrase)) {
Christopher Haster 43:7c010b1db697 83 return NS_ERROR_NO_CONNECTION;
Christopher Haster 41:3f4d5f4862d2 84 }
Christopher Haster 34:9c26a3dcdc1f 85
Christopher Haster 46:6b1bd1268074 86 if (!_esp.getIPAddress()) {
Christopher Haster 46:6b1bd1268074 87 return NS_ERROR_DHCP_FAILURE;
Christopher Haster 41:3f4d5f4862d2 88 }
Christopher Haster 34:9c26a3dcdc1f 89
sam_grove 24:37504440f296 90 return 0;
sam_grove 11:288c15b80a26 91 }
sam_grove 11:288c15b80a26 92
Christopher Haster 34:9c26a3dcdc1f 93 int32_t ESP8266Interface::disconnect()
sam_grove 11:288c15b80a26 94 {
Christopher Haster 45:538e5ce2f0d3 95 _esp.setTimeout(ESP8266_MISC_TIMEOUT);
Christopher Haster 45:538e5ce2f0d3 96
Christopher Haster 41:3f4d5f4862d2 97 if (!_esp.disconnect()) {
Christopher Haster 43:7c010b1db697 98 return NS_ERROR_DEVICE_ERROR;
Christopher Haster 41:3f4d5f4862d2 99 }
Christopher Haster 34:9c26a3dcdc1f 100
sarahmarshy 22:312453862371 101 return 0;
sam_grove 11:288c15b80a26 102 }
sam_grove 11:288c15b80a26 103
Christopher Haster 34:9c26a3dcdc1f 104 const char *ESP8266Interface::getIPAddress()
sarahmarshy 18:9fc7976c7b27 105 {
Christopher Haster 46:6b1bd1268074 106 return _esp.getIPAddress();
sarahmarshy 18:9fc7976c7b27 107 }
sarahmarshy 18:9fc7976c7b27 108
Christopher Haster 34:9c26a3dcdc1f 109 const char *ESP8266Interface::getMACAddress()
sam_grove 11:288c15b80a26 110 {
Christopher Haster 46:6b1bd1268074 111 return _esp.getMACAddress();
sam_grove 11:288c15b80a26 112 }
sam_grove 11:288c15b80a26 113
geky 50:2d7f6b97234e 114 SocketInterface *ESP8266Interface::createSocket(ns_protocol_t proto)
sam_grove 11:288c15b80a26 115 {
Christopher Haster 34:9c26a3dcdc1f 116 // Look for an unused socket
sarahmarshy 18:9fc7976c7b27 117 int id = -1;
Christopher Haster 34:9c26a3dcdc1f 118
Christopher Haster 34:9c26a3dcdc1f 119 for (int i = 0; i < ESP8266_SOCKET_COUNT; i++) {
Christopher Haster 34:9c26a3dcdc1f 120 if (!_ids[i]) {
sam_grove 24:37504440f296 121 id = i;
Christopher Haster 34:9c26a3dcdc1f 122 _ids[i] = true;
sarahmarshy 18:9fc7976c7b27 123 break;
sarahmarshy 18:9fc7976c7b27 124 }
sarahmarshy 18:9fc7976c7b27 125 }
Christopher Haster 34:9c26a3dcdc1f 126
sam_grove 24:37504440f296 127 if (id == -1) {
Christopher Haster 34:9c26a3dcdc1f 128 return 0;
sarahmarshy 22:312453862371 129 }
bridadan 16:b2f781416464 130
Christopher Haster 42:4bf09cadf328 131 return new ESP8266Socket(&_esp, proto, id);
bridadan 16:b2f781416464 132 }
bridadan 16:b2f781416464 133
Christopher Haster 34:9c26a3dcdc1f 134 void ESP8266Interface::destroySocket(SocketInterface *iface)
sarahmarshy 18:9fc7976c7b27 135 {
Christopher Haster 42:4bf09cadf328 136 ESP8266Socket *socket = (ESP8266Socket *)iface;
Christopher Haster 34:9c26a3dcdc1f 137 _ids[socket->getID()] = false;
Christopher Haster 39:7e85bf8003fa 138 delete socket;
sam_grove 13:0186e9e35a24 139 }
sam_grove 13:0186e9e35a24 140
Christopher Haster 40:83c6b4129468 141
Christopher Haster 40:83c6b4129468 142 // ESP8266Socket implementation
Christopher Haster 42:4bf09cadf328 143 ESP8266Socket::ESP8266Socket(
Christopher Haster 40:83c6b4129468 144 ESP8266 *esp,
geky 50:2d7f6b97234e 145 ns_protocol_t proto,
Christopher Haster 40:83c6b4129468 146 int id)
Christopher Haster 40:83c6b4129468 147 : _esp(esp)
Christopher Haster 40:83c6b4129468 148 , _proto(proto)
Christopher Haster 40:83c6b4129468 149 , _id(id)
Christopher Haster 40:83c6b4129468 150 {
Christopher Haster 40:83c6b4129468 151 }
Christopher Haster 40:83c6b4129468 152
Christopher Haster 42:4bf09cadf328 153 ESP8266Socket::~ESP8266Socket()
Christopher Haster 40:83c6b4129468 154 {
Christopher Haster 40:83c6b4129468 155 }
Christopher Haster 40:83c6b4129468 156
Christopher Haster 42:4bf09cadf328 157 int32_t ESP8266Socket::open(const char *ip, uint16_t port)
Christopher Haster 40:83c6b4129468 158 {
Christopher Haster 45:538e5ce2f0d3 159 _esp->setTimeout(ESP8266_MISC_TIMEOUT);
Christopher Haster 45:538e5ce2f0d3 160
geky 50:2d7f6b97234e 161 const char *proto = (_proto == NS_UDP) ? "UDP" : "TCP";
Christopher Haster 40:83c6b4129468 162
Christopher Haster 40:83c6b4129468 163 if (!_esp->open(proto, _id, ip, port)) {
Christopher Haster 43:7c010b1db697 164 return NS_ERROR_TIMEOUT;
Christopher Haster 40:83c6b4129468 165 }
Christopher Haster 40:83c6b4129468 166
Christopher Haster 40:83c6b4129468 167 return 0;
Christopher Haster 40:83c6b4129468 168 }
Christopher Haster 40:83c6b4129468 169
Christopher Haster 42:4bf09cadf328 170 int32_t ESP8266Socket::close()
Christopher Haster 40:83c6b4129468 171 {
Christopher Haster 45:538e5ce2f0d3 172 _esp->setTimeout(ESP8266_MISC_TIMEOUT);
Christopher Haster 45:538e5ce2f0d3 173
Christopher Haster 40:83c6b4129468 174 if (!_esp->close(_id)) {
Christopher Haster 45:538e5ce2f0d3 175 return NS_ERROR_TIMEOUT;
Christopher Haster 40:83c6b4129468 176 }
Christopher Haster 40:83c6b4129468 177
Christopher Haster 40:83c6b4129468 178 return 0;
Christopher Haster 40:83c6b4129468 179 }
Christopher Haster 40:83c6b4129468 180
Christopher Haster 44:7ac7eb406603 181 int32_t ESP8266Socket::send(const void *data, uint32_t size)
Christopher Haster 40:83c6b4129468 182 {
Christopher Haster 45:538e5ce2f0d3 183 _esp->setTimeout(ESP8266_SEND_TIMEOUT);
Christopher Haster 45:538e5ce2f0d3 184
Christopher Haster 44:7ac7eb406603 185 if (!_esp->send(_id, data, size)) {
Christopher Haster 43:7c010b1db697 186 return NS_ERROR_TIMEOUT;
Christopher Haster 40:83c6b4129468 187 }
Christopher Haster 40:83c6b4129468 188
Christopher Haster 40:83c6b4129468 189 return 0;
Christopher Haster 40:83c6b4129468 190 }
Christopher Haster 40:83c6b4129468 191
Christopher Haster 44:7ac7eb406603 192 int32_t ESP8266Socket::recv(void *data, uint32_t size)
Christopher Haster 40:83c6b4129468 193 {
Christopher Haster 45:538e5ce2f0d3 194 _esp->setTimeout(ESP8266_RECV_TIMEOUT);
Christopher Haster 44:7ac7eb406603 195
Christopher Haster 44:7ac7eb406603 196 int32_t recv = _esp->recv(_id, data, size);
Christopher Haster 44:7ac7eb406603 197
Christopher Haster 44:7ac7eb406603 198 if (recv < 0) {
Christopher Haster 44:7ac7eb406603 199 return 0;
Christopher Haster 43:7c010b1db697 200 }
Christopher Haster 43:7c010b1db697 201
Christopher Haster 44:7ac7eb406603 202 return recv;
Christopher Haster 40:83c6b4129468 203 }
Christopher Haster 40:83c6b4129468 204
Christopher Haster 42:4bf09cadf328 205 int ESP8266Socket::getID() const {
Christopher Haster 40:83c6b4129468 206 return _id;
Christopher Haster 40:83c6b4129468 207 }
Christopher Haster 40:83c6b4129468 208