Socket interface for ESP8266. Implements the NetworkSocketAPI. Requires device to use the Espressif Firmware.
Dependencies: ESP8266
Dependents: ESP8266InterfaceTests HelloESP8266Interface
Fork of ESP8266Interface by
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)
Diff: ESP8266Interface.cpp
- Revision:
- 24:37504440f296
- Parent:
- 23:fd0f3197c30b
- Child:
- 26:6e36dd3cec3f
--- a/ESP8266Interface.cpp Thu Jul 23 21:25:30 2015 +0000 +++ b/ESP8266Interface.cpp Sun Jul 26 21:53:45 2015 +0000 @@ -13,39 +13,38 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - + #include "ESP8266Interface.h" -#include <string> -ESP8266Interface::ESP8266Interface(PinName tx, PinName rx) -:esp8266(tx,rx) +ESP8266Interface::ESP8266Interface(PinName tx, PinName rx) : esp8266(tx, rx) { uuidCounter = 0; - for(int i = 0; i<numSockets; i++){ - availableID[i] = -1; - } + std::fill_n(availableID, sizeof(availableID)/sizeof(int), -1); } -int32_t ESP8266Interface::init(void) -{ - if (!esp8266.startup()) +int32_t ESP8266Interface::init(void) +{ + if (!esp8266.startup()) { return -1; - if (!esp8266.reset()) + } + if (!esp8266.reset()) { return -1; - if (!esp8266.wifiMode(3)) + } + if (!esp8266.wifiMode(3)) { return -1; - if (!esp8266.multipleConnections(true)) + } + if (!esp8266.multipleConnections(true)) { return -1; + } return 0; - } -int32_t ESP8266Interface::init(const char *ip, const char *mask, const char *gateway) +int32_t ESP8266Interface::init(const char *ip, const char *mask, const char *gateway) { return -1; } -int32_t ESP8266Interface::connect(uint32_t timeout_ms) +int32_t ESP8266Interface::connect(uint32_t timeout_ms) { return -1; } @@ -53,31 +52,32 @@ int32_t ESP8266Interface::connect(const char *ap, const char *pass_phrase, wifi_security_t security, uint32_t timeout_ms) { esp8266.setTimeout(timeout_ms); - - if (!esp8266.dhcp(1,true)) - return -1; - if (!esp8266.connect(ap,pass_phrase)){ + if (!esp8266.dhcp(1, true)) { return -1; } - return 0; + if (!esp8266.connect(ap, pass_phrase)) { + return -1; + } + return 0; } int32_t ESP8266Interface::disconnect(void) { - if (!esp8266.disconnect()) + if (!esp8266.disconnect()) { return -1; - for(int i=0; i<numSockets; i++){ + } + for(int i=0; i<numSockets; i++) { SocketInterface *socket = sockets[availableID[i]]; deallocateSocket(socket); } return 0; - } char *ESP8266Interface::getIPAddress(void) { - if(!esp8266.getIPAddress(ip)) + if(!esp8266.getIPAddress(ip)) { return NULL; + } return ip; } @@ -96,56 +96,57 @@ return 0; } -int32_t ESP8266Interface::isConnected(void) +int32_t ESP8266Interface::isConnected(void) { - return (getIPAddress()==NULL) ? -1 : 0; + return (getIPAddress() == NULL) ? -1 : 0; } -SocketInterface *ESP8266Interface::allocateSocket(socket_protocol_t socketProtocol) -{ +SocketInterface *ESP8266Interface::allocateSocket(socket_protocol_t socketProtocol) +{ int id = -1; //Look through the array of available sockets for an unused ID - for(int i=0; i<numSockets; i++){ - if (availableID[i] == -1){ - id = i; + for(int i=0; i<numSockets; i++) { + if (availableID[i] == -1) { + id = i; availableID[i] = uuidCounter; break; } } - if (id == -1){ - return NULL;//tried to allocate more than the maximum 5 sockets + if (id == -1) { + return NULL;//tried to allocate more than the maximum 5 sockets } - ESP8266Socket *socket = new ESP8266Socket(uuidCounter++, &esp8266, socketProtocol, (uint8_t)id); + ESP8266Socket *socket = new ESP8266Socket(uuidCounter++, esp8266, socketProtocol, (uint8_t)id); sockets[socket->getHandle()] = socket; return socket; } -int ESP8266Interface::deallocateSocket(SocketInterface *socket) +int ESP8266Interface::deallocateSocket(SocketInterface *socket) { int id = (int)static_cast<ESP8266Socket*>(socket)->getID(); availableID[id] = -1; - + std::map<uint32_t, SocketInterface*>::iterator it; - + // Check if socket is owned by WiFiRadioInterface it = sockets.find(socket->getHandle()); - + if (it != sockets.end()) { // If so, erase it from the internal socket map and deallocate the socket sockets.erase(it); delete socket; - return 0; } else { // Socket is not owned by WiFiRadioInterface, so return -1 error return -1; } + return 0; } -ESP8266Socket::ESP8266Socket(uint32_t handle, ESP8266 *driver, socket_protocol_t type, uint8_t id) -: driver(driver), _id(id) +ESP8266Socket::ESP8266Socket(uint32_t handle, ESP8266 &driver, socket_protocol_t type, uint8_t id) { _handle = handle; - SocketInterface::_type = type; + _driver = &driver; + _type = type; + _id = id; } const char *ESP8266Socket::getHostByName(const char *name) const @@ -155,23 +156,23 @@ void ESP8266Socket::setAddress(const char* addr) { - _addr = (char*)addr; + _addr = addr; } -void ESP8266Socket::setPort(uint16_t port) +void ESP8266Socket::setPort(uint16_t port) { - _port = port; + _port = port; } void ESP8266Socket::setAddressPort(const char* addr, uint16_t port) { - _addr = (char*)addr; + _addr = addr; _port = port; } const char *ESP8266Socket::getAddress(void) const { - return (const char*)_addr; + return _addr; } uint16_t ESP8266Socket::getPort(void) const @@ -195,26 +196,20 @@ return -1; } -int32_t ESP8266Socket::open() +int32_t ESP8266Socket::open() { - string sock_type; - if(_type == SOCK_UDP) - sock_type = "UDP"; - else if(_type == SOCK_TCP) - sock_type = "TCP"; - - if (!driver->openSocket(sock_type, _id, _port, _addr)){ - return -1;//opening socket not succesful + string sock_type = (SOCK_UDP == _type) ? "UDP" : "TCP"; + if (!_driver->openSocket(sock_type, _id, _port, _addr)) { + return -1; } return 0; - + } -int32_t ESP8266Socket::send(const void *data, uint32_t amount, uint32_t timeout_ms) +int32_t ESP8266Socket::send(const void *data, uint32_t amount, uint32_t timeout_ms) { - - driver->setTimeout((int)timeout_ms); - if(!driver->sendData(_id, data, amount)){ + _driver->setTimeout((int)timeout_ms); + if(!_driver->sendData(_id, data, amount)) { return -1; } return 0; @@ -222,15 +217,12 @@ uint32_t ESP8266Socket::recv(void *data, uint32_t amount, uint32_t timeout_ms) { - - return driver->recv(data, amount); - + return _driver->recv(data, amount); } int32_t ESP8266Socket::close() const { - - if (!driver->close(_id)){ + if (!_driver->close(_id)) { return -1;//closing socket not succesful } return 0; @@ -243,5 +235,5 @@ uint8_t ESP8266Socket::getID() { - return _id; -} \ No newline at end of file + return _id; +}