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.
Fork of ESP8266Interface by
ESP8266Interface.cpp
- Committer:
- sarahmarshy
- Date:
- 2015-07-22
- Revision:
- 22:312453862371
- Parent:
- 20:5d0762aa4680
- Child:
- 23:fd0f3197c30b
File content as of revision 22:312453862371:
/* ESP8266Interface Example * Copyright (c) 2015 ARM Limited * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * 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) : serial(tx, rx), atParser(&serial) { uuidCounter = 0; serial.baud(115200); for(int i = 0; i<numSockets; i++){ availableID[i] = -1; } } ESP8266Interface::ESP8266Interface(PinName tx, PinName rx, const char *ap, const char *pass_phrase, wifi_security_t security, uint32_t timeout_ms) : serial(tx, rx), atParser(&serial) { } int32_t ESP8266Interface::init(void) { if (!(atParser.send("AT") && atParser.recv("OK"))) return -1; if (!(atParser.send("AT+RST") && atParser.recv("OK\r\nready"))) return -1; if (!(atParser.send("AT+CWMODE=3") && atParser.recv("OK"))) return -1; if (!(atParser.send("AT+CIPMUX=1") && atParser.recv("OK"))) return -1; return 0; } int32_t ESP8266Interface::init(const char *ip, const char *mask, const char *gateway) { } int32_t ESP8266Interface::connect(uint32_t timeout_ms) { return -1; } int32_t ESP8266Interface::connect(const char *ap, const char *pass_phrase, wifi_security_t security, uint32_t timeout_ms) { if (!(atParser.send("AT+CWDHCP=2,1") && atParser.recv("OK"))) return -1; string connect_command = "AT+CWJAP=\""+(string)ap+"\",\""+(string)pass_phrase+"\""; atParser.setTimeout(10000); if (!(atParser.send(connect_command.c_str()) && atParser.recv("OK"))){ return -1; } return 0; } int32_t ESP8266Interface::disconnect(void) { if (!(atParser.send("AT+CWQAP") && atParser.recv("OK"))) return -1; for(int i=0; i<numSockets; i++){ SocketInterface *socket = sockets[availableID[i]]; deallocateSocket(socket); } return 0; } char *ESP8266Interface::getIPAddress(void) { if (!(atParser.send("AT+CIPSTA?") && atParser.recv("+CIPSTA:\"%[^\"]\"", ip))) return NULL; return ip; } char *ESP8266Interface::getGateway(void) const { return 0; } char *ESP8266Interface::getNetworkMask(void) const { return 0; } char *ESP8266Interface::getMACAddress(void) const { return 0; } int32_t ESP8266Interface::isConnected(void) { return (getIPAddress()==NULL) ? -1 : 0; } 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; availableID[i] = uuidCounter; break; } } if (id == -1){ return NULL;//tried to allocate more than the maximum 5 sockets } ESP8266Socket *socket = new ESP8266Socket(uuidCounter++, &atParser, socketProtocol, (uint8_t)id); sockets[socket->getHandle()] = socket; return 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; } } ESP8266Socket::ESP8266Socket(uint32_t handle, ATParser *atParser, socket_protocol_t type, uint8_t id) : atParser(atParser), _id(id) { _handle = handle; SocketInterface::_type = type; } const char *ESP8266Socket::getHostByName(const char *name) const { return 0; } void ESP8266Socket::setAddress(const char* addr) { _addr = (char*)addr; } void ESP8266Socket::setPort(uint16_t port) { _port = port; } void ESP8266Socket::setAddressPort(const char* addr, uint16_t port) { _addr = (char*)addr; _port = port; } const char *ESP8266Socket::getAddress(void) const { return (const char*)_addr; } uint16_t ESP8266Socket::getPort(void) const { return _port; } int32_t ESP8266Socket::bind(uint16_t port) const { return -1; } int32_t ESP8266Socket::listen(void) const { return -1; } int32_t ESP8266Socket::accept() const { return -1; } int32_t ESP8266Socket::open() { char portstr[5]; char idstr[2]; sprintf(idstr,"%d",_id); sprintf(portstr, "%d", _port); string sock_type; if(_type == SOCK_UDP) sock_type = "UDP"; else if(_type == SOCK_TCP) sock_type = "TCP"; string start_command = "AT+CIPSTART="+(string)idstr+",\""+sock_type+"\",\""+(string)_addr+"\","+(string)portstr; if (!(atParser->send(start_command.c_str()) && atParser->recv("OK"))){ return -1;//opening socket not succesful } return 0; } int32_t ESP8266Socket::send(const void *data, uint32_t amount, uint32_t timeout_ms) { char idstr[2]; sprintf(idstr,"%d",_id); char lenstr[5]; sprintf(lenstr,"%d",(int)amount); atParser->setTimeout((int)timeout_ms); string send_command = "AT+CIPSEND="+(string)idstr+","+(string)lenstr; if(!atParser->send(send_command.c_str())){ return -1; } atParser->write((char*)data, (int)amount); return 0; } uint32_t ESP8266Socket::recv(void *data, uint32_t amount, uint32_t timeout_ms) { atParser->setTimeout((int)timeout_ms); int length; int id; if (!(atParser->recv("+IPD,%d,%d:", &id, &length) && atParser->read((char*)data, length) && atParser->recv("OK"))){ return false; } return length; } int32_t ESP8266Socket::close() const { char idstr[2]; sprintf(idstr,"%d",_id); string close_command = "AT+CIPCLOSE="+(string)idstr; if (!(atParser->send(close_command.c_str()) && atParser->recv("OK"))){ return -1;//opening socket not succesful } return 0; } uint32_t ESP8266Socket::getHandle()const { return _handle; } uint8_t ESP8266Socket::getID() { return _id; }