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@34:9c26a3dcdc1f, 2016-02-22 (annotated)
- Committer:
- Christopher Haster
- Date:
- Mon Feb 22 23:53:46 2016 -0600
- Branch:
- api-changes
- Revision:
- 34:9c26a3dcdc1f
- Parent:
- 33:276cb279df57
- Child:
- 38:eb1e46561a19
Matched changes in NetworkSocketAPI
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" |
| Christopher Haster |
34:9c26a3dcdc1f | 18 | #include "ESP8266SocketInterface.h" |
| sam_grove | 11:288c15b80a26 | 19 | |
| 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 |
34:9c26a3dcdc1f | 26 | int32_t ESP8266Interface::connect( |
| Christopher Haster |
34:9c26a3dcdc1f | 27 | const char *ap, |
| Christopher Haster |
34:9c26a3dcdc1f | 28 | const char *pass_phrase, |
| Christopher Haster |
34:9c26a3dcdc1f | 29 | wifi_security_t, |
| Christopher Haster |
34:9c26a3dcdc1f | 30 | uint32_t timeout_ms) |
| sam_grove | 11:288c15b80a26 | 31 | { |
| Christopher Haster |
34:9c26a3dcdc1f | 32 | _esp.setTimeout(timeout_ms); |
| Christopher Haster |
34:9c26a3dcdc1f | 33 | |
| Christopher Haster |
34:9c26a3dcdc1f | 34 | if (!_esp.startup(3)) return -1; |
| Christopher Haster |
34:9c26a3dcdc1f | 35 | if (!_esp.dhcp(true, 1)) return -1; |
| Christopher Haster |
34:9c26a3dcdc1f | 36 | if (!_esp.connect(ap, pass_phrase)) return -1; |
| Christopher Haster |
34:9c26a3dcdc1f | 37 | |
| Christopher Haster |
34:9c26a3dcdc1f | 38 | _ip_address = _esp.getIPAddress(); |
| Christopher Haster |
34:9c26a3dcdc1f | 39 | _mac_address = _esp.getMACAddress(); |
| Christopher Haster |
34:9c26a3dcdc1f | 40 | if (!_ip_address || !_mac_address) return -1; |
| Christopher Haster |
34:9c26a3dcdc1f | 41 | |
| sam_grove | 24:37504440f296 | 42 | return 0; |
| sam_grove | 11:288c15b80a26 | 43 | } |
| sam_grove | 11:288c15b80a26 | 44 | |
| Christopher Haster |
34:9c26a3dcdc1f | 45 | int32_t ESP8266Interface::disconnect() |
| sam_grove | 11:288c15b80a26 | 46 | { |
| Christopher Haster |
34:9c26a3dcdc1f | 47 | if (!_esp.disconnect()) return -1; |
| Christopher Haster |
34:9c26a3dcdc1f | 48 | |
| sarahmarshy | 22:312453862371 | 49 | return 0; |
| sam_grove | 11:288c15b80a26 | 50 | } |
| sam_grove | 11:288c15b80a26 | 51 | |
| Christopher Haster |
34:9c26a3dcdc1f | 52 | const char *ESP8266Interface::getIPAddress() |
| sarahmarshy | 18:9fc7976c7b27 | 53 | { |
| Christopher Haster |
34:9c26a3dcdc1f | 54 | return _ip_address; |
| sarahmarshy | 18:9fc7976c7b27 | 55 | } |
| sarahmarshy | 18:9fc7976c7b27 | 56 | |
| Christopher Haster |
34:9c26a3dcdc1f | 57 | const char *ESP8266Interface::getMACAddress() |
| sam_grove | 11:288c15b80a26 | 58 | { |
| Christopher Haster |
34:9c26a3dcdc1f | 59 | return _mac_address; |
| sam_grove | 11:288c15b80a26 | 60 | } |
| sam_grove | 11:288c15b80a26 | 61 | |
| Christopher Haster |
34:9c26a3dcdc1f | 62 | SocketInterface *ESP8266Interface::createSocket(socket_protocol_t proto) |
| sam_grove | 11:288c15b80a26 | 63 | { |
| Christopher Haster |
34:9c26a3dcdc1f | 64 | // Look for an unused socket |
| sarahmarshy | 18:9fc7976c7b27 | 65 | int id = -1; |
| Christopher Haster |
34:9c26a3dcdc1f | 66 | |
| Christopher Haster |
34:9c26a3dcdc1f | 67 | for (int i = 0; i < ESP8266_SOCKET_COUNT; i++) { |
| Christopher Haster |
34:9c26a3dcdc1f | 68 | if (!_ids[i]) { |
| sam_grove | 24:37504440f296 | 69 | id = i; |
| Christopher Haster |
34:9c26a3dcdc1f | 70 | _ids[i] = true; |
| sarahmarshy | 18:9fc7976c7b27 | 71 | break; |
| sarahmarshy | 18:9fc7976c7b27 | 72 | } |
| sarahmarshy | 18:9fc7976c7b27 | 73 | } |
| Christopher Haster |
34:9c26a3dcdc1f | 74 | |
| sam_grove | 24:37504440f296 | 75 | if (id == -1) { |
| Christopher Haster |
34:9c26a3dcdc1f | 76 | return 0; |
| sarahmarshy | 22:312453862371 | 77 | } |
| bridadan | 16:b2f781416464 | 78 | |
| Christopher Haster |
34:9c26a3dcdc1f | 79 | return new ESP8266SocketInterface(&_esp, proto, id); |
| bridadan | 16:b2f781416464 | 80 | } |
| bridadan | 16:b2f781416464 | 81 | |
| Christopher Haster |
34:9c26a3dcdc1f | 82 | void ESP8266Interface::destroySocket(SocketInterface *iface) |
| sarahmarshy | 18:9fc7976c7b27 | 83 | { |
| Christopher Haster |
34:9c26a3dcdc1f | 84 | ESP8266SocketInterface *socket = (ESP8266SocketInterface *)iface; |
| Christopher Haster |
34:9c26a3dcdc1f | 85 | _ids[socket->getID()] = false; |
| sam_grove | 13:0186e9e35a24 | 86 | } |
| sam_grove | 13:0186e9e35a24 | 87 |
