Skip to content

Commit 7e91ca5

Browse files
ESP8266: static address and dhcp added
1 parent 5a07aab commit 7e91ca5

File tree

4 files changed

+67
-3
lines changed

4 files changed

+67
-3
lines changed

components/wifi/esp8266-driver/ESP8266/ESP8266.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,32 @@ const char *ESP8266::ip_addr(void)
401401
return _ip_buffer;
402402
}
403403

404+
const bool ESP8266::set_ip_addr(const char* ip, const char* gateway, const char* netmask)
405+
{
406+
if (ip == nullptr || ip[0] == '\0') {
407+
return false;
408+
}
409+
410+
bool ok = false;
411+
bool parser_send = false;
412+
413+
_smutex.lock();
414+
415+
if ((gateway == nullptr) || (netmask == nullptr) || gateway[0] == '\0' || netmask[0] == '\0') {
416+
parser_send = _parser.send("AT+CIPSTA_CUR=\"%s\"", ip);
417+
} else {
418+
parser_send = _parser.send("AT+CIPSTA_CUR=\"%s\",\"%s\",\"%s\"", ip, gateway, netmask);
419+
}
420+
421+
if (parser_send && _parser.recv("OK\n")) {
422+
ok = true;
423+
} else {
424+
ok = false;
425+
}
426+
_smutex.unlock();
427+
return ok;
428+
}
429+
404430
const char *ESP8266::mac_addr(void)
405431
{
406432
_smutex.lock();

components/wifi/esp8266-driver/ESP8266/ESP8266.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,17 @@ class ESP8266 {
194194
*/
195195
const char *ip_addr(void);
196196

197+
/**
198+
* Set static IP address, gateway and netmask
199+
*
200+
* @param ip IP address to set
201+
* @param gateway (optional) gateway to set
202+
* @param netmask (optional) netmask to set
203+
*
204+
* @return true if operation was successful and flase otherwise
205+
*/
206+
const bool set_ip_addr(const char* ip, const char* gateway, const char* netmask);
207+
197208
/**
198209
* Get the MAC address of ESP8266
199210
*

components/wifi/esp8266-driver/ESP8266Interface.cpp

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@ ESP8266Interface::ESP8266Interface(PinName tx, PinName rx, bool debug, PinName r
121121
_oob_event_id(0),
122122
_connect_event_id(0),
123123
_disconnect_event_id(0),
124-
_software_conn_stat(IFACE_STATUS_DISCONNECTED)
124+
_software_conn_stat(IFACE_STATUS_DISCONNECTED),
125+
_dhcp(true)
125126
{
126127
memset(_cbs, 0, sizeof(_cbs));
127128
memset(ap_ssid, 0, sizeof(ap_ssid));
@@ -246,7 +247,7 @@ void ESP8266Interface::_connect_async()
246247
return;
247248
}
248249

249-
if (!_esp.dhcp(true, 1)) {
250+
if (_dhcp && !_esp.dhcp(true, 1)) {
250251
_connect_retval = NSAPI_ERROR_DHCP_FAILURE;
251252
_esp.uart_enable_input(false);
252253
_software_conn_stat = IFACE_STATUS_DISCONNECTED;
@@ -406,6 +407,25 @@ int ESP8266Interface::set_channel(uint8_t channel)
406407
return NSAPI_ERROR_UNSUPPORTED;
407408
}
408409

410+
nsapi_error_t ESP8266Interface::set_network(const SocketAddress &ip_address, const SocketAddress &netmask, const SocketAddress &gateway)
411+
{
412+
// netmask and gateway switched on purpose. ESP takes different argument order.
413+
if (_esp.set_ip_addr(ip_address.get_ip_address(), gateway.get_ip_address(), netmask.get_ip_address())) {
414+
_dhcp = false;
415+
return NSAPI_ERROR_OK;
416+
} else {
417+
return NSAPI_ERROR_DEVICE_ERROR;
418+
}
419+
}
420+
421+
nsapi_error_t ESP8266Interface::set_dhcp(bool dhcp) {
422+
_dhcp = dhcp;
423+
if (_esp.dhcp(dhcp, 1)) {
424+
return NSAPI_ERROR_OK;
425+
} else {
426+
return NSAPI_ERROR_DEVICE_ERROR;
427+
}
428+
}
409429

410430
void ESP8266Interface::_disconnect_async()
411431
{

components/wifi/esp8266-driver/ESP8266Interface.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,12 @@ class ESP8266Interface : public NetworkStack, public WiFiInterface {
132132
*/
133133
virtual int set_channel(uint8_t channel);
134134

135+
/** @copydoc NetworkInterface::set_network */
136+
virtual nsapi_error_t set_network(const SocketAddress &ip_address, const SocketAddress &netmask, const SocketAddress &gateway);
137+
138+
/** @copydoc NetworkInterface::dhcp */
139+
virtual nsapi_error_t set_dhcp(bool dhcp);
140+
135141
/** Stop the interface
136142
* @return 0 on success, negative on failure
137143
*/
@@ -518,7 +524,8 @@ class ESP8266Interface : public NetworkStack, public WiFiInterface {
518524
void _connect_async();
519525
void _disconnect_async();
520526
rtos::Mutex _cmutex; // Protect asynchronous connection logic
521-
esp_connection_software_status_t _software_conn_stat ;
527+
esp_connection_software_status_t _software_conn_stat;
528+
bool _dhcp;
522529

523530
};
524531
#endif

0 commit comments

Comments
 (0)