From 9f347c56c4257e24b17c07d770fd437fa88772e3 Mon Sep 17 00:00:00 2001 From: Michal Paszta Date: Mon, 30 Mar 2020 19:36:45 +0300 Subject: [PATCH] ESP8266: static address and dhcp added --- .../wifi/esp8266-driver/ESP8266/ESP8266.cpp | 26 +++++++++++++++++++ .../wifi/esp8266-driver/ESP8266/ESP8266.h | 11 ++++++++ .../wifi/esp8266-driver/ESP8266Interface.cpp | 25 ++++++++++++++++-- .../wifi/esp8266-driver/ESP8266Interface.h | 9 ++++++- 4 files changed, 68 insertions(+), 3 deletions(-) diff --git a/components/wifi/esp8266-driver/ESP8266/ESP8266.cpp b/components/wifi/esp8266-driver/ESP8266/ESP8266.cpp index 1520027335..66dccb5fe7 100644 --- a/components/wifi/esp8266-driver/ESP8266/ESP8266.cpp +++ b/components/wifi/esp8266-driver/ESP8266/ESP8266.cpp @@ -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(); diff --git a/components/wifi/esp8266-driver/ESP8266/ESP8266.h b/components/wifi/esp8266-driver/ESP8266/ESP8266.h index e93c5cc956..f0235630e3 100644 --- a/components/wifi/esp8266-driver/ESP8266/ESP8266.h +++ b/components/wifi/esp8266-driver/ESP8266/ESP8266.h @@ -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 * diff --git a/components/wifi/esp8266-driver/ESP8266Interface.cpp b/components/wifi/esp8266-driver/ESP8266Interface.cpp index b438b43af3..bb6d1f9e5e 100644 --- a/components/wifi/esp8266-driver/ESP8266Interface.cpp +++ b/components/wifi/esp8266-driver/ESP8266Interface.cpp @@ -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,26 @@ 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) +{ + // 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) +{ + _dhcp = dhcp; + if (_esp.dhcp(dhcp, 1)) { + return NSAPI_ERROR_OK; + } else { + return NSAPI_ERROR_DEVICE_ERROR; + } +} void ESP8266Interface::_disconnect_async() { diff --git a/components/wifi/esp8266-driver/ESP8266Interface.h b/components/wifi/esp8266-driver/ESP8266Interface.h index d371bb457b..7848f43502 100644 --- a/components/wifi/esp8266-driver/ESP8266Interface.h +++ b/components/wifi/esp8266-driver/ESP8266Interface.h @@ -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