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:
Fri Apr 01 17:28:04 2016 +0000
Revision:
54:e78fad32cfff
Parent:
51:eb8c3577e22d
Child:
56:34829ec3a3da
Matched 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 40:83c6b4129468 33 ESP8266Interface::~ESP8266Interface()
Christopher Haster 40:83c6b4129468 34 {
Christopher Haster 40:83c6b4129468 35 }
Christopher Haster 40:83c6b4129468 36
Christopher Haster 34:9c26a3dcdc1f 37 int32_t ESP8266Interface::connect(
Christopher Haster 34:9c26a3dcdc1f 38 const char *ap,
Christopher Haster 34:9c26a3dcdc1f 39 const char *pass_phrase,
geky 50:2d7f6b97234e 40 ns_security_t)
sam_grove 11:288c15b80a26 41 {
Christopher Haster 45:538e5ce2f0d3 42 _esp.setTimeout(ESP8266_CONNECT_TIMEOUT);
Christopher Haster 45:538e5ce2f0d3 43
Christopher Haster 41:3f4d5f4862d2 44 if (!_esp.startup(3)) {
Christopher Haster 43:7c010b1db697 45 return NS_ERROR_DEVICE_ERROR;
Christopher Haster 41:3f4d5f4862d2 46 }
Christopher Haster 41:3f4d5f4862d2 47
Christopher Haster 41:3f4d5f4862d2 48 if (!_esp.dhcp(true, 1)) {
Christopher Haster 43:7c010b1db697 49 return NS_ERROR_DHCP_FAILURE;
Christopher Haster 41:3f4d5f4862d2 50 }
Christopher Haster 41:3f4d5f4862d2 51
Christopher Haster 41:3f4d5f4862d2 52 if (!_esp.connect(ap, pass_phrase)) {
Christopher Haster 43:7c010b1db697 53 return NS_ERROR_NO_CONNECTION;
Christopher Haster 41:3f4d5f4862d2 54 }
Christopher Haster 34:9c26a3dcdc1f 55
Christopher Haster 46:6b1bd1268074 56 if (!_esp.getIPAddress()) {
Christopher Haster 46:6b1bd1268074 57 return NS_ERROR_DHCP_FAILURE;
Christopher Haster 41:3f4d5f4862d2 58 }
Christopher Haster 34:9c26a3dcdc1f 59
sam_grove 24:37504440f296 60 return 0;
sam_grove 11:288c15b80a26 61 }
sam_grove 11:288c15b80a26 62
Christopher Haster 34:9c26a3dcdc1f 63 int32_t ESP8266Interface::disconnect()
sam_grove 11:288c15b80a26 64 {
Christopher Haster 45:538e5ce2f0d3 65 _esp.setTimeout(ESP8266_MISC_TIMEOUT);
Christopher Haster 45:538e5ce2f0d3 66
Christopher Haster 41:3f4d5f4862d2 67 if (!_esp.disconnect()) {
Christopher Haster 43:7c010b1db697 68 return NS_ERROR_DEVICE_ERROR;
Christopher Haster 41:3f4d5f4862d2 69 }
Christopher Haster 34:9c26a3dcdc1f 70
sarahmarshy 22:312453862371 71 return 0;
sam_grove 11:288c15b80a26 72 }
sam_grove 11:288c15b80a26 73
Christopher Haster 34:9c26a3dcdc1f 74 const char *ESP8266Interface::getIPAddress()
sarahmarshy 18:9fc7976c7b27 75 {
Christopher Haster 46:6b1bd1268074 76 return _esp.getIPAddress();
sarahmarshy 18:9fc7976c7b27 77 }
sarahmarshy 18:9fc7976c7b27 78
Christopher Haster 34:9c26a3dcdc1f 79 const char *ESP8266Interface::getMACAddress()
sam_grove 11:288c15b80a26 80 {
Christopher Haster 46:6b1bd1268074 81 return _esp.getMACAddress();
sam_grove 11:288c15b80a26 82 }
sam_grove 11:288c15b80a26 83
geky 50:2d7f6b97234e 84 SocketInterface *ESP8266Interface::createSocket(ns_protocol_t proto)
sam_grove 11:288c15b80a26 85 {
Christopher Haster 34:9c26a3dcdc1f 86 // Look for an unused socket
sarahmarshy 18:9fc7976c7b27 87 int id = -1;
Christopher Haster 34:9c26a3dcdc1f 88
Christopher Haster 34:9c26a3dcdc1f 89 for (int i = 0; i < ESP8266_SOCKET_COUNT; i++) {
Christopher Haster 34:9c26a3dcdc1f 90 if (!_ids[i]) {
sam_grove 24:37504440f296 91 id = i;
Christopher Haster 34:9c26a3dcdc1f 92 _ids[i] = true;
sarahmarshy 18:9fc7976c7b27 93 break;
sarahmarshy 18:9fc7976c7b27 94 }
sarahmarshy 18:9fc7976c7b27 95 }
Christopher Haster 34:9c26a3dcdc1f 96
sam_grove 24:37504440f296 97 if (id == -1) {
Christopher Haster 34:9c26a3dcdc1f 98 return 0;
sarahmarshy 22:312453862371 99 }
bridadan 16:b2f781416464 100
Christopher Haster 42:4bf09cadf328 101 return new ESP8266Socket(&_esp, proto, id);
bridadan 16:b2f781416464 102 }
bridadan 16:b2f781416464 103
Christopher Haster 34:9c26a3dcdc1f 104 void ESP8266Interface::destroySocket(SocketInterface *iface)
sarahmarshy 18:9fc7976c7b27 105 {
Christopher Haster 42:4bf09cadf328 106 ESP8266Socket *socket = (ESP8266Socket *)iface;
geky 51:eb8c3577e22d 107 _ids[socket->_id] = false;
Christopher Haster 39:7e85bf8003fa 108 delete socket;
sam_grove 13:0186e9e35a24 109 }
sam_grove 13:0186e9e35a24 110
Christopher Haster 40:83c6b4129468 111
Christopher Haster 40:83c6b4129468 112 // ESP8266Socket implementation
geky 51:eb8c3577e22d 113 int32_t ESP8266Interface::ESP8266Socket::open(const char *ip, uint16_t port)
Christopher Haster 40:83c6b4129468 114 {
Christopher Haster 45:538e5ce2f0d3 115 _esp->setTimeout(ESP8266_MISC_TIMEOUT);
Christopher Haster 45:538e5ce2f0d3 116
geky 50:2d7f6b97234e 117 const char *proto = (_proto == NS_UDP) ? "UDP" : "TCP";
Christopher Haster 40:83c6b4129468 118
Christopher Haster 40:83c6b4129468 119 if (!_esp->open(proto, _id, ip, port)) {
Christopher Haster 43:7c010b1db697 120 return NS_ERROR_TIMEOUT;
Christopher Haster 40:83c6b4129468 121 }
Christopher Haster 40:83c6b4129468 122
Christopher Haster 40:83c6b4129468 123 return 0;
Christopher Haster 40:83c6b4129468 124 }
Christopher Haster 40:83c6b4129468 125
geky 51:eb8c3577e22d 126 int32_t ESP8266Interface::ESP8266Interface::ESP8266Socket::close()
Christopher Haster 40:83c6b4129468 127 {
Christopher Haster 45:538e5ce2f0d3 128 _esp->setTimeout(ESP8266_MISC_TIMEOUT);
Christopher Haster 45:538e5ce2f0d3 129
Christopher Haster 40:83c6b4129468 130 if (!_esp->close(_id)) {
Christopher Haster 45:538e5ce2f0d3 131 return NS_ERROR_TIMEOUT;
Christopher Haster 40:83c6b4129468 132 }
Christopher Haster 40:83c6b4129468 133
Christopher Haster 40:83c6b4129468 134 return 0;
Christopher Haster 40:83c6b4129468 135 }
Christopher Haster 40:83c6b4129468 136
geky 51:eb8c3577e22d 137 int32_t ESP8266Interface::ESP8266Socket::send(const void *data, uint32_t size)
Christopher Haster 40:83c6b4129468 138 {
Christopher Haster 45:538e5ce2f0d3 139 _esp->setTimeout(ESP8266_SEND_TIMEOUT);
Christopher Haster 45:538e5ce2f0d3 140
Christopher Haster 44:7ac7eb406603 141 if (!_esp->send(_id, data, size)) {
Christopher Haster 43:7c010b1db697 142 return NS_ERROR_TIMEOUT;
Christopher Haster 40:83c6b4129468 143 }
Christopher Haster 40:83c6b4129468 144
geky 54:e78fad32cfff 145 return size;
Christopher Haster 40:83c6b4129468 146 }
Christopher Haster 40:83c6b4129468 147
geky 51:eb8c3577e22d 148 int32_t ESP8266Interface::ESP8266Socket::recv(void *data, uint32_t size)
Christopher Haster 40:83c6b4129468 149 {
Christopher Haster 45:538e5ce2f0d3 150 _esp->setTimeout(ESP8266_RECV_TIMEOUT);
Christopher Haster 44:7ac7eb406603 151
Christopher Haster 44:7ac7eb406603 152 int32_t recv = _esp->recv(_id, data, size);
Christopher Haster 44:7ac7eb406603 153
Christopher Haster 44:7ac7eb406603 154 if (recv < 0) {
geky 54:e78fad32cfff 155 return NS_ERROR_WOULD_BLOCK;
Christopher Haster 43:7c010b1db697 156 }
Christopher Haster 43:7c010b1db697 157
Christopher Haster 44:7ac7eb406603 158 return recv;
Christopher Haster 40:83c6b4129468 159 }
Christopher Haster 40:83c6b4129468 160