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 Feb 25 03:32:37 2016 -0600
Branch:
api-changes
Revision:
45:538e5ce2f0d3
Parent:
44:7ac7eb406603
Child:
46:6b1bd1268074
Added more appropriate timeouts for driver operations

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sarahmarshy 18:9fc7976c7b27 1 /* ESP8266Interface Example
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 45:538e5ce2f0d3 23 #define ESP8266_MISC_TIMEOUT 5000
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,
Christopher Haster 42:4bf09cadf328 35 socket_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;
Christopher Haster 42:4bf09cadf328 51 socket_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,
Christopher Haster 38:eb1e46561a19 70 wifi_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 34:9c26a3dcdc1f 86 _ip_address = _esp.getIPAddress();
Christopher Haster 34:9c26a3dcdc1f 87 _mac_address = _esp.getMACAddress();
Christopher Haster 41:3f4d5f4862d2 88 if (!_ip_address || !_mac_address) {
Christopher Haster 43:7c010b1db697 89 return NS_ERROR_NO_ADDRESS;
Christopher Haster 41:3f4d5f4862d2 90 }
Christopher Haster 34:9c26a3dcdc1f 91
sam_grove 24:37504440f296 92 return 0;
sam_grove 11:288c15b80a26 93 }
sam_grove 11:288c15b80a26 94
Christopher Haster 34:9c26a3dcdc1f 95 int32_t ESP8266Interface::disconnect()
sam_grove 11:288c15b80a26 96 {
Christopher Haster 45:538e5ce2f0d3 97 _esp.setTimeout(ESP8266_MISC_TIMEOUT);
Christopher Haster 45:538e5ce2f0d3 98
Christopher Haster 41:3f4d5f4862d2 99 if (!_esp.disconnect()) {
Christopher Haster 43:7c010b1db697 100 return NS_ERROR_DEVICE_ERROR;
Christopher Haster 41:3f4d5f4862d2 101 }
Christopher Haster 34:9c26a3dcdc1f 102
sarahmarshy 22:312453862371 103 return 0;
sam_grove 11:288c15b80a26 104 }
sam_grove 11:288c15b80a26 105
Christopher Haster 34:9c26a3dcdc1f 106 const char *ESP8266Interface::getIPAddress()
sarahmarshy 18:9fc7976c7b27 107 {
Christopher Haster 34:9c26a3dcdc1f 108 return _ip_address;
sarahmarshy 18:9fc7976c7b27 109 }
sarahmarshy 18:9fc7976c7b27 110
Christopher Haster 34:9c26a3dcdc1f 111 const char *ESP8266Interface::getMACAddress()
sam_grove 11:288c15b80a26 112 {
Christopher Haster 34:9c26a3dcdc1f 113 return _mac_address;
sam_grove 11:288c15b80a26 114 }
sam_grove 11:288c15b80a26 115
Christopher Haster 34:9c26a3dcdc1f 116 SocketInterface *ESP8266Interface::createSocket(socket_protocol_t proto)
sam_grove 11:288c15b80a26 117 {
Christopher Haster 34:9c26a3dcdc1f 118 // Look for an unused socket
sarahmarshy 18:9fc7976c7b27 119 int id = -1;
Christopher Haster 34:9c26a3dcdc1f 120
Christopher Haster 34:9c26a3dcdc1f 121 for (int i = 0; i < ESP8266_SOCKET_COUNT; i++) {
Christopher Haster 34:9c26a3dcdc1f 122 if (!_ids[i]) {
sam_grove 24:37504440f296 123 id = i;
Christopher Haster 34:9c26a3dcdc1f 124 _ids[i] = true;
sarahmarshy 18:9fc7976c7b27 125 break;
sarahmarshy 18:9fc7976c7b27 126 }
sarahmarshy 18:9fc7976c7b27 127 }
Christopher Haster 34:9c26a3dcdc1f 128
sam_grove 24:37504440f296 129 if (id == -1) {
Christopher Haster 34:9c26a3dcdc1f 130 return 0;
sarahmarshy 22:312453862371 131 }
bridadan 16:b2f781416464 132
Christopher Haster 42:4bf09cadf328 133 return new ESP8266Socket(&_esp, proto, id);
bridadan 16:b2f781416464 134 }
bridadan 16:b2f781416464 135
Christopher Haster 34:9c26a3dcdc1f 136 void ESP8266Interface::destroySocket(SocketInterface *iface)
sarahmarshy 18:9fc7976c7b27 137 {
Christopher Haster 42:4bf09cadf328 138 ESP8266Socket *socket = (ESP8266Socket *)iface;
Christopher Haster 34:9c26a3dcdc1f 139 _ids[socket->getID()] = false;
Christopher Haster 39:7e85bf8003fa 140 delete socket;
sam_grove 13:0186e9e35a24 141 }
sam_grove 13:0186e9e35a24 142
Christopher Haster 40:83c6b4129468 143
Christopher Haster 40:83c6b4129468 144 // ESP8266Socket implementation
Christopher Haster 42:4bf09cadf328 145 ESP8266Socket::ESP8266Socket(
Christopher Haster 40:83c6b4129468 146 ESP8266 *esp,
Christopher Haster 40:83c6b4129468 147 socket_protocol_t proto,
Christopher Haster 40:83c6b4129468 148 int id)
Christopher Haster 40:83c6b4129468 149 : _esp(esp)
Christopher Haster 40:83c6b4129468 150 , _proto(proto)
Christopher Haster 40:83c6b4129468 151 , _id(id)
Christopher Haster 40:83c6b4129468 152 {
Christopher Haster 40:83c6b4129468 153 }
Christopher Haster 40:83c6b4129468 154
Christopher Haster 42:4bf09cadf328 155 ESP8266Socket::~ESP8266Socket()
Christopher Haster 40:83c6b4129468 156 {
Christopher Haster 40:83c6b4129468 157 }
Christopher Haster 40:83c6b4129468 158
Christopher Haster 42:4bf09cadf328 159 int32_t ESP8266Socket::open(const char *ip, uint16_t port)
Christopher Haster 40:83c6b4129468 160 {
Christopher Haster 45:538e5ce2f0d3 161 _esp->setTimeout(ESP8266_MISC_TIMEOUT);
Christopher Haster 45:538e5ce2f0d3 162
Christopher Haster 40:83c6b4129468 163 const char *proto = (_proto == SOCK_UDP) ? "UDP" : "TCP";
Christopher Haster 40:83c6b4129468 164
Christopher Haster 40:83c6b4129468 165 if (!_esp->open(proto, _id, ip, port)) {
Christopher Haster 43:7c010b1db697 166 return NS_ERROR_TIMEOUT;
Christopher Haster 40:83c6b4129468 167 }
Christopher Haster 40:83c6b4129468 168
Christopher Haster 40:83c6b4129468 169 return 0;
Christopher Haster 40:83c6b4129468 170 }
Christopher Haster 40:83c6b4129468 171
Christopher Haster 42:4bf09cadf328 172 int32_t ESP8266Socket::close()
Christopher Haster 40:83c6b4129468 173 {
Christopher Haster 45:538e5ce2f0d3 174 _esp->setTimeout(ESP8266_MISC_TIMEOUT);
Christopher Haster 45:538e5ce2f0d3 175
Christopher Haster 40:83c6b4129468 176 if (!_esp->close(_id)) {
Christopher Haster 45:538e5ce2f0d3 177 return NS_ERROR_TIMEOUT;
Christopher Haster 40:83c6b4129468 178 }
Christopher Haster 40:83c6b4129468 179
Christopher Haster 40:83c6b4129468 180 return 0;
Christopher Haster 40:83c6b4129468 181 }
Christopher Haster 40:83c6b4129468 182
Christopher Haster 44:7ac7eb406603 183 int32_t ESP8266Socket::send(const void *data, uint32_t size)
Christopher Haster 40:83c6b4129468 184 {
Christopher Haster 45:538e5ce2f0d3 185 _esp->setTimeout(ESP8266_SEND_TIMEOUT);
Christopher Haster 45:538e5ce2f0d3 186
Christopher Haster 44:7ac7eb406603 187 if (!_esp->send(_id, data, size)) {
Christopher Haster 43:7c010b1db697 188 return NS_ERROR_TIMEOUT;
Christopher Haster 40:83c6b4129468 189 }
Christopher Haster 40:83c6b4129468 190
Christopher Haster 40:83c6b4129468 191 return 0;
Christopher Haster 40:83c6b4129468 192 }
Christopher Haster 40:83c6b4129468 193
Christopher Haster 44:7ac7eb406603 194 int32_t ESP8266Socket::recv(void *data, uint32_t size)
Christopher Haster 40:83c6b4129468 195 {
Christopher Haster 45:538e5ce2f0d3 196 _esp->setTimeout(ESP8266_RECV_TIMEOUT);
Christopher Haster 44:7ac7eb406603 197
Christopher Haster 44:7ac7eb406603 198 int32_t recv = _esp->recv(_id, data, size);
Christopher Haster 44:7ac7eb406603 199
Christopher Haster 44:7ac7eb406603 200 if (recv < 0) {
Christopher Haster 44:7ac7eb406603 201 return 0;
Christopher Haster 43:7c010b1db697 202 }
Christopher Haster 43:7c010b1db697 203
Christopher Haster 44:7ac7eb406603 204 return recv;
Christopher Haster 40:83c6b4129468 205 }
Christopher Haster 40:83c6b4129468 206
Christopher Haster 42:4bf09cadf328 207 int ESP8266Socket::getID() const {
Christopher Haster 40:83c6b4129468 208 return _id;
Christopher Haster 40:83c6b4129468 209 }
Christopher Haster 40:83c6b4129468 210