Skip to content

Commit b24b6ee

Browse files
authored
Merge pull request #21 from sarahmarshy/firmware-v2.0
Driver compatible with 2.0 Espressif firmware
2 parents 4ed87bf + 26581eb commit b24b6ee

File tree

4 files changed

+53
-4
lines changed

4 files changed

+53
-4
lines changed

ESP8266/ESP8266.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ bool ESP8266::startup(int mode)
3232
}
3333

3434
bool success = reset()
35-
&& _parser.send("AT+CWMODE=%d", mode)
35+
&& _parser.send("AT+CWMODE_CUR=%d", mode)
3636
&& _parser.recv("OK")
3737
&& _parser.send("AT+CIPMUX=1")
3838
&& _parser.recv("OK");
@@ -174,16 +174,20 @@ bool ESP8266::open(const char *type, int id, const char* addr, int port)
174174
if(id > 4) {
175175
return false;
176176
}
177-
178177
return _parser.send("AT+CIPSTART=%d,\"%s\",\"%s\",%d", id, type, addr, port)
179178
&& _parser.recv("OK");
180179
}
181180

181+
bool ESP8266::dns_lookup(const char* name, char* ip)
182+
{
183+
return _parser.send("AT+CIPDOMAIN=\"%s\"", name) && _parser.recv("+CIPDOMAIN:%s%*[\r]%*[\n]", ip);
184+
}
185+
182186
bool ESP8266::send(int id, const void *data, uint32_t amount)
183187
{
184188
//May take a second try if device is busy
185189
for (unsigned i = 0; i < 2; i++) {
186-
if (_parser.send("AT+CIPSEND=%d,%d", id, amount)
190+
if (_parser.send("AT+CIPSENDBUF=%d,%d", id, amount)
187191
&& _parser.recv(">")
188192
&& _parser.write((char*)data, (int)amount) >= 0) {
189193
return true;

ESP8266/ESP8266.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,14 @@ class ESP8266
116116
* see @a nsapi_error
117117
*/
118118
int scan(WiFiAccessPoint *res, unsigned limit);
119+
120+
/**Perform a dns query
121+
*
122+
* @param name Hostname to resolve
123+
* @param ip Buffer to store IP address
124+
* @return 0 true on success, false on failure
125+
*/
126+
bool dns_lookup(const char *name, char *ip);
119127

120128
/**
121129
* Open a socketed connection

ESP8266Interface.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,29 @@ int ESP8266Interface::connect()
6767
return NSAPI_ERROR_OK;
6868
}
6969

70+
nsapi_error_t ESP8266Interface::gethostbyname(const char* name, SocketAddress *address, nsapi_version_t version)
71+
{
72+
if (address->set_ip_address(name)) {
73+
if (version != NSAPI_UNSPEC && address->get_ip_version() != version) {
74+
return NSAPI_ERROR_DNS_FAILURE;
75+
}
76+
77+
return NSAPI_ERROR_OK;
78+
}
79+
80+
char* ipbuff = new char[NSAPI_IP_SIZE];
81+
int ret = 0;
82+
83+
if(!_esp.dns_lookup(name, ipbuff)) {
84+
ret = NSAPI_ERROR_DEVICE_ERROR;
85+
}
86+
else {
87+
address->set_ip_address(ipbuff);
88+
}
89+
free(ipbuff);
90+
return ret;
91+
}
92+
7093
int ESP8266Interface::set_credentials(const char *ssid, const char *pass, nsapi_security_t security)
7194
{
7295
memset(ap_ssid, 0, sizeof(ap_ssid));

ESP8266Interface.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,21 @@ class ESP8266Interface : public NetworkStack, public WiFiInterface
5757
*/
5858
virtual int connect(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE,
5959
uint8_t channel = 0);
60-
60+
61+
/** Translates a hostname to an IP address with specific version
62+
*
63+
* The hostname may be either a domain name or an IP address. If the
64+
* hostname is an IP address, no network transactions will be performed.
65+
*
66+
*
67+
* @param host Hostname to resolve
68+
* @param address Destination for the host SocketAddress
69+
* @param version IP version of address to resolve, NSAPI_UNSPEC indicates
70+
* version is chosen by the stack (defaults to NSAPI_UNSPEC)
71+
* @return 0 on success, negative error code on failure
72+
*/
73+
virtual nsapi_error_t gethostbyname(const char* name, SocketAddress *address, nsapi_version_t version);
74+
6175
/** Set the WiFi network credentials
6276
*
6377
* @param ssid Name of the network to connect to

0 commit comments

Comments
 (0)