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