test
Fork of ESP8266 by
ESP8266.cpp@18:11f2f6bd2e97, 2016-02-18 (annotated)
- Committer:
- Christopher Haster
- Date:
- Thu Feb 18 16:11:03 2016 -0600
- Revision:
- 18:11f2f6bd2e97
- Parent:
- 17:8b541b19f391
- Child:
- 20:32e5a5a328e0
Backported changes to espressif driver
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 |
18:11f2f6bd2e97 | 42 | return _parser.send("AT+RST") |
Christopher Haster |
18:11f2f6bd2e97 | 43 | && _parser.recv("OK\r\nready"); |
Christopher Haster |
17:8b541b19f391 | 44 | } |
Christopher Haster |
17:8b541b19f391 | 45 | |
Christopher Haster |
18:11f2f6bd2e97 | 46 | bool ESP8266::dhcp(bool enabled, int mode) |
Christopher Haster |
17:8b541b19f391 | 47 | { |
Christopher Haster |
17:8b541b19f391 | 48 | //only 3 valid modes |
Christopher Haster |
17:8b541b19f391 | 49 | if(mode < 0 || mode > 2) { |
Christopher Haster |
17:8b541b19f391 | 50 | return false; |
Christopher Haster |
17:8b541b19f391 | 51 | } |
Christopher Haster |
18:11f2f6bd2e97 | 52 | |
Christopher Haster |
18:11f2f6bd2e97 | 53 | return _parser.send("AT+CWDHCP=%d,%d", enabled?1:0, mode) |
Christopher Haster |
18:11f2f6bd2e97 | 54 | && _parser.recv("OK"); |
Christopher Haster |
17:8b541b19f391 | 55 | } |
Christopher Haster |
17:8b541b19f391 | 56 | |
Christopher Haster |
17:8b541b19f391 | 57 | bool ESP8266::connect(const char *ap, const char *passPhrase) |
Christopher Haster |
17:8b541b19f391 | 58 | { |
Christopher Haster |
18:11f2f6bd2e97 | 59 | return _parser.send("AT+CWJAP=\"%s\",\"%s\"", ap, passPhrase) |
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::disconnect(void) |
Christopher Haster |
17:8b541b19f391 | 64 | { |
Christopher Haster |
18:11f2f6bd2e97 | 65 | return _parser.send("AT+CWQAP") && _parser.recv("OK"); |
Christopher Haster |
17:8b541b19f391 | 66 | } |
Christopher Haster |
17:8b541b19f391 | 67 | |
Christopher Haster |
18:11f2f6bd2e97 | 68 | const char *ESP8266::getIPAddress(void) |
Christopher Haster |
17:8b541b19f391 | 69 | { |
Christopher Haster |
18:11f2f6bd2e97 | 70 | if (!(_parser.send("AT+CIFSR") |
Christopher Haster |
18:11f2f6bd2e97 | 71 | && _parser.recv("+CIFSR:STAIP,\"%[^\"]\"", _ip_buffer) |
Christopher Haster |
18:11f2f6bd2e97 | 72 | && _parser.recv("OK"))) { |
Christopher Haster |
18:11f2f6bd2e97 | 73 | return 0; |
Christopher Haster |
18:11f2f6bd2e97 | 74 | } |
Christopher Haster |
18:11f2f6bd2e97 | 75 | |
Christopher Haster |
18:11f2f6bd2e97 | 76 | return _ip_buffer; |
Christopher Haster |
18:11f2f6bd2e97 | 77 | } |
Christopher Haster |
18:11f2f6bd2e97 | 78 | |
Christopher Haster |
18:11f2f6bd2e97 | 79 | const char *ESP8266::getMACAddress(void) |
Christopher Haster |
18:11f2f6bd2e97 | 80 | { |
Christopher Haster |
18:11f2f6bd2e97 | 81 | if (!(_parser.send("AT+CIFSR") |
Christopher Haster |
18:11f2f6bd2e97 | 82 | && _parser.recv("+CIFSR:STAMAC,\"%[^\"]\"", _mac_buffer) |
Christopher Haster |
18:11f2f6bd2e97 | 83 | && _parser.recv("OK"))) { |
Christopher Haster |
18:11f2f6bd2e97 | 84 | return 0; |
Christopher Haster |
18:11f2f6bd2e97 | 85 | } |
Christopher Haster |
18:11f2f6bd2e97 | 86 | |
Christopher Haster |
18:11f2f6bd2e97 | 87 | return _mac_buffer; |
Christopher Haster |
17:8b541b19f391 | 88 | } |
Christopher Haster |
17:8b541b19f391 | 89 | |
Christopher Haster |
17:8b541b19f391 | 90 | bool ESP8266::isConnected(void) |
Christopher Haster |
17:8b541b19f391 | 91 | { |
Christopher Haster |
18:11f2f6bd2e97 | 92 | return getIPAddress() != 0; |
Christopher Haster |
17:8b541b19f391 | 93 | } |
Christopher Haster |
17:8b541b19f391 | 94 | |
Christopher Haster |
18:11f2f6bd2e97 | 95 | bool ESP8266::open(const char *type, int id, const char* addr, int port) |
Christopher Haster |
17:8b541b19f391 | 96 | { |
Christopher Haster |
17:8b541b19f391 | 97 | //IDs only 0-4 |
Christopher Haster |
17:8b541b19f391 | 98 | if(id > 4) { |
Christopher Haster |
17:8b541b19f391 | 99 | return false; |
Christopher Haster |
17:8b541b19f391 | 100 | } |
Christopher Haster |
17:8b541b19f391 | 101 | |
Christopher Haster |
18:11f2f6bd2e97 | 102 | return _parser.send("AT+CIPSTART=%d,\"%s\",\"%s\",%d", id, type, addr, port) |
Christopher Haster |
18:11f2f6bd2e97 | 103 | && _parser.recv("OK"); |
Christopher Haster |
17:8b541b19f391 | 104 | } |
Christopher Haster |
17:8b541b19f391 | 105 | |
Christopher Haster |
18:11f2f6bd2e97 | 106 | bool ESP8266::send(int id, const void *data, uint32_t amount) |
Christopher Haster |
17:8b541b19f391 | 107 | { |
Christopher Haster |
18:11f2f6bd2e97 | 108 | //May take a second try if device is busy |
Christopher Haster |
18:11f2f6bd2e97 | 109 | for (unsigned i = 0; i < 2; i++) { |
Christopher Haster |
18:11f2f6bd2e97 | 110 | if (_parser.send("AT+CIPSEND=%d,%d", id, amount) |
Christopher Haster |
18:11f2f6bd2e97 | 111 | && _parser.recv(">") |
Christopher Haster |
18:11f2f6bd2e97 | 112 | && _parser.write((char*)data, (int)amount) >= 0) { |
Christopher Haster |
18:11f2f6bd2e97 | 113 | return true; |
Christopher Haster |
18:11f2f6bd2e97 | 114 | } |
Christopher Haster |
18:11f2f6bd2e97 | 115 | } |
Christopher Haster |
17:8b541b19f391 | 116 | |
Christopher Haster |
18:11f2f6bd2e97 | 117 | return false; |
Christopher Haster |
17:8b541b19f391 | 118 | } |
Christopher Haster |
17:8b541b19f391 | 119 | |
Christopher Haster |
18:11f2f6bd2e97 | 120 | int32_t ESP8266::recv(int id, void *data, uint32_t amount) |
Christopher Haster |
17:8b541b19f391 | 121 | { |
Christopher Haster |
18:11f2f6bd2e97 | 122 | uint32_t recv_amount; |
Christopher Haster |
18:11f2f6bd2e97 | 123 | int recv_id; |
Christopher Haster |
18:11f2f6bd2e97 | 124 | |
Christopher Haster |
18:11f2f6bd2e97 | 125 | if (!(_parser.recv("+IPD,%d,%d:", &recv_id, &recv_amount) |
Christopher Haster |
18:11f2f6bd2e97 | 126 | && recv_id == id |
Christopher Haster |
18:11f2f6bd2e97 | 127 | && recv_amount <= amount |
Christopher Haster |
18:11f2f6bd2e97 | 128 | && _parser.read((char*)data, recv_amount) |
Christopher Haster |
18:11f2f6bd2e97 | 129 | && _parser.recv("OK"))) { |
Christopher Haster |
18:11f2f6bd2e97 | 130 | return -1; |
Christopher Haster |
17:8b541b19f391 | 131 | } |
Christopher Haster |
18:11f2f6bd2e97 | 132 | |
Christopher Haster |
18:11f2f6bd2e97 | 133 | return recv_amount; |
Christopher Haster |
17:8b541b19f391 | 134 | } |
Christopher Haster |
17:8b541b19f391 | 135 | |
Christopher Haster |
17:8b541b19f391 | 136 | bool ESP8266::close(int id) |
Christopher Haster |
17:8b541b19f391 | 137 | { |
Christopher Haster |
17:8b541b19f391 | 138 | //IDs only 0-4 |
Christopher Haster |
17:8b541b19f391 | 139 | if(id > 4) { |
Christopher Haster |
17:8b541b19f391 | 140 | return false; |
Christopher Haster |
17:8b541b19f391 | 141 | } |
Christopher Haster |
17:8b541b19f391 | 142 | |
Christopher Haster |
18:11f2f6bd2e97 | 143 | return _parser.send("AT+CIPCLOSE=%d", id) |
Christopher Haster |
18:11f2f6bd2e97 | 144 | && _parser.recv("OK"); |
Christopher Haster |
17:8b541b19f391 | 145 | } |
Christopher Haster |
17:8b541b19f391 | 146 | |
Christopher Haster |
17:8b541b19f391 | 147 | void ESP8266::setTimeout(uint32_t timeout_ms) |
Christopher Haster |
17:8b541b19f391 | 148 | { |
Christopher Haster |
18:11f2f6bd2e97 | 149 | _parser.setTimeout(timeout_ms); |
Christopher Haster |
18:11f2f6bd2e97 | 150 | } |
Christopher Haster |
18:11f2f6bd2e97 | 151 | |
Christopher Haster |
18:11f2f6bd2e97 | 152 | bool ESP8266::readable() |
Christopher Haster |
18:11f2f6bd2e97 | 153 | { |
Christopher Haster |
18:11f2f6bd2e97 | 154 | return _serial.readable(); |
Christopher Haster |
18:11f2f6bd2e97 | 155 | } |
Christopher Haster |
18:11f2f6bd2e97 | 156 | |
Christopher Haster |
18:11f2f6bd2e97 | 157 | bool ESP8266::writeable() |
Christopher Haster |
18:11f2f6bd2e97 | 158 | { |
Christopher Haster |
18:11f2f6bd2e97 | 159 | return _serial.writeable(); |
Christopher Haster |
18:11f2f6bd2e97 | 160 | } |
Christopher Haster |
18:11f2f6bd2e97 | 161 |