Skip to content

ESP8266: static address configuration and dhcp enable/disable added #12721

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
May 13, 2020
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
26 changes: 26 additions & 0 deletions components/wifi/esp8266-driver/ESP8266/ESP8266.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,32 @@ const char *ESP8266::ip_addr(void)
return _ip_buffer;
}

const bool ESP8266::set_ip_addr(const char *ip, const char *gateway, const char *netmask)
{
if (ip == nullptr || ip[0] == '\0') {
return false;
}

bool ok = false;
bool parser_send = false;

_smutex.lock();

if ((gateway == nullptr) || (netmask == nullptr) || gateway[0] == '\0' || netmask[0] == '\0') {
parser_send = _parser.send("AT+CIPSTA_CUR=\"%s\"", ip);
} else {
parser_send = _parser.send("AT+CIPSTA_CUR=\"%s\",\"%s\",\"%s\"", ip, gateway, netmask);
}

if (parser_send && _parser.recv("OK\n")) {
ok = true;
} else {
ok = false;
}
_smutex.unlock();
return ok;
}

const char *ESP8266::mac_addr(void)
{
_smutex.lock();
Expand Down
11 changes: 11 additions & 0 deletions components/wifi/esp8266-driver/ESP8266/ESP8266.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,17 @@ class ESP8266 {
*/
const char *ip_addr(void);

/**
* Set static IP address, gateway and netmask
*
* @param ip IP address to set
* @param gateway (optional) gateway to set
* @param netmask (optional) netmask to set
*
* @return true if operation was successful and flase otherwise
*/
const bool set_ip_addr(const char *ip, const char *gateway, const char *netmask);

/**
* Get the MAC address of ESP8266
*
Expand Down
35 changes: 33 additions & 2 deletions components/wifi/esp8266-driver/ESP8266Interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ ESP8266Interface::ESP8266Interface(PinName tx, PinName rx, bool debug, PinName r
_oob_event_id(0),
_connect_event_id(0),
_disconnect_event_id(0),
_software_conn_stat(IFACE_STATUS_DISCONNECTED)
_software_conn_stat(IFACE_STATUS_DISCONNECTED),
_dhcp(true)
{
memset(_cbs, 0, sizeof(_cbs));
memset(ap_ssid, 0, sizeof(ap_ssid));
Expand Down Expand Up @@ -246,7 +247,7 @@ void ESP8266Interface::_connect_async()
return;
}

if (!_esp.dhcp(true, 1)) {
if (_dhcp && !_esp.dhcp(true, 1)) {
_connect_retval = NSAPI_ERROR_DHCP_FAILURE;
_esp.uart_enable_input(false);
_software_conn_stat = IFACE_STATUS_DISCONNECTED;
Expand Down Expand Up @@ -406,6 +407,36 @@ int ESP8266Interface::set_channel(uint8_t channel)
return NSAPI_ERROR_UNSUPPORTED;
}

nsapi_error_t ESP8266Interface::set_network(const SocketAddress &ip_address, const SocketAddress &netmask, const SocketAddress &gateway)
{
nsapi_error_t init_result = _init();
if (NSAPI_ERROR_OK != init_result) {
return init_result;
}

// netmask and gateway switched on purpose. ESP takes different argument order.
if (_esp.set_ip_addr(ip_address.get_ip_address(), gateway.get_ip_address(), netmask.get_ip_address())) {
_dhcp = false;
return NSAPI_ERROR_OK;
} else {
return NSAPI_ERROR_DEVICE_ERROR;
}
}

nsapi_error_t ESP8266Interface::set_dhcp(bool dhcp)
{
nsapi_error_t init_result = _init();
if (NSAPI_ERROR_OK != init_result) {
return init_result;
}

_dhcp = dhcp;
if (_esp.dhcp(dhcp, 1)) {
return NSAPI_ERROR_OK;
} else {
return NSAPI_ERROR_DEVICE_ERROR;
}
}

void ESP8266Interface::_disconnect_async()
{
Expand Down
9 changes: 8 additions & 1 deletion components/wifi/esp8266-driver/ESP8266Interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@ class ESP8266Interface : public NetworkStack, public WiFiInterface {
*/
virtual int set_channel(uint8_t channel);

/** @copydoc NetworkInterface::set_network */
virtual nsapi_error_t set_network(const SocketAddress &ip_address, const SocketAddress &netmask, const SocketAddress &gateway);

/** @copydoc NetworkInterface::dhcp */
virtual nsapi_error_t set_dhcp(bool dhcp);

/** Stop the interface
* @return 0 on success, negative on failure
*/
Expand Down Expand Up @@ -518,7 +524,8 @@ class ESP8266Interface : public NetworkStack, public WiFiInterface {
void _connect_async();
void _disconnect_async();
rtos::Mutex _cmutex; // Protect asynchronous connection logic
esp_connection_software_status_t _software_conn_stat ;
esp_connection_software_status_t _software_conn_stat;
bool _dhcp;

};
#endif
Expand Down