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:
Wed Feb 24 23:14:45 2016 -0600
Branch:
api-changes
Revision:
43:7c010b1db697
Parent:
42:4bf09cadf328
Child:
44:7ac7eb406603
Added error codes where necessary

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