// Lua: ip = wifi.xx.setip() static int wifi_setip( lua_State* L, uint8_t mode ) { struct ip_info pTempIp; wifi_get_ip_info(mode, &pTempIp); if (!lua_istable(L, 1)) return luaL_error( L, "wrong arg type" ); uint32_t ip = parse_key(L, "ip"); if(ip!=0) pTempIp.ip.addr = ip; ip = parse_key(L, "netmask"); if(ip!=0) pTempIp.netmask.addr = ip; ip = parse_key(L, "gateway"); if(mode==SOFTAP_IF || ip!=0) pTempIp.gw.addr = ip; if(STATION_IF == mode) { wifi_station_dhcpc_stop(); lua_pushboolean(L,wifi_set_ip_info(mode, &pTempIp)); } else { wifi_softap_dhcps_stop(); lua_pushboolean(L,wifi_set_ip_info(mode, &pTempIp)); wifi_softap_dhcps_start(); } return 1; }
void setup_wifi_st_mode(void) { // wifi_set_opmode((wifi_get_opmode()|STATION_MODE)&STATIONAP_MODE); wifi_set_opmode(wifi_get_opmode() | STATION_MODE); struct station_config stconfig; wifi_station_disconnect(); wifi_station_dhcpc_stop(); if(wifi_station_get_config(&stconfig)) { memset(stconfig.ssid, 0, sizeof(stconfig.ssid)); memset(stconfig.password, 0, sizeof(stconfig.password)); os_sprintf(stconfig.ssid, "%s", sysCfg.sta_ssid); os_sprintf(stconfig.password, "%s", sysCfg.sta_pwd); if(!wifi_station_set_config(&stconfig)) { #ifdef PLATFORM_DEBUG INFO("ESP8266 not set station config!\r\n"); #endif } } wifi_station_connect(); wifi_station_dhcpc_start(); wifi_station_set_auto_connect(1); #ifdef PLATFORM_DEBUG INFO("ESP8266 in STA mode configured.\r\n"); #endif }
void ICACHE_FLASH_ATTR setup_station(char ssid[], char password[]) { // Stop previous connection wifi_station_disconnect(); wifi_station_dhcpc_stop(); // Create config struct struct station_config staConfig; wifi_station_get_config(&staConfig); // Set SSID in struct os_memset(staConfig.ssid, 0, sizeof(staConfig.ssid)); os_memcpy(staConfig.ssid, ssid, os_strlen(ssid)); // Set Password in struct os_memset(staConfig.password, 0, sizeof(staConfig.password)); os_memcpy(staConfig.password, password, os_strlen(password)); // Use config struct wifi_station_set_config(&staConfig); //print("Set Station info"); wifi_station_connect(); wifi_station_dhcpc_start(); wifi_station_set_auto_connect(1); connection_status = CONNECTION_CONNECTING; }
int sj_wifi_setup_sta(const struct sys_config_wifi_sta *cfg) { int res; struct station_config sta_cfg; /* If in AP-only mode, switch to station. If in STA or AP+STA, keep it. */ if (wifi_get_opmode() == SOFTAP_MODE) { wifi_set_opmode_current(STATION_MODE); } wifi_station_disconnect(); memset(&sta_cfg, 0, sizeof(sta_cfg)); sta_cfg.bssid_set = 0; strncpy((char *) &sta_cfg.ssid, cfg->ssid, 32); strncpy((char *) &sta_cfg.password, cfg->pass, 64); res = wifi_station_set_config_current(&sta_cfg); if (!res) { LOG(LL_ERROR, ("Failed to set station config")); return 0; } if (cfg->ip != NULL && cfg->netmask != NULL) { struct ip_info info; memset(&info, 0, sizeof(info)); info.ip.addr = ipaddr_addr(cfg->ip); info.netmask.addr = ipaddr_addr(cfg->netmask); if (cfg->gw != NULL) info.gw.addr = ipaddr_addr(cfg->gw); wifi_station_dhcpc_stop(); wifi_set_ip_info(STATION_IF, &info); LOG(LL_INFO, ("WiFi STA IP config: %s %s %s", cfg->ip, cfg->netmask, (cfg->gw ? cfg->ip : ""))); } LOG(LL_INFO, ("WiFi STA: Joining %s", sta_cfg.ssid)); return wifi_station_connect(); }
/** * set new mode * @param m WiFiMode_t */ bool ESP8266WiFiGenericClass::mode(WiFiMode_t m) { if(_persistent){ if(wifi_get_opmode() == (uint8) m && wifi_get_opmode_default() == (uint8) m){ return true; } } else if(wifi_get_opmode() == (uint8) m){ return true; } bool ret = false; if (m != WIFI_STA && m != WIFI_AP_STA) // calls lwIP's dhcp_stop(), // safe to call even if not started wifi_station_dhcpc_stop(); ETS_UART_INTR_DISABLE(); if(_persistent) { ret = wifi_set_opmode(m); } else { ret = wifi_set_opmode_current(m); } ETS_UART_INTR_ENABLE(); return ret; }
/** * Change IP configuration settings disabling the dhcp client * @param local_ip Static ip configuration * @param gateway Static gateway configuration * @param subnet Static Subnet mask * @param dns1 Static DNS server 1 * @param dns2 Static DNS server 2 */ bool ESP8266WiFiSTAClass::config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1, IPAddress dns2) { if(!WiFi.enableSTA(true)) { return false; } struct ip_info info; info.ip.addr = static_cast<uint32_t>(local_ip); info.gw.addr = static_cast<uint32_t>(gateway); info.netmask.addr = static_cast<uint32_t>(subnet); wifi_station_dhcpc_stop(); if(wifi_set_ip_info(STATION_IF, &info)) { _useStaticIp = true; } else { return false; } ip_addr_t d; if(dns1 != (uint32_t)0x00000000) { // Set DNS1-Server d.addr = static_cast<uint32_t>(dns1); dns_setserver(0, &d); } if(dns2 != (uint32_t)0x00000000) { // Set DNS2-Server d.addr = static_cast<uint32_t>(dns2); dns_setserver(1, &d); } return true; }
void STC_FLASHMEM IP::configureStatic(WiFi::Network network, char const *IP, char const *netmask, char const *gateway) { ip_info info; info.ip.addr = ipaddr_addr(APtr<char>(f_strdup(IP)).get()); info.netmask.addr = ipaddr_addr(APtr<char>(f_strdup(netmask)).get()); info.gw.addr = ipaddr_addr(APtr<char>(f_strdup(gateway)).get()); Critical critical; wifi_station_dhcpc_stop(); wifi_softap_dhcps_stop(); wifi_set_ip_info(network, &info); }
// You will have to set the DNS-Server manually later since this will not enable DHCP2 void ESP8266WiFiClass::config(IPAddress local_ip, IPAddress gateway, IPAddress subnet) { struct ip_info info; info.ip.addr = static_cast<uint32_t>(local_ip); info.gw.addr = static_cast<uint32_t>(gateway); info.netmask.addr = static_cast<uint32_t>(subnet); wifi_station_dhcpc_stop(); wifi_set_ip_info(STATION_IF, &info); _useStaticIp = true; }
// configure Wifi, specifically DHCP vs static IP address based on flash config void ICACHE_FLASH_ATTR configWifiIP() { if (flashConfig.staticip == 0) { // let's DHCP! wifi_station_set_hostname(flashConfig.hostname); if (wifi_station_dhcpc_status() == DHCP_STARTED) wifi_station_dhcpc_stop(); wifi_station_dhcpc_start(); DBG("Wifi uses DHCP, hostname=%s\n", flashConfig.hostname); } else { // no DHCP, we got static network config! wifi_station_dhcpc_stop(); struct ip_info ipi; ipi.ip.addr = flashConfig.staticip; ipi.netmask.addr = flashConfig.netmask; ipi.gw.addr = flashConfig.gateway; wifi_set_ip_info(0, &ipi); DBG("Wifi uses static IP %d.%d.%d.%d\n", IP2STR(&ipi.ip.addr)); } #ifdef DEBUGIP debugIP(); #endif }
LOCAL void ICACHE_FLASH_ATTR setup_ap_mode() { wifi_station_disconnect(); wifi_station_dhcpc_stop(); wifi_set_opmode(SOFTAP_MODE); struct softap_config apconfig; if (wifi_softap_get_config(&apconfig)) { wifi_softap_dhcps_stop(); char macaddr[6]; wifi_get_macaddr(SOFTAP_IF, macaddr); os_memset(apconfig.ssid, 0, sizeof(apconfig.ssid)); os_memset(apconfig.password, 0, sizeof(apconfig.password)); apconfig.ssid_len = os_sprintf(apconfig.ssid, "MacGyver-IoT_%02x%02x%02x%02x%02x%02x", MAC2STR(macaddr)); //os_sprintf(apconfig.password, "%02x%02x%02x%02x%02x%02x", MAC2STR(macaddr)); // 18fe349bc6b6 //os_sprintf(apconfig.password, "test"); // 18fe349bc6b6 //apconfig.authmode = AUTH_WPA_WPA2_PSK; // AUTH_OPEN apconfig.authmode = AUTH_OPEN; apconfig.ssid_hidden = 0; apconfig.channel = 7; apconfig.max_connection = 10; if (!wifi_softap_set_config(&apconfig)) { // CTRL not set AP config! } struct ip_info ipinfo; if (wifi_get_ip_info(SOFTAP_IF, &ipinfo)) { IP4_ADDR(&ipinfo.ip, 192, 168, 4, 1); IP4_ADDR(&ipinfo.gw, 192, 168, 4, 1); IP4_ADDR(&ipinfo.netmask, 255, 255, 255, 0); if (!wifi_set_ip_info(SOFTAP_IF, &ipinfo)) { // CTRL not set IP config! } } wifi_softap_dhcps_start(); } if (wifi_get_phy_mode() != PHY_MODE_11N) wifi_set_phy_mode(PHY_MODE_11N); }
void ESP8266WiFiClass::config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns) { struct ip_info info; info.ip.addr = static_cast<uint32_t>(local_ip); info.gw.addr = static_cast<uint32_t>(gateway); info.netmask.addr = static_cast<uint32_t>(subnet); wifi_station_dhcpc_stop(); wifi_set_ip_info(STATION_IF, &info); // Set DNS-Server ip_addr_t d; d.addr = static_cast<uint32_t>(dns); dns_setserver(0,&d); _useStaticIp = true; }
// ---------------------------------------------------------------------------- // Set up and connect for STA Mode // ---------------------------------------------------------------------------- void connect_st_mode(void) { struct station_config stconfig; wifi_set_opmode(STATION_MODE); wifi_station_disconnect(); // just in case wifi_station_dhcpc_stop(); if(wifi_station_get_config(&stconfig)) { os_memset(stconfig.ssid, 0, sizeof(stconfig.ssid)); os_memset(stconfig.password, 0, sizeof(stconfig.password)); os_sprintf((char *)stconfig.ssid, "%s", WIFI_CLIENTSSID); os_sprintf((char *)stconfig.password, "%s", WIFI_CLIENTPASSWORD); if(!wifi_station_set_config(&stconfig)) { os_printf("ESP8266 not set station config!\n"); } } wifi_station_connect(); wifi_station_dhcpc_start(); os_printf("ESP8266 in STA mode configured.\n"); }
void ICACHE_FLASH_ATTR user_init() { // Initialize gpio_init(); uart_init(2400, 2400); os_delay_us(10000); //print("\r\n:)\r\n"); if (0 && DEBUG) { wifi_station_disconnect(); wifi_station_dhcpc_stop(); } //setup_server(); // ------------------------------------------------------------------------- ets_uart_printf("S"); //Set GPIO2 to output mode PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO2_U, FUNC_GPIO2); //Set GPIO2 low gpio_output_set(0, BIT2, BIT2, 0); //Disarm timer os_timer_disarm(&check_timer); //Setup timer os_timer_setfn(&check_timer, (os_timer_func_t *)check_timerfunc, NULL); //Arm the timer //1000 is the fire time in ms //0 for once and 1 for repeating os_timer_arm(&check_timer, 5000, 1); // Start going //Start os task system_os_task(user_procTask, user_procTaskPrio,user_procTaskQueue, user_procTaskQueueLen); system_os_task(recvTask, recvTaskPrio, recvTaskQueue, recvTaskQueueLen); system_init_done_cb(init_done); }
LOCAL void ICACHE_FLASH_ATTR setup_wifi_st_mode(void) { wifi_set_opmode(STATION_MODE); struct station_config stconfig; wifi_station_disconnect(); wifi_station_dhcpc_stop(); if(wifi_station_get_config(&stconfig)) { os_memcpy(&stconfig.ssid, WIFI_CLIENTSSID, sizeof(WIFI_CLIENTSSID)); os_memcpy(&stconfig.password, WIFI_CLIENTPASSWORD, sizeof(WIFI_CLIENTPASSWORD)); wifi_station_set_config(&stconfig); ets_uart_printf("SSID: %s\n",stconfig.ssid); stringDraw(2, 1, (char*)stconfig.ssid); } wifi_station_connect(); wifi_station_dhcpc_start(); wifi_station_set_auto_connect(1); ets_uart_printf("STA mode\n"); }
static void ICACHE_FLASH_ATTR _network_config_address(void) { #if CONFIG_USE_DHCP #error Not implemented yet! #else wifi_set_opmode(STATIONAP_MODE); uint8_t ret; ret = wifi_station_dhcpc_stop(); if (!ret) os_printf("dhcpc stop failed\r\n"); ret = ipaddr_aton(CONFIG_NETWORK_IP_ADDRESS, &ip_info.ip); if (!ret) os_printf("network IP address parse failed\r\n"); ret = ipaddr_aton(CONFIG_NETWORK_NETMASK, &ip_info.netmask); if (!ret) os_printf("network mask parse failed\r\n"); ret = ipaddr_aton(CONFIG_NETWORK_GATEWAY, &ip_info.gw); if (!ret) os_printf("network gateway parse failed\r\n"); wifi_set_ip_info(STATION_IF, &ip_info); #endif }
void ICACHE_FLASH_ATTR reset_wifi_configs(){ DPRINT("Resetting WiFi config..."); wifi_set_opmode(STATION_MODE); struct station_config stconfig; wifi_station_disconnect(); wifi_station_dhcpc_stop(); if(wifi_station_get_config(&stconfig)) { DPRINT("Setting config..."); os_memset(stconfig.ssid, 0, sizeof(stconfig.ssid)); os_memset(stconfig.password, 0, sizeof(stconfig.password)); os_sprintf((char*)stconfig.ssid, "%s", WIFI_CLIENTSSID); os_sprintf((char*)stconfig.password, "%s", WIFI_CLIENTPASS); Config::I().wifi_configured(wifi_station_set_config(&stconfig)); DPRINT("Config setting %s",Config::I().wifi_configured()?"success":"failed"); } wifi_station_connect(); wifi_station_dhcpc_start(); wifi_station_set_auto_connect(1); DPRINT("ESP8266 in STA mode configured."); }
LOCAL void ICACHE_FLASH_ATTR setup_wifi_st_mode(void) { wifi_set_opmode(STATION_MODE); struct station_config stconfig; wifi_station_disconnect(); wifi_station_dhcpc_stop(); if(wifi_station_get_config(&stconfig)) { os_memset(stconfig.ssid, 0, sizeof(stconfig.ssid)); os_memset(stconfig.password, 0, sizeof(stconfig.password)); os_sprintf(stconfig.ssid, "%s", WIFI_CLIENTSSID); os_sprintf(stconfig.password, "%s", WIFI_CLIENTPASSWORD); if(!wifi_station_set_config(&stconfig)) { DHT22_DEBUG("ESP8266 not set station config!\r\n"); } } wifi_station_connect(); wifi_station_dhcpc_start(); wifi_station_set_auto_connect(1); DHT22_DEBUG("ESP8266 in STA mode configured.\r\n"); }
void ICACHE_FLASH_ATTR wifi_init() { /*** Connect to WiFi ***/ wifi_set_opmode(STATION_MODE); wifi_station_disconnect(); char ssid[32] = WIFI_SSID; char pass[64] = WIFI_PASS; struct station_config sta_conf; sta_conf.bssid_set = 0; os_memcpy(&sta_conf.ssid, ssid, 32); os_memcpy(&sta_conf.password, pass, 64); wifi_station_set_config(&sta_conf); wifi_station_connect(); /*** Network configuration ***/ wifi_station_dhcpc_stop(); struct ip_info ip_conf; ip_conf.ip.addr = string_to_ip(IP_ADDR); ip_conf.netmask.addr = string_to_ip(IP_SUBNET); wifi_set_ip_info(STATION_IF, &ip_conf); }
//============================================================================================================================ void ICACHE_FLASH_ATTR setup_wifi_st_mode(void) { wifi_set_opmode(STATION_MODE); struct station_config stconfig; wifi_station_disconnect(); wifi_station_dhcpc_stop(); if(wifi_station_get_config(&stconfig)) { os_memset(stconfig.ssid, 0, sizeof(stconfig.ssid)); os_memset(stconfig.password, 0, sizeof(stconfig.password)); os_sprintf(stconfig.ssid, "%s", configs.hwSettings.wifi.SSID); os_sprintf(stconfig.password, "%s", configs.hwSettings.wifi.SSID_PASS); ets_uart_printf("\r\nSTA SSID:%s", configs.hwSettings.wifi.SSID); ets_uart_printf("\r\nSTA SSIDPAS:%s\r\n", configs.hwSettings.wifi.SSID_PASS); if(!wifi_station_set_config(&stconfig)) { ets_uart_printf("ESP8266 not set station config!\r\n"); } } wifi_station_connect(); wifi_station_dhcpc_start(); wifi_station_set_auto_connect(1); ets_uart_printf("ESP8266 in STA mode configured.\r\n"); }
/****************************************************************************** * FunctionName : udp_test_port_recv * Returns : none *******************************************************************************/ LOCAL void ICACHE_FLASH_ATTR udp_test_port_recv(void *arg, struct udp_pcb *upcb, struct pbuf *p, ip_addr_t *addr, u16_t port) { uint8 usrdata[32]; if (p == NULL) return; if(p->tot_len < 2) { pbuf_free(p); return; } uint16 length = mMIN(p->tot_len, sizeof(usrdata)-1); #if DEBUGSOO > 0 os_printf("udp " IPSTR ":%u [%d]\n", IP2STR(addr), port, p->tot_len); #endif length = pbuf_copy_partial(p, usrdata, length, 0); pbuf_free(p); uint8 *pudpbuf = (uint8 *)os_zalloc(udpbufsize+1); if(pudpbuf == NULL) return; uint16 udpbuflen = 0; int x = 0; if(length>2) x = atoi((char *)&usrdata[2]); if ((length>1)&&(usrdata[1]=='?')) switch(usrdata[0]) { case 'M': system_print_meminfo(); case 'A': { udp_puts("\nChip_id: %08x Flash_id: %08x\nsys_time:%08x ADC:%d\n", system_get_chip_id(), spi_flash_get_id(), system_get_time(), system_adc_read()); struct softap_config wiconfig; wifi_softap_get_config(&wiconfig); udp_puts("OPMode:%u SSID:'%s' Pwd:'%s' Ch:%u Authmode:%u MaxCon:%u Phu:%u ACon:%u\n", wifi_get_opmode(), wiconfig.ssid, wiconfig.password, wiconfig.channel, wiconfig.authmode, wiconfig.max_connection, wifi_get_phy_mode(), wifi_station_get_auto_connect()); udp_puts("Connect status:%u\n", wifi_station_get_connect_status()); }; case 'I': udp_puts("heapsize: %d\n", system_get_free_heap_size() + udpbufsize); udpbuflen += print_udp_psc(pudpbuf+udpbuflen, udpbufsize-udpbuflen); udpbuflen += print_tcp_psc(pudpbuf+udpbuflen, udpbufsize-udpbuflen); udpbuflen += chow_tcp_connection_info(pudpbuf+udpbuflen, udpbufsize-udpbuflen); break; case 'H': udp_puts("heapsize: %d\n", system_get_free_heap_size() + udpbufsize); break; case 'U': udp_puts("heapsize: %d\n", system_get_free_heap_size() + udpbufsize); udpbuflen += print_udp_psc(pudpbuf+udpbuflen, udpbufsize-udpbuflen); break; case 'T': udp_puts("heapsize: %d\n", system_get_free_heap_size() + udpbufsize); udpbuflen += print_tcp_psc(pudpbuf+udpbuflen, udpbufsize-udpbuflen); break; #ifdef USE_SRV_WEB_PORT case 'S': udp_puts("heapsize: %d\n", system_get_free_heap_size() + udpbufsize); udpbuflen += chow_tcp_connection_info(pudpbuf+udpbuflen, udpbufsize-udpbuflen); break; #endif case 'R': system_restart(); break; case 'P': udp_puts("system_set_os_print(%u)\n", x); system_set_os_print(x); break; case 'O': udp_puts("wifi_set_opmode(%u):%u\n", x, wifi_set_opmode(x)); break; case 'B': udp_puts("wifi_station_set_auto_connect(%u):%u\n", x, wifi_station_set_auto_connect(x)); break; case 'D': switch(x) { case 0: udp_puts("wifi_station_dhcpc_start:%u\n", wifi_station_dhcpc_start()); break; case 1: udp_puts("wifi_station_dhcpc_stop:%u\n", wifi_station_dhcpc_stop()); break; case 2: udp_puts("wifi_softap_dhcps_start:%u\n",wifi_softap_dhcps_start()); break; case 3: udp_puts("wifi_softap_dhcps_stop:%u\n", wifi_softap_dhcps_stop()); break; default: udp_puts("D(%u)?\n", x); } break; case 'F': if(flashchip != NULL) { udp_puts("FlashID: 0x%08x\nChip size: %d\nBlock size: %d\nSector size: %d\nPage size: %d\nStatus mask: 0x%08x\n", flashchip->deviceId, flashchip->chip_size, flashchip->block_size, flashchip->sector_size, flashchip->page_size, flashchip->status_mask ); udp_puts("Real Flash size: %u\n", spi_flash_real_size()); } else udp_puts("Unknown Flash type!\n"); break; case 'E': udp_puts("wifi_set_sleep_type(%d):%u\n", x, wifi_set_sleep_type(x)); break; case 'G': udp_puts("g_ic = %p\n", &g_ic); break; default: udp_puts("???\n"); } if(udpbuflen) { struct pbuf *z = pbuf_alloc(PBUF_TRANSPORT, udpbuflen, PBUF_RAM); if(z != NULL) { err_t err = pbuf_take(z, pudpbuf, udpbuflen); os_free(pudpbuf); if(err == ERR_OK) { udp_sendto(upcb, z, addr, port); } pbuf_free(z); return; } } os_free(pudpbuf); }
void wifi_init() { wifi_set_opmode(NULL_MODE); //Next time start up in NULL mode wifi_set_opmode_current(NULL_MODE); //Initialize station config parameters struct station_config stconf; memset(&stconf, 0, sizeof(stconf)); strncpy((char *)stconf.ssid, szWifiSsid, sizeof(stconf.ssid)); strncpy((char *)stconf.password, szWifiPassword, sizeof(stconf.password)); stconf.ssid[sizeof(stconf.ssid)-1]='\0'; stconf.password[sizeof(stconf.password)-1]='\0'; //Initialize AP config parameters struct softap_config apconf; memset(&apconf, 0, sizeof(apconf)); // SSID strncpy((char *)apconf.ssid, szWifiSsid, sizeof(apconf.ssid)); apconf.ssid[sizeof(apconf.ssid)-1]='\0'; // Password & encryption strncpy((char *)apconf.password, szWifiPassword, sizeof(apconf.password)); apconf.password[sizeof(apconf.password)-1]='\0'; if (strlen(szWifiPassword) >= 8) { apconf.authmode = AUTH_WPA_WPA2_PSK; } else { // if password <8 characters, don't use password. apconf.authmode = AUTH_WEP; memset(apconf.password, 0, sizeof(apconf.password)); } apconf.max_connection = 255; apconf.beacon_interval = 100; wifi_set_opmode_current(nWifiMode); wifi_softap_set_config_current(&apconf); wifi_station_set_config_current(&stconf); struct ip_info ipinfo; memset(&ipinfo, 0, sizeof(ipinfo)); ipinfo.ip.addr = aIP; ipinfo.netmask.addr = aNetmask; ipinfo.gw.addr = aGateway; if (nWifiMode == STATION_MODE) { wifi_station_dhcpc_stop(); wifi_station_set_hostname(szHostname); if (bUseDhcp) { wifi_station_dhcpc_start(); } else { wifi_set_ip_info(STATION_IF, &ipinfo); } } else { // DHCP Server should be stopped to change IP information wifi_softap_dhcps_stop(); wifi_set_ip_info(SOFTAP_IF, &ipinfo); uint32_t aFirstIp = (aIP & aNetmask) | 1; uint32_t aLastIp = (aIP & aNetmask) | (-1 & ~aNetmask); struct dhcps_lease leases; memset(&leases, 0, sizeof(leases)); // Determine whether the range before or after our IP is bigger if (aIP - aFirstIp > aLastIp - aIP) { leases.start_ip.addr = aFirstIp; leases.end_ip.addr = aIP - 1; } else { leases.start_ip.addr = aIP + 1; leases.end_ip.addr = aLastIp; } wifi_softap_set_dhcps_lease(&leases); uint8_t offer_router = 0; wifi_softap_set_dhcps_offer_option(OFFER_ROUTER, &offer_router); wifi_softap_dhcps_start(); } }
/****************************************************************************** * FunctionName : user_esp_platform_init * Description : device parame init based on espressif platform * Parameters : none * Returns : none *******************************************************************************/ void ICACHE_FLASH_ATTR user_esp_platform_init(void) { os_sprintf(iot_version,"%s%d.%d.%dt%d(%s)",VERSION_TYPE,IOT_VERSION_MAJOR,\ IOT_VERSION_MINOR,IOT_VERSION_REVISION,device_type,UPGRADE_FALG); os_printf("IOT VERSION = %s\n",iot_version); system_param_load(ESP_PARAM_START_SEC, 0, &esp_param, sizeof(esp_param)); struct rst_info *rtc_info = system_get_rst_info(); os_printf("reset reason: %x\n", rtc_info->reason); if (rtc_info->reason == REASON_WDT_RST || rtc_info->reason == REASON_EXCEPTION_RST || rtc_info->reason == REASON_SOFT_WDT_RST) { if (rtc_info->reason == REASON_EXCEPTION_RST) { os_printf("Fatal exception (%d):\n", rtc_info->exccause); } os_printf("epc1=0x%08x, epc2=0x%08x, epc3=0x%08x, excvaddr=0x%08x, depc=0x%08x\n", rtc_info->epc1, rtc_info->epc2, rtc_info->epc3, rtc_info->excvaddr, rtc_info->depc); } /***add by tzx for saving ip_info to avoid dhcp_client start****/ struct dhcp_client_info dhcp_info; struct ip_info sta_info; system_rtc_mem_read(64,&dhcp_info,sizeof(struct dhcp_client_info)); if(dhcp_info.flag == 0x01 ) { if (true == wifi_station_dhcpc_status()) { wifi_station_dhcpc_stop(); } sta_info.ip = dhcp_info.ip_addr; sta_info.gw = dhcp_info.gw; sta_info.netmask = dhcp_info.netmask; if ( true != wifi_set_ip_info(STATION_IF,&sta_info)) { os_printf("set default ip wrong\n"); } } os_memset(&dhcp_info,0,sizeof(struct dhcp_client_info)); system_rtc_mem_write(64,&dhcp_info,sizeof(struct rst_info)); #if AP_CACHE wifi_station_ap_number_set(AP_CACHE_NUMBER); #endif #if 0 { char sofap_mac[6] = {0x16, 0x34, 0x56, 0x78, 0x90, 0xab}; char sta_mac[6] = {0x12, 0x34, 0x56, 0x78, 0x90, 0xab}; struct ip_info info; wifi_set_macaddr(SOFTAP_IF, sofap_mac); wifi_set_macaddr(STATION_IF, sta_mac); IP4_ADDR(&info.ip, 192, 168, 3, 200); IP4_ADDR(&info.gw, 192, 168, 3, 1); IP4_ADDR(&info.netmask, 255, 255, 255, 0); wifi_set_ip_info(STATION_IF, &info); IP4_ADDR(&info.ip, 10, 10, 10, 1); IP4_ADDR(&info.gw, 10, 10, 10, 1); IP4_ADDR(&info.netmask, 255, 255, 255, 0); wifi_set_ip_info(SOFTAP_IF, &info); } #endif if (esp_param.activeflag != 1) { #ifdef SOFTAP_ENCRYPT struct softap_config config; char password[33]; char macaddr[6]; wifi_softap_get_config(&config); wifi_get_macaddr(SOFTAP_IF, macaddr); os_memset(config.password, 0, sizeof(config.password)); os_sprintf(password, MACSTR "_%s", MAC2STR(macaddr), PASSWORD); os_memcpy(config.password, password, os_strlen(password)); config.authmode = AUTH_WPA_WPA2_PSK; wifi_softap_set_config(&config); #endif wifi_station_set_hostname( HOST_NAME ); wifi_set_opmode(STATION_MODE); } #if SENSOR_DEVICE user_sensor_init(esp_param.activeflag); #endif #if 0 if (wifi_get_opmode() != SOFTAP_MODE) { os_timer_disarm(&client_timer); os_timer_setfn(&client_timer, (os_timer_func_t *)user_esp_platform_check_ip, 1); os_timer_arm(&client_timer, 100, 0); } // 2015-12-27 single_key[0] = key_init_single( 12, PERIPHS_IO_MUX_MTDI_U, FUNC_GPIO12, NULL, GPIO_INTER); keys.key_num = 1; keys.single_key = single_key; key_init(&keys); #endif }
/** * Change IP configuration settings disabling the dhcp client * @param local_ip Static ip configuration * @param gateway Static gateway configuration * @param subnet Static Subnet mask * @param dns1 Static DNS server 1 * @param dns2 Static DNS server 2 */ bool ESP8266WiFiSTAClass::config(IPAddress local_ip, IPAddress arg1, IPAddress arg2, IPAddress arg3, IPAddress dns2) { if(!WiFi.enableSTA(true)) { return false; } //ESP argument order is: ip, gateway, subnet, dns1 //Arduino arg order is: ip, dns, gateway, subnet. //first, check whether dhcp should be used, which is when ip == 0 && gateway == 0 && subnet == 0. bool espOrderUseDHCP = (local_ip == 0U && arg1 == 0U && arg2 == 0U); bool arduinoOrderUseDHCP = (local_ip == 0U && arg2 == 0U && arg3 == 0U); if (espOrderUseDHCP || arduinoOrderUseDHCP) { _useStaticIp = false; wifi_station_dhcpc_start(); return true; } //To allow compatibility, check first octet of 3rd arg. If 255, interpret as ESP order, otherwise Arduino order. IPAddress gateway = arg1; IPAddress subnet = arg2; IPAddress dns1 = arg3; if(subnet[0] != 255) { //octet is not 255 => interpret as Arduino order gateway = arg2; subnet = arg3[0] == 0 ? IPAddress(255,255,255,0) : arg3; //arg order is arduino and 4th arg not given => assign it arduino default dns1 = arg1; } // check whether all is IPv4 (or gateway not set) if (!(local_ip.isV4() && subnet.isV4() && (!gateway.isSet() || gateway.isV4()))) { return false; } //ip and gateway must be in the same subnet if((local_ip.v4() & subnet.v4()) != (gateway.v4() & subnet.v4())) { return false; } struct ip_info info; info.ip.addr = local_ip.v4(); info.gw.addr = gateway.v4(); info.netmask.addr = subnet.v4(); wifi_station_dhcpc_stop(); if(wifi_set_ip_info(STATION_IF, &info)) { _useStaticIp = true; } else { return false; } if(dns1.isSet()) { // Set DNS1-Server dns_setserver(0, dns1); } if(dns2.isSet()) { // Set DNS2-Server dns_setserver(1, dns2); } return true; }
void ICACHE_FLASH_ATTR settings_apply( userSettings_t *settings ) { //Description if (!((settings->magic==0x42)&&(settings->version==SETTINGS_VERSION))) { os_printf("<ERROR: CAN NOT APPLY CORRUPT SETTINGS!>\n\r"); return; } if (settings->connection_successful) { ap_stop(); } else { ap_start(); } //Device information wifi_station_set_hostname(settings->name); //Network struct station_config stationConf; wifi_station_get_config( &stationConf ); stationConf.bssid_set = 0; os_memcpy( &stationConf.ssid, settings->ssid, 32 ); os_memcpy( &stationConf.password, settings->password, 64 ); wifi_station_set_config_current( &stationConf ); os_printf("########################## Wifi config set to SSID '%s' and PASSWORD '%s'.\n\r", settings->ssid, settings->password); if (settings->enable_dhcp) { wifi_station_dhcpc_start(); } else { wifi_station_dhcpc_stop(); struct ip_info dhcpc_ip_info; IP4_ADDR(&dhcpc_ip_info.ip, settings->static_ip_1, settings->static_ip_2, settings->static_ip_3, settings->static_ip_4); IP4_ADDR(&dhcpc_ip_info.gw, settings->static_gateway_1, settings->static_gateway_2, settings->static_gateway_3, settings->static_gateway_4); IP4_ADDR(&dhcpc_ip_info.netmask, settings->static_netmask_1, settings->static_netmask_2, settings->static_netmask_3, settings->static_netmask_4); wifi_set_ip_info(STATION_IF, &dhcpc_ip_info); } wifi_station_disconnect(); //MDNS if (settings->enable_mdns) { /*wifi_set_broadcast_if(STATIONAP_MODE); struct mdns_info *info = (struct mdns_info *) os_zalloc(sizeof(struct mdns_info)); info->host_name = settings->mdns_hostname; info->ipAddr = station_ipconfig.ip.addr; //ESP8266 station IP info->server_name = "FirmwaRe MDNS"; info->server_port = 80; info->txt_data[0] = “version = now”; info->txt_data[1] = “user1 = data1”; info->txt_data[2] = “user2 = data2”; espconn_mdns_init(info);*/ } //NTP if (settings->enable_ntp) { //settings->ntpserver //settings->timezone //settings->enable_summertime } //Security /* settings->password; */ //Digital outputs #if OUTPUT1>-1 if (settings->bootstate&1){ board_setOutput(OUTPUT1, true); } else { board_setOutput(OUTPUT1, false); } #endif #if OUTPUT2>-1 if (settings->bootstate&2) { board_setOutput(OUTPUT2, true); } else { board_setOutput(OUTPUT2, false); } #endif #if OUTPUT3>-1 if (settings->bootstate&4) { board_setOutput(OUTPUT3, true); } else { board_setOutput(OUTPUT3, false); } #endif #if OUTPUT4>-1 if (settings->bootstate&8) { board_setOutput(OUTPUT4, true); } else { board_setOutput(OUTPUT4, false); } #endif #if OUTPUT5>-1 if (settings->bootstate&16) { board_setOutput(OUTPUT5, true); } else { board_setOutput(OUTPUT5, false); } #endif #if OUTPUT6>-1 if (settings->bootstate&32) { board_setOutput(OUTPUT6, true); } else { board_setOutput(OUTPUT6, false); } #endif #if OUTPUT7>-1 if (settings->bootstate&64) { board_setOutput(OUTPUT7, true); } else { board_setOutput(OUTPUT7, false); } #endif #if OUTPUT8>-1 if (settings->bootstate&128) { board_setOutput(OUTPUT8, true); } else { board_setOutput(OUTPUT8, false); } #endif //ESPLight setPWM(settings->bootstate_R, settings->bootstate_G, settings->bootstate_B); //settings->ledstrip_type; //settings->ledstrip_length; //PKA /* settings->pka_wb settings->pka_wb_time */ }