Skip to content

Commit 8e789b0

Browse files
authored
Merge pull request #12721 from michalpasztamobica/esp8266_set_network
ESP8266: static address configuration and dhcp enable/disable added
2 parents cb4449a + 195fdc1 commit 8e789b0

File tree

4 files changed

+78
-3
lines changed

4 files changed

+78
-3
lines changed

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,32 @@ const char *ESP8266::ip_addr(void)
412412
return _ip_buffer;
413413
}
414414

415+
const bool ESP8266::set_ip_addr(const char *ip, const char *gateway, const char *netmask)
416+
{
417+
if (ip == nullptr || ip[0] == '\0') {
418+
return false;
419+
}
420+
421+
bool ok = false;
422+
bool parser_send = false;
423+
424+
_smutex.lock();
425+
426+
if ((gateway == nullptr) || (netmask == nullptr) || gateway[0] == '\0' || netmask[0] == '\0') {
427+
parser_send = _parser.send("AT+CIPSTA_CUR=\"%s\"", ip);
428+
} else {
429+
parser_send = _parser.send("AT+CIPSTA_CUR=\"%s\",\"%s\",\"%s\"", ip, gateway, netmask);
430+
}
431+
432+
if (parser_send && _parser.recv("OK\n")) {
433+
ok = true;
434+
} else {
435+
ok = false;
436+
}
437+
_smutex.unlock();
438+
return ok;
439+
}
440+
415441
const char *ESP8266::mac_addr(void)
416442
{
417443
_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: 33 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,36 @@ 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+
nsapi_error_t init_result = _init();
413+
if (NSAPI_ERROR_OK != init_result) {
414+
return init_result;
415+
}
416+
417+
// netmask and gateway switched on purpose. ESP takes different argument order.
418+
if (_esp.set_ip_addr(ip_address.get_ip_address(), gateway.get_ip_address(), netmask.get_ip_address())) {
419+
_dhcp = false;
420+
return NSAPI_ERROR_OK;
421+
} else {
422+
return NSAPI_ERROR_DEVICE_ERROR;
423+
}
424+
}
425+
426+
nsapi_error_t ESP8266Interface::set_dhcp(bool dhcp)
427+
{
428+
nsapi_error_t init_result = _init();
429+
if (NSAPI_ERROR_OK != init_result) {
430+
return init_result;
431+
}
432+
433+
_dhcp = dhcp;
434+
if (_esp.dhcp(dhcp, 1)) {
435+
return NSAPI_ERROR_OK;
436+
} else {
437+
return NSAPI_ERROR_DEVICE_ERROR;
438+
}
439+
}
409440

410441
void ESP8266Interface::_disconnect_async()
411442
{

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)