Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: ESP8266
Fork of ESP8266Interface by
ESP8266Interface.cpp@33:276cb279df57, 2016-02-22 (annotated)
- Committer:
- geky
- Date:
- Mon Feb 22 23:56:33 2016 +0000
- Revision:
- 33:276cb279df57
- Parent:
- 30:c19f1e61063b
- Child:
- 34:9c26a3dcdc1f
Matched changes to ESP8266
Who changed what in which revision?
| User | Revision | Line number | New 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 | |
| sam_grove | 30:c19f1e61063b | 19 | ESP8266Interface::ESP8266Interface(PinName tx, PinName rx, uint8_t trace) : esp8266(tx, rx, trace) |
| sam_grove | 11:288c15b80a26 | 20 | { |
| sarahmarshy | 22:312453862371 | 21 | uuidCounter = 0; |
| sam_grove | 24:37504440f296 | 22 | std::fill_n(availableID, sizeof(availableID)/sizeof(int), -1); |
| sam_grove | 11:288c15b80a26 | 23 | } |
| sam_grove | 11:288c15b80a26 | 24 | |
| sam_grove | 24:37504440f296 | 25 | int32_t ESP8266Interface::init(void) |
| sam_grove | 24:37504440f296 | 26 | { |
| sarahmarshy | 18:9fc7976c7b27 | 27 | return 0; |
| sam_grove | 11:288c15b80a26 | 28 | } |
| sam_grove | 11:288c15b80a26 | 29 | |
| sam_grove | 24:37504440f296 | 30 | int32_t ESP8266Interface::init(const char *ip, const char *mask, const char *gateway) |
| sam_grove | 11:288c15b80a26 | 31 | { |
| sarahmarshy | 23:fd0f3197c30b | 32 | return -1; |
| sam_grove | 11:288c15b80a26 | 33 | } |
| sam_grove | 11:288c15b80a26 | 34 | |
| sam_grove | 24:37504440f296 | 35 | int32_t ESP8266Interface::connect(uint32_t timeout_ms) |
| sam_grove | 11:288c15b80a26 | 36 | { |
| sam_grove | 11:288c15b80a26 | 37 | return -1; |
| sam_grove | 11:288c15b80a26 | 38 | } |
| sam_grove | 11:288c15b80a26 | 39 | |
| sarahmarshy | 18:9fc7976c7b27 | 40 | int32_t ESP8266Interface::connect(const char *ap, const char *pass_phrase, wifi_security_t security, uint32_t timeout_ms) |
| sam_grove | 11:288c15b80a26 | 41 | { |
| sarahmarshy | 23:fd0f3197c30b | 42 | esp8266.setTimeout(timeout_ms); |
| geky | 33:276cb279df57 | 43 | if (!esp8266.startup(3)) { |
| geky | 33:276cb279df57 | 44 | return -1; |
| geky | 33:276cb279df57 | 45 | } |
| geky | 33:276cb279df57 | 46 | if (!esp8266.dhcp(true, 1)) { |
| sarahmarshy | 18:9fc7976c7b27 | 47 | return -1; |
| sarahmarshy | 18:9fc7976c7b27 | 48 | } |
| sam_grove | 24:37504440f296 | 49 | if (!esp8266.connect(ap, pass_phrase)) { |
| sam_grove | 24:37504440f296 | 50 | return -1; |
| sam_grove | 24:37504440f296 | 51 | } |
| geky | 33:276cb279df57 | 52 | if (!esp8266.isConnected()) { |
| geky | 33:276cb279df57 | 53 | return -1; |
| geky | 33:276cb279df57 | 54 | } |
| sam_grove | 24:37504440f296 | 55 | return 0; |
| sam_grove | 11:288c15b80a26 | 56 | } |
| sam_grove | 11:288c15b80a26 | 57 | |
| sarahmarshy | 22:312453862371 | 58 | int32_t ESP8266Interface::disconnect(void) |
| sam_grove | 11:288c15b80a26 | 59 | { |
| sam_grove | 24:37504440f296 | 60 | if (!esp8266.disconnect()) { |
| sarahmarshy | 22:312453862371 | 61 | return -1; |
| sam_grove | 24:37504440f296 | 62 | } |
| sam_grove | 30:c19f1e61063b | 63 | for (int i=0; i<numSockets; i++) { |
| sarahmarshy | 22:312453862371 | 64 | SocketInterface *socket = sockets[availableID[i]]; |
| sam_grove | 30:c19f1e61063b | 65 | if (socket) { |
| sam_grove | 30:c19f1e61063b | 66 | deallocateSocket(socket); |
| sam_grove | 30:c19f1e61063b | 67 | } |
| sarahmarshy | 22:312453862371 | 68 | } |
| sarahmarshy | 22:312453862371 | 69 | return 0; |
| sam_grove | 11:288c15b80a26 | 70 | } |
| sam_grove | 11:288c15b80a26 | 71 | |
| sarahmarshy | 18:9fc7976c7b27 | 72 | char *ESP8266Interface::getIPAddress(void) |
| sarahmarshy | 18:9fc7976c7b27 | 73 | { |
| geky | 33:276cb279df57 | 74 | const char *src = esp8266.getIPAddress(); |
| geky | 33:276cb279df57 | 75 | if (!src) return 0; |
| geky | 33:276cb279df57 | 76 | |
| geky | 33:276cb279df57 | 77 | strcpy(ip, src); |
| sarahmarshy | 18:9fc7976c7b27 | 78 | return ip; |
| sarahmarshy | 18:9fc7976c7b27 | 79 | } |
| sarahmarshy | 18:9fc7976c7b27 | 80 | |
| sarahmarshy | 18:9fc7976c7b27 | 81 | char *ESP8266Interface::getGateway(void) const |
| sam_grove | 11:288c15b80a26 | 82 | { |
| sam_grove | 11:288c15b80a26 | 83 | return 0; |
| sam_grove | 11:288c15b80a26 | 84 | } |
| sam_grove | 11:288c15b80a26 | 85 | |
| sarahmarshy | 18:9fc7976c7b27 | 86 | char *ESP8266Interface::getNetworkMask(void) const |
| sam_grove | 11:288c15b80a26 | 87 | { |
| sam_grove | 11:288c15b80a26 | 88 | return 0; |
| sam_grove | 11:288c15b80a26 | 89 | } |
| sam_grove | 11:288c15b80a26 | 90 | |
| sarahmarshy | 18:9fc7976c7b27 | 91 | char *ESP8266Interface::getMACAddress(void) const |
| sam_grove | 11:288c15b80a26 | 92 | { |
| sam_grove | 11:288c15b80a26 | 93 | return 0; |
| sam_grove | 11:288c15b80a26 | 94 | } |
| sam_grove | 11:288c15b80a26 | 95 | |
| sam_grove | 24:37504440f296 | 96 | int32_t ESP8266Interface::isConnected(void) |
| sam_grove | 11:288c15b80a26 | 97 | { |
| sam_grove | 24:37504440f296 | 98 | return (getIPAddress() == NULL) ? -1 : 0; |
| sam_grove | 11:288c15b80a26 | 99 | } |
| sam_grove | 11:288c15b80a26 | 100 | |
| sam_grove | 24:37504440f296 | 101 | SocketInterface *ESP8266Interface::allocateSocket(socket_protocol_t socketProtocol) |
| sam_grove | 24:37504440f296 | 102 | { |
| sarahmarshy | 18:9fc7976c7b27 | 103 | int id = -1; |
| sarahmarshy | 18:9fc7976c7b27 | 104 | //Look through the array of available sockets for an unused ID |
| sam_grove | 24:37504440f296 | 105 | for(int i=0; i<numSockets; i++) { |
| sam_grove | 24:37504440f296 | 106 | if (availableID[i] == -1) { |
| sam_grove | 24:37504440f296 | 107 | id = i; |
| sarahmarshy | 18:9fc7976c7b27 | 108 | availableID[i] = uuidCounter; |
| sarahmarshy | 18:9fc7976c7b27 | 109 | break; |
| sarahmarshy | 18:9fc7976c7b27 | 110 | } |
| sarahmarshy | 18:9fc7976c7b27 | 111 | } |
| sam_grove | 24:37504440f296 | 112 | if (id == -1) { |
| sam_grove | 24:37504440f296 | 113 | return NULL;//tried to allocate more than the maximum 5 sockets |
| sarahmarshy | 18:9fc7976c7b27 | 114 | } |
| sam_grove | 24:37504440f296 | 115 | ESP8266Socket *socket = new ESP8266Socket(uuidCounter++, esp8266, socketProtocol, (uint8_t)id); |
| sarahmarshy | 22:312453862371 | 116 | sockets[socket->getHandle()] = socket; |
| sarahmarshy | 18:9fc7976c7b27 | 117 | return socket; |
| sam_grove | 11:288c15b80a26 | 118 | } |
| sam_grove | 13:0186e9e35a24 | 119 | |
| sam_grove | 24:37504440f296 | 120 | int ESP8266Interface::deallocateSocket(SocketInterface *socket) |
| bridadan | 16:b2f781416464 | 121 | { |
| sarahmarshy | 18:9fc7976c7b27 | 122 | int id = (int)static_cast<ESP8266Socket*>(socket)->getID(); |
| sarahmarshy | 18:9fc7976c7b27 | 123 | availableID[id] = -1; |
| sam_grove | 24:37504440f296 | 124 | |
| sarahmarshy | 22:312453862371 | 125 | std::map<uint32_t, SocketInterface*>::iterator it; |
| sam_grove | 24:37504440f296 | 126 | |
| sarahmarshy | 22:312453862371 | 127 | // Check if socket is owned by WiFiRadioInterface |
| sarahmarshy | 22:312453862371 | 128 | it = sockets.find(socket->getHandle()); |
| sam_grove | 24:37504440f296 | 129 | |
| sarahmarshy | 22:312453862371 | 130 | if (it != sockets.end()) { |
| sarahmarshy | 22:312453862371 | 131 | // If so, erase it from the internal socket map and deallocate the socket |
| sarahmarshy | 22:312453862371 | 132 | sockets.erase(it); |
| sarahmarshy | 22:312453862371 | 133 | delete socket; |
| sarahmarshy | 22:312453862371 | 134 | } else { |
| sarahmarshy | 22:312453862371 | 135 | // Socket is not owned by WiFiRadioInterface, so return -1 error |
| sarahmarshy | 22:312453862371 | 136 | return -1; |
| sarahmarshy | 22:312453862371 | 137 | } |
| sam_grove | 24:37504440f296 | 138 | return 0; |
| bridadan | 16:b2f781416464 | 139 | } |
| sarahmarshy | 25:91c4e9d34b77 | 140 | void ESP8266Interface::getHostByName(const char *name, char* hostIP) |
| sarahmarshy | 25:91c4e9d34b77 | 141 | { |
| sarahmarshy | 25:91c4e9d34b77 | 142 | SocketInterface* sock = this->allocateSocket(SOCK_UDP); |
| sarahmarshy | 29:f2ae8f47729b | 143 | DnsQuery dns(sock, name, hostIP); |
| sarahmarshy | 25:91c4e9d34b77 | 144 | this->deallocateSocket(sock); |
| sarahmarshy | 25:91c4e9d34b77 | 145 | } |
| bridadan | 16:b2f781416464 | 146 | |
| sam_grove | 24:37504440f296 | 147 | ESP8266Socket::ESP8266Socket(uint32_t handle, ESP8266 &driver, socket_protocol_t type, uint8_t id) |
| sarahmarshy | 18:9fc7976c7b27 | 148 | { |
| sarahmarshy | 22:312453862371 | 149 | _handle = handle; |
| sam_grove | 24:37504440f296 | 150 | _driver = &driver; |
| sam_grove | 24:37504440f296 | 151 | _type = type; |
| sam_grove | 24:37504440f296 | 152 | _id = id; |
| sarahmarshy | 18:9fc7976c7b27 | 153 | } |
| sarahmarshy | 18:9fc7976c7b27 | 154 | |
| sarahmarshy | 18:9fc7976c7b27 | 155 | const char *ESP8266Socket::getHostByName(const char *name) const |
| sam_grove | 13:0186e9e35a24 | 156 | { |
| sam_grove | 13:0186e9e35a24 | 157 | return 0; |
| sam_grove | 13:0186e9e35a24 | 158 | } |
| sam_grove | 13:0186e9e35a24 | 159 | |
| sarahmarshy | 18:9fc7976c7b27 | 160 | void ESP8266Socket::setAddress(const char* addr) |
| bridadan | 16:b2f781416464 | 161 | { |
| sam_grove | 24:37504440f296 | 162 | _addr = addr; |
| sarahmarshy | 18:9fc7976c7b27 | 163 | } |
| sarahmarshy | 18:9fc7976c7b27 | 164 | |
| sam_grove | 24:37504440f296 | 165 | void ESP8266Socket::setPort(uint16_t port) |
| sarahmarshy | 18:9fc7976c7b27 | 166 | { |
| sam_grove | 24:37504440f296 | 167 | _port = port; |
| bridadan | 16:b2f781416464 | 168 | } |
| bridadan | 16:b2f781416464 | 169 | |
| sarahmarshy | 18:9fc7976c7b27 | 170 | void ESP8266Socket::setAddressPort(const char* addr, uint16_t port) |
| bridadan | 16:b2f781416464 | 171 | { |
| sam_grove | 24:37504440f296 | 172 | _addr = addr; |
| sarahmarshy | 18:9fc7976c7b27 | 173 | _port = port; |
| bridadan | 16:b2f781416464 | 174 | } |
| bridadan | 16:b2f781416464 | 175 | |
| sarahmarshy | 18:9fc7976c7b27 | 176 | const char *ESP8266Socket::getAddress(void) const |
| sarahmarshy | 18:9fc7976c7b27 | 177 | { |
| sam_grove | 24:37504440f296 | 178 | return _addr; |
| sarahmarshy | 18:9fc7976c7b27 | 179 | } |
| sarahmarshy | 18:9fc7976c7b27 | 180 | |
| sarahmarshy | 18:9fc7976c7b27 | 181 | uint16_t ESP8266Socket::getPort(void) const |
| sarahmarshy | 18:9fc7976c7b27 | 182 | { |
| sarahmarshy | 18:9fc7976c7b27 | 183 | return _port; |
| sarahmarshy | 18:9fc7976c7b27 | 184 | } |
| sarahmarshy | 18:9fc7976c7b27 | 185 | |
| sarahmarshy | 18:9fc7976c7b27 | 186 | |
| sarahmarshy | 18:9fc7976c7b27 | 187 | int32_t ESP8266Socket::bind(uint16_t port) const |
| sam_grove | 13:0186e9e35a24 | 188 | { |
| sam_grove | 13:0186e9e35a24 | 189 | return -1; |
| sam_grove | 13:0186e9e35a24 | 190 | } |
| sam_grove | 13:0186e9e35a24 | 191 | |
| sarahmarshy | 18:9fc7976c7b27 | 192 | int32_t ESP8266Socket::listen(void) const |
| sam_grove | 13:0186e9e35a24 | 193 | { |
| sam_grove | 13:0186e9e35a24 | 194 | return -1; |
| sam_grove | 13:0186e9e35a24 | 195 | } |
| sam_grove | 13:0186e9e35a24 | 196 | |
| sarahmarshy | 18:9fc7976c7b27 | 197 | int32_t ESP8266Socket::accept() const |
| sam_grove | 13:0186e9e35a24 | 198 | { |
| sam_grove | 13:0186e9e35a24 | 199 | return -1; |
| sam_grove | 13:0186e9e35a24 | 200 | } |
| sam_grove | 13:0186e9e35a24 | 201 | |
| sam_grove | 24:37504440f296 | 202 | int32_t ESP8266Socket::open() |
| sam_grove | 13:0186e9e35a24 | 203 | { |
| sarahmarshy | 26:6e36dd3cec3f | 204 | |
| geky | 33:276cb279df57 | 205 | const char *sock_type = (SOCK_UDP == _type) ? "UDP" : "TCP"; |
| geky | 33:276cb279df57 | 206 | if (!_driver->open(sock_type, _id, _addr, _port)) { |
| sam_grove | 24:37504440f296 | 207 | return -1; |
| sarahmarshy | 18:9fc7976c7b27 | 208 | } |
| sam_grove | 13:0186e9e35a24 | 209 | return 0; |
| sam_grove | 24:37504440f296 | 210 | |
| sam_grove | 13:0186e9e35a24 | 211 | } |
| sam_grove | 13:0186e9e35a24 | 212 | |
| sam_grove | 24:37504440f296 | 213 | int32_t ESP8266Socket::send(const void *data, uint32_t amount, uint32_t timeout_ms) |
| bridadan | 16:b2f781416464 | 214 | { |
| sam_grove | 24:37504440f296 | 215 | _driver->setTimeout((int)timeout_ms); |
| geky | 33:276cb279df57 | 216 | if(!_driver->send(_id, data, amount)) { |
| sarahmarshy | 18:9fc7976c7b27 | 217 | return -1; |
| sarahmarshy | 18:9fc7976c7b27 | 218 | } |
| bridadan | 16:b2f781416464 | 219 | return 0; |
| bridadan | 16:b2f781416464 | 220 | } |
| bridadan | 16:b2f781416464 | 221 | |
| sarahmarshy | 18:9fc7976c7b27 | 222 | uint32_t ESP8266Socket::recv(void *data, uint32_t amount, uint32_t timeout_ms) |
| sam_grove | 13:0186e9e35a24 | 223 | { |
| sarahmarshy | 27:eaeecaaae611 | 224 | _driver->setTimeout((int)timeout_ms); |
| geky | 33:276cb279df57 | 225 | return _driver->recv(_id, data, amount); |
| sam_grove | 13:0186e9e35a24 | 226 | } |
| sam_grove | 13:0186e9e35a24 | 227 | |
| sarahmarshy | 18:9fc7976c7b27 | 228 | int32_t ESP8266Socket::close() const |
| sam_grove | 13:0186e9e35a24 | 229 | { |
| sam_grove | 24:37504440f296 | 230 | if (!_driver->close(_id)) { |
| sarahmarshy | 23:fd0f3197c30b | 231 | return -1;//closing socket not succesful |
| sarahmarshy | 18:9fc7976c7b27 | 232 | } |
| sam_grove | 13:0186e9e35a24 | 233 | return 0; |
| sam_grove | 13:0186e9e35a24 | 234 | } |
| sarahmarshy | 22:312453862371 | 235 | |
| sarahmarshy | 22:312453862371 | 236 | uint32_t ESP8266Socket::getHandle()const |
| sarahmarshy | 22:312453862371 | 237 | { |
| sarahmarshy | 22:312453862371 | 238 | return _handle; |
| sarahmarshy | 22:312453862371 | 239 | } |
| sarahmarshy | 22:312453862371 | 240 | |
| sarahmarshy | 18:9fc7976c7b27 | 241 | uint8_t ESP8266Socket::getID() |
| sam_grove | 13:0186e9e35a24 | 242 | { |
| sam_grove | 24:37504440f296 | 243 | return _id; |
| sarahmarshy | 25:91c4e9d34b77 | 244 | } |
