test
Fork of ESP8266 by
ESP8266.cpp@25:d288181f5944, 2017-11-25 (annotated)
- Committer:
- tgw
- Date:
- Sat Nov 25 03:36:23 2017 +0000
- Revision:
- 25:d288181f5944
- Parent:
- 21:1a9b555962b5
test
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Christopher Haster |
17:8b541b19f391 | 1 | /* ESP8266 Example |
Christopher Haster |
17:8b541b19f391 | 2 | * Copyright (c) 2015 ARM Limited |
Christopher Haster |
17:8b541b19f391 | 3 | * |
Christopher Haster |
17:8b541b19f391 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
Christopher Haster |
17:8b541b19f391 | 5 | * you may not use this file except in compliance with the License. |
Christopher Haster |
17:8b541b19f391 | 6 | * You may obtain a copy of the License at |
Christopher Haster |
17:8b541b19f391 | 7 | * |
Christopher Haster |
17:8b541b19f391 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Christopher Haster |
17:8b541b19f391 | 9 | * |
Christopher Haster |
17:8b541b19f391 | 10 | * Unless required by applicable law or agreed to in writing, software |
Christopher Haster |
17:8b541b19f391 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
Christopher Haster |
17:8b541b19f391 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Christopher Haster |
17:8b541b19f391 | 13 | * See the License for the specific language governing permissions and |
Christopher Haster |
17:8b541b19f391 | 14 | * limitations under the License. |
Christopher Haster |
17:8b541b19f391 | 15 | */ |
Christopher Haster |
17:8b541b19f391 | 16 | |
Christopher Haster |
17:8b541b19f391 | 17 | #include "ESP8266.h" |
Christopher Haster |
17:8b541b19f391 | 18 | |
Christopher Haster |
18:11f2f6bd2e97 | 19 | ESP8266::ESP8266(PinName tx, PinName rx, bool debug) |
Christopher Haster |
18:11f2f6bd2e97 | 20 | : _serial(tx, rx, 1024), _parser(_serial) |
Christopher Haster |
17:8b541b19f391 | 21 | { |
Christopher Haster |
18:11f2f6bd2e97 | 22 | _serial.baud(115200); |
Christopher Haster |
18:11f2f6bd2e97 | 23 | _parser.debugOn(debug); |
Christopher Haster |
17:8b541b19f391 | 24 | } |
Christopher Haster |
17:8b541b19f391 | 25 | |
Christopher Haster |
18:11f2f6bd2e97 | 26 | bool ESP8266::startup(int mode) |
Christopher Haster |
17:8b541b19f391 | 27 | { |
Christopher Haster |
17:8b541b19f391 | 28 | //only 3 valid modes |
Christopher Haster |
17:8b541b19f391 | 29 | if(mode < 1 || mode > 3) { |
Christopher Haster |
17:8b541b19f391 | 30 | return false; |
Christopher Haster |
17:8b541b19f391 | 31 | } |
Christopher Haster |
17:8b541b19f391 | 32 | |
Christopher Haster |
18:11f2f6bd2e97 | 33 | return reset() |
Christopher Haster |
18:11f2f6bd2e97 | 34 | && _parser.send("AT+CWMODE=%d", mode) |
Christopher Haster |
18:11f2f6bd2e97 | 35 | && _parser.recv("OK") |
Christopher Haster |
18:11f2f6bd2e97 | 36 | && _parser.send("AT+CIPMUX=1") |
Christopher Haster |
18:11f2f6bd2e97 | 37 | && _parser.recv("OK"); |
Christopher Haster |
17:8b541b19f391 | 38 | } |
Christopher Haster |
17:8b541b19f391 | 39 | |
Christopher Haster |
18:11f2f6bd2e97 | 40 | bool ESP8266::reset(void) |
Christopher Haster |
17:8b541b19f391 | 41 | { |
Christopher Haster |
20:32e5a5a328e0 | 42 | for (int i = 0; i < 2; i++) { |
Christopher Haster |
20:32e5a5a328e0 | 43 | if (_parser.send("AT+RST") |
Christopher Haster |
20:32e5a5a328e0 | 44 | && _parser.recv("OK\r\nready")) { |
Christopher Haster |
20:32e5a5a328e0 | 45 | return true; |
Christopher Haster |
20:32e5a5a328e0 | 46 | } |
Christopher Haster |
20:32e5a5a328e0 | 47 | } |
Christopher Haster |
20:32e5a5a328e0 | 48 | |
Christopher Haster |
20:32e5a5a328e0 | 49 | return false; |
Christopher Haster |
17:8b541b19f391 | 50 | } |
Christopher Haster |
17:8b541b19f391 | 51 | |
Christopher Haster |
18:11f2f6bd2e97 | 52 | bool ESP8266::dhcp(bool enabled, int mode) |
Christopher Haster |
17:8b541b19f391 | 53 | { |
Christopher Haster |
17:8b541b19f391 | 54 | //only 3 valid modes |
Christopher Haster |
17:8b541b19f391 | 55 | if(mode < 0 || mode > 2) { |
Christopher Haster |
17:8b541b19f391 | 56 | return false; |
Christopher Haster |
17:8b541b19f391 | 57 | } |
Christopher Haster |
18:11f2f6bd2e97 | 58 | |
Christopher Haster |
18:11f2f6bd2e97 | 59 | return _parser.send("AT+CWDHCP=%d,%d", enabled?1:0, mode) |
Christopher Haster |
18:11f2f6bd2e97 | 60 | && _parser.recv("OK"); |
Christopher Haster |
17:8b541b19f391 | 61 | } |
Christopher Haster |
17:8b541b19f391 | 62 | |
Christopher Haster |
17:8b541b19f391 | 63 | bool ESP8266::connect(const char *ap, const char *passPhrase) |
Christopher Haster |
17:8b541b19f391 | 64 | { |
Christopher Haster |
18:11f2f6bd2e97 | 65 | return _parser.send("AT+CWJAP=\"%s\",\"%s\"", ap, passPhrase) |
Christopher Haster |
18:11f2f6bd2e97 | 66 | && _parser.recv("OK"); |
Christopher Haster |
17:8b541b19f391 | 67 | } |
Christopher Haster |
17:8b541b19f391 | 68 | |
Christopher Haster |
17:8b541b19f391 | 69 | bool ESP8266::disconnect(void) |
Christopher Haster |
17:8b541b19f391 | 70 | { |
Christopher Haster |
18:11f2f6bd2e97 | 71 | return _parser.send("AT+CWQAP") && _parser.recv("OK"); |
Christopher Haster |
17:8b541b19f391 | 72 | } |
Christopher Haster |
17:8b541b19f391 | 73 | |
Christopher Haster |
18:11f2f6bd2e97 | 74 | const char *ESP8266::getIPAddress(void) |
Christopher Haster |
17:8b541b19f391 | 75 | { |
Christopher Haster |
18:11f2f6bd2e97 | 76 | if (!(_parser.send("AT+CIFSR") |
Christopher Haster |
18:11f2f6bd2e97 | 77 | && _parser.recv("+CIFSR:STAIP,\"%[^\"]\"", _ip_buffer) |
Christopher Haster |
18:11f2f6bd2e97 | 78 | && _parser.recv("OK"))) { |
Christopher Haster |
18:11f2f6bd2e97 | 79 | return 0; |
Christopher Haster |
18:11f2f6bd2e97 | 80 | } |
Christopher Haster |
18:11f2f6bd2e97 | 81 | |
Christopher Haster |
18:11f2f6bd2e97 | 82 | return _ip_buffer; |
Christopher Haster |
18:11f2f6bd2e97 | 83 | } |
Christopher Haster |
18:11f2f6bd2e97 | 84 | |
Christopher Haster |
18:11f2f6bd2e97 | 85 | const char *ESP8266::getMACAddress(void) |
Christopher Haster |
18:11f2f6bd2e97 | 86 | { |
Christopher Haster |
18:11f2f6bd2e97 | 87 | if (!(_parser.send("AT+CIFSR") |
Christopher Haster |
18:11f2f6bd2e97 | 88 | && _parser.recv("+CIFSR:STAMAC,\"%[^\"]\"", _mac_buffer) |
Christopher Haster |
18:11f2f6bd2e97 | 89 | && _parser.recv("OK"))) { |
Christopher Haster |
18:11f2f6bd2e97 | 90 | return 0; |
Christopher Haster |
18:11f2f6bd2e97 | 91 | } |
Christopher Haster |
18:11f2f6bd2e97 | 92 | |
Christopher Haster |
18:11f2f6bd2e97 | 93 | return _mac_buffer; |
Christopher Haster |
17:8b541b19f391 | 94 | } |
Christopher Haster |
17:8b541b19f391 | 95 | |
Christopher Haster |
17:8b541b19f391 | 96 | bool ESP8266::isConnected(void) |
Christopher Haster |
17:8b541b19f391 | 97 | { |
Christopher Haster |
18:11f2f6bd2e97 | 98 | return getIPAddress() != 0; |
Christopher Haster |
17:8b541b19f391 | 99 | } |
Christopher Haster |
17:8b541b19f391 | 100 | |
Christopher Haster |
18:11f2f6bd2e97 | 101 | bool ESP8266::open(const char *type, int id, const char* addr, int port) |
Christopher Haster |
17:8b541b19f391 | 102 | { |
Christopher Haster |
17:8b541b19f391 | 103 | //IDs only 0-4 |
Christopher Haster |
17:8b541b19f391 | 104 | if(id > 4) { |
Christopher Haster |
17:8b541b19f391 | 105 | return false; |
Christopher Haster |
17:8b541b19f391 | 106 | } |
Christopher Haster |
17:8b541b19f391 | 107 | |
Christopher Haster |
18:11f2f6bd2e97 | 108 | return _parser.send("AT+CIPSTART=%d,\"%s\",\"%s\",%d", id, type, addr, port) |
Christopher Haster |
18:11f2f6bd2e97 | 109 | && _parser.recv("OK"); |
Christopher Haster |
17:8b541b19f391 | 110 | } |
Christopher Haster |
17:8b541b19f391 | 111 | |
Christopher Haster |
18:11f2f6bd2e97 | 112 | bool ESP8266::send(int id, const void *data, uint32_t amount) |
Christopher Haster |
17:8b541b19f391 | 113 | { |
Christopher Haster |
18:11f2f6bd2e97 | 114 | //May take a second try if device is busy |
Christopher Haster |
18:11f2f6bd2e97 | 115 | for (unsigned i = 0; i < 2; i++) { |
Christopher Haster |
18:11f2f6bd2e97 | 116 | if (_parser.send("AT+CIPSEND=%d,%d", id, amount) |
Christopher Haster |
18:11f2f6bd2e97 | 117 | && _parser.recv(">") |
Christopher Haster |
18:11f2f6bd2e97 | 118 | && _parser.write((char*)data, (int)amount) >= 0) { |
Christopher Haster |
18:11f2f6bd2e97 | 119 | return true; |
Christopher Haster |
18:11f2f6bd2e97 | 120 | } |
Christopher Haster |
18:11f2f6bd2e97 | 121 | } |
Christopher Haster |
17:8b541b19f391 | 122 | |
Christopher Haster |
18:11f2f6bd2e97 | 123 | return false; |
Christopher Haster |
17:8b541b19f391 | 124 | } |
Christopher Haster |
17:8b541b19f391 | 125 | |
Christopher Haster |
18:11f2f6bd2e97 | 126 | int32_t ESP8266::recv(int id, void *data, uint32_t amount) |
Christopher Haster |
17:8b541b19f391 | 127 | { |
Christopher Haster |
18:11f2f6bd2e97 | 128 | uint32_t recv_amount; |
Christopher Haster |
18:11f2f6bd2e97 | 129 | int recv_id; |
Christopher Haster |
18:11f2f6bd2e97 | 130 | |
Christopher Haster |
18:11f2f6bd2e97 | 131 | if (!(_parser.recv("+IPD,%d,%d:", &recv_id, &recv_amount) |
Christopher Haster |
18:11f2f6bd2e97 | 132 | && recv_id == id |
Christopher Haster |
18:11f2f6bd2e97 | 133 | && recv_amount <= amount |
Christopher Haster |
18:11f2f6bd2e97 | 134 | && _parser.read((char*)data, recv_amount) |
Christopher Haster |
18:11f2f6bd2e97 | 135 | && _parser.recv("OK"))) { |
Christopher Haster |
18:11f2f6bd2e97 | 136 | return -1; |
Christopher Haster |
17:8b541b19f391 | 137 | } |
Christopher Haster |
18:11f2f6bd2e97 | 138 | |
Christopher Haster |
18:11f2f6bd2e97 | 139 | return recv_amount; |
Christopher Haster |
17:8b541b19f391 | 140 | } |
Christopher Haster |
17:8b541b19f391 | 141 | |
Christopher Haster |
17:8b541b19f391 | 142 | bool ESP8266::close(int id) |
Christopher Haster |
17:8b541b19f391 | 143 | { |
geky | 21:1a9b555962b5 | 144 | //May take a second try if device is busy |
geky | 21:1a9b555962b5 | 145 | for (unsigned i = 0; i < 2; i++) { |
geky | 21:1a9b555962b5 | 146 | if (_parser.send("AT+CIPCLOSE=%d", id) |
geky | 21:1a9b555962b5 | 147 | && _parser.recv("OK")) { |
geky | 21:1a9b555962b5 | 148 | return true; |
geky | 21:1a9b555962b5 | 149 | } |
Christopher Haster |
17:8b541b19f391 | 150 | } |
Christopher Haster |
17:8b541b19f391 | 151 | |
geky | 21:1a9b555962b5 | 152 | return false; |
Christopher Haster |
17:8b541b19f391 | 153 | } |
Christopher Haster |
17:8b541b19f391 | 154 | |
Christopher Haster |
17:8b541b19f391 | 155 | void ESP8266::setTimeout(uint32_t timeout_ms) |
Christopher Haster |
17:8b541b19f391 | 156 | { |
Christopher Haster |
18:11f2f6bd2e97 | 157 | _parser.setTimeout(timeout_ms); |
Christopher Haster |
18:11f2f6bd2e97 | 158 | } |
Christopher Haster |
18:11f2f6bd2e97 | 159 | |
Christopher Haster |
18:11f2f6bd2e97 | 160 | bool ESP8266::readable() |
Christopher Haster |
18:11f2f6bd2e97 | 161 | { |
Christopher Haster |
18:11f2f6bd2e97 | 162 | return _serial.readable(); |
Christopher Haster |
18:11f2f6bd2e97 | 163 | } |
Christopher Haster |
18:11f2f6bd2e97 | 164 | |
Christopher Haster |
18:11f2f6bd2e97 | 165 | bool ESP8266::writeable() |
Christopher Haster |
18:11f2f6bd2e97 | 166 | { |
Christopher Haster |
18:11f2f6bd2e97 | 167 | return _serial.writeable(); |
Christopher Haster |
18:11f2f6bd2e97 | 168 | } |
Christopher Haster |
18:11f2f6bd2e97 | 169 |