Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added MDNS functions #95

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 79 additions & 27 deletions ESP8266.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/**
* @file ESP8266.cpp
* @brief The implementation of class ESP8266.
* @author Wu Pengfei<[email protected]>
* @brief The implementation of class ESP8266.
* @author Wu Pengfei<[email protected]>
* @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
Expand Down Expand Up @@ -147,6 +147,10 @@ bool ESP8266::joinAP(String ssid, String pwd)
return sATCWJAP(ssid, pwd);
}

bool ESP8266::setStaticIp(String ip, String gateway, String mask){
return sATCIPSTACUR(ip, gateway, mask);
}

bool ESP8266::enableClientDHCP(uint8_t mode, boolean enabled)
{
return sATCWDHCP(mode, enabled);
Expand Down Expand Up @@ -263,6 +267,16 @@ bool ESP8266::stopServer(void)
return stopTCPServer();
}

bool ESP8266::enableMDNS(String hostname, String servername, uint32_t port)
{
return sATCMDNS(true, hostname, servername, port);
}

bool ESP8266::disableMDNS(void)
{
return sATCMDNS(false, "", "", 0);
}

bool ESP8266::send(const uint8_t *buffer, uint32_t len)
{
return sATCIPSENDSingle(buffer, len);
Expand Down Expand Up @@ -311,25 +325,25 @@ uint32_t ESP8266::recvPkg(uint8_t *buffer, uint32_t buffer_size, uint32_t *data_
uint32_t ret;
unsigned long start;
uint32_t i;

if (buffer == NULL) {
return 0;
}

start = millis();
while (millis() - start < timeout) {
if(m_puart->available() > 0) {
a = m_puart->read();
data += a;
}

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) {
if (index_comma != -1 && index_comma < index_colon) {
id = data.substring(index_PIPDcomma + 5, index_comma).toInt();
if (id < 0 || id > 4) {
return 0;
Expand All @@ -349,7 +363,7 @@ uint32_t ESP8266::recvPkg(uint8_t *buffer, uint32_t buffer_size, uint32_t *data_
}
}
}

if (has_data) {
i = 0;
ret = len > buffer_size ? buffer_size : len;
Expand All @@ -362,7 +376,7 @@ uint32_t ESP8266::recvPkg(uint8_t *buffer, uint32_t buffer_size, uint32_t *data_
if (i == ret) {
rx_empty();
if (data_len) {
*data_len = len;
*data_len = len;
}
if (index_comma != -1 && coming_mux_id) {
*coming_mux_id = id;
Expand All @@ -374,7 +388,7 @@ uint32_t ESP8266::recvPkg(uint8_t *buffer, uint32_t buffer_size, uint32_t *data_
return 0;
}

void ESP8266::rx_empty(void)
void ESP8266::rx_empty(void)
{
while(m_puart->available() > 0) {
m_puart->read();
Expand All @@ -394,7 +408,7 @@ String ESP8266::recvString(String target, uint32_t timeout)
}
if (data.indexOf(target) != -1) {
break;
}
}
}
return data;
}
Expand Down Expand Up @@ -475,7 +489,7 @@ bool ESP8266::eAT(void)
return recvFind("OK");
}

bool ESP8266::eATRST(void)
bool ESP8266::eATRST(void)
{
rx_empty();
m_puart->println("AT+RST");
Expand All @@ -486,10 +500,10 @@ bool ESP8266::eATGMR(String &version)
{
rx_empty();
m_puart->println("AT+GMR");
return recvFindAndFilter("OK", "\r\r\n", "\r\n\r\nOK", version);
return recvFindAndFilter("OK", "\r\r\n", "\r\n\r\nOK", version);
}

bool ESP8266::qATCWMODE(uint8_t *mode)
bool ESP8266::qATCWMODE(uint8_t *mode)
{
String str_mode;
bool ret;
Expand All @@ -498,7 +512,7 @@ bool ESP8266::qATCWMODE(uint8_t *mode)
}
rx_empty();
m_puart->println("AT+CWMODE?");
ret = recvFindAndFilter("OK", "+CWMODE:", "\r\n\r\nOK", str_mode);
ret = recvFindAndFilter("OK", "+CWMODE:", "\r\n\r\nOK", str_mode);
if (ret) {
*mode = (uint8_t)str_mode.toInt();
return true;
Expand All @@ -513,7 +527,7 @@ bool ESP8266::sATCWMODE(uint8_t mode)
rx_empty();
m_puart->print("AT+CWMODE=");
m_puart->println(mode);

data = recvString("OK", "no change");
if (data.indexOf("OK") != -1 || data.indexOf("no change") != -1) {
return true;
Expand All @@ -530,7 +544,25 @@ bool ESP8266::sATCWJAP(String ssid, String pwd)
m_puart->print("\",\"");
m_puart->print(pwd);
m_puart->println("\"");


data = recvString("OK", "FAIL", 10000);
if (data.indexOf("OK") != -1) {
return true;
}
return false;
}

bool ESP8266::sATCIPSTACUR(String ip, String gateway, String mask)
{
String data;
rx_empty();
m_puart->print("AT+CIPSTA_CUR=\"");
m_puart->print(ip);
m_puart->print("\",\"");
m_puart->print(gateway);
m_puart->print("\",\"");
m_puart->print(mask);
m_puart->println("\"");
data = recvString("OK", "FAIL", 10000);
if (data.indexOf("OK") != -1) {
return true;
Expand All @@ -544,15 +576,15 @@ bool ESP8266::sATCWDHCP(uint8_t mode, boolean enabled)
if (enabled) {
strEn = "1";
}


String data;
rx_empty();
m_puart->print("AT+CWDHCP=");
m_puart->print(strEn);
m_puart->print(",");
m_puart->println(mode);

data = recvString("OK", "FAIL", 10000);
if (data.indexOf("OK") != -1) {
return true;
Expand Down Expand Up @@ -588,7 +620,7 @@ bool ESP8266::sATCWSAP(String ssid, String pwd, uint8_t chl, uint8_t ecn)
m_puart->print(chl);
m_puart->print(",");
m_puart->println(ecn);

data = recvString("OK", "ERROR", 5000);
if (data.indexOf("OK") != -1) {
return true;
Expand Down Expand Up @@ -621,7 +653,7 @@ bool ESP8266::sATCIPSTARTSingle(String type, String addr, uint32_t port)
m_puart->print(addr);
m_puart->print("\",");
m_puart->println(port);

data = recvString("OK", "ERROR", "ALREADY CONNECT", 10000);
if (data.indexOf("OK") != -1 || data.indexOf("ALREADY CONNECT") != -1) {
return true;
Expand All @@ -640,7 +672,7 @@ bool ESP8266::sATCIPSTARTMultiple(uint8_t mux_id, String type, String addr, uint
m_puart->print(addr);
m_puart->print("\",");
m_puart->println(port);

data = recvString("OK", "ERROR", "ALREADY CONNECT", 10000);
if (data.indexOf("OK") != -1 || data.indexOf("ALREADY CONNECT") != -1) {
return true;
Expand Down Expand Up @@ -683,7 +715,7 @@ bool ESP8266::sATCIPCLOSEMulitple(uint8_t mux_id)
rx_empty();
m_puart->print("AT+CIPCLOSE=");
m_puart->println(mux_id);

data = recvString("OK", "link is not", 5000);
if (data.indexOf("OK") != -1 || data.indexOf("link is not") != -1) {
return true;
Expand All @@ -708,7 +740,7 @@ bool ESP8266::sATCIPMUX(uint8_t mode)
rx_empty();
m_puart->print("AT+CIPMUX=");
m_puart->println(mode);

data = recvString("OK", "Link is builded");
if (data.indexOf("OK") != -1) {
return true;
Expand All @@ -722,7 +754,7 @@ bool ESP8266::sATCIPSERVER(uint8_t mode, uint32_t port)
rx_empty();
m_puart->print("AT+CIPSERVER=1,");
m_puart->println(port);

data = recvString("OK", "no change");
if (data.indexOf("OK") != -1 || data.indexOf("no change") != -1) {
return true;
Expand All @@ -741,4 +773,24 @@ bool ESP8266::sATCIPSTO(uint32_t timeout)
m_puart->println(timeout);
return recvFind("OK");
}

bool ESP8266::sATCMDNS(bool enable, String hostname, String servername, uint32_t port)
{
String data;
if (enable) {
rx_empty();
m_puart->print("AT+MDNS=1,\"");
m_puart->print(hostname);
m_puart->print("\",\"");
m_puart->print(servername);
m_puart->print("\",");
m_puart->println(port);
} else {
rx_empty();
m_puart->println("AT+MDNS=0");
}
data = recvString("OK", "ERROR");
if (data.indexOf("OK") != -1 || data.indexOf("no change") != -1) {
return true;
}
return false;
}
Loading