/** @brief Serial Bridge task initialize @param[in] port: network port */ MEMSPACE bridge_task_init(int port) { static struct espconn esp_data_config; static esp_tcp esp_data_tcp_config; if(!(uart_send_queue = queue_new(BUFFER_SIZE))) reset(); if(!(uart_receive_queue = queue_new(BUFFER_SIZE))) reset(); if(!(tcp_data_send_buffer = malloc(BUFFER_SIZE))) reset(); wifi_set_sleep_type(NONE_SLEEP_T); tcp_accept(&esp_data_config, &esp_data_tcp_config, port, tcp_data_connect_callback); espconn_regist_time(&esp_data_config, 0, 0); esp_data_tcp_connection = 0; system_os_task(bridge_task, bridge_task_id, bridge_task_queue, bridge_task_queue_length); system_os_post(bridge_task_id, 0, 0); printf("\nbridge task init done\n"); }
static void ICACHE_FLASH_ATTR light_pwm_smooth_adj_proc(void) { if (TotalUsedLightEvtNum > 0) { user_light_set_period(LightEvtArr[CurEvtIdxToBeUse].period); os_memcpy(current_duty, LightEvtArr[CurEvtIdxToBeUse].duty, sizeof(current_duty)); CurEvtIdxToBeUse++; if (CurEvtIdxToBeUse > (LIGHT_EVT_QNUM - 1)) { CurEvtIdxToBeUse = 0; } LightEvtFree(); if (change_finish) { light_dh_pwm_adj_proc(NULL); } } if (change_finish) { light_save_target_duty(); if (check_pwm_duty_zero()) { if (light_sleep_flg == 0) { os_printf("light sleep en\r\n"); wifi_set_sleep_type(LIGHT_SLEEP_T); light_sleep_flg = 1; } } } }
void user_init(void) { ETS_UART_INTR_DISABLE(); UART_SetBaudrate(UART0, BIT_RATE_9600); UART_ResetFifo(UART0); UART_SetBaudrate(UART1, BIT_RATE_115200); UART_ResetFifo(UART1); flash_param_init(); flash_param = flash_param_get(); emsRxBuf = allocateRcvMsgBuff(); uart_init(BIT_RATE_9600, BIT_RATE_115200); rtc_clock_calibration = system_rtc_clock_cali_proc(); // get RTC clock period os_printf("rtc_clock_calibration: %0x\n", rtc_clock_calibration >>12 ); os_printf("system_get_rtc_time: %d\n", system_get_rtc_time()); os_printf("system_get_time: %d\n", system_get_time()); serverInit(flash_param->port); wifi_set_sleep_type(LIGHT_SLEEP_T); system_os_task(recvTask, recvTaskPrio, recvTaskQueue, recvTaskQueueLen); ETS_UART_INTR_ENABLE(); }
//This cgi changes the operating mode: STA / AP / STA+AP int ICACHE_FLASH_ATTR cgiWiFiSetMode(HttpdConnData *connData) { int len; char buff[1024]; if (connData->conn==NULL) { // Connection aborted. Clean up. return HTTPD_CGI_DONE; } len=httpdFindArg(connData->getArgs, "mode", buff, sizeof(buff)); if (len!=0) { int m = atoi(buff); os_printf("Wifi switching to mode %d\n", m); #ifndef DEMO_MODE wifi_set_opmode(m&3); if (m == 1) { wifi_set_sleep_type(SLEEP_MODE); // STA-only mode, reset into STA+AP after a timeout os_timer_disarm(&resetTimer); os_timer_setfn(&resetTimer, resetTimerCb, NULL); os_timer_arm(&resetTimer, RESET_TIMEOUT, 0); } jsonHeader(connData, 200); #endif } else { jsonHeader(connData, 400); } return HTTPD_CGI_DONE; }
//This routine is ran some time after a connection attempt to an access point. If //the connect succeeds, this gets the module in STA-only mode. static void ICACHE_FLASH_ATTR resetTimerCb(void *arg) { int x = wifi_station_get_connect_status(); int m = wifi_get_opmode() & 0x3; os_printf("Wifi check: mode=%s status=%d\n", wifiMode[m], x); if (x == STATION_GOT_IP) { if (m != 1) { // We're happily connected, go to STA mode os_printf("Wifi got IP. Going into STA mode..\n"); wifi_set_opmode(1); wifi_set_sleep_type(SLEEP_MODE); os_timer_arm(&resetTimer, RESET_TIMEOUT, 0); //os_timer_disarm(&deepTimer); //os_timer_setfn(&deepTimer, deepSleepCb, NULL); //os_timer_arm(&deepTimer, 1000, 1); } log_uart(false); // no more resetTimer at this point, gotta use physical reset to recover if in trouble } else { if (m != 3) { os_printf("Wifi connect failed. Going into STA+AP mode..\n"); wifi_set_opmode(3); } log_uart(true); os_printf("Enabling/continuing uart log\n"); os_timer_arm(&resetTimer, RESET_TIMEOUT, 0); } }
STATIC mp_obj_t esp_sleep_type(mp_uint_t n_args, const mp_obj_t *args) { if (n_args == 0) { return mp_obj_new_int(wifi_get_sleep_type()); } else { wifi_set_sleep_type(mp_obj_get_int(args[0])); return mp_const_none; } }
void init_yun() { mjyun_statechanged(mjyun_stated_cb); mjyun_ondata(mjyun_receive); mjyun_onconnected(mjyun_connected); mjyun_ondisconnected(mjyun_disconnected); mjyun_run(&mjyun_conf); wifi_set_sleep_type(MODEM_SLEEP_T); }
void AppWIFI::init() { wifi_set_sleep_type(NONE_SLEEP_T); //don`t enable/disable again to save eeprom cycles if(!WifiStation.isEnabled()) { debugapp("AppWIFI::init enable WifiStation"); WifiStation.enable(true, true); } if(WifiAccessPoint.isEnabled()) { debugapp("AppWIFI::init WifiAccessPoint disablw"); WifiAccessPoint.enable(false, true); } _con_ctr = 0; if(app.isFirstRun()) { debugapp("AppWIFI::init initial run - setting up AP"); app.cfg.network.connection.mdnshostname = String(DEFAULT_AP_SSIDPREFIX) + String(system_get_chip_id()); app.cfg.network.ap.ssid = String(DEFAULT_AP_SSIDPREFIX) + String(system_get_chip_id()); app.cfg.save(); WifiAccessPoint.setIP(_ApIP); } WifiEvents.onStationDisconnect(onStationDisconnectDelegate(&AppWIFI::_STADisconnect, this)); WifiEvents.onStationConnect(onStationConnectDelegate(&AppWIFI::_STAConnected, this)); WifiEvents.onStationGotIP(onStationGotIPDelegate(&AppWIFI::_STAGotIP, this)); if(WifiStation.getSSID() == "") { debugapp("AppWIFI::init no AP to connect to - start own AP"); // No wifi to connect to - initialize AP startAp(); // already scan for avaialble networks to speedup things later scan(); } else { //configure WifiClient if (!app.cfg.network.connection.dhcp && !app.cfg.network.connection.ip.isNull()) { debugapp("AppWIFI::init setting static ip"); if(WifiStation.isEnabledDHCP()) { debugapp("AppWIFI::init disabled dhcp"); WifiStation.enableDHCP(false); } if ( !(WifiStation.getIP() == app.cfg.network.connection.ip) || !(WifiStation.getNetworkGateway() == app.cfg.network.connection.gateway) || !(WifiStation.getNetworkMask() == app.cfg.network.connection.netmask)) { debugapp("AppWIFI::init updating ip configuration"); WifiStation.setIP(app.cfg.network.connection.ip, app.cfg.network.connection.netmask, app.cfg.network.connection.gateway); } } else { debugapp("AppWIFI::init dhcp"); if(!WifiStation.isEnabledDHCP()) { debugapp("AppWIFI::init enabling dhcp"); WifiStation.enableDHCP(true); } } } }
void initWifi() { hardReset(); if (config.host.highSpeed) { wifi_set_sleep_type(NONE_SLEEP_T); } else { wifi_set_sleep_type(LIGHT_SLEEP_T); } // Connecting to an access point currently causes a hang. :( // TODO(maruel): Figure it out. //WifiEvents.onStationConnect(onStationConnect); //WifiEvents.onStationDisconnect(onStationDisconnect); //WifiEvents.onStationAuthModeChange(onStationAuthModeChange); WifiEvents.onStationGotIP(onStationGotIP); //WifiEvents.onAccessPointConnect(onAccessPointConnect); //WifiEvents.onAccessPointDisconnect(onAccessPointDisconnect); //WifiEvents.onAccessPointProbeReqRecved(onAccessPointProbeReqRecved); if (config.has_wifiClient && config.wifiClient.has_ssid && config.wifiClient.has_password) { Serial.printf("wifi client \"%s\"\n", config.wifiClient.ssid); if (!WifiStation.config(config.wifiClient.ssid, config.wifiClient.password, true)) { Serial.println("failure"); } WifiStation.enable(true, false); WifiStation.connect(); } else if (config.has_wifiAP && config.wifiAP.has_ssid && config.wifiAP.has_password) { Serial.printf("wifi AP \"%s\"\n", config.wifiAP.ssid); // TODO(maruel): Channel is hardcoded to 7, beacon at 200ms. if (!WifiAccessPoint.config(config.wifiAP.ssid, config.wifiAP.password, AUTH_WPA2_PSK)) { Serial.println("failure"); } WifiAccessPoint.enable(true, false); } else { Serial.printf("wifi default AP: \"%s\"\n", config.host.name); // TODO(maruel): Scan networks. // TODO(maruel): Use WifiStation.smartConfigStart() //if (!WifiAccessPoint.config(config.host.name, chipID, AUTH_WPA2_PSK)) { if (!WifiAccessPoint.config(config.host.name, "", AUTH_OPEN)) { Serial.println("failure"); } WifiAccessPoint.enable(true, false); } }
// Init the wireless, which consists of setting a timer if we expect to connect to an AP // so we can revert to STA+AP mode if we can't connect. void ICACHE_FLASH_ATTR wifiInit() { // wifi_set_phy_mode(2); // limit to 802.11b/g 'cause n is flaky int x = wifi_get_opmode() & 0x3; DBG("Wifi init, mode=%s\n", wifiMode[x]); configWifiIP(); // The default sleep mode should be modem_sleep, but we set it here explicitly for good // measure. We can't use light_sleep because that powers off everthing and we would loose // all connections. wifi_set_sleep_type(MODEM_SLEEP_T); wifi_set_event_handler_cb(wifiHandleEventCb); // check on the wifi in a few seconds to see whether we need to switch mode os_timer_disarm(&resetTimer); os_timer_setfn(&resetTimer, resetTimerCb, NULL); os_timer_arm(&resetTimer, RESET_TIMEOUT, 0); }
// Lua: realtype = sleeptype(type) static int wifi_sleeptype( lua_State* L ) { unsigned type; if ( lua_isnumber(L, 1) ){ type = lua_tointeger(L, 1); if ( type != NONE_SLEEP_T && type != LIGHT_SLEEP_T && type != MODEM_SLEEP_T ) return luaL_error( L, "wrong arg type" ); if(!wifi_set_sleep_type(type)){ lua_pushnil(L); return 1; } } type = wifi_get_sleep_type(); lua_pushinteger( L, type ); return 1; }
void init() { spiffs_mount(); // Mount file system, in order to work with files Serial.begin(SERIAL_BAUD_RATE); // 115200 by default Serial.systemDebugOutput(false); Serial.commandProcessing(false); //SET higher CPU freq & disable wifi sleep system_update_cpu_freq(SYS_CPU_160MHZ); wifi_set_sleep_type(NONE_SLEEP_T); ActiveConfig = loadConfig(); // Attach Wifi events handlers WifiEvents.onStationDisconnect(STADisconnect); WifiEvents.onStationGotIP(STAGotIP); startWebServer(); counterTimer.initializeMs(1000, counter_loop).start(); }
irom void setup() { #ifdef DEBUG uart_init(115200, 115200); #endif os_delay_us(100); INFO("\r\n\r\n\r\n\r\n\r\n\r\n"); INFO("\r\nWelcom to Noduino Open Ctrl4relay!\r\n"); INFO("Current firmware is user%d.bin\r\n", system_upgrade_userbin_check()+1); INFO("%s", noduino_banner); param_load(); relay_init(); led_init(); xkey_init(); init_yun(); wifi_set_sleep_type(MODEM_SLEEP_T); }
//Main routine. Initialize stdout, the I/O, filesystem and the webserver and we're done. void user_init(void) { mmInit(mmData); //Redirect stdout to websocket os_install_putc1((void *)stdoutPutcharWs); ioInit(); captdnsInit(); telnetInit(333); // 0x40200000 is the base address for spi flash memory mapping, ESPFS_POS is the position // where image is written in flash that is defined in Makefile. #ifdef ESPFS_POS espFsInit((void*)(0x40200000 + ESPFS_POS)); #else espFsInit((void*)(webpages_espfs_start)); #endif httpdInit(builtInUrls, 80); os_timer_disarm(&websockTimer); os_timer_setfn(&websockTimer, websockTimerCb, NULL); os_timer_arm(&websockTimer, 1000, 1); os_printf("\nReady\n"); wifi_set_sleep_type(LIGHT_SLEEP_T); }
void init() { //SET higher CPU freq & disable wifi sleep system_update_cpu_freq(SYS_CPU_160MHZ); wifi_set_sleep_type(NONE_SLEEP_T); inputchip.begin(); outputchip.begin(); Serial.begin(SERIAL_BAUD_RATE); // 115200 by default WifiStation.enable(false); WifiAccessPoint.enable(false); Serial.systemDebugOutput(false); // Allow debug output to serial Serial.println("<-= Sming start =->"); inputchip.pinMode(0xFFFF); // Use word-write mode to set all of the pins on inputchip to be inputs inputchip.pullupMode(0xFFFF); // Use word-write mode to Turn on the internal pull-up resistors. inputchip.inputInvert(0x0000); // Use word-write mode to invert the inputs so that logic 0 is read as HIGH outputchip.pinMode(0x0000); // Use word-write mode to Set all of the pins on outputchip to be outputs procTimer.initializeMs(200, loop).start(); }
/** * set Sleep mode * @param type sleep_type_t * @return bool */ bool ESP8266WiFiGenericClass::setSleepMode(WiFiSleepType_t type) { return wifi_set_sleep_type((sleep_type_t) type); }
/* Init the wireless * * Call both Soft-AP and Station default config * Change values according to Makefile hard-coded variables * Anyway set wifi opmode to STA+AP, it will change to STA if CHANGE_TO_STA is set to yes in Makefile * Call a timer to check the STA connection */ void ICACHE_FLASH_ATTR wifiInit() { // Check the wifi opmode int x = wifi_get_opmode() & 0x3; // If STA is enabled switch to STA+AP to allow for recovery, it will then switch to STA-only // once it gets an IP address if (x == 1) wifi_set_opmode(3); // Call both STATION and SOFTAP default config wifi_station_get_config_default(&stconf); wifi_softap_get_config_default(&apconf); DBG("Wifi init, mode=%s\n",wifiMode[x]); // Change STATION parameters, if defined in the Makefile #if defined(STA_SSID) && defined(STA_PASS) // Set parameters if (os_strlen((char*)stconf.ssid) == 0 && os_strlen((char*)stconf.password) == 0) { os_strncpy((char*)stconf.ssid, VERS_STR(STA_SSID), 32); os_strncpy((char*)stconf.password, VERS_STR(STA_PASS), 64); wifi_set_opmode(3); DBG("Wifi pre-config trying to connect to AP %s pw %s\n",(char*)stconf.ssid, (char*)stconf.password); // wifi_set_phy_mode(2); // limit to 802.11b/g 'cause n is flaky stconf.bssid_set = 0; wifi_station_set_config(&stconf); } #endif // Change SOFT_AP settings, if defined in Makefile #if defined(AP_SSID) // Check if ssid and pass are alphanumeric values int ssidlen = os_strlen(VERS_STR(AP_SSID)); if(checkString(VERS_STR(AP_SSID)) && ssidlen > 7 && ssidlen < 32){ // Clean memory and set the value of SSID os_memset(apconf.ssid, 0, 32); os_memcpy(apconf.ssid, VERS_STR(AP_SSID), os_strlen(VERS_STR(AP_SSID))); // Specify the length of ssid apconf.ssid_len= ssidlen; #if defined(AP_PASS) // If pass is at least 8 and less than 64 int passlen = os_strlen(VERS_STR(AP_PASS)); if( checkString(VERS_STR(AP_PASS)) && passlen > 7 && passlen < 64 ){ // Clean memory and set the value of PASS os_memset(apconf.password, 0, 64); os_memcpy(apconf.password, VERS_STR(AP_PASS), passlen); // Can't choose auth mode without a valid ssid and password #ifdef AP_AUTH_MODE // If set, use specified auth mode if(AP_AUTH_MODE >= 0 && AP_AUTH_MODE <=4) apconf.authmode = AP_AUTH_MODE; #else // If not, use WPA2 apconf.authmode = AUTH_WPA_WPA2_PSK; #endif }else if ( passlen == 0){ // If ssid is ok and no pass, set auth open apconf.authmode = AUTH_OPEN; // Remove stored password os_memset(apconf.password, 0, 64); } #endif }// end of ssid and pass check #ifdef AP_SSID_HIDDEN // If set, use specified ssid hidden parameter if(AP_SSID_HIDDEN == 0 || AP_SSID_HIDDEN ==1) apconf.ssid_hidden = AP_SSID_HIDDEN; #endif #ifdef AP_MAX_CONN // If set, use specified max conn number if(AP_MAX_CONN > 0 && AP_MAX_CONN <5) apconf.max_connection = AP_MAX_CONN; #endif #ifdef AP_BEACON_INTERVAL // If set use specified beacon interval if(AP_BEACON_INTERVAL >= 100 && AP_BEACON_INTERVAL <= 60000) apconf.beacon_interval = AP_BEACON_INTERVAL; #endif // Check softap config bool softap_set_conf = wifi_softap_set_config(&apconf); // Debug info DBG("Wifi Soft-AP parameters change: %s\n",softap_set_conf? "success":"fail"); #endif // if defined(AP_SSID) configWifiIP(); // The default sleep mode should be modem_sleep, but we set it here explicitly for good // measure. We can't use light_sleep because that powers off everthing and we would loose // all connections. wifi_set_sleep_type(MODEM_SLEEP_T); wifi_set_event_handler_cb(wifiHandleEventCb); // check on the wifi in a few seconds to see whether we need to switch mode os_timer_disarm(&resetTimer); os_timer_setfn(&resetTimer, resetTimerCb, NULL); os_timer_arm(&resetTimer, RESET_TIMEOUT, 0); }
static void _esp_wifi_setup(void) { esp_wifi_netdev_t* dev = &_esp_wifi_dev; ESP_WIFI_DEBUG("%p", dev); if (dev->netdev.driver) { ESP_WIFI_DEBUG("early returning previously initialized device"); return; } /* initialize netdev data structure */ dev->rx_len = 0; dev->state = ESP_WIFI_DISCONNECTED; dev->event = EVENT_MAX; /* set the netdev driver */ dev->netdev.driver = &_esp_wifi_driver; #ifndef MODULE_ESP_NOW /* set the WiFi interface mode */ if (!wifi_set_opmode_current(ESP_WIFI_MODE)) { ESP_WIFI_LOG_ERROR("could not set WiFi working mode"); return; } /* set the WiFi SoftAP configuration */ if (!wifi_softap_set_config_current((struct softap_config *)&softap_cfg)) { ESP_WIFI_LOG_ERROR("could not set WiFi configuration"); return; } #endif /* set the WiFi station configuration */ if (!wifi_station_set_config_current((struct station_config *)&station_cfg)) { ESP_WIFI_LOG_ERROR("could not set WiFi configuration"); return; } /* get station mac address and store it in device address */ if (!wifi_get_macaddr(ESP_WIFI_STATION_IF, dev->mac)) { ESP_WIFI_LOG_ERROR("could not get MAC address of WiFi interface"); return; } ESP_WIFI_DEBUG("own MAC addr is " MAC_STR, MAC_STR_ARG(dev->mac)); /* set auto reconnect policy */ wifi_station_set_reconnect_policy(true); wifi_station_set_auto_connect(true); /* register callbacks */ wifi_set_event_handler_cb(_esp_wifi_handle_event_cb); /* reconnect timer initialization */ _esp_wifi_reconnect_timer.callback = &_esp_wifi_reconnect_timer_cb; _esp_wifi_reconnect_timer.arg = dev; /* set the the reconnect timer */ xtimer_set(&_esp_wifi_reconnect_timer, ESP_WIFI_RECONNECT_TIME); /* avoid the WiFi modem going into sleep mode */ wifi_set_sleep_type(NONE_SLEEP_T); /* connect */ wifi_station_connect(); _esp_wifi_dev.state = ESP_WIFI_CONNECTING; return; }
/****************************************************************************** * 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); }
//Read configuration settings and apply them bool WIFI_CONFIG::Setup() { char pwd[MAX_PASSWORD_LENGTH+1]; char sbuf[MAX_SSID_LENGTH+1]; char hostname [MAX_HOSTNAME_LENGTH+1]; int wstatus; IPAddress currentIP; byte bflag=0; //set the sleep mode if (!CONFIG::read_byte(EP_SLEEP_MODE, &bflag )) { return false; } wifi_set_sleep_type ((sleep_type)bflag); sleep_mode=bflag; //AP or client ? if (!CONFIG::read_byte(EP_WIFI_MODE, &bflag ) || !CONFIG::read_string(EP_SSID, sbuf , MAX_SSID_LENGTH) ||!CONFIG::read_string(EP_PASSWORD, pwd , MAX_PASSWORD_LENGTH)) { return false; } if (!CONFIG::read_string(EP_HOSTNAME, hostname , MAX_HOSTNAME_LENGTH)) { strcpy(hostname,get_default_hostname()); } //disconnect if connected WiFi.disconnect(); //this is AP mode if (bflag==AP_MODE) { //setup Soft AP WiFi.mode(WIFI_AP); WiFi.softAP(sbuf, pwd); //setup PHY_MODE if (!CONFIG::read_byte(EP_PHY_MODE, &bflag )) { return false; } wifi_set_phy_mode((phy_mode)bflag); //get current config struct softap_config apconfig; wifi_softap_get_config(&apconfig); //set the chanel if (!CONFIG::read_byte(EP_CHANNEL, &bflag )) { return false; } apconfig.channel=bflag; //set Authentification type if (!CONFIG::read_byte(EP_AUTH_TYPE, &bflag )) { return false; } apconfig.authmode=(AUTH_MODE)bflag; //set the visibility of SSID if (!CONFIG::read_byte(EP_SSID_VISIBLE, &bflag )) { return false; } apconfig.ssid_hidden=!bflag; //no need to add these settings to configuration just use default ones apconfig.max_connection=DEFAULT_MAX_CONNECTIONS; apconfig.beacon_interval=DEFAULT_BEACON_INTERVAL; //apply settings to current and to default if (!wifi_softap_set_config(&apconfig) || !wifi_softap_set_config_current(&apconfig)) { Serial.println(F("M117 Error Wifi AP!")); delay(1000); } } else { //setup station mode WiFi.mode(WIFI_STA); WiFi.begin(sbuf, pwd); delay(500); //setup PHY_MODE if (!CONFIG::read_byte(EP_PHY_MODE, &bflag )) { return false; } wifi_set_phy_mode((phy_mode)bflag); delay(500); byte i=0; //try to connect while (WiFi.status() != WL_CONNECTED && i<40) { switch(WiFi.status()) { case 1: Serial.print(FPSTR(M117_)); Serial.println(F("No SSID found!")); break; case 4: Serial.print(FPSTR(M117_)); Serial.println(F("No Connection!")); break; default: Serial.print(FPSTR(M117_)); Serial.println(F("Connecting...")); break; } delay(500); i++; } if (WiFi.status() != WL_CONNECTED) { return false; } WiFi.hostname(hostname); } //DHCP or Static IP ? if (!CONFIG::read_byte(EP_IP_MODE, &bflag )) { return false; } if (bflag==STATIC_IP_MODE) { byte ip_buf[4]; //get the IP if (!CONFIG::read_buffer(EP_IP_VALUE,ip_buf , IP_LENGTH)) { return false; } IPAddress local_ip (ip_buf[0],ip_buf[1],ip_buf[2],ip_buf[3]); //get the gateway if (!CONFIG::read_buffer(EP_GATEWAY_VALUE,ip_buf , IP_LENGTH)) { return false; } IPAddress gateway (ip_buf[0],ip_buf[1],ip_buf[2],ip_buf[3]); //get the mask if (!CONFIG::read_buffer(EP_MASK_VALUE,ip_buf , IP_LENGTH)) { return false; } IPAddress subnet (ip_buf[0],ip_buf[1],ip_buf[2],ip_buf[3]); //apply according active wifi mode if (wifi_get_opmode()==WIFI_AP || wifi_get_opmode()==WIFI_AP_STA) { WiFi.softAPConfig( local_ip, gateway, subnet); } else { WiFi.config( local_ip, gateway, subnet); } } #ifdef MDNS_FEATURE // Set up mDNS responder: if (!mdns.begin(hostname)) { Serial.print(FPSTR(M117_)); Serial.println(F("Error with mDNS!")); delay(1000); } #endif //Get IP if (wifi_get_opmode()==WIFI_STA) { currentIP=WiFi.localIP(); } else { currentIP=WiFi.softAPIP(); } Serial.print(FPSTR(M117_)); Serial.println(currentIP); return true; }
/** * set Sleep mode * @param type sleep_type_t * @return bool */ bool ESP8266WiFiGenericClass::setSleepMode(WiFiSleepType_t type, uint8_t listenInterval) { /** * datasheet: * wifi_set_sleep_level(): Set sleep level of modem sleep and light sleep This configuration should be called before calling wifi_set_sleep_type Modem-sleep and light sleep mode have minimum and maximum sleep levels. - In minimum sleep level, station wakes up at every DTIM to receive beacon. Broadcast data will not be lost because it is transmitted after DTIM. However, it can not save much more power if DTIM period is short, as specified in AP. - In maximum sleep level, station wakes up at every listen interval to receive beacon. Broadcast data may be lost because station may be in sleep state at DTIM time. If listen interval is longer, more power will be saved, but it’s very likely to lose more broadcast data. - Default setting is minimum sleep level. Further reading: https://routerguide.net/dtim-interval-period-best-setting/ wifi_set_listen_interval(): Set listen interval of maximum sleep level for modem sleep and light sleep It only works when sleep level is set as MAX_SLEEP_T It should be called following the order: wifi_set_sleep_level(MAX_SLEEP_T) wifi_set_listen_interval wifi_set_sleep_type forum: https://github.com/espressif/ESP8266_NONOS_SDK/issues/165#issuecomment-416121920 default value seems to be 3 (as recommended by https://routerguide.net/dtim-interval-period-best-setting/) */ #ifdef DEBUG_ESP_WIFI if (listenInterval && type == WIFI_NONE_SLEEP) DEBUG_WIFI_GENERIC("listenInterval not usable with WIFI_NONE_SLEEP\n"); #endif if (type == WIFI_LIGHT_SLEEP || type == WIFI_MODEM_SLEEP) { if (listenInterval) { if (!wifi_set_sleep_level(MAX_SLEEP_T)) { DEBUG_WIFI_GENERIC("wifi_set_sleep_level(MAX_SLEEP_T): error\n"); return false; } if (listenInterval > 10) { DEBUG_WIFI_GENERIC("listenInterval must be in [1..10]\n"); #ifndef DEBUG_ESP_WIFI // stay within datasheet range when not in debug mode listenInterval = 10; #endif } if (!wifi_set_listen_interval(listenInterval)) { DEBUG_WIFI_GENERIC("wifi_set_listen_interval(%d): error\n", listenInterval); return false; } } else { if (!wifi_set_sleep_level(MIN_SLEEP_T)) { DEBUG_WIFI_GENERIC("wifi_set_sleep_level(MIN_SLEEP_T): error\n"); return false; } } } bool ret = wifi_set_sleep_type((sleep_type_t) type); if (!ret) { DEBUG_WIFI_GENERIC("wifi_set_sleep_type(%d): error\n", (int)type); } return ret; }