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:
Christopher Haster
Date:
Wed Feb 24 22:21:43 2016 -0600
Branch:
api-changes
Revision:
41:3f4d5f4862d2
Parent:
40:83c6b4129468
Child:
42:4bf09cadf328
Matched mbed coding standards

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 24:37504440f296 16
sarahmarshy 18:9fc7976c7b27 17 #include "ESP8266Interface.h"
sam_grove 11:288c15b80a26 18
Christopher Haster 40:83c6b4129468 19 // ESP8266Interface implementation
Christopher Haster 34:9c26a3dcdc1f 20 ESP8266Interface::ESP8266Interface(PinName tx, PinName rx, bool debug)
Christopher Haster 34:9c26a3dcdc1f 21 : _esp(tx, rx, debug)
sam_grove 24:37504440f296 22 {
Christopher Haster 34:9c26a3dcdc1f 23 memset(_ids, 0, sizeof(_ids));
sam_grove 11:288c15b80a26 24 }
sam_grove 11:288c15b80a26 25
Christopher Haster 40:83c6b4129468 26 ESP8266Interface::~ESP8266Interface()
Christopher Haster 40:83c6b4129468 27 {
Christopher Haster 40:83c6b4129468 28 }
Christopher Haster 40:83c6b4129468 29
Christopher Haster 34:9c26a3dcdc1f 30 int32_t ESP8266Interface::connect(
Christopher Haster 34:9c26a3dcdc1f 31 const char *ap,
Christopher Haster 34:9c26a3dcdc1f 32 const char *pass_phrase,
Christopher Haster 38:eb1e46561a19 33 wifi_security_t)
sam_grove 11:288c15b80a26 34 {
Christopher Haster 41:3f4d5f4862d2 35 if (!_esp.startup(3)) {
Christopher Haster 41:3f4d5f4862d2 36 return -1;
Christopher Haster 41:3f4d5f4862d2 37 }
Christopher Haster 41:3f4d5f4862d2 38
Christopher Haster 41:3f4d5f4862d2 39 if (!_esp.dhcp(true, 1)) {
Christopher Haster 41:3f4d5f4862d2 40 return -1;
Christopher Haster 41:3f4d5f4862d2 41 }
Christopher Haster 41:3f4d5f4862d2 42
Christopher Haster 41:3f4d5f4862d2 43 if (!_esp.connect(ap, pass_phrase)) {
Christopher Haster 41:3f4d5f4862d2 44 return -1;
Christopher Haster 41:3f4d5f4862d2 45 }
Christopher Haster 34:9c26a3dcdc1f 46
Christopher Haster 34:9c26a3dcdc1f 47 _ip_address = _esp.getIPAddress();
Christopher Haster 34:9c26a3dcdc1f 48 _mac_address = _esp.getMACAddress();
Christopher Haster 41:3f4d5f4862d2 49 if (!_ip_address || !_mac_address) {
Christopher Haster 41:3f4d5f4862d2 50 return -1;
Christopher Haster 41:3f4d5f4862d2 51 }
Christopher Haster 34:9c26a3dcdc1f 52
sam_grove 24:37504440f296 53 return 0;
sam_grove 11:288c15b80a26 54 }
sam_grove 11:288c15b80a26 55
Christopher Haster 34:9c26a3dcdc1f 56 int32_t ESP8266Interface::disconnect()
sam_grove 11:288c15b80a26 57 {
Christopher Haster 41:3f4d5f4862d2 58 if (!_esp.disconnect()) {
Christopher Haster 41:3f4d5f4862d2 59 return -1;
Christopher Haster 41:3f4d5f4862d2 60 }
Christopher Haster 34:9c26a3dcdc1f 61
sarahmarshy 22:312453862371 62 return 0;
sam_grove 11:288c15b80a26 63 }
sam_grove 11:288c15b80a26 64
Christopher Haster 34:9c26a3dcdc1f 65 const char *ESP8266Interface::getIPAddress()
sarahmarshy 18:9fc7976c7b27 66 {
Christopher Haster 34:9c26a3dcdc1f 67 return _ip_address;
sarahmarshy 18:9fc7976c7b27 68 }
sarahmarshy 18:9fc7976c7b27 69
Christopher Haster 34:9c26a3dcdc1f 70 const char *ESP8266Interface::getMACAddress()
sam_grove 11:288c15b80a26 71 {
Christopher Haster 34:9c26a3dcdc1f 72 return _mac_address;
sam_grove 11:288c15b80a26 73 }
sam_grove 11:288c15b80a26 74
Christopher Haster 34:9c26a3dcdc1f 75 SocketInterface *ESP8266Interface::createSocket(socket_protocol_t proto)
sam_grove 11:288c15b80a26 76 {
Christopher Haster 34:9c26a3dcdc1f 77 // Look for an unused socket
sarahmarshy 18:9fc7976c7b27 78 int id = -1;
Christopher Haster 34:9c26a3dcdc1f 79
Christopher Haster 34:9c26a3dcdc1f 80 for (int i = 0; i < ESP8266_SOCKET_COUNT; i++) {
Christopher Haster 34:9c26a3dcdc1f 81 if (!_ids[i]) {
sam_grove 24:37504440f296 82 id = i;
Christopher Haster 34:9c26a3dcdc1f 83 _ids[i] = true;
sarahmarshy 18:9fc7976c7b27 84 break;
sarahmarshy 18:9fc7976c7b27 85 }
sarahmarshy 18:9fc7976c7b27 86 }
Christopher Haster 34:9c26a3dcdc1f 87
sam_grove 24:37504440f296 88 if (id == -1) {
Christopher Haster 34:9c26a3dcdc1f 89 return 0;
sarahmarshy 22:312453862371 90 }
bridadan 16:b2f781416464 91
Christopher Haster 40:83c6b4129468 92 return new ESP8266Interface::ESP8266Socket(&_esp, proto, id);
bridadan 16:b2f781416464 93 }
bridadan 16:b2f781416464 94
Christopher Haster 34:9c26a3dcdc1f 95 void ESP8266Interface::destroySocket(SocketInterface *iface)
sarahmarshy 18:9fc7976c7b27 96 {
Christopher Haster 40:83c6b4129468 97 ESP8266Interface::ESP8266Socket *socket = (ESP8266Interface::ESP8266Socket *)iface;
Christopher Haster 34:9c26a3dcdc1f 98 _ids[socket->getID()] = false;
Christopher Haster 39:7e85bf8003fa 99 delete socket;
sam_grove 13:0186e9e35a24 100 }
sam_grove 13:0186e9e35a24 101
Christopher Haster 40:83c6b4129468 102
Christopher Haster 40:83c6b4129468 103 // ESP8266Socket implementation
Christopher Haster 40:83c6b4129468 104 ESP8266Interface::ESP8266Socket::ESP8266Socket(
Christopher Haster 40:83c6b4129468 105 ESP8266 *esp,
Christopher Haster 40:83c6b4129468 106 socket_protocol_t proto,
Christopher Haster 40:83c6b4129468 107 int id)
Christopher Haster 40:83c6b4129468 108 : _esp(esp)
Christopher Haster 40:83c6b4129468 109 , _proto(proto)
Christopher Haster 40:83c6b4129468 110 , _id(id)
Christopher Haster 40:83c6b4129468 111 {
Christopher Haster 40:83c6b4129468 112 }
Christopher Haster 40:83c6b4129468 113
Christopher Haster 40:83c6b4129468 114 ESP8266Interface::ESP8266Socket::~ESP8266Socket()
Christopher Haster 40:83c6b4129468 115 {
Christopher Haster 40:83c6b4129468 116 }
Christopher Haster 40:83c6b4129468 117
Christopher Haster 40:83c6b4129468 118 int32_t ESP8266Interface::ESP8266Socket::open(const char *ip, uint16_t port)
Christopher Haster 40:83c6b4129468 119 {
Christopher Haster 40:83c6b4129468 120 const char *proto = (_proto == SOCK_UDP) ? "UDP" : "TCP";
Christopher Haster 40:83c6b4129468 121
Christopher Haster 40:83c6b4129468 122 if (!_esp->open(proto, _id, ip, port)) {
Christopher Haster 40:83c6b4129468 123 return -1;
Christopher Haster 40:83c6b4129468 124 }
Christopher Haster 40:83c6b4129468 125
Christopher Haster 40:83c6b4129468 126 return 0;
Christopher Haster 40:83c6b4129468 127 }
Christopher Haster 40:83c6b4129468 128
Christopher Haster 40:83c6b4129468 129 int32_t ESP8266Interface::ESP8266Socket::close()
Christopher Haster 40:83c6b4129468 130 {
Christopher Haster 40:83c6b4129468 131 if (!_esp->close(_id)) {
Christopher Haster 40:83c6b4129468 132 return -1;
Christopher Haster 40:83c6b4129468 133 }
Christopher Haster 40:83c6b4129468 134
Christopher Haster 40:83c6b4129468 135 return 0;
Christopher Haster 40:83c6b4129468 136 }
Christopher Haster 40:83c6b4129468 137
Christopher Haster 40:83c6b4129468 138 int32_t ESP8266Interface::ESP8266Socket::send(const void *data, uint32_t amount)
Christopher Haster 40:83c6b4129468 139 {
Christopher Haster 40:83c6b4129468 140 if (!_esp->send(_id, data, amount)) {
Christopher Haster 40:83c6b4129468 141 return -1;
Christopher Haster 40:83c6b4129468 142 }
Christopher Haster 40:83c6b4129468 143
Christopher Haster 40:83c6b4129468 144 return 0;
Christopher Haster 40:83c6b4129468 145 }
Christopher Haster 40:83c6b4129468 146
Christopher Haster 40:83c6b4129468 147 int32_t ESP8266Interface::ESP8266Socket::recv(void *data, uint32_t amount)
Christopher Haster 40:83c6b4129468 148 {
Christopher Haster 40:83c6b4129468 149 return _esp->recv(_id, data, amount);
Christopher Haster 40:83c6b4129468 150 }
Christopher Haster 40:83c6b4129468 151
Christopher Haster 40:83c6b4129468 152 int ESP8266Interface::ESP8266Socket::getID() const {
Christopher Haster 40:83c6b4129468 153 return _id;
Christopher Haster 40:83c6b4129468 154 }
Christopher Haster 40:83c6b4129468 155