Merge pull request #12721 from michalpasztamobica/esp8266_set_network

ESP8266: static address configuration and dhcp enable/disable added
pull/12971/head
Martin Kojtal 2020-05-13 19:26:26 +02:00 committed by GitHub
commit 8e789b0162
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 78 additions and 3 deletions

View File

@ -412,6 +412,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();

View File

@ -194,6 +194,17 @@ public:
*/
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
*

View File

@ -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));
@ -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;
@ -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()
{

View File

@ -132,6 +132,12 @@ public:
*/
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
*/
@ -518,7 +524,8 @@ private:
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