diff --git a/components/modules/wifi_ap.c b/components/modules/wifi_ap.c index 574299e8..5705592c 100644 --- a/components/modules/wifi_ap.c +++ b/components/modules/wifi_ap.c @@ -37,6 +37,7 @@ #include "ip_fmt.h" #include "nodemcu_esp_event.h" #include +#include "dhcpserver/dhcpserver_options.h" // Note: these are documented in wifi.md, update there too if changed here! @@ -122,6 +123,76 @@ void wifi_ap_init (void) // --- Lua API funcs ----------------------------------------------------- +static int wifi_ap_setip(lua_State *L) +{ + tcpip_adapter_ip_info_t ipInfo; + ip_addr_t dns; + uint8_t opt; + size_t len; + const char *str; + + luaL_checkanytable (L, 1); + + //memset(&ipInfo, 0, sizeof(tcpip_adapter_ip_info_t)); + + lua_getfield (L, 1, "ip"); + str = luaL_checklstring (L, -1, &len); + if(!ipaddr_aton(str, &ipInfo.ip)) + { + return luaL_error(L, "Could not parse IP address, aborting"); + } + + lua_getfield (L, 1, "gateway"); + str = luaL_checklstring (L, -1, &len); + if(!ipaddr_aton(str, &ipInfo.gw)) + { + return luaL_error(L, "Could not parse Gateway address, aborting"); + } + + lua_getfield (L, 1, "netmask"); + str = luaL_checklstring (L, -1, &len); + if(!ipaddr_aton(str, &ipInfo.netmask)) + { + return luaL_error(L, "Could not parse Netmask, aborting"); + } + + ESP_ERROR_CHECK(tcpip_adapter_dhcps_stop(TCPIP_ADAPTER_IF_AP)); + + tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_AP, &ipInfo); + + lua_getfield (L, 1, "dns"); + str = luaL_optlstring(L, -1, "", &len); + if(ipaddr_aton(str, &dns)) + { + + opt = 1; + dhcps_dns_setserver(&dns); + tcpip_adapter_dhcps_option(TCPIP_ADAPTER_OP_SET, DOMAIN_NAME_SERVER, &opt, sizeof(opt)); + + } + + ESP_ERROR_CHECK(tcpip_adapter_dhcps_start(TCPIP_ADAPTER_IF_AP)); + + return 0; +} + +static int wifi_ap_sethostname(lua_State *L) +{ + + size_t l; + esp_err_t err; + const char *hostname = luaL_checklstring(L, 1, &l); + + err = tcpip_adapter_set_hostname(TCPIP_ADAPTER_IF_AP, hostname); + + if (err != ESP_OK) + return luaL_error (L, "failed to set hostname, code %d", err); + + lua_pushboolean (L, err==ESP_OK); + + return 1; +} + static int wifi_ap_config (lua_State *L) { luaL_checkanytable (L, 1); @@ -181,6 +252,8 @@ static int wifi_ap_on (lua_State *L) const LUA_REG_TYPE wifi_ap_map[] = { + { LSTRKEY( "setip" ), LFUNCVAL( wifi_ap_setip ) }, + { LSTRKEY( "sethostname" ), LFUNCVAL( wifi_ap_sethostname ) }, { LSTRKEY( "config" ), LFUNCVAL( wifi_ap_config ) }, { LSTRKEY( "on" ), LFUNCVAL( wifi_ap_on ) }, { LSTRKEY( "getmac" ), LFUNCVAL( wifi_ap_getmac ) }, diff --git a/components/modules/wifi_sta.c b/components/modules/wifi_sta.c index d2848eea..813dec30 100644 --- a/components/modules/wifi_sta.c +++ b/components/modules/wifi_sta.c @@ -40,6 +40,8 @@ #include "nodemcu_esp_event.h" #include +#include "lwip/ip_addr.h" + static int scan_cb_ref = LUA_NOREF; // --- Event handling ----------------------------------------------------- @@ -161,6 +163,68 @@ static void do_connect (const system_event_t *evt) // --- Lua API functions ---------------------------------------------------- +static int wifi_sta_setip(lua_State *L) +{ + tcpip_adapter_ip_info_t ipInfo; + tcpip_adapter_dns_info_t dnsinfo; + size_t len; + const char *str; + + luaL_checkanytable (L, 1); + + //memset(&ipInfo, 0, sizeof(tcpip_adapter_ip_info_t)); + + lua_getfield (L, 1, "ip"); + str = luaL_checklstring (L, -1, &len); + if(!ipaddr_aton(str, &ipInfo.ip)) + { + return luaL_error(L, "Could not parse IP address, aborting"); + } + + lua_getfield (L, 1, "netmask"); + str = luaL_checklstring (L, -1, &len); + if(!ipaddr_aton(str, &ipInfo.netmask)) + { + return luaL_error(L, "Could not parse Netmask, aborting"); + } + + lua_getfield (L, 1, "gateway"); + str = luaL_checklstring (L, -1, &len); + if(!ipaddr_aton(str, &ipInfo.gw)) + { + return luaL_error(L, "Could not parse Gateway address, aborting"); + } + + lua_getfield (L, 1, "dns"); + str = luaL_optlstring(L, -1, str, &len); + if(!ipaddr_aton(str, &dnsinfo.ip)) + { + return luaL_error(L, "Could not parse DNS address, aborting"); + } + + ESP_ERROR_CHECK(tcpip_adapter_dhcpc_stop(TCPIP_ADAPTER_IF_STA)); + tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_STA, &ipInfo); + tcpip_adapter_set_dns_info(TCPIP_ADAPTER_IF_STA, TCPIP_ADAPTER_DNS_MAIN, &dnsinfo); + + return 0; +} + +static int wifi_sta_sethostname(lua_State *L) +{ + + size_t l; + esp_err_t err; + const char *hostname = luaL_checklstring(L, 1, &l); + + err = tcpip_adapter_set_hostname(TCPIP_ADAPTER_IF_STA, hostname); + + if (err != ESP_OK) + return luaL_error (L, "failed to set hostname, code %d", err); + + lua_pushboolean (L, err==ESP_OK); + + return 1; +} static int wifi_sta_config (lua_State *L) { @@ -385,6 +449,8 @@ static int wifi_sta_scan (lua_State *L) const LUA_REG_TYPE wifi_sta_map[] = { + { LSTRKEY( "setip" ), LFUNCVAL( wifi_sta_setip ) }, + { LSTRKEY( "sethostname" ), LFUNCVAL( wifi_sta_sethostname )}, { LSTRKEY( "config" ), LFUNCVAL( wifi_sta_config ) }, { LSTRKEY( "connect" ), LFUNCVAL( wifi_sta_connect ) }, { LSTRKEY( "disconnect" ), LFUNCVAL( wifi_sta_disconnect ) }, diff --git a/docs/modules/wifi.md b/docs/modules/wifi.md index 54b7606f..6f58ab8b 100644 --- a/docs/modules/wifi.md +++ b/docs/modules/wifi.md @@ -378,6 +378,57 @@ wifi.sta.scan({ hidden = 1 }, function(err,arr) end) ``` +## wifi.sta.setip() + +Sets IP address, netmask, gateway, dns address in station mode. + +Options set by this function are not saved to flash. + +#### Syntax +`wifi.sta.setip(cfg)` + +#### Parameters +- `cfg` table to hold configuration: + - `ip` device ip address. + - `netmask` network netmask. + - `gateway` gateway address. + - `dns` name server address. + +#### Returns +`nil` + +#### Example +```lua + cfg={} + cfg.ip=192.168.0.10 + cfg.netmask=255.255.255.0 + cfg.gateway=192.168.0.1 + cfg.dns=8.8.8.8 + wifi.sta.setip(cfg) +``` + +## wifi.sta.sethostname + +Sets station hostname + +Must be called before `wifi.sta.connect()` + +Options set by this function are not saved to flash. + +#### Syntax +`wifi.sta.sethostname(hostname)` + +#### Returns +true if success, false otherwise + +#### Parameters +`hostname` must only contain letters, numbers and hyphens('-') and be 32 characters or less with first and last character being alphanumeric + +#### Example +```lua + wifi.sta.sethostname("ESP32") +``` + # wifi.ap Module ## wifi.ap.config() @@ -460,3 +511,52 @@ None #### Returns MAC address as string e.g. "18:fe:34:a2:d7:34" + +## wifi.ap.setip() + +Sets IP address, netmask, gateway, dns address in AccessPoint mode. + +Options set by this function are not saved to flash. + +#### Syntax +`wifi.ap.setip(cfg)` + +#### Parameters +- `cfg` table to hold configuration: + - `ip` device ip address. + - `netmask` network netmask. + - `gateway` gateway address. + - `dns` name server address, which will be provided to clients over DHCP. (Optional) + +#### Returns +`nil` + +#### Example +```lua + cfg={} + cfg.ip=192.168.0.10 + cfg.netmask=255.255.255.0 + cfg.gateway=192.168.0.1 + cfg.dns=8.8.8.8 + wifi.ap.setip(cfg) +``` + +## wifi.ap.sethostname + +Sets AccessPoint hostname. + +Options set by this function are not saved to flash. + +#### Syntax +`wifi.ap.sethostname(hostname)` + +#### Returns +true if success, false otherwise + +#### Parameters +`hostname` must only contain letters, numbers and hyphens('-') and be 32 characters or less with first and last character being alphanumeric + +#### Example +```lua + wifi.ap.sethostname("ESP32") +```