Skip to content

Driver compatible with 2.0 Espressif firmware #21

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

Merged
merged 2 commits into from
Apr 10, 2017
Merged
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
10 changes: 7 additions & 3 deletions ESP8266/ESP8266.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ bool ESP8266::startup(int mode)
}

bool success = reset()
&& _parser.send("AT+CWMODE=%d", mode)
&& _parser.send("AT+CWMODE_CUR=%d", mode)
&& _parser.recv("OK")
&& _parser.send("AT+CIPMUX=1")
&& _parser.recv("OK");
Expand Down Expand Up @@ -174,16 +174,20 @@ bool ESP8266::open(const char *type, int id, const char* addr, int port)
if(id > 4) {
return false;
}

return _parser.send("AT+CIPSTART=%d,\"%s\",\"%s\",%d", id, type, addr, port)
&& _parser.recv("OK");
}

bool ESP8266::dns_lookup(const char* name, char* ip)
{
return _parser.send("AT+CIPDOMAIN=\"%s\"", name) && _parser.recv("+CIPDOMAIN:%s%*[\r]%*[\n]", ip);
}

bool ESP8266::send(int id, const void *data, uint32_t amount)
{
//May take a second try if device is busy
for (unsigned i = 0; i < 2; i++) {
if (_parser.send("AT+CIPSEND=%d,%d", id, amount)
if (_parser.send("AT+CIPSENDBUF=%d,%d", id, amount)
&& _parser.recv(">")
&& _parser.write((char*)data, (int)amount) >= 0) {
return true;
Expand Down
8 changes: 8 additions & 0 deletions ESP8266/ESP8266.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,14 @@ class ESP8266
* see @a nsapi_error
*/
int scan(WiFiAccessPoint *res, unsigned limit);

/**Perform a dns query
*
* @param name Hostname to resolve
* @param ip Buffer to store IP address
* @return 0 true on success, false on failure
*/
bool dns_lookup(const char *name, char *ip);

/**
* Open a socketed connection
Expand Down
23 changes: 23 additions & 0 deletions ESP8266Interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,29 @@ int ESP8266Interface::connect()
return NSAPI_ERROR_OK;
}

nsapi_error_t ESP8266Interface::gethostbyname(const char* name, SocketAddress *address, nsapi_version_t version)
{
if (address->set_ip_address(name)) {
if (version != NSAPI_UNSPEC && address->get_ip_version() != version) {
return NSAPI_ERROR_DNS_FAILURE;
}

return NSAPI_ERROR_OK;
}

char* ipbuff = new char[NSAPI_IP_SIZE];
int ret = 0;

if(!_esp.dns_lookup(name, ipbuff)) {
ret = NSAPI_ERROR_DEVICE_ERROR;
}
else {
address->set_ip_address(ipbuff);
}
free(ipbuff);
return ret;
}

int ESP8266Interface::set_credentials(const char *ssid, const char *pass, nsapi_security_t security)
{
memset(ap_ssid, 0, sizeof(ap_ssid));
Expand Down
16 changes: 15 additions & 1 deletion ESP8266Interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,21 @@ class ESP8266Interface : public NetworkStack, public WiFiInterface
*/
virtual int connect(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE,
uint8_t channel = 0);


/** Translates a hostname to an IP address with specific version
*
* The hostname may be either a domain name or an IP address. If the
* hostname is an IP address, no network transactions will be performed.
*
*
* @param host Hostname to resolve
* @param address Destination for the host SocketAddress
* @param version IP version of address to resolve, NSAPI_UNSPEC indicates
* version is chosen by the stack (defaults to NSAPI_UNSPEC)
* @return 0 on success, negative error code on failure
*/
virtual nsapi_error_t gethostbyname(const char* name, SocketAddress *address, nsapi_version_t version);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for conforming to the dox-style 👍


/** Set the WiFi network credentials
*
* @param ssid Name of the network to connect to
Expand Down