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.
Revision 3:4cd9171ba989, committed 2016-06-16
- Comitter:
- lixianyu
- Date:
- Thu Jun 16 08:08:30 2016 +0000
- Parent:
- 2:6cd3b0947188
- Child:
- 4:e5018e5ba340
- Commit message:
- 9600?????ESP8266????????115200??ESP8266?????????9600????????????Timer???????
Changed in this revision
--- a/AM2321.cpp Wed Jun 15 03:08:40 2016 +0000 +++ b/AM2321.cpp Thu Jun 16 08:08:30 2016 +0000 @@ -25,11 +25,8 @@ // #include "AM2321.h" -#include "I2Cdev.h" -//#include <Wire.h> #include "mbed.h" extern I2C g_i2c; -//I2C gI2C(P0_11, P0_10); extern Serial pc; #define I2C_ADDR_AM2321 (0xB8 >> 1) //AM2321温湿度计I2C地址
--- a/AM2321.h Wed Jun 15 03:08:40 2016 +0000
+++ b/AM2321.h Thu Jun 16 08:08:30 2016 +0000
@@ -28,12 +28,8 @@
#ifndef __ARDUINO_AM2321_H__
#define __ARDUINO_AM2321_H__
-//#include "mbed.h"
-//#include <Arduino.h>
-
#define LIBAM2321_VERSION "0.1.0"
-
class AM2321
{
public:
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Blynk.lib Thu Jun 16 08:08:30 2016 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/vshymanskyy/code/Blynk/#1538810a5d87
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/BlynkSimpleShieldEsp8266_HardSer.h Thu Jun 16 08:08:30 2016 +0000
@@ -0,0 +1,210 @@
+/**
+ * @file BlynkSimpleShieldEsp8266.h
+ * @author Volodymyr Shymanskyy
+ * @license This project is released under the MIT License (MIT)
+ * @copyright Copyright (c) 2015 Volodymyr Shymanskyy
+ * @date Jun 2015
+ * @brief
+ *
+ */
+
+#ifndef BlynkSimpleShieldEsp8266_h
+#define BlynkSimpleShieldEsp8266_h
+
+#ifdef ESP8266
+#error This code is not intended to run on the ESP8266 platform! Please check your Tools->Board setting.
+#endif
+
+#ifndef BLYNK_INFO_CONNECTION
+#define BLYNK_INFO_CONNECTION "ESP8266"
+#endif
+
+extern Serial pc;
+//#define LOG_ESP8266
+#ifdef LOG_ESP8266
+#define ESP8266_LOG pc.printf
+#define LOG_ENTER ESP8266_LOG("Enter %s\r\n", __func__);
+#else
+#define ESP8266_LOG(...)
+#define LOG_ENTER
+#endif
+
+#define BLYNK_SEND_ATOMIC
+
+// TODO: Remove this hotfix
+#define BLYNK_NO_INFO
+
+//#include <BlynkApiArduino.h>
+#include <BlynkApiMbed.h>
+#include <Blynk/BlynkProtocol.h>
+#include <utility/BlynkFifo.h>
+#include <ESP8266_HardSer.h>
+
+class BlynkTransportShieldEsp8266
+{
+ static void onData(uint8_t mux_id, uint32_t len, void* ptr) {
+ ((BlynkTransportShieldEsp8266*)ptr)->onData(mux_id, len);
+ }
+
+ void onData(uint8_t mux_id, uint32_t len) {
+ //BLYNK_LOG2("Got ", len);
+ while (len) {
+ if (client->getUart()->readable()) {
+ uint8_t b = client->getUart()->getc();
+ if(!buffer.push(b)) {
+ BLYNK_LOG1(BLYNK_F("Buffer overflow"));
+ ESP8266_LOG(BLYNK_F("Buffer overflow"));
+ }
+ len--;
+ }
+ }
+ }
+
+public:
+ BlynkTransportShieldEsp8266()
+ : client(NULL)
+ , status(false)
+ , domain(NULL)
+ , port(0)
+ {}
+
+ void begin_domain(ESP8266* esp8266, const char* d, uint16_t p) {
+ LOG_ENTER;
+ client = esp8266;
+ client->setOnData(onData, this);
+ domain = d;
+ port = p;
+ }
+
+ bool connect() {
+ ESP8266_LOG("connect(), domain = %s, port = %d\r\n", domain, port);
+ if (!domain || !port)
+ return false;
+ status = client->createTCP(domain, port);
+ return status;
+ }
+
+ void disconnect() {
+ #if 0
+ LOG_ENTER;
+
+ status = false;
+ buffer.clear();
+ client->releaseTCP();
+ #endif
+ }
+
+ size_t read(void* buf, size_t len) {
+ uint32_t start = millis();
+ ESP8266_LOG("Waiting: %d, Occuied: %d\r\n", len, buffer.getOccupied());
+ //BLYNK_LOG4("Waiting: ", len, " Occuied: ", buffer.getOccupied());
+ while ((buffer.getOccupied() < len) && (millis() - start < 4500)) {
+ client->run();
+ }
+ return buffer.read((uint8_t*)buf, len);
+ }
+ size_t write(const void* buf, size_t len) {
+ ESP8266_LOG("Enter write, len = %d\r\n", len);
+ if (client->send((const uint8_t*)buf, len)) {
+ return len;
+ }
+ return 0;
+ }
+
+ bool connected() {
+ return status;
+ }
+
+ int available() {
+ LOG_ENTER;
+ client->run();
+ //BLYNK_LOG2("Still: ", buffer.getOccupied());
+ return buffer.getOccupied();
+ }
+
+private:
+ ESP8266* client;
+ bool status;
+ BlynkFifo<uint8_t,256> buffer;
+ const char* domain;
+ uint16_t port;
+};
+
+class BlynkWifi
+ : public BlynkProtocol<BlynkTransportShieldEsp8266>
+{
+ typedef BlynkProtocol<BlynkTransportShieldEsp8266> Base;
+public:
+ BlynkWifi(BlynkTransportShieldEsp8266& transp)
+ : Base(transp)
+ , wifi(NULL)
+ {}
+
+ bool connectWiFi(const char* ssid, const char* pass) {
+ LOG_ENTER;
+ delay(500);
+ BLYNK_LOG2(BLYNK_F("Connecting to "), ssid);
+ ESP8266_LOG(BLYNK_F("Connecting to %s\r\n"), ssid);
+ /*if (!wifi->restart()) {
+ BLYNK_LOG1(BLYNK_F("Failed to restart"));
+ return false;
+ }*/
+ if (!wifi->setEcho(0)) {
+ BLYNK_LOG1(BLYNK_F("Failed to disable Echo"));
+ ESP8266_LOG(BLYNK_F("Failed to disable Echo"));
+ return false;
+ }
+ if (!wifi->setOprToStation()) {
+ BLYNK_LOG1(BLYNK_F("Failed to set STA mode"));
+ ESP8266_LOG(BLYNK_F("Failed to set STA mode"));
+ return false;
+ }
+ if (wifi->joinAP(ssid, pass)) {
+ BLYNK_LOG2(BLYNK_F("IP: "), wifi->getLocalIP().c_str());
+ ESP8266_LOG(BLYNK_F("IP: "), wifi->getLocalIP().c_str());
+ } else {
+ BLYNK_LOG1(BLYNK_F("Failed to connect WiFi"));
+ ESP8266_LOG(BLYNK_F("Failed to connect WiFi"));
+ return false;
+ }
+ if (!wifi->disableMUX()) {
+ BLYNK_LOG1(BLYNK_F("Failed to disable MUX"));
+ ESP8266_LOG(BLYNK_F("Failed to disable MUX"));
+ }
+ BLYNK_LOG1(BLYNK_F("Connected to WiFi"));
+ ESP8266_LOG(BLYNK_F("Connected to WiFi"));
+ return true;
+ }
+
+ void config(ESP8266& esp8266,
+ const char* auth,
+ const char* domain = BLYNK_DEFAULT_DOMAIN,
+ uint16_t port = BLYNK_DEFAULT_PORT) {
+ LOG_ENTER;
+ Base::begin(auth);
+ wifi = &esp8266;
+ this->conn.begin_domain(wifi, domain, port);
+ }
+
+ void begin(const char* auth,
+ ESP8266& esp8266,
+ const char* ssid,
+ const char* pass,
+ const char* domain = BLYNK_DEFAULT_DOMAIN,
+ uint16_t port = BLYNK_DEFAULT_PORT) {
+ LOG_ENTER;
+ config(esp8266, auth, domain, port);
+ connectWiFi(ssid, pass);
+ }
+
+private:
+ ESP8266* wifi;
+};
+
+static BlynkTransportShieldEsp8266 _blynkTransport;
+BlynkWifi Blynk(_blynkTransport);
+
+#include <BlynkWidgets.h>
+
+#endif
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Config.h Thu Jun 16 08:08:30 2016 +0000 @@ -0,0 +1,11 @@ +#ifndef __CONFIG_H_ +#define __CONFIG_H_ + +// Uncomment this to turn on the OLED +//#define OPEN_OLED + +// Uncomment this to open PM25 +//#define OPEN_PM25 + + +#endif // __CONFIG_H_ \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/ESP8266.cpp Thu Jun 16 08:08:30 2016 +0000
@@ -0,0 +1,1533 @@
+/**
+ * @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_HardSer.h"
+//#include <avr/pgmspace.h>
+#ifdef ESP8266_USE_SOFTWARE_SERIAL
+ESP8266::ESP8266(SoftwareSerial &uart): m_puart(&uart)
+{
+ m_onData = NULL;
+ m_onDataPtr = NULL;
+}
+#else
+ESP8266::ESP8266(HardwareSerial &uart): m_puart(&uart)
+{
+ m_onData = NULL;
+ m_onDataPtr = NULL;
+}
+#endif
+
+bool ESP8266::kick(void)
+{
+ LOG_ENTER;
+ return eAT();
+}
+
+bool ESP8266::restart(void)
+{
+ LOG_ENTER;
+ unsigned long start;
+ if (eATRST()) {
+ //delay(2000);
+ wait_ms(2000);
+ //start = millis();
+ start = g_Timer.read_ms();
+ while (g_Timer.read_ms() - start < 3000) {
+ if (eAT()) {
+ //delay(1500); /* Waiting for stable */
+ wait_ms(1500);
+ return true;
+ }
+ //delay(100);
+ wait_ms(100);
+ }
+ }
+ return false;
+}
+
+String ESP8266::getVersion(void)
+{
+ LOG_ENTER;
+ String version;
+ eATGMR(version);
+ return version;
+}
+
+bool ESP8266::setEcho(uint8_t mode)
+{
+ LOG_ENTER;
+ return eATE(mode);
+}
+
+bool ESP8266::restore(void)
+{
+ LOG_ENTER;
+ return eATRESTORE();
+}
+
+bool ESP8266::setUart(uint32_t baudrate,uint8_t pattern)
+{
+ LOG_ENTER;
+ return eATSETUART(baudrate,pattern);
+}
+
+bool ESP8266::deepSleep(uint32_t time)
+{
+ LOG_ENTER;
+ return eATGSLP(time);
+}
+
+bool ESP8266::setOprToStation(uint8_t pattern1,uint8_t pattern2)
+{
+ LOG_ENTER;
+ uint8_t mode;
+ if (!qATCWMODE(&mode,pattern1)) {
+ return false;
+ }
+ if (mode == 1) {
+ return true;
+ } else {
+ if (sATCWMODE(1,pattern2)) {
+ return true;
+ } else {
+ return false;
+ }
+ }
+}
+
+String ESP8266::getWifiModeList(void)
+{
+ LOG_ENTER;
+ String list;
+ eATCWMODE(list);
+ return list;
+}
+
+bool ESP8266::setOprToSoftAP(uint8_t pattern1,uint8_t pattern2)
+{
+ LOG_ENTER;
+ uint8_t mode;
+ if (!qATCWMODE(&mode,pattern1)) {
+ return false;
+ }
+ if (mode == 2) {
+ return true;
+ } else {
+ if (sATCWMODE(2,pattern2) ) {
+ return true;
+ } else {
+ return false;
+ }
+ }
+}
+
+bool ESP8266::setOprToStationSoftAP(uint8_t pattern1,uint8_t pattern2)
+{
+ LOG_ENTER;
+ uint8_t mode;
+ if (!qATCWMODE(&mode,pattern1)) {
+ return false;
+ }
+ if (mode == 3) {
+ return true;
+ } else {
+ if (sATCWMODE(3,pattern2) ) {
+ return true;
+ } else {
+ return false;
+ }
+ }
+}
+
+uint8_t ESP8266::getOprMode(uint8_t pattern1)
+{
+ LOG_ENTER;
+ uint8_t mode;
+ if (!qATCWMODE(&mode,pattern1)) {
+ return 0;
+ } else {
+ return mode;
+ }
+}
+
+String ESP8266::getNowConecAp(uint8_t pattern)
+{
+ LOG_ENTER;
+ String ssid;
+ qATCWJAP(ssid,pattern);
+ return ssid;
+}
+
+String ESP8266::getAPList(void)
+{
+ LOG_ENTER;
+ String list;
+ eATCWLAP(list);
+ return list;
+}
+
+bool ESP8266::joinAP(String ssid, String pwd,uint8_t pattern)
+{
+ LOG_ENTER;
+ return sATCWJAP(ssid, pwd,pattern);
+}
+
+bool ESP8266::leaveAP(void)
+{
+ LOG_ENTER;
+ return eATCWQAP();
+}
+
+String ESP8266::getSoftAPParam(uint8_t pattern)
+{
+ LOG_ENTER;
+ String list;
+ qATCWSAP(list,pattern);
+ return list;
+}
+
+bool ESP8266::setSoftAPParam(String ssid, String pwd, uint8_t chl, uint8_t ecn,uint8_t pattern)
+{
+ LOG_ENTER;
+ return sATCWSAP(ssid, pwd, chl, ecn,pattern);
+}
+
+String ESP8266::getJoinedDeviceIP(void)
+{
+ LOG_ENTER;
+ String list;
+ eATCWLIF(list);
+ return list;
+}
+
+String ESP8266::getDHCP(uint8_t pattern)
+{
+ LOG_ENTER;
+ String dhcp;
+ qATCWDHCP(dhcp,pattern);
+ return dhcp;
+}
+
+bool ESP8266::setDHCP(uint8_t mode, uint8_t en, uint8_t pattern)
+{
+ LOG_ENTER;
+ return sATCWDHCP(mode, en, pattern);
+}
+
+bool ESP8266::setAutoConnect(uint8_t en)
+{
+ LOG_ENTER;
+ return eATCWAUTOCONN(en);
+}
+
+String ESP8266::getStationMac(uint8_t pattern)
+{
+ LOG_ENTER;
+ String mac;
+ qATCIPSTAMAC(mac,pattern);
+ return mac;
+}
+
+bool ESP8266::setStationMac(String mac,uint8_t pattern)
+{
+ LOG_ENTER;
+ return eATCIPSTAMAC(mac,pattern);
+}
+
+String ESP8266::getStationIp(uint8_t pattern)
+{
+ LOG_ENTER;
+ String ip;
+ qATCIPSTAIP(ip,pattern);
+ return ip;
+}
+
+bool ESP8266::setStationIp(String ip,String gateway,String netmask,uint8_t pattern)
+{
+ LOG_ENTER;
+ return eATCIPSTAIP(ip,gateway,netmask,pattern);
+}
+
+String ESP8266::getAPIp(uint8_t pattern)
+{
+ LOG_ENTER;
+ String ip;
+ qATCIPAP(ip,pattern);
+ return ip;
+}
+
+bool ESP8266::setAPIp(String ip,uint8_t pattern)
+{
+ LOG_ENTER;
+ return eATCIPAP(ip,pattern);
+}
+
+bool ESP8266::startSmartConfig(uint8_t type)
+{
+ LOG_ENTER;
+ return eCWSTARTSMART(type);
+}
+
+bool ESP8266::stopSmartConfig(void)
+{
+ LOG_ENTER;
+ return eCWSTOPSMART();
+}
+
+
+String ESP8266::getIPStatus(void)
+{
+ LOG_ENTER;
+ String list;
+ eATCIPSTATUS(list);
+ return list;
+}
+
+String ESP8266::getLocalIP(void)
+{
+ LOG_ENTER;
+ String list;
+ eATCIFSR(list);
+ return list;
+}
+
+bool ESP8266::enableMUX(void)
+{
+ LOG_ENTER;
+ return sATCIPMUX(1);
+}
+
+bool ESP8266::disableMUX(void)
+{
+ LOG_ENTER;
+ return sATCIPMUX(0);
+}
+
+bool ESP8266::createTCP(String addr, uint32_t port)
+{
+ LOG_ENTER;
+ return sATCIPSTARTSingle("TCP", addr, port);
+}
+
+bool ESP8266::releaseTCP(void)
+{
+ LOG_ENTER;
+ return eATCIPCLOSESingle();
+}
+
+bool ESP8266::registerUDP(String addr, uint32_t port)
+{
+ LOG_ENTER;
+ return sATCIPSTARTSingle("UDP", addr, port);
+}
+
+bool ESP8266::unregisterUDP(void)
+{
+ LOG_ENTER;
+ return eATCIPCLOSESingle();
+}
+
+bool ESP8266::createTCP(uint8_t mux_id, String addr, uint32_t port)
+{
+ LOG_ENTER;
+ return sATCIPSTARTMultiple(mux_id, "TCP", addr, port);
+}
+
+bool ESP8266::releaseTCP(uint8_t mux_id)
+{
+ LOG_ENTER;
+ return sATCIPCLOSEMulitple(mux_id);
+}
+
+bool ESP8266::registerUDP(uint8_t mux_id, String addr, uint32_t port)
+{
+ LOG_ENTER;
+ return sATCIPSTARTMultiple(mux_id, "UDP", addr, port);
+}
+
+bool ESP8266::unregisterUDP(uint8_t mux_id)
+{
+ LOG_ENTER;
+ return sATCIPCLOSEMulitple(mux_id);
+}
+
+bool ESP8266::setTCPServerTimeout(uint32_t timeout)
+{
+ LOG_ENTER;
+ return sATCIPSTO(timeout);
+}
+
+bool ESP8266::startTCPServer(uint32_t port)
+{
+ LOG_ENTER;
+ if (sATCIPSERVER(1, port)) {
+ return true;
+ }
+ return false;
+}
+
+bool ESP8266::stopTCPServer(void)
+{
+ LOG_ENTER;
+ sATCIPSERVER(0);
+ restart();
+ return false;
+}
+
+bool ESP8266::setCIPMODE(uint8_t mode)
+{
+ LOG_ENTER;
+ return sATCIPMODE(mode);
+}
+
+bool ESP8266::saveTransLink (uint8_t mode,String ip,uint32_t port)
+{
+ LOG_ENTER;
+ return eATSAVETRANSLINK(mode,ip,port);
+}
+
+bool ESP8266::setPing(String ip)
+{
+ LOG_ENTER;
+ return eATPING(ip);
+}
+
+
+bool ESP8266::startServer(uint32_t port)
+{
+ LOG_ENTER;
+ return startTCPServer(port);
+}
+
+bool ESP8266::stopServer(void)
+{
+ LOG_ENTER;
+ return stopTCPServer();
+}
+
+bool ESP8266::send(const uint8_t *buffer, uint32_t len)
+{
+ LOG_ENTER;
+ return sATCIPSENDSingle(buffer, len);
+}
+
+bool ESP8266::sendFromFlash(uint8_t mux_id, const uint8_t *buffer, uint32_t len)
+{
+ LOG_ENTER;
+ return sATCIPSENDMultipleFromFlash(mux_id, buffer, len);
+}
+
+bool ESP8266::sendFromFlash(const uint8_t *buffer, uint32_t len)
+{
+ LOG_ENTER;
+ return sATCIPSENDSingleFromFlash(buffer, len);
+}
+
+bool ESP8266::send(uint8_t mux_id, const uint8_t *buffer, uint32_t len)
+{
+ LOG_ENTER;
+ return sATCIPSENDMultiple(mux_id, buffer, len);
+}
+
+void ESP8266::run()
+{
+ LOG_ENTER;
+ rx_empty();
+}
+
+/*----------------------------------------------------------------------------*/
+/* +IPD,<id>,<len>:<data> */
+/* +IPD,<len>:<data> */
+
+uint32_t ESP8266::checkIPD(String& data)
+{
+ //Serial.print("### check: ");
+ //Serial.println(data);
+ LOG_ENTER;
+ int32_t index_PIPDcomma = -1;
+ int32_t index_colon = -1; /* : */
+ int32_t index_comma = -1; /* , */
+ int32_t len = -1;
+ int8_t id = -1;
+ {
+ // Just for easier diffing
+ index_PIPDcomma = data.indexOf("+IPD,");
+ if (index_PIPDcomma != -1) {
+ index_colon = data.indexOf(':', index_PIPDcomma + 5);
+ if (index_colon != -1) {
+ index_comma = data.indexOf(',', index_PIPDcomma + 5);
+ /* +IPD,id,len:data */
+ if (index_comma != -1 && index_comma < index_colon) {
+ id = data.substring(index_PIPDcomma + 5, index_comma).toInt();
+ if (id < 0 || id > 4) {
+ return 0;
+ }
+ len = data.substring(index_comma + 1, index_colon).toInt();
+ if (len <= 0) {
+ return 0;
+ }
+ } else { /* +IPD,len:data */
+ len = data.substring(index_PIPDcomma + 5, index_colon).toInt();
+ if (len <= 0) {
+ return 0;
+ }
+ }
+ if (m_onData) {
+ m_onData(id, len, m_onDataPtr);
+ }
+ return len;
+ }
+ }
+ }
+ return 0;
+}
+
+void ESP8266::rx_empty(void)
+{
+ LOG_ENTER;
+ String data;
+ char a;
+ unsigned long start = g_Timer.read_ms();
+ while (g_Timer.read_ms() - start < 10) {
+ if (m_puart->readable()) {
+ a = m_puart->getc();
+ if(a == '\0') continue;
+ data += a;
+ if (checkIPD(data)) {
+ data = "";
+ }
+ start = g_Timer.read_ms();
+ }
+ }
+}
+
+String ESP8266::recvString(String target, uint32_t timeout)
+{
+ LOG_ENTER;
+ String data;
+ char a;
+ unsigned long start = g_Timer.read_ms();
+ while (g_Timer.read_ms() - start < timeout) {
+ while(m_puart->readable() > 0) {
+ a = m_puart->getc();
+ ESP8266_LOG("a=0x%02X ", a);
+ if(a == '\0') continue;
+ data += a;
+ if (data.indexOf(target) != -1) {
+ ESP8266_LOG("\r\ndata = %s\r\n", data.c_str());
+ return data;
+ } else if (checkIPD(data)) {
+ data = "";
+ }
+ }
+ }
+ ESP8266_LOG("\r\n");
+ ESP8266_LOG("data1 = %s\r\n", data.c_str());
+ return data;
+}
+
+String ESP8266::recvString(String target1, String target2, uint32_t timeout)
+{
+ LOG_ENTER;
+ String data;
+ char a;
+ unsigned long start = g_Timer.read_ms();
+ while (g_Timer.read_ms() - start < timeout) {
+ while(m_puart->readable() > 0) {
+ a = m_puart->getc();
+ ESP8266_LOG("a=0x%02x ", a);
+ if(a == '\0') continue;
+ data += a;
+ if (data.indexOf(target1) != -1) {
+ ESP8266_LOG("\r\ndata2 = %s\r\n", data.c_str());
+ return data;
+ } else if (data.indexOf(target2) != -1) {
+ ESP8266_LOG("\r\ndata3 = %s\r\n", data.c_str());
+ return data;
+ } else if (checkIPD(data)) {
+ data = "";
+ }
+ }
+ }
+ ESP8266_LOG("\r\ndata4 = %s\r\n", data.c_str());
+ return data;
+}
+
+String ESP8266::recvString(String target1, String target2, String target3, uint32_t timeout)
+{
+ LOG_ENTER;
+ String data;
+ char a;
+ unsigned long start = g_Timer.read_ms();
+ while (g_Timer.read_ms() - start < timeout) {
+ while(m_puart->readable() > 0) {
+ a = m_puart->getc();
+ ESP8266_LOG("a=0x%02x ", a);
+ if(a == '\0') continue;
+ data += a;
+
+ if (data.indexOf(target1) != -1) {
+ ESP8266_LOG("\r\ndata5 = %s\r\n", data.c_str());
+ return data;
+ } else if (data.indexOf(target2) != -1) {
+ ESP8266_LOG("\r\ndata6 = %s\r\n", data.c_str());
+ return data;
+ } else if (data.indexOf(target3) != -1) {
+ ESP8266_LOG("\r\ndata7 = %s\r\n", data.c_str());
+ return data;
+ } else if (checkIPD(data)) {
+ data = "";
+ }
+ }
+ }
+ ESP8266_LOG("\r\ndata8 = %s\r\n", data.c_str());
+ return data;
+}
+
+bool ESP8266::recvFind(String target, uint32_t timeout)
+{
+ LOG_ENTER;
+ String data_tmp;
+ data_tmp = recvString(target, timeout);
+ if (data_tmp.indexOf(target) != -1) {
+ return true;
+ }
+ return false;
+}
+
+bool ESP8266::recvFindAndFilter(String target, String begin, String end, String &data, uint32_t timeout)
+{
+ LOG_ENTER;
+ String data_tmp;
+ data_tmp = recvString(target, timeout);
+ if (data_tmp.indexOf(target) != -1) {
+ int32_t index1 = data_tmp.indexOf(begin);
+ int32_t index2 = data_tmp.indexOf(end);
+ if (index1 != -1 && index2 != -1) {
+ index1 += begin.length();
+ data = data_tmp.substring(index1, index2);
+ return true;
+ }
+ }
+ data = data_tmp;
+ return false;
+}
+
+bool ESP8266::eAT(void)
+{
+ LOG_ENTER;
+ rx_empty();
+ //m_puart->println(F("AT"));
+ m_puart->printf("AT\r\n");
+ return recvFind("OK");
+}
+
+bool ESP8266::eATRST(void)
+{
+ LOG_ENTER;
+ rx_empty();
+ //m_puart->println(F("AT+RST"));
+ m_puart->printf("AT+RST\r\n");
+ return recvFind("OK");
+}
+
+bool ESP8266::eATGMR(String &version)
+{
+ LOG_ENTER;
+ rx_empty();
+ wait_ms(3000);
+ //m_puart->println(F("AT+GMR"));
+ m_puart->printf("AT+GMR\r\n");
+ return recvFindAndFilter("OK", "\r\r\n", "\r\n\r\nOK", version, 10000);
+}
+
+bool ESP8266::eATGSLP(uint32_t time)
+{
+ LOG_ENTER;
+ rx_empty();
+ //m_puart->print(F("AT+GSLP="));
+ //m_puart->println(time);
+ m_puart->printf("AT+GSLP=%u\r\n", time);
+ return recvFind("OK");
+}
+
+bool ESP8266::eATE(uint8_t mode)
+{
+ LOG_ENTER;
+ rx_empty();
+ //m_puart->print(F("ATE"));
+ //m_puart->println(mode);
+ m_puart->printf("ATE%d\r\n", mode);
+ return recvFind("OK");
+}
+
+bool ESP8266::eATRESTORE(void)
+{
+ LOG_ENTER;
+ rx_empty();
+ //m_puart->println(F("AT+RESTORE"));
+ m_puart->printf("AT+RESTORE\r\n");
+ return recvFind("OK");
+}
+
+bool ESP8266::eATSETUART(uint32_t baudrate,uint8_t pattern)
+{
+ LOG_ENTER;
+ rx_empty();
+ if(pattern>3||pattern<1) {
+ return false;
+ }
+ switch(pattern) {
+ case 1:
+ //m_puart->print(F("AT+UART="));
+ m_puart->printf("AT+UART=");
+ break;
+ case 2:
+ //m_puart->print(F("AT+UART_CUR="));
+ m_puart->printf("AT+UART_CUR=");
+ break;
+ case 3:
+ //m_puart->print(F("AT+UART_DEF="));
+ m_puart->printf("AT+UART_DEF=");
+ break;
+ }
+ #if 0
+ m_puart->print(baudrate);
+ m_puart->print(F(","));
+ m_puart->print(8);
+ m_puart->print(F(","));
+ m_puart->print(1);
+ m_puart->print(F(","));
+ m_puart->print(0);
+ m_puart->print(F(","));
+ m_puart->println(0);
+ #else
+ m_puart->printf("%u,%d,%d,%d,%d\r\n", baudrate, 8, 1, 0, 0);
+ #endif
+ if(recvFind("OK",5000)) {
+ //m_puart->begin(baudrate);
+ m_puart->baud(baudrate);
+ return true;
+ } else {
+ return false;
+ }
+}
+
+bool ESP8266::qATCWMODE(uint8_t *mode,uint8_t pattern)
+{
+ LOG_ENTER;
+ String str_mode;
+ bool ret;
+ if (!mode||!pattern) {
+ return false;
+ }
+ rx_empty();
+ switch(pattern) {
+ case 1 :
+ //m_puart->println(F("AT+CWMODE_DEF?"));
+ m_puart->printf("AT+CWMODE_DEF?\r\n");
+ break;
+ case 2:
+ //m_puart->println(F("AT+CWMODE_CUR?"));
+ m_puart->printf("AT+CWMODE_CUR?\r\n");
+ break;
+ default:
+ //m_puart->println(F("AT+CWMODE?"));
+ m_puart->printf("AT+CWMODE?\r\n");
+ }
+ ret = recvFindAndFilter("OK", ":", "\r\n\r\nOK", str_mode);
+ if (ret) {
+ *mode = (uint8_t)str_mode.toInt();
+ return true;
+ } else {
+ return false;
+ }
+}
+
+bool ESP8266::eATCWMODE(String &list)
+{
+ LOG_ENTER;
+ rx_empty();
+ //m_puart->println(F("AT+CWMODE=?"));
+ m_puart->printf("AT+CWMODE=?\r\n");
+ return recvFindAndFilter("OK", "+CWMODE:(", ")\r\n\r\nOK", list);
+}
+
+bool ESP8266::sATCWMODE(uint8_t mode,uint8_t pattern)
+{
+ LOG_ENTER;
+ if(!pattern) {
+ return false;
+ }
+ String data;
+ rx_empty();
+ switch(pattern) {
+ case 1 :
+ //m_puart->print(F("AT+CWMODE_DEF="));
+ m_puart->printf("AT+CWMODE_DEF=");
+ break;
+ case 2:
+ //m_puart->print(F("AT+CWMODE_CUR="));
+ m_puart->printf("AT+CWMODE_CUR=");
+ break;
+ default:
+ //m_puart->print(F("AT+CWMODE="));
+ m_puart->printf("AT+CWMODE=");
+ }
+ //m_puart->println(mode);
+ m_puart->printf("%d\r\n", mode);
+ data = recvString("OK", "no change");
+
+ if (data.indexOf("OK") != -1 || data.indexOf("no change") != -1) {
+ return true;
+ }
+ return false;
+}
+
+
+bool ESP8266::qATCWJAP(String &ssid,uint8_t pattern)
+{
+ LOG_ENTER;
+ bool ret;
+ if (!pattern) {
+ return false;
+ }
+ rx_empty();
+ switch(pattern) {
+ case 1 :
+ //m_puart->println(F("AT+CWJAP_DEF?"));
+ m_puart->printf("AT+CWJAP_DEF?\r\n");
+ break;
+ case 2:
+ //m_puart->println(F("AT+CWJAP_CUR?"));
+ m_puart->printf("AT+CWJAP_CUR?\r\n");
+ break;
+ default:
+ //m_puart->println(F("AT+CWJAP?"));
+ m_puart->printf("AT+CWJAP?\r\n");
+ }
+ ssid = recvString("OK", "No AP");
+ if (ssid.indexOf("OK") != -1 || ssid.indexOf("No AP") != -1) {
+ return true;
+ }
+ return false;
+
+}
+
+bool ESP8266::sATCWJAP(String ssid, String pwd, uint8_t pattern)
+{
+ LOG_ENTER;
+ String data;
+ if (!pattern) {
+ return false;
+ }
+ rx_empty();
+ switch(pattern) {
+ case 1 :
+ //m_puart->print(F("AT+CWJAP_DEF=\""));
+ m_puart->printf("AT+CWJAP_DEF=\"");
+ break;
+ case 2:
+ //m_puart->print(F("AT+CWJAP_CUR=\""));
+ m_puart->printf("AT+CWJAP_CUR=\"");
+ break;
+ default:
+ //m_puart->print(F("AT+CWJAP=\""));
+ m_puart->printf("AT+CWJAP=\"");
+ }
+#if 0
+ m_puart->print(ssid);
+ m_puart->print(F("\",\""));
+ m_puart->print(pwd);
+ m_puart->println(F("\""));
+#else
+ //TODO:
+ m_puart->printf("%s\",\"%s\"\r\n", ssid.c_str(), pwd.c_str());
+#endif
+ data = recvString("OK", "FAIL", 20000);
+ if (data.indexOf("OK") != -1) {
+ return true;
+ }
+ return false;
+}
+
+bool ESP8266::eATCWLAP(String &list)
+{
+ LOG_ENTER;
+ String data;
+ rx_empty();
+ //m_puart->println(F("AT+CWLAP"));
+ m_puart->printf("AT+CWLAP\r\n");
+ return recvFindAndFilter("OK", "\r\r\n", "\r\n\r\nOK", list, 15000);
+}
+
+bool ESP8266::eATCWQAP(void)
+{
+ LOG_ENTER;
+ String data;
+ rx_empty();
+ //m_puart->println(F("AT+CWQAP"));
+ m_puart->printf("AT+CWQAP\r\n");
+ return recvFind("OK");
+}
+
+bool ESP8266::qATCWSAP(String &List,uint8_t pattern)
+{
+ LOG_ENTER;
+ if (!pattern) {
+ return false;
+ }
+ rx_empty();
+ switch(pattern) {
+ case 1 :
+ //m_puart->println(F("AT+CWSAP_DEF?"));
+ m_puart->printf("AT+CWSAP_DEF?\r\n");
+ break;
+ case 2:
+ //m_puart->println(F("AT+CWSAP_CUR?"));
+ m_puart->printf("AT+CWSAP_CUR?\r\n");
+ break;
+ default:
+ //m_puart->println(F("AT+CWSAP?"));
+ m_puart->printf("AT+CWSAP?\r\n");
+ }
+ return recvFindAndFilter("OK", "\r\r\n", "\r\n\r\nOK", List,10000);
+}
+
+bool ESP8266::sATCWSAP(String ssid, String pwd, uint8_t chl, uint8_t ecn,uint8_t pattern)
+{
+ LOG_ENTER;
+ String data;
+ if (!pattern) {
+ return false;
+ }
+ rx_empty();
+ switch(pattern) {
+ case 1 :
+ //m_puart->print(F("AT+CWSAP_DEF=\""));
+ m_puart->printf("AT+CWSAP_DEF=\"");
+ break;
+ case 2:
+ //m_puart->print(F("AT+CWSAP_CUR=\""));
+ m_puart->printf("AT+CWSAP_CUR=\"");
+ break;
+ default:
+ //m_puart->print(F("AT+CWSAP=\""));
+ m_puart->printf("AT+CWSAP=\"");
+ }
+ #if 0
+ m_puart->print(ssid);
+ m_puart->print(F("\",\""));
+ m_puart->print(pwd);
+ m_puart->print(F("\","));
+ m_puart->print(chl);
+ m_puart->print(F(","));
+ m_puart->println(ecn);
+ #else
+ m_puart->printf("%s\",\"%s\",%d,%d\r\n", ssid.c_str(), pwd.c_str(), chl, ecn);
+ #endif
+ data = recvString("OK", "ERROR", 5000);
+ if (data.indexOf("OK") != -1) {
+ return true;
+ }
+ return false;
+}
+
+bool ESP8266::eATCWLIF(String &list)
+{
+ LOG_ENTER;
+ String data;
+ rx_empty();
+ //m_puart->println(F("AT+CWLIF"));
+ m_puart->printf("AT+CWLIF\r\n");
+ return recvFindAndFilter("OK", "\r\r\n", "\r\n\r\nOK", list);
+}
+
+bool ESP8266::qATCWDHCP(String &List,uint8_t pattern)
+{
+ LOG_ENTER;
+ if (!pattern) {
+ return false;
+ }
+ rx_empty();
+ switch(pattern) {
+ case 1 :
+ //m_puart->println(F("AT+CWDHCP_DEF?"));
+ m_puart->printf("AT+CWDHCP_DEF?\r\n");
+ break;
+ case 2:
+ //m_puart->println(F("AT+CWDHCP_CUR?"));
+ m_puart->printf("AT+CWDHCP_CUR?\r\n");
+ break;
+ default:
+ //m_puart->println(F("AT+CWDHCP?"));
+ m_puart->printf("AT+CWDHCP?\r\n");
+ }
+
+ return recvFindAndFilter("OK", "\r\r\n", "\r\nOK", List,10000);
+
+}
+
+
+bool ESP8266::sATCWDHCP(uint8_t mode, uint8_t en, uint8_t pattern)
+{
+ LOG_ENTER;
+ String data;
+ if (!pattern) {
+ return false;
+ }
+ rx_empty();
+ switch(pattern) {
+ case 1 :
+ //m_puart->print(F("AT+CWDHCP_DEF="));
+ m_puart->printf("AT+CWDHCP_DEF=");
+ break;
+ case 2:
+ //m_puart->print(F("AT+CWDHCP_CUR="));
+ m_puart->printf("AT+CWDHCP_CUR=");
+ break;
+ default:
+ //m_puart->print(F("AT+CWDHCP="));
+ m_puart->printf("AT+CWDHCP=");
+ }
+ #if 0
+ m_puart->print(mode);
+ m_puart->print(F(","));
+ m_puart->println(en);
+ #else
+ m_puart->printf("%d,%d\r\n", mode, en);
+ #endif
+ data = recvString("OK", "ERROR", 2000);
+
+ if (data.indexOf("OK") != -1) {
+ return true;
+ }
+ return false;
+}
+
+
+bool ESP8266::eATCWAUTOCONN(uint8_t en)
+{
+ LOG_ENTER;
+ rx_empty();
+ if(en>1||en<0) {
+ return false;
+ }
+ #if 0
+ m_puart->print(F("AT+CWAUTOCONN="));
+ m_puart->println(en);
+ #else
+ m_puart->printf("AT+CWAUTOCONN=%d\r\n", en);
+ #endif
+ return recvFind("OK");
+
+}
+
+bool ESP8266::qATCIPSTAMAC(String &mac,uint8_t pattern)
+{
+ LOG_ENTER;
+ rx_empty();
+ if (!pattern) {
+ return false;
+ }
+ switch(pattern) {
+ case 1 :
+ //m_puart->println(F("AT+CIPSTAMAC_DEF?"));
+ m_puart->printf("AT+CIPSTAMAC_DEF?\r\n");
+ break;
+ case 2:
+ //m_puart->println(F("AT+CIPSTAMAC_CUR?"));
+ m_puart->printf("AT+CIPSTAMAC_CUR?\r\n");
+ break;
+ default:
+ //m_puart->println(F("AT+CIPSTAMAC?"));
+ m_puart->printf("AT+CIPSTAMAC?\r\n");
+ }
+ return recvFindAndFilter("OK", "\r\r\n", "\r\n\r\nOK", mac,2000);
+
+}
+
+
+
+bool ESP8266::eATCIPSTAMAC(String mac,uint8_t pattern)
+{
+ LOG_ENTER;
+ rx_empty();
+ if (!pattern) {
+ return false;
+ }
+ switch(pattern) {
+ case 1 :
+ //m_puart->print(F("AT+CIPSTAMAC_DEF="));
+ m_puart->printf("AT+CIPSTAMAC_DEF=");
+ break;
+ case 2:
+ //m_puart->print(F("AT+CIPSTAMAC_CUR="));
+ m_puart->printf("AT+CIPSTAMAC_CUR=");
+ break;
+ default:
+ //m_puart->print(F("AT+CIPSTAMAC="));
+ m_puart->printf("AT+CIPSTAMAC=");
+ }
+ #if 0
+ m_puart->print(F("\""));
+ m_puart->print(mac);
+ m_puart->println(F("\""));
+ #else
+ m_puart->printf("\"%s\"\r\n", mac.c_str());
+ #endif
+ return recvFind("OK");
+
+}
+
+bool ESP8266::qATCIPSTAIP(String &ip,uint8_t pattern)
+{
+ LOG_ENTER;
+ rx_empty();
+ if (!pattern) {
+ return false;
+ }
+ switch(pattern) {
+ case 1 :
+ //m_puart->println(F("AT+CIPSTA_DEF?"));
+ m_puart->printf("AT+CIPSTA_DEF?\r\n");
+ break;
+ case 2:
+ //m_puart->println(F("AT+CIPSTA_CUR?"));
+ m_puart->printf("AT+CIPSTA_CUR?\r\n");
+ break;
+ default:
+ //m_puart->println(F("AT+CIPSTA?"));
+ m_puart->printf("AT+CIPSTA?\r\n");
+ }
+ return recvFindAndFilter("OK", "\r\r\n", "\r\n\r\nOK", ip,2000);
+
+}
+
+bool ESP8266::eATCIPSTAIP(String ip,String gateway,String netmask,uint8_t pattern)
+{
+ LOG_ENTER;
+ rx_empty();
+ if (!pattern) {
+ return false;
+ }
+ switch(pattern) {
+ case 1 :
+ //m_puart->print(F("AT+CIPSTA_DEF="));
+ m_puart->printf("AT+CIPSTA_DEF=");
+ break;
+ case 2:
+ //m_puart->print(F("AT+CIPSTA_CUR="));
+ m_puart->printf("AT+CIPSTA_CUR=");
+ break;
+ default:
+ //m_puart->print(F("AT+CIPSTA="));
+ m_puart->printf("AT+CIPSTA=");
+ }
+ #if 0
+ m_puart->print(F("\""));
+ m_puart->print(ip);
+ m_puart->print(F("\",\""));
+ m_puart->print(gateway);
+ m_puart->print(F("\",\""));
+ m_puart->print(netmask);
+ m_puart->println(F("\""));
+ #else
+ m_puart->printf("\"%s\",\"%s\",\"%s\"\r\n", ip.c_str(), gateway.c_str(), netmask.c_str());
+ #endif
+ return recvFind("OK");
+
+}
+
+
+bool ESP8266::qATCIPAP(String &ip,uint8_t pattern)
+{
+ LOG_ENTER;
+ rx_empty();
+ if (!pattern) {
+ return false;
+ }
+ switch(pattern) {
+ case 1 :
+ //m_puart->println(F("AT+CIPAP_DEF?"));
+ m_puart->printf("AT+CIPAP_DEF?\r\n");
+ break;
+ case 2:
+ //m_puart->println(F("AT+CIPAP_CUR?"));
+ m_puart->printf("AT+CIPAP_CUR?\r\n");
+ break;
+ default:
+ //m_puart->println(F("AT+CIPAP?"));
+ m_puart->printf("AT+CIPAP?\r\n");
+ }
+ return recvFindAndFilter("OK", "\r\r\n", "\r\n\r\nOK", ip,2000);
+
+}
+
+
+bool ESP8266::eATCIPAP(String ip,uint8_t pattern)
+{
+ LOG_ENTER;
+ rx_empty();
+ if (!pattern) {
+ return false;
+ }
+ switch(pattern) {
+ case 1 :
+ //m_puart->print(F("AT+CIPAP_DEF="));
+ m_puart->printf("AT+CIPAP_DEF=");
+ break;
+ case 2:
+ //m_puart->print(F("AT+CIPAP_CUR="));
+ m_puart->printf("AT+CIPAP_CUR=");
+ break;
+ default:
+ //m_puart->print(F("AT+CIPAP="));
+ m_puart->printf("AT+CIPAP=");
+ }
+ #if 0
+ m_puart->print(F("\""));
+ m_puart->print(ip);
+ m_puart->println(F("\""));
+ #else
+ m_puart->printf("\"%s\"\r\n", ip.c_str());
+ #endif
+ return recvFind("OK");
+
+}
+
+
+bool ESP8266::eCWSTARTSMART(uint8_t type)
+{
+ LOG_ENTER;
+ rx_empty();
+ #if 0
+ m_puart->print(F("AT+CWSTARTSMART="));
+ m_puart->println(type);
+ #else
+ m_puart->printf("AT+CWSTARTSMART=%d\r\n", type);
+ #endif
+ return recvFind("OK");
+}
+
+bool ESP8266::eCWSTOPSMART(void)
+{
+ LOG_ENTER;
+ rx_empty();
+ //m_puart->println(F("AT+CWSTOPSMART"));
+ m_puart->printf("AT+CWSTOPSMART\r\n");
+ return recvFind("OK");
+}
+
+bool ESP8266::eATCIPSTATUS(String &list)
+{
+ LOG_ENTER;
+ String data;
+ //delay(100);
+ wait_ms(100);
+ rx_empty();
+ //m_puart->println(F("AT+CIPSTATUS"));
+ m_puart->printf("AT+CIPSTATUS\r\n");
+ return recvFindAndFilter("OK", "\r\r\n", "\r\n\r\nOK", list);
+}
+
+bool ESP8266::sATCIPSTARTSingle(String type, String addr, uint32_t port)
+{
+ LOG_ENTER;
+ String data;
+ rx_empty();
+ #if 0
+ m_puart->print(F("AT+CIPSTART=\""));
+ m_puart->print(type);
+ m_puart->print(F("\",\""));
+ m_puart->print(addr);
+ m_puart->print(F("\","));
+ m_puart->println(port);
+ #else
+ m_puart->printf("AT+CIPSTART=\"%s\",\"%s\",%u\r\n", type.c_str(), addr.c_str(), port);
+ #endif
+ data = recvString("OK", "ERROR", "ALREADY CONNECT", 10000);
+ if (data.indexOf("OK") != -1 || data.indexOf("ALREADY CONNECT") != -1) {
+ return true;
+ }
+ return false;
+}
+
+bool ESP8266::sATCIPSTARTMultiple(uint8_t mux_id, String type, String addr, uint32_t port)
+{
+ LOG_ENTER;
+ String data;
+ rx_empty();
+ #if 0
+ m_puart->print(F("AT+CIPSTART="));
+ m_puart->print(mux_id);
+ m_puart->print(F(",\""));
+ m_puart->print(type);
+ m_puart->print(F("\",\""));
+ m_puart->print(addr);
+ m_puart->print(F("\","));
+ m_puart->println(port);
+ #else
+ m_puart->printf("AT+CIPSTART=%d,\"%s\",\"%s\",%u\r\n", mux_id, type.c_str(), addr.c_str(), port);
+ #endif
+ data = recvString("OK", "ERROR", "ALREADY CONNECT", 10000);
+ if (data.indexOf("OK") != -1 || data.indexOf("ALREADY CONNECT") != -1) {
+ return true;
+ }
+ return false;
+}
+
+bool ESP8266::sATCIPSENDSingle(const uint8_t *buffer, uint32_t len)
+{
+ LOG_ENTER;
+ rx_empty();
+ #if 0
+ m_puart->print(F("AT+CIPSEND="));
+ m_puart->println(len);
+ #else
+ m_puart->printf("AT+CIPSEND=%u\r\n", len);
+ #endif
+ if (recvFind(">", 5000)) {
+ rx_empty();
+ for (uint32_t i = 0; i < len; i++) {
+ //m_puart->write(buffer[i]);
+ m_puart->putc(buffer[i]);
+ }
+ return recvFind("SEND OK", 10000);
+ }
+ return false;
+}
+
+bool ESP8266::sATCIPSENDMultiple(uint8_t mux_id, const uint8_t *buffer, uint32_t len)
+{
+ LOG_ENTER;
+ rx_empty();
+ #if 0
+ m_puart->print(F("AT+CIPSEND="));
+ m_puart->print(mux_id);
+ m_puart->print(F(","));
+ m_puart->println(len);
+ #else
+ m_puart->printf("AT+CIPSEND=%d,%u\r\n", mux_id, len);
+ #endif
+ if (recvFind(">", 5000)) {
+ rx_empty();
+ for (uint32_t i = 0; i < len; i++) {
+ //m_puart->write(buffer[i]);
+ m_puart->putc(buffer[i]);
+ }
+ return recvFind("SEND OK", 10000);
+ }
+ return false;
+}
+
+bool ESP8266::sATCIPSENDSingleFromFlash(const uint8_t *buffer, uint32_t len)
+{
+ LOG_ENTER;
+ rx_empty();
+ #if 0
+ m_puart->print(F("AT+CIPSEND="));
+ m_puart->println(len);
+ #else
+ m_puart->printf("AT+CIPSEND=%u\r\n", len);
+ #endif
+ if (recvFind(">", 5000)) {
+ rx_empty();
+ for (uint32_t i = 0; i < len; i++) {
+ //m_puart->write((char) pgm_read_byte(&buffer[i]));
+ m_puart->putc(buffer[i]);
+ }
+ return recvFind("SEND OK", 10000);
+ }
+ return false;
+}
+
+bool ESP8266::sATCIPSENDMultipleFromFlash(uint8_t mux_id, const uint8_t *buffer, uint32_t len)
+{
+ LOG_ENTER;
+ rx_empty();
+ #if 0
+ m_puart->print(F("AT+CIPSEND="));
+ m_puart->print(mux_id);
+ m_puart->print(F(","));
+ m_puart->println(len);
+ #else
+ m_puart->printf("AT+CIPSEND=%d,%u\r\n", mux_id, len);
+ #endif
+ if (recvFind(">", 5000)) {
+ rx_empty();
+ for (uint32_t i = 0; i < len; i++) {
+ //m_puart->write((char) pgm_read_byte(&buffer[i]));
+ m_puart->putc(buffer[i]);
+ }
+ return recvFind("SEND OK", 10000);
+ }
+ return false;
+}
+
+bool ESP8266::sATCIPCLOSEMulitple(uint8_t mux_id)
+{
+ LOG_ENTER;
+ String data;
+ rx_empty();
+ #if 0
+ m_puart->print(F("AT+CIPCLOSE="));
+ m_puart->println(mux_id);
+ #else
+ m_puart->printf("AT+CIPCLOSE=%d\r\n", mux_id);
+ #endif
+ data = recvString("OK", "link is not", 5000);
+ if (data.indexOf("OK") != -1 || data.indexOf("link is not") != -1) {
+ return true;
+ }
+ return false;
+}
+
+bool ESP8266::eATCIPCLOSESingle(void)
+{
+ LOG_ENTER;
+ rx_empty();
+ //m_puart->println(F("AT+CIPCLOSE"));
+ m_puart->printf("AT+CIPCLOSE\r\n");
+ return recvFind("OK", 5000);
+}
+
+bool ESP8266::eATCIFSR(String &list)
+{
+ LOG_ENTER;
+ rx_empty();
+ //m_puart->println(F("AT+CIFSR"));
+ m_puart->printf("AT+CIFSR\r\n");
+ return recvFindAndFilter("OK", "\r\r\n", "\r\n\r\nOK", list);
+}
+
+bool ESP8266::sATCIPMUX(uint8_t mode)
+{
+ LOG_ENTER;
+ String data;
+ rx_empty();
+ #if 0
+ m_puart->print(F("AT+CIPMUX="));
+ m_puart->println(mode);
+ #else
+ m_puart->printf("AT+CIPMUX=%d\r\n", mode);
+ #endif
+ data = recvString("OK", "Link is builded");
+ if (data.indexOf("OK") != -1) {
+ return true;
+ }
+ return false;
+}
+
+bool ESP8266::sATCIPSERVER(uint8_t mode, uint32_t port)
+{
+ LOG_ENTER;
+ String data;
+ if (mode) {
+ rx_empty();
+ #if 0
+ m_puart->print(F("AT+CIPSERVER=1,"));
+ m_puart->println(port);
+ #else
+ m_puart->printf("AT+CIPSERVER=1,%u\r\n", port);
+ #endif
+ data = recvString("OK", "no change");
+ if (data.indexOf("OK") != -1 || data.indexOf("no change") != -1) {
+ return true;
+ }
+ return false;
+ } else {
+ rx_empty();
+ //m_puart->println(F("AT+CIPSERVER=0"));
+ m_puart->printf("AT+CIPSERVER=0\r\n");
+ return recvFind("\r\r\n");
+ }
+}
+
+bool ESP8266::sATCIPMODE(uint8_t mode)
+{
+ LOG_ENTER;
+ String data;
+ if(mode>1||mode<0) {
+ return false;
+ }
+ rx_empty();
+ #if 0
+ m_puart->print(F("AT+CIPMODE="));
+ m_puart->println(mode);
+ #else
+ m_puart->printf("AT+CIPMODE=%d\r\n", mode);
+ #endif
+ data = recvString("OK", "Link is builded",2000);
+ if (data.indexOf("OK") != -1 ) {
+ return true;
+ }
+ return false;
+}
+
+bool ESP8266::eATSAVETRANSLINK(uint8_t mode,String ip,uint32_t port)
+{
+ LOG_ENTER;
+ String data;
+ rx_empty();
+ #if 0
+ m_puart->print(F("AT+SAVETRANSLINK="));
+ m_puart->print(mode);
+ m_puart->print(F(",\""));
+ m_puart->print(ip);
+ m_puart->print(F("\","));
+ m_puart->println(port);
+ #else
+ m_puart->printf("AT+SAVETRANSLINK=%d,\"%s\",%u\r\n", mode, ip.c_str(), port);
+ #endif
+ data = recvString("OK", "ERROR",2000);
+ if (data.indexOf("OK") != -1 ) {
+ return true;
+ }
+ return false;
+}
+
+bool ESP8266::eATPING(String ip)
+{
+ LOG_ENTER;
+ rx_empty();
+ #if 0
+ m_puart->print(F("AT+PING="));
+ m_puart->print(F("\""));
+ m_puart->print(ip);
+ m_puart->println(F("\""));
+ #else
+ m_puart->printf("AT+PING=\"%s\"\r\n", ip.c_str());
+ #endif
+ return recvFind("OK",2000);
+}
+
+bool ESP8266::sATCIPSTO(uint32_t timeout)
+{
+ LOG_ENTER;
+ rx_empty();
+ #if 0
+ m_puart->print(F("AT+CIPSTO="));
+ m_puart->println(timeout);
+ #else
+ m_puart->printf("AT+CIPSTO=%u\r\n", timeout);
+ #endif
+ return recvFind("OK");
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/ESP8266_HardSer.h Thu Jun 16 08:08:30 2016 +0000
@@ -0,0 +1,720 @@
+/**
+ * @file ESP8266.h
+ * @brief The definition 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.
+ */
+#ifndef __ESP8266_H__
+#define __ESP8266_H__
+
+//#include "Arduino.h"
+#include "mbed.h"
+#include "WString.h"
+
+extern Timer g_Timer;
+extern Serial pc;
+//#define LOG_ESP8266
+#ifdef LOG_ESP8266
+#define ESP8266_LOG pc.printf
+#define LOG_ENTER ESP8266_LOG("Enter %s\r\n", __func__);
+#else
+#define ESP8266_LOG(...)
+#define LOG_ENTER
+#endif
+
+//#define ESP8266_USE_SOFTWARE_SERIAL
+
+#ifdef ESP8266_USE_SOFTWARE_SERIAL
+//#include "SoftwareSerial.h"
+#define SoftwareSerial Serial
+#else
+#define HardwareSerial Serial
+#endif
+
+#define VERSION_18 0X18
+#define VERSION_22 0X22
+#define DEFAULT_PATTERN 3
+
+/**
+ * You can modify the macro to choose a different version
+ */
+
+#define USER_SEL_VERSION VERSION_22
+
+/**
+ * Provide an easy-to-use way to manipulate ESP8266.
+ */
+class ESP8266 {
+ public:
+
+ typedef void (*onData)(uint8_t mux_id, uint32_t len, void* ptr);
+
+#ifdef ESP8266_USE_SOFTWARE_SERIAL
+ /*
+ * Constuctor.
+ *
+ * @param uart - an reference of SoftwareSerial object.
+ * @warning parameter baud depends on the AT firmware. 9600 is an common value.
+ */
+
+ ESP8266(SoftwareSerial &uart);
+
+ SoftwareSerial* getUart() { return m_puart; }
+
+#else /* HardwareSerial */
+ /*
+ * Constuctor.
+ *
+ * @param uart - an reference of HardwareSerial object.
+ * @warning parameter baud depends on the AT firmware. 9600 is an common value.
+ */
+
+ ESP8266(HardwareSerial &uart);
+
+ HardwareSerial* getUart() { return m_puart; }
+
+#endif /* #ifdef ESP8266_USE_SOFTWARE_SERIAL */
+
+ void setOnData(onData cbk, void* ptr) {
+ m_onData = cbk;
+ m_onDataPtr = ptr;
+ }
+
+ void run();
+
+ /**
+ * Verify ESP8266 whether live or not.
+ *
+ * Actually, this method will send command "AT" to ESP8266 and waiting for "OK".
+ *
+ * @retval true - alive.
+ * @retval false - dead.
+ */
+ bool kick(void);
+
+ /**
+ * Restart ESP8266 by "AT+RST".
+ *
+ * This method will take 3 seconds or more.
+ *
+ * @retval true - success.
+ * @retval false - failure.
+ */
+ bool restart(void);
+
+ /**
+ * Get the version of AT Command Set.
+ *
+ * @return the string of version.
+ */
+ String getVersion(void);
+
+ /**
+ * Start function of deep sleep.
+ *
+ * @param time - the sleep time.
+ * @retval true - success.
+ * @retval false - failure.
+ * @note the feature requires hardware support.
+ */
+ bool deepSleep(uint32_t time);
+
+ /**
+ * Switch the echo function.
+ *
+ * @param mode - 1 start echo -0 stop echo
+ * @retval true - success.
+ * @retval false - failure.
+ *
+ */
+ bool setEcho(uint8_t mode);
+
+ /**
+ * Restore factory.
+ * @retval true - success.
+ * @retval false - failure.
+ * @note The operation can lead to restart the machine.
+ */
+ bool restore(void);
+
+ /**
+ * Set up a serial port configuration.
+ *
+ * @param pattern -1 send "AT+UART=", -2 send "AT+UART_CUR=", -3 send "AT+UART_DEF=".
+ * @param baudrate - the uart baudrate.
+ * @retval true - success.
+ * @retval false - failure.
+ * @note Only allows baud rate design, for the other parameters:databits- 8,stopbits -1,parity -0,flow control -0 .
+ */
+ bool setUart(uint32_t baudrate,uint8_t pattern);
+
+ /**
+ * Set operation mode to station.
+ *
+ * @param pattern1 -1, send "AT+CWMODE_DEF?",-2,send "AT+CWMODE_CUR?",-3,send "AT+CWMODE?".
+ * @param pattern2 -1, send "AT+CWMODE_DEF=",-2,send "AT+CWMODE_CUR=",-3,send "AT+CWMODE=".
+ * @retval true - success.
+ * @retval false - failure.
+ *
+ */
+ bool setOprToStation(uint8_t pattern1=DEFAULT_PATTERN,uint8_t pattern2=DEFAULT_PATTERN);
+
+ /**
+ * Get the model values list.
+ *
+ * @return the list of model.
+ */
+ String getWifiModeList(void);
+
+ /**
+ * Set operation mode to softap.
+ * @param pattern1 -1, send "AT+CWMODE_DEF?",-2,send "AT+CWMODE_CUR?",-3,send "AT+CWMODE?".
+ * @param pattern2 -1, send "AT+CWMODE_DEF=",-2,send "AT+CWMODE_CUR=",-3,send "AT+CWMODE=".
+ *
+ * @retval true - success.
+ * @retval false - failure.
+ */
+ bool setOprToSoftAP(uint8_t pattern1=DEFAULT_PATTERN,uint8_t pattern2=DEFAULT_PATTERN);
+
+ /**
+ * Set operation mode to station + softap.
+ * @param pattern1 -1, send "AT+CWMODE_DEF?",-2,send "AT+CWMODE_CUR?",-3,send "AT+CWMODE?".
+ * @param pattern2 -1, send "AT+CWMODE_DEF=",-2,send "AT+CWMODE_CUR=",-3,send "AT+CWMODE=".
+ *
+ * @retval true - success.
+ * @retval false - failure.
+ */
+ bool setOprToStationSoftAP(uint8_t pattern1=DEFAULT_PATTERN,uint8_t pattern2=DEFAULT_PATTERN);
+
+ /**
+ * Get the operation mode.
+ * @param pattern1 -1, send "AT+CWMODE_DEF?",-2,send "AT+CWMODE_CUR?",-3,send "AT+CWMODE?".
+ *
+ * @retval 0 - failure.
+ * @retval 1 - mode Station.
+ * @retval 2 - mode AP.
+ * @retval 3 - mode AP + station.
+ */
+ uint8_t getOprMode(uint8_t pattern1=DEFAULT_PATTERN);
+
+ /**
+ * Search available AP list and return it.
+ *
+ * @return the list of available APs.
+ * @note This method will occupy a lot of memeory(hundreds of Bytes to a couple of KBytes).
+ * Do not call this method unless you must and ensure that your board has enough memery left.
+ */
+ String getAPList(void);
+
+ /**
+ * Search and returns the current connect AP.
+ *
+ * @param pattern -1, send "AT+CWJAP_DEF?",-2,send "AT+CWJAP_CUR?",-3,send "AT+CWJAP?".
+ * @return the ssid of AP connected now.
+ */
+ String getNowConecAp(uint8_t pattern=DEFAULT_PATTERN);
+
+ /**
+ * Join in AP.
+ *
+ * @param pattern -1 send "AT+CWJAP_DEF=" -2 send "AT+CWJAP_CUR=" -3 send "AT+CWJAP=".
+ * @param ssid - SSID of AP to join in.
+ * @param pwd - Password of AP to join in.
+ * @retval true - success.
+ * @retval false - failure.
+ * @note This method will take a couple of seconds.
+ */
+ bool joinAP(String ssid, String pwd,uint8_t pattern=DEFAULT_PATTERN);
+
+ /**
+ * Leave AP joined before.
+ *
+ * @retval true - success.
+ * @retval false - failure.
+ */
+ bool leaveAP(void);
+
+ /**
+ * Set SoftAP parameters.
+ *
+ * @param pattern -1 send "AT+CWSAP_DEF=" -2 send "AT+CWSAP_CUR=" -3 send "AT+CWSAP=".
+ * @param ssid - SSID of SoftAP.
+ * @param pwd - PASSWORD of SoftAP.
+ * @param chl - the channel (1 - 13, default: 7).
+ * @param ecn - the way of encrypstion (0 - OPEN, 1 - WEP,
+ * 2 - WPA_PSK, 3 - WPA2_PSK, 4 - WPA_WPA2_PSK, default: 4).
+ * @retval true - success.
+ * @retval false - failure.
+ * @note This method should not be called when station mode.
+ */
+ bool setSoftAPParam(String ssid, String pwd, uint8_t chl = 7, uint8_t ecn = 4,uint8_t pattern=DEFAULT_PATTERN);
+
+ /**
+ * get SoftAP parameters.
+ *
+ * @param pattern -1 send "AT+CWSAP_DEF?" -2 send "AT+CWSAP_CUR?" -3 send "AT+CWSAP?".
+ * @note This method should not be called when station mode.
+ */
+ String getSoftAPParam(uint8_t pattern=DEFAULT_PATTERN);
+
+ /**
+ * Get the IP list of devices connected to SoftAP.
+ *
+ * @return the list of IP.
+ * @note This method should not be called when station mode.
+ */
+ String getJoinedDeviceIP(void);
+
+ /**
+ * Get the current state of DHCP.
+ *
+ * @param pattern -1 send "AT+CWDHCP_DEF?" -2 send "AT+CWDHCP_CUR?" -3 send "AT+CWDHCP?".
+ * @return the state of DHCP.
+ *
+ */
+ String getDHCP(uint8_t pattern=DEFAULT_PATTERN);
+
+ /**
+ * Set the state of DHCP.
+ * @param pattern -1 send "AT+CWDHCP_DEF=" -2 send "AT+CWDHCP_CUR=" -3 send "AT+CWDHCP=".
+ * @param mode - set ap or set station or set ap + station.
+ * @param en - 0 disable DHCP - 1 enable DHCP.
+ * @retval true - success.
+ * @retval false - failure.
+ */
+ bool setDHCP(uint8_t mode, uint8_t en, uint8_t pattern=DEFAULT_PATTERN);
+
+ /**
+ * make boot automatically connected.
+ * @param en -1 enable -0 disable.
+ * @retval true - success.
+ * @retval false - failure.
+ */
+ bool setAutoConnect(uint8_t en);
+
+ /**
+ * Get the station's MAC address.
+ * @param pattern -1 send "AT+CIPSTAMAC_DEF?=" -2 send "AT+CIPSTAMAC_CUR?" -3 send "AT+CIPSTAMAC?".
+ * @return mac address.
+ * @note This method should not be called when ap mode.
+ */
+ String getStationMac(uint8_t pattern=DEFAULT_PATTERN);
+
+ /**
+ * Set the station's MAC address.
+ * @param pattern -1 send "AT+CIPSTAMAC_DEF=" -2 send "AT+CIPSTAMAC_CUR=" -3 send "AT+CIPSTAMAC=".
+ * @param mac - the mac address of station.
+ * @retval true - success.
+ * @retval false - failure.
+ */
+ bool setStationMac(String mac,uint8_t pattern=DEFAULT_PATTERN);
+
+ /**
+ * Get the station's IP.
+ * @param pattern -1 send "AT+CIPSTA_DEF?" -2 send "AT+CIPSTA_CUR?" -3 send "AT+CIPSTA?".
+ * @return the station's IP.
+ * @note This method should not be called when ap mode.
+ */
+ String getStationIp(uint8_t pattern=DEFAULT_PATTERN);
+
+ /**
+ * Set the station's IP.
+ * @param pattern -1 send "AT+CIPSTA_DEF=" -2 send "AT+CIPSTA_CUR=" -3 send "AT+CIPSTA=".
+ * @param ip - the ip of station.
+ * @param gateway -the gateway of station.
+ * @param netmask -the netmask of station.
+ * @retval true - success.
+ * @retval false - failure.
+ * @note This method should not be called when ap mode.
+ */
+ bool setStationIp(String ip,String gateway,String netmask,uint8_t pattern=DEFAULT_PATTERN);
+
+ /**
+ * Get the AP's IP.
+ * @param pattern -1 send "AT+CIPAP_DEF?" -2 send "AT+CIPAP_CUR?" -3 send "AT+CIPAP?".
+ * @return ap's ip.
+ * @note This method should not be called when station mode.
+ *
+ */
+ String getAPIp(uint8_t pattern=DEFAULT_PATTERN);
+
+ /**
+ * Set the AP IP.
+ * @param pattern -1 send "AT+CIPAP_DEF=" -2 send "AT+CIPAP_CUR=" -3 send "AT+CIPAP=".
+ * @param ip - the ip of AP.
+ * @retval true - success.
+ * @retval false - failure.
+ * @note This method should not be called when station mode.
+ */
+ bool setAPIp(String ip,uint8_t pattern=DEFAULT_PATTERN);
+
+ /**
+ * start smartconfig.
+ * @param type -1:ESP_TOUCH -2:AirKiss.
+ * @retval true - success.
+ * @retval false - failure.
+ */
+ bool startSmartConfig(uint8_t type);
+
+ /**
+ * stop smartconfig.
+ *
+ * @retval true - success.
+ * @retval false - failure.
+ */
+ bool stopSmartConfig(void);
+
+ /**
+ * Get the current status of connection(UDP and TCP).
+ *
+ * @return the status.
+ */
+ String getIPStatus(void);
+
+ /**
+ * Get the IP address of ESP8266.
+ *
+ * @return the IP list.
+ */
+ String getLocalIP(void);
+
+ /**
+ * Enable IP MUX(multiple connection mode).
+ *
+ * In multiple connection mode, a couple of TCP and UDP communication can be builded.
+ * They can be distinguished by the identifier of TCP or UDP named mux_id.
+ *
+ * @retval true - success.
+ * @retval false - failure.
+ */
+ bool enableMUX(void);
+
+ /**
+ * Disable IP MUX(single connection mode).
+ *
+ * In single connection mode, only one TCP or UDP communication can be builded.
+ *
+ * @retval true - success.
+ * @retval false - failure.
+ */
+ bool disableMUX(void);
+
+ /**
+ * Create TCP connection in single mode.
+ *
+ * @param addr - the IP or domain name of the target host.
+ * @param port - the port number of the target host.
+ * @retval true - success.
+ * @retval false - failure.
+ */
+ bool createTCP(String addr, uint32_t port);
+
+ /**
+ * Release TCP connection in single mode.
+ *
+ * @retval true - success.
+ * @retval false - failure.
+ */
+ bool releaseTCP(void);
+
+ /**
+ * Register UDP port number in single mode.
+ *
+ * @param addr - the IP or domain name of the target host.
+ * @param port - the port number of the target host.
+ * @retval true - success.
+ * @retval false - failure.
+ */
+ bool registerUDP(String addr, uint32_t port);
+
+ /**
+ * Unregister UDP port number in single mode.
+ *
+ * @retval true - success.
+ * @retval false - failure.
+ */
+ bool unregisterUDP(void);
+
+ /**
+ * Create TCP connection in multiple mode.
+ *
+ * @param mux_id - the identifier of this TCP(available value: 0 - 4).
+ * @param addr - the IP or domain name of the target host.
+ * @param port - the port number of the target host.
+ * @retval true - success.
+ * @retval false - failure.
+ */
+ bool createTCP(uint8_t mux_id, String addr, uint32_t port);
+
+ /**
+ * Release TCP connection in multiple mode.
+ *
+ * @param mux_id - the identifier of this TCP(available value: 0 - 4).
+ * @retval true - success.
+ * @retval false - failure.
+ */
+ bool releaseTCP(uint8_t mux_id);
+
+ /**
+ * Register UDP port number in multiple mode.
+ *
+ * @param mux_id - the identifier of this TCP(available value: 0 - 4).
+ * @param addr - the IP or domain name of the target host.
+ * @param port - the port number of the target host.
+ * @retval true - success.
+ * @retval false - failure.
+ */
+ bool registerUDP(uint8_t mux_id, String addr, uint32_t port);
+
+ /**
+ * Unregister UDP port number in multiple mode.
+ *
+ * @param mux_id - the identifier of this TCP(available value: 0 - 4).
+ * @retval true - success.
+ * @retval false - failure.
+ */
+ bool unregisterUDP(uint8_t mux_id);
+
+ /**
+ * Set the timeout of TCP Server.
+ *
+ * @param timeout - the duration for timeout by second(0 ~ 28800, default:180).
+ * @retval true - success.
+ * @retval false - failure.
+ */
+ bool setTCPServerTimeout(uint32_t timeout = 180);
+
+ /**
+ * Start TCP Server(Only in multiple mode).
+ *
+ * After started, user should call method: getIPStatus to know the status of TCP connections.
+ * The methods of receiving data can be called for user's any purpose. After communication,
+ * release the TCP connection is needed by calling method: releaseTCP with mux_id.
+ *
+ * @param port - the port number to listen(default: 333).
+ * @retval true - success.
+ * @retval false - failure.
+ *
+ * @see String getIPStatus(void);
+ * @see uint32_t recv(uint8_t *coming_mux_id, uint8_t *buffer, uint32_t len, uint32_t timeout);
+ * @see bool releaseTCP(uint8_t mux_id);
+ */
+ bool startTCPServer(uint32_t port = 333);
+
+ /**
+ * Stop TCP Server(Only in multiple mode).
+ *
+ * @retval true - success.
+ * @retval false - failure.
+ */
+ bool stopTCPServer(void);
+
+ /**
+ *Set the module transfer mode
+ *
+ * @retval true - success.
+ * @retval false - failure.
+ */
+ bool setCIPMODE(uint8_t mode);
+
+ /**
+ * Start Server(Only in multiple mode).
+ *
+ * @param port - the port number to listen(default: 333).
+ * @retval true - success.
+ * @retval false - failure.
+ *
+ * @see String getIPStatus(void);
+ * @see uint32_t recv(uint8_t *coming_mux_id, uint8_t *buffer, uint32_t len, uint32_t timeout);
+ */
+ bool startServer(uint32_t port = 333);
+
+ /**
+ * Stop Server(Only in multiple mode).
+ *
+ * @retval true - success.
+ * @retval false - failure.
+ */
+ bool stopServer(void);
+ /**
+ * Save the passthrough links
+ *
+ * @retval true - success.
+ * @retval false - failure.
+ */
+ bool saveTransLink (uint8_t mode,String ip,uint32_t port);
+
+ /**
+ * PING COMMAND.
+ *
+ * @retval true - success.
+ * @retval false - failure.
+ */
+ bool setPing(String ip);
+
+ /**
+ * Send data based on TCP or UDP builded already in single mode.
+ *
+ * @param buffer - the buffer of data to send.
+ * @param len - the length of data to send.
+ * @retval true - success.
+ * @retval false - failure.
+ */
+ bool send(const uint8_t *buffer, uint32_t len);
+
+ /**
+ * Send data based on one of TCP or UDP builded already in multiple mode.
+ *
+ * @param mux_id - the identifier of this TCP(available value: 0 - 4).
+ * @param buffer - the buffer of data to send.
+ * @param len - the length of data to send.
+ * @retval true - success.
+ * @retval false - failure.
+ */
+ bool send(uint8_t mux_id, const uint8_t *buffer, uint32_t len);
+
+ /**
+ * Send data based on TCP or UDP builded already in single mode.
+ *
+ * @param buffer - the buffer of data to send from flash memeory.
+ * @param len - the length of data to send.
+ * @retval true - success.
+ * @retval false - failure.
+ */
+ bool sendFromFlash(const uint8_t *buffer, uint32_t len);
+
+ /**
+ * Send data based on one of TCP or UDP builded already in multiple mode.
+ *
+ * @param mux_id - the identifier of this TCP(available value: 0 - 4).
+ * @param buffer - the buffer of data to send from flash memeory.
+ * @param len - the length of data to send.
+ * @retval true - success.
+ * @retval false - failure.
+ */
+ bool sendFromFlash(uint8_t mux_id, const uint8_t *buffer, uint32_t len);
+
+ private:
+
+ /*
+ * Empty the buffer or UART RX.
+ */
+ void rx_empty(void);
+
+ /*
+ * Recvive data from uart. Return all received data if target found or timeout.
+ */
+ String recvString(String target, uint32_t timeout = 1000);
+
+ /*
+ * Recvive data from uart. Return all received data if one of target1 and target2 found or timeout.
+ */
+ String recvString(String target1, String target2, uint32_t timeout = 1000);
+
+ /*
+ * Recvive data from uart. Return all received data if one of target1, target2 and target3 found or timeout.
+ */
+ String recvString(String target1, String target2, String target3, uint32_t timeout = 1000);
+
+ /*
+ * Recvive data from uart and search first target. Return true if target found, false for timeout.
+ */
+ bool recvFind(String target, uint32_t timeout = 1000);
+
+ /*
+ * Recvive data from uart and search first target and cut out the substring between begin and end(excluding begin and end self).
+ * Return true if target found, false for timeout.
+ */
+ bool recvFindAndFilter(String target, String begin, String end, String &data, uint32_t timeout = 1000);
+
+ /*
+ * Receive a package from uart.
+ *
+ * @param buffer - the buffer storing data.
+ * @param buffer_size - guess what!
+ * @param data_len - the length of data actually received(maybe more than buffer_size, the remained data will be abandoned).
+ * @param timeout - the duration waitting data comming.
+ * @param coming_mux_id - in single connection mode, should be NULL and not NULL in multiple.
+ */
+ uint32_t checkIPD(String& data);
+
+
+ bool eAT(void);
+ bool eATRST(void);
+ bool eATGMR(String &version);
+ bool eATGSLP(uint32_t time);
+ bool eATE(uint8_t mode);
+ bool eATRESTORE(void);
+ bool eATSETUART(uint32_t baudrate,uint8_t pattern);
+
+ bool qATCWMODE(uint8_t *mode,uint8_t pattern=3);
+ bool eATCWMODE(String &list) ;
+ bool sATCWMODE(uint8_t mode,uint8_t pattern=3);
+ bool qATCWJAP(String &ssid,uint8_t pattern=3) ;
+ bool sATCWJAP(String ssid, String pwd,uint8_t pattern=3);
+ bool eATCWLAP(String &list);
+ bool eATCWQAP(void);
+ bool qATCWSAP(String &List,uint8_t pattern=3);
+ bool sATCWSAP(String ssid, String pwd, uint8_t chl, uint8_t ecn,uint8_t pattern=3);
+ bool eATCWLIF(String &list);
+ bool qATCWDHCP(String &List,uint8_t pattern=3);
+ bool sATCWDHCP(uint8_t mode, uint8_t en, uint8_t pattern=3);
+ bool eATCWAUTOCONN(uint8_t en);
+ bool qATCIPSTAMAC(String &mac,uint8_t pattern=3);
+ bool eATCIPSTAMAC(String mac,uint8_t pattern=3);
+ bool qATCIPSTAIP(String &ip,uint8_t pattern=3);
+ bool eATCIPSTAIP(String ip,String gateway,String netmask,uint8_t pattern=3);
+ bool qATCIPAP(String &ip,uint8_t pattern=3);
+ bool eATCIPAP(String ip,uint8_t pattern=3);
+ bool eCWSTARTSMART(uint8_t type);
+ bool eCWSTOPSMART(void);
+
+
+ bool eATCIPSTATUS(String &list);
+ bool sATCIPSTARTSingle(String type, String addr, uint32_t port);
+ bool sATCIPSTARTMultiple(uint8_t mux_id, String type, String addr, uint32_t port);
+ bool sATCIPSENDSingle(const uint8_t *buffer, uint32_t len);
+ bool sATCIPSENDMultiple(uint8_t mux_id, const uint8_t *buffer, uint32_t len);
+ bool sATCIPSENDSingleFromFlash(const uint8_t *buffer, uint32_t len);
+ bool sATCIPSENDMultipleFromFlash(uint8_t mux_id, const uint8_t *buffer, uint32_t len);
+ bool sATCIPCLOSEMulitple(uint8_t mux_id);
+ bool eATCIPCLOSESingle(void);
+ bool eATCIFSR(String &list);
+ bool sATCIPMUX(uint8_t mode);
+ bool sATCIPSERVER(uint8_t mode, uint32_t port = 333);
+ bool sATCIPMODE(uint8_t mode);
+ bool eATSAVETRANSLINK(uint8_t mode,String ip,uint32_t port);
+ bool eATPING(String ip);
+ bool sATCIPSTO(uint32_t timeout);
+
+ /*
+ * +IPD,len:data
+ * +IPD,id,len:data
+ */
+
+#ifdef ESP8266_USE_SOFTWARE_SERIAL
+ SoftwareSerial *m_puart; /* The UART to communicate with ESP8266 */
+#else
+ HardwareSerial *m_puart; /* The UART to communicate with ESP8266 */
+#endif
+ onData m_onData;
+ void* m_onDataPtr;
+};
+
+#endif /* #ifndef __ESP8266_H__ */
+
+
--- a/I2Cdev.cpp Wed Jun 15 03:08:40 2016 +0000
+++ b/I2Cdev.cpp Thu Jun 16 08:08:30 2016 +0000
@@ -1,3 +1,4 @@
+#if 0
#include "I2Cdev.h"
extern DigitalOut myled;
@@ -335,3 +336,4 @@
int status = g_i2c.write(devAddr << 1, (char*)buffer, realLen, false);
return status == 0;
}
+#endif
\ No newline at end of file
--- a/I2Cdev.h Wed Jun 15 03:08:40 2016 +0000 +++ b/I2Cdev.h Thu Jun 16 08:08:30 2016 +0000 @@ -1,5 +1,6 @@ #ifndef _I2CDEV_H_ #define _I2CDEV_H_ +#if 0 #include "mbed.h" #define I2CDEV_DEFAULT_READ_TIMEOUT 1000 @@ -31,3 +32,4 @@ }; #endif /* _I2CDEV_H_ */ +#endif \ No newline at end of file
--- a/WiFiBlynk.h Wed Jun 15 03:08:40 2016 +0000
+++ b/WiFiBlynk.h Thu Jun 16 08:08:30 2016 +0000
@@ -1,11 +1,10 @@
-#if 0
-#include <ESP8266_HardSer.h>
-#include <BlynkSimpleShieldEsp8266_HardSer.h>
-#include <SimpleTimer.h>
-// Set ESP8266 Serial object
-#define EspSerial Serial1
+#include "Config.h"
+#include "ESP8266_HardSer.h"
+#include "BlynkSimpleShieldEsp8266_HardSer.h"
+#include "SimpleTimer.h"
+Serial EspSerial(D3, D2);//tx, rx
ESP8266 wifi(EspSerial);
void senTempHumi()
@@ -28,5 +27,4 @@
{
Blynk.virtualWrite(V6, sensorPM25);
//BLYNK_PRINT.println(sensorPM25);
-}
-#endif
\ No newline at end of file
+}
\ No newline at end of file
--- a/main.cpp Wed Jun 15 03:08:40 2016 +0000
+++ b/main.cpp Thu Jun 16 08:08:30 2016 +0000
@@ -1,30 +1,37 @@
#include "mbed.h"
+#include "Config.h"
#include "MicroduinoPinNames.h"
DigitalOut myled(P0_20);
I2C g_i2c(P0_11, P0_10);//SDA, SCL
-#if 1
#include "SimpleTimer.h"
#include "userDef.h"
#include "sensor.h"
+#ifdef OPEN_OLED
#include "oled.h"
#endif
-//#include "WiFiBlynk.h"
+#include "WiFiBlynk.h"
-//Serial pc(P0_4, P0_0); // tx, rx
-
+Serial pc(P0_4, P0_0); // tx, rx
Timer g_Timer;
+Ticker g_Ticker;
void led_flash()
{
static int count = 0;
count++;
//pc.printf("count : %d, ms : %d\r\n", count, g_Timer.read_ms());
- //myled = 1;
+ myled = 1;
wait_ms(70);
myled = 0;
}
+static void led_flash_fast()
+{
+ myled = !myled;
+}
+
+#ifdef OPEN_OLED
Adafruit_SSD1306_I2c adaf(g_i2c, P0_13, 0x78, 64, 128);
void update_oled()
{
@@ -33,46 +40,56 @@
myled = 0;
oled(adaf, sensor_tem, sensor_hum, sensor_light, sensorPM25, Sensor_etoh);
}
+#endif
int main()
{
- //pc.baud(9600);
+ g_Ticker.attach_us(led_flash_fast, 30000);
+ pc.baud(115200);
+ pc.printf("Enter main()\r\n");
//myled = 1;
g_Timer.start();
SimpleTimer gSimpleTimer(g_Timer);
-
- //Serial.begin(9600); // See the connection status in Serial Monitor
- // Set ESP8266 baud rate
- //EspSerial.begin(115200);
+
+ /*
+ * Set ESP8266 baud rate
+ * 在LPC824上,波特率设为115200时,ESP8266是无法工作
+ */
+ EspSerial.baud(9600);
- //Blynk.begin(auth, wifi, SSID, PASS);
-
+ Blynk.begin(auth, wifi, SSID, PASS);
+ wait(5.0);
+ g_Ticker.detach();
+
// Setup a function to be called every second
- //gSimpleTimer.setInterval(2000L, senTempHumi);
+ gSimpleTimer.setInterval(2000L, senTempHumi);
gSimpleTimer.setInterval(1000, updateLight);
gSimpleTimer.setInterval(5000, updateCH4);
gSimpleTimer.setInterval(4000, updateTempHumi);
//gSimpleTimer.setInterval(3000, PM25);
- //gSimpleTimer.setInterval(4000, led_flash);
+ gSimpleTimer.setInterval(4000, led_flash);
+#ifdef OPEN_OLED
gSimpleTimer.setInterval(1000, update_oled);
-
+#endif
//PM25_init();
wait_ms(2000);
+#ifdef OPEN_OLED
oled_init(adaf);
+#endif
int pretime = g_Timer.read_ms();
while(1) {
//pc.printf("Enter while(1)\r\n");
//myled = !myled;
- //Blynk.run(); // All the Blynk Magic happens here...
+ Blynk.run(); // All the Blynk Magic happens here...
gSimpleTimer.run();
- #if 0
+#if 0
int curtime = g_Timer.read_ms();
if (curtime - pretime > 4000) {
//updateTempHumi();
led_flash();
pretime = curtime;
}
- #endif
-
+#endif
+
}
}
--- a/sensor.h Wed Jun 15 03:08:40 2016 +0000
+++ b/sensor.h Thu Jun 16 08:08:30 2016 +0000
@@ -1,25 +1,28 @@
-//#include <SoftwareSerial.h>
#include "MicroduinoPinNames.h"
+#include "Config.h"
#include "AM2321.h"
extern Serial pc;
#define INTERVAL_pm25 200
//SoftwareSerial pmSerial(4,5); //PM2.5传感器通讯软串口
+#ifdef OPEN_PM25
Serial pmSerial(D5, D4); //tx,rx
+#endif
AnalogIn gLight(A0);
AnalogIn gCH4(A2);
AM2321 am2321;
float sensor_tem,sensor_hum,sensor_light,Sensor_etoh;
-float sensorPM25;
+float sensorPM25 = 6.6;
long map(long x, long in_min, long in_max, long out_min, long out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
+#ifdef OPEN_PM25
void PM25_init(void)
{
//pc.printf("Enter PM25_init\r\n");
@@ -71,12 +74,13 @@
sensorPM25 = dustDensity;
//pc.printf("sensorPM25 = %f\r\n", sensorPM25);
}
+#endif
// 读取光照传感器
void updateLight()
{
//sensor_light = map(analogRead(A0), 0, 1023, 0, 255);
- float lvf = gLight;
+ //float lvf = gLight;
uint16_t lt = gLight.read_u16();
//pc.printf("light = %d, lvf = %f\r\n", lt, lvf);
sensor_light = map(lt, 0, 65535, 0, 255);// 这里和Arduino不一样,不知道为什么
@@ -85,6 +89,7 @@
// 读取甲醛传感器
void updateCH4()
{
+ sensorPM25 += 1.0;
//Sensor_etoh = map(analogRead(A2), 0, 1023, 0, 30);
uint16_t ch4 = gCH4.read_u16();
Sensor_etoh = map(ch4, 0, 65535, 0, 30);
--- a/userDef.h Wed Jun 15 03:08:40 2016 +0000 +++ b/userDef.h Thu Jun 16 08:08:30 2016 +0000 @@ -1,9 +1,9 @@ -#define SSID "type your ssid" -#define PASS "type your pass" - -char auth[] = "type your blynk token"; - - - - +#define SSID "TaiYangGong2.4G" +#define PASS "6hy5BIktT" + +char auth[] = "75709bec06a94d0088fd9382bc825070"; + + + +