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: ArduinoAPI
Fork of WeeESP8266 by
Revision 5:5fa655d7212c, committed 2015-02-06
- Comitter:
- itead
- Date:
- Fri Feb 06 09:26:37 2015 +0000
- Parent:
- 4:c8e2381d34d2
- Child:
- 6:18eb2d9fb2ac
- Commit message:
- okay
Changed in this revision
| ESP8266.cpp | Show annotated file Show diff for this revision Revisions of this file |
| ESP8266.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/ESP8266.cpp Fri Feb 06 04:50:49 2015 +0000
+++ b/ESP8266.cpp Fri Feb 06 09:26:37 2015 +0000
@@ -1,2 +1,323 @@
+/**
+ * @file ESP8266.cpp
+ * @brief The implementation of class ESP8266.
+ * @author Wu Pengfei<pengfei.wu@itead.cc>
+ * @date 2015.02
+ *
+ * @par Copyright:
+ * Copyright (c) 2015 ITEAD Intelligent Systems Co., Ltd. \n\n
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version. \n\n
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
#include "ESP8266.h"
+#include "log.h"
+ESP8266::ESP8266(ArduinoSerial &uart): m_puart(&uart)
+{
+ m_puart->begin(9600);
+ m_puart->flush();
+ m_puart->setTimeout(1000);
+ logDebug("Create Instance ok");
+}
+
+
+bool ESP8266::kick(void)
+{
+ return eAT();
+}
+
+bool ESP8266::restart(void)
+{
+ unsigned long start;
+ if (eATRST()) {
+ delay(2000);
+ start = millis();
+ while (millis() - start < 3000) {
+ if (eAT()) {
+ delay(500);
+ return true;
+ }
+ delay(100);
+ }
+ }
+ return false;
+}
+
+String ESP8266::getVersion(void)
+{
+ return eATGMR();
+}
+
+bool ESP8266::setOprToStation(void)
+{
+ if (sATCWMODE(1) && restart()) {
+ return true;
+ }
+ return false;
+}
+
+bool ESP8266::setOprToSoftAP(void)
+{
+ if (sATCWMODE(2) && restart()) {
+ return true;
+ }
+ return false;
+}
+
+bool ESP8266::setOprToStationSoftAP(void)
+{
+ if (sATCWMODE(3) && restart()) {
+ return true;
+ }
+ return false;
+}
+
+String ESP8266::getAPList(void)
+{
+ return "";
+}
+
+bool ESP8266::joinAP(String ssid, String pwd)
+{
+ return false;
+}
+
+bool ESP8266::leaveAP(void)
+{
+ return false;
+}
+
+bool ESP8266::setSoftAPParam(String ssid, String pwd, uint8_t chl, uint8_t ecn)
+{
+ return false;
+}
+
+String ESP8266::getJoinedDeviceIP(void)
+{
+ return "";
+}
+
+String ESP8266::getIPStatus(void)
+{
+ return "";
+}
+
+String ESP8266::getLocalIP(void)
+{
+ return "";
+}
+
+bool ESP8266::enableMUX(void)
+{
+ return false;
+}
+
+bool ESP8266::disableMUX(void)
+{
+ return false;
+}
+
+bool ESP8266::createTCP(String addr, uint32_t port)
+{
+ return false;
+}
+
+bool ESP8266::releaseTCP(void)
+{
+ return false;
+}
+
+bool ESP8266::registerUDP(String addr, uint32_t port)
+{
+ return false;
+}
+
+bool ESP8266::unregisterUDP(void)
+{
+ return false;
+}
+
+bool ESP8266::createTCP(uint8_t mux_id, String addr, uint32_t port)
+{
+ return false;
+}
+
+bool ESP8266::releaseTCP(uint8_t mux_id)
+{
+ return false;
+}
+
+bool ESP8266::registerUDP(uint8_t mux_id, String addr, uint32_t port)
+{
+ return false;
+}
+
+bool ESP8266::unregisterUDP(uint8_t mux_id)
+{
+ return false;
+}
+
+bool ESP8266::setTCPServerTimeout(uint32_t timeout)
+{
+ return false;
+}
+
+bool ESP8266::startTCPServer(uint32_t port)
+{
+ return false;
+}
+
+bool ESP8266::stopTCPServer(void)
+{
+ return false;
+}
+
+bool ESP8266::send(const uint8_t *buffer, uint32_t len)
+{
+ return false;
+}
+
+bool ESP8266::send(uint8_t mux_id, const char *buffer, uint32_t len)
+{
+ return false;
+}
+
+uint32_t ESP8266::recv(uint8_t *buffer, uint32_t len, uint32_t timeout)
+{
+ return false;
+}
+
+uint32_t ESP8266::recv(uint8_t mux_id, uint8_t *buffer, uint32_t len, uint32_t timeout)
+{
+ return false;
+}
+
+uint32_t ESP8266::recv(uint8_t *coming_mux_id, uint8_t *buffer, uint32_t len, uint32_t timeout)
+{
+ return false;
+}
+
+
+String ESP8266::recvFind(String target, uint32_t timeout)
+{
+ String data;
+ char a;
+ unsigned long start = millis();
+ while (millis() - start < timeout) {
+ while(m_puart->available() > 0) {
+ a = m_puart->readChr();
+ data += a;
+ }
+ if (data.indexOf(target) != -1) {
+ break;
+ }
+ }
+ return data;
+}
+
+
+String ESP8266::recvFind(String target1, String target2, uint32_t timeout)
+{
+ String data;
+ char a;
+ unsigned long start = millis();
+ while (millis() - start < timeout) {
+ while(m_puart->available() > 0) {
+ a = m_puart->readChr();
+ data += a;
+ }
+ if (data.indexOf(target1) != -1) {
+ break;
+ } else if (data.indexOf(target2) != -1) {
+ break;
+ }
+ }
+ return data;
+}
+
+String ESP8266::recvFind(String target1, String target2, String target3, uint32_t timeout)
+{
+ String data;
+ char a;
+ unsigned long start = millis();
+ while (millis() - start < timeout) {
+ while(m_puart->available() > 0) {
+ a = m_puart->readChr();
+ data += a;
+ }
+ if (data.indexOf(target1) != -1) {
+ break;
+ } else if (data.indexOf(target2) != -1) {
+ break;
+ } else if (data.indexOf(target3) != -1) {
+ break;
+ }
+ }
+ return data;
+}
+
+
+bool ESP8266::eAT(void)
+{
+ m_puart->setTimeout(100);
+ m_puart->flush();
+ m_puart->println("AT");
+ if (m_puart->find("OK")) {
+ return true;
+ } else {
+ return false;
+ }
+}
+
+bool ESP8266::eATRST(void)
+{
+ m_puart->setTimeout(100);
+ m_puart->flush();
+ m_puart->println("AT+RST");
+ if (m_puart->find("OK")) {
+ return true;
+ } else {
+ return false;
+ }
+}
+
+String ESP8266::eATGMR(void)
+{
+ String data;
+ m_puart->flush();
+ m_puart->println("AT+GMR");
+ data = recvFind("OK", "ERROR");
+ if (data.indexOf("OK") != -1) {
+ int32_t index1 = data.indexOf("\r\r\n");
+ int32_t index2 = data.indexOf("\r\n\r\nOK");
+ if (index1 != -1 && index2 != -1) {
+ index1 += strlen("\r\r\n");
+ return data.substring(index1, index2);
+ }
+ }
+ return "";
+}
+
+
+bool ESP8266::sATCWMODE(uint8_t mode)
+{
+ String data;
+ m_puart->flush();
+ m_puart->print("AT+CWMODE=");
+ m_puart->println(mode);
+ data = recvFind("OK");
+ if (data.indexOf("OK") != -1) {
+ return true;
+ }
+ return false;
+}
+
--- a/ESP8266.h Fri Feb 06 04:50:49 2015 +0000
+++ b/ESP8266.h Fri Feb 06 09:26:37 2015 +0000
@@ -339,6 +339,15 @@
uint32_t recv(uint8_t *coming_mux_id, uint8_t *buffer, uint32_t len, uint32_t timeout = 1000);
private:
+ String recvFind(String target, uint32_t timeout = 1000);
+ String recvFind(String target1, String target2, uint32_t timeout = 1000);
+ String recvFind(String target1, String target2, String target3, uint32_t timeout = 1000);
+
+ bool eAT(void);
+ bool eATRST(void);
+ String eATGMR(void);
+
+ bool sATCWMODE(uint8_t mode);
ArduinoSerial *m_puart; /* The UART to communicate with ESP8266 */
};
