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:
sarahmarshy
Date:
Mon Jul 27 15:17:18 2015 +0000
Revision:
25:91c4e9d34b77
Parent:
23:fd0f3197c30b
Child:
26:6e36dd3cec3f
DnsQuery support

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 11:288c15b80a26 16
sarahmarshy 18:9fc7976c7b27 17 #include "ESP8266Interface.h"
sarahmarshy 18:9fc7976c7b27 18 #include <string>
sam_grove 11:288c15b80a26 19
sarahmarshy 18:9fc7976c7b27 20 ESP8266Interface::ESP8266Interface(PinName tx, PinName rx)
sarahmarshy 23:fd0f3197c30b 21 :esp8266(tx,rx)
sam_grove 11:288c15b80a26 22 {
sarahmarshy 22:312453862371 23 uuidCounter = 0;
sarahmarshy 22:312453862371 24 for(int i = 0; i<numSockets; i++){
sarahmarshy 18:9fc7976c7b27 25 availableID[i] = -1;
sarahmarshy 18:9fc7976c7b27 26 }
sam_grove 11:288c15b80a26 27 }
sam_grove 11:288c15b80a26 28
sarahmarshy 18:9fc7976c7b27 29 int32_t ESP8266Interface::init(void)
sarahmarshy 18:9fc7976c7b27 30 {
sarahmarshy 23:fd0f3197c30b 31 if (!esp8266.startup())
sarahmarshy 18:9fc7976c7b27 32 return -1;
sarahmarshy 23:fd0f3197c30b 33 if (!esp8266.reset())
sarahmarshy 18:9fc7976c7b27 34 return -1;
sarahmarshy 23:fd0f3197c30b 35 if (!esp8266.wifiMode(3))
sarahmarshy 18:9fc7976c7b27 36 return -1;
sarahmarshy 23:fd0f3197c30b 37 if (!esp8266.multipleConnections(true))
sarahmarshy 18:9fc7976c7b27 38 return -1;
sarahmarshy 18:9fc7976c7b27 39 return 0;
sarahmarshy 18:9fc7976c7b27 40
sam_grove 11:288c15b80a26 41 }
sam_grove 11:288c15b80a26 42
sarahmarshy 18:9fc7976c7b27 43 int32_t ESP8266Interface::init(const char *ip, const char *mask, const char *gateway)
sam_grove 11:288c15b80a26 44 {
sarahmarshy 23:fd0f3197c30b 45 return -1;
sam_grove 11:288c15b80a26 46 }
sam_grove 11:288c15b80a26 47
sarahmarshy 18:9fc7976c7b27 48 int32_t ESP8266Interface::connect(uint32_t timeout_ms)
sam_grove 11:288c15b80a26 49 {
sam_grove 11:288c15b80a26 50 return -1;
sam_grove 11:288c15b80a26 51 }
sam_grove 11:288c15b80a26 52
sarahmarshy 18:9fc7976c7b27 53 int32_t ESP8266Interface::connect(const char *ap, const char *pass_phrase, wifi_security_t security, uint32_t timeout_ms)
sam_grove 11:288c15b80a26 54 {
sarahmarshy 23:fd0f3197c30b 55 esp8266.setTimeout(timeout_ms);
sarahmarshy 23:fd0f3197c30b 56
sarahmarshy 23:fd0f3197c30b 57 if (!esp8266.dhcp(1,true))
sarahmarshy 18:9fc7976c7b27 58 return -1;
sarahmarshy 23:fd0f3197c30b 59 if (!esp8266.connect(ap,pass_phrase)){
sarahmarshy 18:9fc7976c7b27 60 return -1;
sarahmarshy 18:9fc7976c7b27 61 }
sarahmarshy 18:9fc7976c7b27 62 return 0;
sam_grove 11:288c15b80a26 63 }
sam_grove 11:288c15b80a26 64
sarahmarshy 22:312453862371 65 int32_t ESP8266Interface::disconnect(void)
sam_grove 11:288c15b80a26 66 {
sarahmarshy 23:fd0f3197c30b 67 if (!esp8266.disconnect())
sarahmarshy 22:312453862371 68 return -1;
sarahmarshy 22:312453862371 69 for(int i=0; i<numSockets; i++){
sarahmarshy 22:312453862371 70 SocketInterface *socket = sockets[availableID[i]];
sarahmarshy 22:312453862371 71 deallocateSocket(socket);
sarahmarshy 22:312453862371 72 }
sarahmarshy 22:312453862371 73 return 0;
sarahmarshy 22:312453862371 74
sam_grove 11:288c15b80a26 75 }
sam_grove 11:288c15b80a26 76
sarahmarshy 18:9fc7976c7b27 77 char *ESP8266Interface::getIPAddress(void)
sarahmarshy 18:9fc7976c7b27 78 {
sarahmarshy 23:fd0f3197c30b 79 if(!esp8266.getIPAddress(ip))
sarahmarshy 18:9fc7976c7b27 80 return NULL;
sarahmarshy 18:9fc7976c7b27 81 return ip;
sarahmarshy 18:9fc7976c7b27 82 }
sarahmarshy 18:9fc7976c7b27 83
sarahmarshy 18:9fc7976c7b27 84 char *ESP8266Interface::getGateway(void) const
sam_grove 11:288c15b80a26 85 {
sam_grove 11:288c15b80a26 86 return 0;
sam_grove 11:288c15b80a26 87 }
sam_grove 11:288c15b80a26 88
sarahmarshy 18:9fc7976c7b27 89 char *ESP8266Interface::getNetworkMask(void) const
sam_grove 11:288c15b80a26 90 {
sam_grove 11:288c15b80a26 91 return 0;
sam_grove 11:288c15b80a26 92 }
sam_grove 11:288c15b80a26 93
sarahmarshy 18:9fc7976c7b27 94 char *ESP8266Interface::getMACAddress(void) const
sam_grove 11:288c15b80a26 95 {
sam_grove 11:288c15b80a26 96 return 0;
sam_grove 11:288c15b80a26 97 }
sam_grove 11:288c15b80a26 98
sarahmarshy 18:9fc7976c7b27 99 int32_t ESP8266Interface::isConnected(void)
sam_grove 11:288c15b80a26 100 {
sarahmarshy 18:9fc7976c7b27 101 return (getIPAddress()==NULL) ? -1 : 0;
sam_grove 11:288c15b80a26 102 }
sam_grove 11:288c15b80a26 103
sarahmarshy 18:9fc7976c7b27 104 SocketInterface *ESP8266Interface::allocateSocket(socket_protocol_t socketProtocol)
sarahmarshy 18:9fc7976c7b27 105 {
sarahmarshy 18:9fc7976c7b27 106 int id = -1;
sarahmarshy 18:9fc7976c7b27 107 //Look through the array of available sockets for an unused ID
sarahmarshy 22:312453862371 108 for(int i=0; i<numSockets; i++){
sarahmarshy 18:9fc7976c7b27 109 if (availableID[i] == -1){
sarahmarshy 18:9fc7976c7b27 110 id = i;
sarahmarshy 18:9fc7976c7b27 111 availableID[i] = uuidCounter;
sarahmarshy 18:9fc7976c7b27 112 break;
sarahmarshy 18:9fc7976c7b27 113 }
sarahmarshy 18:9fc7976c7b27 114 }
sarahmarshy 18:9fc7976c7b27 115 if (id == -1){
sarahmarshy 18:9fc7976c7b27 116 return NULL;//tried to allocate more than the maximum 5 sockets
sarahmarshy 18:9fc7976c7b27 117 }
sarahmarshy 23:fd0f3197c30b 118 ESP8266Socket *socket = new ESP8266Socket(uuidCounter++, &esp8266, socketProtocol, (uint8_t)id);
sarahmarshy 22:312453862371 119 sockets[socket->getHandle()] = socket;
sarahmarshy 18:9fc7976c7b27 120 return socket;
sam_grove 11:288c15b80a26 121 }
sam_grove 13:0186e9e35a24 122
sarahmarshy 18:9fc7976c7b27 123 int ESP8266Interface::deallocateSocket(SocketInterface *socket)
bridadan 16:b2f781416464 124 {
sarahmarshy 18:9fc7976c7b27 125 int id = (int)static_cast<ESP8266Socket*>(socket)->getID();
sarahmarshy 18:9fc7976c7b27 126 availableID[id] = -1;
sarahmarshy 22:312453862371 127
sarahmarshy 22:312453862371 128 std::map<uint32_t, SocketInterface*>::iterator it;
sarahmarshy 22:312453862371 129
sarahmarshy 22:312453862371 130 // Check if socket is owned by WiFiRadioInterface
sarahmarshy 22:312453862371 131 it = sockets.find(socket->getHandle());
sarahmarshy 22:312453862371 132
sarahmarshy 22:312453862371 133 if (it != sockets.end()) {
sarahmarshy 22:312453862371 134 // If so, erase it from the internal socket map and deallocate the socket
sarahmarshy 22:312453862371 135 sockets.erase(it);
sarahmarshy 22:312453862371 136 delete socket;
sarahmarshy 22:312453862371 137 return 0;
sarahmarshy 22:312453862371 138 } else {
sarahmarshy 22:312453862371 139 // Socket is not owned by WiFiRadioInterface, so return -1 error
sarahmarshy 22:312453862371 140 return -1;
sarahmarshy 22:312453862371 141 }
bridadan 16:b2f781416464 142 }
sarahmarshy 25:91c4e9d34b77 143 void ESP8266Interface::getHostByName(const char *name, char* hostIP)
sarahmarshy 25:91c4e9d34b77 144 {
sarahmarshy 25:91c4e9d34b77 145 SocketInterface* sock = this->allocateSocket(SOCK_UDP);
sarahmarshy 25:91c4e9d34b77 146 IPADDRESS_t ip;
sarahmarshy 25:91c4e9d34b77 147 DnsQuery dns(sock);
sarahmarshy 25:91c4e9d34b77 148 dns.gethostbyname(name, ip);
sarahmarshy 25:91c4e9d34b77 149 sock->close();
sarahmarshy 25:91c4e9d34b77 150 this->deallocateSocket(sock);
sarahmarshy 25:91c4e9d34b77 151 char* resolved = ip.string_format();
sarahmarshy 25:91c4e9d34b77 152 memcpy(hostIP, resolved, strlen(resolved)+1);
sarahmarshy 25:91c4e9d34b77 153 printf("IP:%s\n",resolved);
sarahmarshy 25:91c4e9d34b77 154 }
bridadan 16:b2f781416464 155
sarahmarshy 23:fd0f3197c30b 156 ESP8266Socket::ESP8266Socket(uint32_t handle, ESP8266 *driver, socket_protocol_t type, uint8_t id)
sarahmarshy 23:fd0f3197c30b 157 : driver(driver), _id(id)
sarahmarshy 18:9fc7976c7b27 158 {
sarahmarshy 22:312453862371 159 _handle = handle;
sarahmarshy 18:9fc7976c7b27 160 SocketInterface::_type = type;
sarahmarshy 18:9fc7976c7b27 161 }
sarahmarshy 18:9fc7976c7b27 162
sarahmarshy 18:9fc7976c7b27 163 const char *ESP8266Socket::getHostByName(const char *name) const
sam_grove 13:0186e9e35a24 164 {
sam_grove 13:0186e9e35a24 165 return 0;
sam_grove 13:0186e9e35a24 166 }
sam_grove 13:0186e9e35a24 167
sarahmarshy 18:9fc7976c7b27 168 void ESP8266Socket::setAddress(const char* addr)
bridadan 16:b2f781416464 169 {
sarahmarshy 18:9fc7976c7b27 170 _addr = (char*)addr;
sarahmarshy 18:9fc7976c7b27 171 }
sarahmarshy 18:9fc7976c7b27 172
sarahmarshy 18:9fc7976c7b27 173 void ESP8266Socket::setPort(uint16_t port)
sarahmarshy 18:9fc7976c7b27 174 {
sarahmarshy 18:9fc7976c7b27 175 _port = port;
bridadan 16:b2f781416464 176 }
bridadan 16:b2f781416464 177
sarahmarshy 18:9fc7976c7b27 178 void ESP8266Socket::setAddressPort(const char* addr, uint16_t port)
bridadan 16:b2f781416464 179 {
sarahmarshy 18:9fc7976c7b27 180 _addr = (char*)addr;
sarahmarshy 18:9fc7976c7b27 181 _port = port;
bridadan 16:b2f781416464 182 }
bridadan 16:b2f781416464 183
sarahmarshy 18:9fc7976c7b27 184 const char *ESP8266Socket::getAddress(void) const
sarahmarshy 18:9fc7976c7b27 185 {
sarahmarshy 18:9fc7976c7b27 186 return (const char*)_addr;
sarahmarshy 18:9fc7976c7b27 187 }
sarahmarshy 18:9fc7976c7b27 188
sarahmarshy 18:9fc7976c7b27 189 uint16_t ESP8266Socket::getPort(void) const
sarahmarshy 18:9fc7976c7b27 190 {
sarahmarshy 18:9fc7976c7b27 191 return _port;
sarahmarshy 18:9fc7976c7b27 192 }
sarahmarshy 18:9fc7976c7b27 193
sarahmarshy 18:9fc7976c7b27 194
sarahmarshy 18:9fc7976c7b27 195 int32_t ESP8266Socket::bind(uint16_t port) const
sam_grove 13:0186e9e35a24 196 {
sam_grove 13:0186e9e35a24 197 return -1;
sam_grove 13:0186e9e35a24 198 }
sam_grove 13:0186e9e35a24 199
sarahmarshy 18:9fc7976c7b27 200 int32_t ESP8266Socket::listen(void) const
sam_grove 13:0186e9e35a24 201 {
sam_grove 13:0186e9e35a24 202 return -1;
sam_grove 13:0186e9e35a24 203 }
sam_grove 13:0186e9e35a24 204
sarahmarshy 18:9fc7976c7b27 205 int32_t ESP8266Socket::accept() const
sam_grove 13:0186e9e35a24 206 {
sam_grove 13:0186e9e35a24 207 return -1;
sam_grove 13:0186e9e35a24 208 }
sam_grove 13:0186e9e35a24 209
sarahmarshy 18:9fc7976c7b27 210 int32_t ESP8266Socket::open()
sam_grove 13:0186e9e35a24 211 {
sarahmarshy 18:9fc7976c7b27 212 string sock_type;
sarahmarshy 18:9fc7976c7b27 213 if(_type == SOCK_UDP)
sarahmarshy 18:9fc7976c7b27 214 sock_type = "UDP";
sarahmarshy 18:9fc7976c7b27 215 else if(_type == SOCK_TCP)
sarahmarshy 18:9fc7976c7b27 216 sock_type = "TCP";
sarahmarshy 23:fd0f3197c30b 217
sarahmarshy 23:fd0f3197c30b 218 if (!driver->openSocket(sock_type, _id, _port, _addr)){
sarahmarshy 25:91c4e9d34b77 219 return -1;//opening socket not successful
sarahmarshy 18:9fc7976c7b27 220 }
sam_grove 13:0186e9e35a24 221 return 0;
sarahmarshy 18:9fc7976c7b27 222
sam_grove 13:0186e9e35a24 223 }
sam_grove 13:0186e9e35a24 224
sarahmarshy 18:9fc7976c7b27 225 int32_t ESP8266Socket::send(const void *data, uint32_t amount, uint32_t timeout_ms)
bridadan 16:b2f781416464 226 {
sarahmarshy 18:9fc7976c7b27 227
sarahmarshy 23:fd0f3197c30b 228 driver->setTimeout((int)timeout_ms);
sarahmarshy 23:fd0f3197c30b 229 if(!driver->sendData(_id, data, amount)){
sarahmarshy 18:9fc7976c7b27 230 return -1;
sarahmarshy 18:9fc7976c7b27 231 }
bridadan 16:b2f781416464 232 return 0;
bridadan 16:b2f781416464 233 }
bridadan 16:b2f781416464 234
sarahmarshy 18:9fc7976c7b27 235 uint32_t ESP8266Socket::recv(void *data, uint32_t amount, uint32_t timeout_ms)
sam_grove 13:0186e9e35a24 236 {
sarahmarshy 23:fd0f3197c30b 237
sarahmarshy 23:fd0f3197c30b 238 return driver->recv(data, amount);
sarahmarshy 23:fd0f3197c30b 239
sam_grove 13:0186e9e35a24 240 }
sam_grove 13:0186e9e35a24 241
sarahmarshy 18:9fc7976c7b27 242 int32_t ESP8266Socket::close() const
sam_grove 13:0186e9e35a24 243 {
sarahmarshy 18:9fc7976c7b27 244
sarahmarshy 23:fd0f3197c30b 245 if (!driver->close(_id)){
sarahmarshy 25:91c4e9d34b77 246 return -1;//closing socket not successful
sarahmarshy 18:9fc7976c7b27 247 }
sam_grove 13:0186e9e35a24 248 return 0;
sam_grove 13:0186e9e35a24 249 }
sarahmarshy 22:312453862371 250
sarahmarshy 22:312453862371 251 uint32_t ESP8266Socket::getHandle()const
sarahmarshy 22:312453862371 252 {
sarahmarshy 22:312453862371 253 return _handle;
sarahmarshy 22:312453862371 254 }
sarahmarshy 22:312453862371 255
sarahmarshy 18:9fc7976c7b27 256 uint8_t ESP8266Socket::getID()
sam_grove 13:0186e9e35a24 257 {
sarahmarshy 18:9fc7976c7b27 258 return _id;
sarahmarshy 25:91c4e9d34b77 259 }
sarahmarshy 25:91c4e9d34b77 260