static int do_wifi(const struct sys_config *cfg) { int result = 1; int gpio = cfg->wifi.ap.trigger_on_gpio; int trigger_ap = 0; wifi_set_opmode_current(STATION_MODE); wifi_station_set_auto_connect(0); wifi_station_disconnect(); if (gpio >= 0) { sj_gpio_set_mode(gpio, GPIO_MODE_INPUT, GPIO_PULL_PULLUP); trigger_ap = sj_gpio_read(gpio) == GPIO_LEVEL_HIGH; } if (!trigger_ap && cfg->wifi.ap.mode == 2 && cfg->wifi.sta.enable) { wifi_set_opmode_current(STATIONAP_MODE); result = sj_wifi_setup_ap(&cfg->wifi.ap) ? sj_wifi_setup_sta(&cfg->wifi.sta) : 0; } else if (!trigger_ap && cfg->wifi.sta.enable) { wifi_set_opmode_current(STATION_MODE); result = sj_wifi_setup_sta(&cfg->wifi.sta); } else if (trigger_ap || cfg->wifi.ap.mode > 0) { wifi_set_opmode_current(SOFTAP_MODE); result = sj_wifi_setup_ap(&cfg->wifi.ap); } else { LOG(LL_WARN, ("No wifi mode specified")); } return result; }
/** * 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; }
// Init function void ICACHE_FLASH_ATTR user_init() { struct station_config station_conf; // Enable GPIO gpio_init(); PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO2_U, FUNC_GPIO2); PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO0_U, FUNC_GPIO0); gpio_output_set(0, 0, (1<<ELGPIO), 0); // Set station mode wifi_set_opmode_current(STATION_MODE); // Set AP settings os_memcpy(&station_conf.ssid, SSID, 32); os_memcpy(&station_conf.password, SSID_PASSWORD, 64); wifi_station_set_config(&station_conf); // Set an event handler for WiFi events wifi_set_event_handler_cb(wifi_callback); // Setup poll and EL timers, but don't start them yet os_timer_disarm(&poll_timer); os_timer_setfn(&poll_timer, poll_timer_callback, NULL); os_timer_disarm(&el_timer); os_timer_setfn(&el_timer, el_timer_callback, NULL); os_printf("user_init() complete!\n\r"); }
int sj_wifi_setup_sta(const char *ssid, const char *pass) { int res; struct station_config stationConf; /* Switch to station mode if not already in it. */ if (wifi_get_opmode() != 0x1) { wifi_set_opmode_current(0x1); } wifi_station_disconnect(); stationConf.bssid_set = 0; strncpy((char *) &stationConf.ssid, ssid, 32); strncpy((char *) &stationConf.password, pass, 64); res = wifi_station_set_config_current(&stationConf); if (!res) { fprintf(stderr, "Failed to set station config\n"); return 0; } res = wifi_station_connect(); if (res) { wifi_setting_up = 1; } return 1; }
void setup_ap(void) { int off = 0; struct ip_info info; struct softap_config cfg; wifi_set_opmode_current(SOFTAP_MODE); memset(&cfg, 0, sizeof(cfg)); strcpy((char *) cfg.ssid, AP_SSID); strcpy((char *) cfg.password, AP_PASS); cfg.ssid_len = strlen((const char *) cfg.ssid); cfg.authmode = strlen((const char *) cfg.password) ? AUTH_WPA2_PSK : AUTH_OPEN; cfg.channel = AP_CHAN; cfg.ssid_hidden = 0; cfg.max_connection = 10; cfg.beacon_interval = 100; /* ms */ printf("Setting up AP '%s' on channel %d\n", cfg.ssid, cfg.channel); wifi_softap_set_config_current(&cfg); wifi_softap_dhcps_stop(); wifi_softap_set_dhcps_offer_option(OFFER_ROUTER, &off); wifi_softap_dhcps_start(); wifi_get_ip_info(SOFTAP_IF, &info); printf("WiFi AP: SSID %s, channel %d, IP " IPSTR "\n", cfg.ssid, cfg.channel, IP2STR(&info.ip)); }
//Init function void ICACHE_FLASH_ATTR user_init(void) { scan_complete = true; // Configure the UART0 and UART1 (TX only) to 9600 uart_init(BIT_RATE_115200,BIT_RATE_115200); //Set station mode wifi_set_opmode_current( STATION_MODE ); //Disarm timer os_timer_disarm(&trilaterate_timer); //Setup timer os_timer_setfn(&trilaterate_timer, (os_timer_func_t *)scanner, NULL); //&trilaterate_timer is callback //delay in ms //0 for once and 1 for repeating //This callback timer value can't be too small as a new scan will start before the previous finishes. //The callback function checks if the scan is complete before starting a new scan. os_timer_arm(&trilaterate_timer, 10, 1); //100Hz //Start os task system_os_task(user_procTask, user_procTaskPrio,user_procTaskQueue, user_procTaskQueueLen); }
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(); }
/*JSON{ "type" : "staticmethod", "class" : "ESP8266WiFi", "name" : "getAccessPoints", "generate" : "jswrap_ESP8266WiFi_getAccessPoints", "params" : [ ["callback","JsVar","Function to call back when access points retrieved."] ] }*/ void jswrap_ESP8266WiFi_getAccessPoints( JsVar *callback //!< Function to call back when access points retrieved. ) { os_printf("> ESP8266WiFi_getAccessPoints\n"); if (callback == NULL || !jsvIsFunction(callback)) { jsExceptionHere(JSET_ERROR, "No callback."); return; } // If we had saved a previous scan callback function, release it. if (g_jsScanCallback != NULL) { jsvUnLock(g_jsScanCallback); } // Save the callback for the scan in the global variable called jsScanCallback. g_jsScanCallback = jsvLockAgainSafe(callback); // Ask the ESP8266 to perform a network scan after first entering // station mode. The network scan will eventually result in a callback // being executed (scanCB) which will contain the results. // Ensure we are in station mode wifi_set_opmode_current(STATION_MODE); // Request a scan of the network calling "scanCB" on completion wifi_station_scan(NULL, scanCB); os_printf("< ESP8266WiFi_getAccessPoints\n"); }
void ap_stop( void ) { if (wifi_get_opmode()!=STATION_MODE) { wifi_set_opmode_current( STATION_MODE ); os_printf("Switched to station mode.\n\r"); board_statusLed2(0); } }
LOCAL void initDone() { wifi_set_opmode_current(STATION_MODE); struct station_config stationConfig; strncpy(stationConfig.ssid, "ur_ssid", 32); strncpy(stationConfig.password, "ur_password", 64); wifi_station_set_config(&stationConfig); wifi_station_connect(); }
// Function that will run after initialization is done, contains information to connect to the WIFI and SNTP settings LOCAL ICACHE_FLASH_ATTR void initDone() { wifi_set_opmode_current(STATION_MODE); struct station_config stationConfig; strncpy(stationConfig.ssid, ur_ssid, 32); strncpy(stationConfig.password, ur_ssid_password, 64); wifi_station_set_config(&stationConfig); wifi_station_connect(); my_sntp_init(); // This will setup the sntp and it seems to take some time. }
LOCAL void initDone() { os_printf("Initialized! \n"); struct station_config config; strncpy(config.ssid, SSID, 32); strncpy(config.password, SSID_PASSWORD, 64); wifi_set_opmode_current(STATION_MODE); wifi_station_set_config_current(&config); wifi_station_connect(); }
/*JSON{ "type" : "staticmethod", "class" : "ESP8266WiFi", "name" : "beAccessPoint", "generate" : "jswrap_ESP8266WiFi_beAccessPoint", "params" : [ ["jsv_ssid", "JsVar", "The network SSID"], ["jsv_password", "JsVar", "The password to allow stations to connect to the access point"] ] }*/ void jswrap_ESP8266WiFi_beAccessPoint( JsVar *jsv_ssid, //!< The network identity that the access point will advertize itself as. JsVar *jsv_password //!< The password a station will need to connect to the access point. ) { os_printf("> jswrap_ESP8266WiFi_beAccessPoint\n"); // Validate that the SSID and password are somewhat useful. if (jsv_ssid == NULL || !jsvIsString(jsv_ssid)) { jsExceptionHere(JSET_ERROR, "No SSID."); return; } // Build our SoftAP configuration details struct softap_config softApConfig; memset(&softApConfig, 0, sizeof(softApConfig)); // If no password has been supplied, then be open. Otherwise, use WPA2 and the // password supplied. Also check that the password is at least 8 characters long. if (jsv_password == NULL || !jsvIsString(jsv_password)) { softApConfig.authmode = AUTH_OPEN; } else { if (jsvGetStringLength(jsv_password) < 8) { jsExceptionHere(JSET_ERROR, "Password must be 8 characters or more in length."); return; } softApConfig.authmode = AUTH_WPA2_PSK; int len = jsvGetString(jsv_password, (char *)softApConfig.password, sizeof(softApConfig.password)-1); softApConfig.password[len]='\0'; } int len = jsvGetString(jsv_ssid, (char *)softApConfig.ssid, sizeof(softApConfig.ssid)-1); softApConfig.ssid[len]='\0'; // Define that we are in Soft AP mode. os_printf("Wifi: switching to soft-AP mode, authmode=%d\n", softApConfig.authmode); wifi_set_opmode_current(SOFTAP_MODE); softApConfig.ssid_len = 0; // Null terminated SSID softApConfig.ssid_hidden = 0; // Not hidden. softApConfig.max_connection = 4; // Maximum number of connections. // Set the WiFi configuration. int rc = wifi_softap_set_config_current(&softApConfig); // We should really check that becoming an access point works, however as of SDK 1.4, we // are finding that if we are currently connected to an access point and we switch to being // an access point, it works ... but returns 1 indicating an error. /* if (rc != 1) { os_printf(" - Error returned from wifi_softap_set_config_current=%d\n", rc); jsExceptionHere(JSET_ERROR, "Error setting ESP8266 softap config."); } */ os_printf("< jswrap_ESP8266WiFi_beAccessPoint\n"); }
static void wifi_connect() { struct station_config conf; wifi_station_set_auto_connect(FALSE); wifi_set_event_handler_cb(wifi_change_cb); strcpy((char *) conf.ssid, NETWORK_SSID); strcpy((char *) conf.password, NETWORK_PWD); printf("connecting to %s\n", conf.ssid); conf.bssid_set = 0; wifi_set_opmode_current(STATION_MODE); wifi_station_disconnect(); wifi_station_set_config_current(&conf); wifi_station_connect(); }
void ESP8266WiFiClass::_mode(WiFiMode m) { if(wifi_get_opmode() == (uint8)m) { return; } ETS_UART_INTR_DISABLE(); if (_persistent) wifi_set_opmode(m); else wifi_set_opmode_current(m); ETS_UART_INTR_ENABLE(); }
void ICACHE_FLASH_ATTR WIFI_Connect(uint8_t* ssid, uint8_t* pass, WifiCallback cb) { struct station_config stationConf; INFO("WIFI_INIT\r\n"); wifi_set_opmode_current(STATION_MODE); wifiCb = cb; os_memset(&stationConf, 0, sizeof(struct station_config)); os_sprintf(stationConf.ssid, "%s", ssid); os_sprintf(stationConf.password, "%s", pass); wifi_station_set_config_current(&stationConf); os_timer_disarm(&WiFiLinker); os_timer_setfn(&WiFiLinker, (os_timer_func_t *)wifi_check_ip, NULL); os_timer_arm(&WiFiLinker, 1000, 0); wifi_station_connect(); }
LOCAL void ICACHE_FLASH_ATTR initDone() { wifi_set_opmode_current(SOFTAP_MODE); struct softap_config config; os_strcpy(config.ssid, "Free WiFi - Coursety @threatbutt"); os_strcpy(config.password, ""); config.ssid_len = 0; config.authmode = AUTH_OPEN; config.ssid_hidden = 0; config.max_connection = 4; wifi_softap_set_config_current(&config); wifi_softap_dhcps_start(); uint8 stationCount = wifi_softap_get_station_num(); struct station_info currentstation; } // End of initDone
//Init function void ICACHE_FLASH_ATTR user_init() { char ssid[32] = "MY_SSID"; char password[64] = "MY_PASS"; struct station_config stationConf; os_printf("Init a\n\r"); uart_init(BIT_RATE_115200, BIT_RATE_115200); gpio16_output_conf(); gpio16_output_set(0); PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDI_U, FUNC_GPIO12); PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTCK_U, FUNC_GPIO13); PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTMS_U, FUNC_GPIO14); gpio_output_set(0, BIT12|BIT13|BIT14, BIT12|BIT13|BIT14, 0); //Set station mode //wifi_set_macaddr(uint8 if_index, uint8 *macaddr) wifi_set_opmode_current( STATION_MODE ); os_memcpy(&stationConf.ssid, ssid, 32); os_memcpy(&stationConf.password, password, 64); stationConf.bssid_set = 0; wifi_station_set_config_current(&stationConf); // wifi_status_led_install (16, uint32 gpio_name, FUNC_GPIO16) os_printf("Init Ok! %d\n\r", wifi_station_get_connect_status()); wifi_station_set_auto_connect(1); wifi_station_connect(); wifi_station_dhcpc_start(); user_server_init(8888); //Start os task system_os_task(loop, user_procTaskPrio,user_procTaskQueue, user_procTaskQueueLen); // system_os_post(user_procTaskPrio, 0, 0 ); }
/** * set new mode * @param m WiFiMode_t */ bool ESP8266WiFiGenericClass::mode(WiFiMode_t m) { if(wifi_get_opmode() == (uint8) m) { return true; } bool ret = false; ETS_UART_INTR_DISABLE(); if(_persistent) { ret = wifi_set_opmode(m); } else { ret = wifi_set_opmode_current(m); } ETS_UART_INTR_ENABLE(); return ret; }
void setup_ap() { struct softap_config cfg; wifi_set_opmode_current(SOFTAP_MODE); memset(&cfg, 0, sizeof(cfg)); strcpy((char *) cfg.ssid, AP_SSID); strcpy((char *) cfg.password, AP_PASS); cfg.ssid_len = strlen((const char *) cfg.ssid); cfg.authmode = strlen((const char *) cfg.password) ? AUTH_WPA2_PSK : AUTH_OPEN; cfg.channel = AP_CHAN; cfg.ssid_hidden = 0; cfg.max_connection = 10; cfg.beacon_interval = 100; /* ms */ LOG(LL_INFO, ("Setting up AP '%s' on channel %d", cfg.ssid, cfg.channel)); wifi_softap_set_config_current(&cfg); }
void IRAM user_init (void) { syscalls_init (); thread_isr_stack_init (); /* set system frequency */ system_update_cpu_freq(ESP8266_CPU_FREQUENCY); /* reinit system timer as microsecond timer */ system_timer_reinit (); /* setup the serial communication */ uart_div_modify(0, UART_CLK_FREQ / STDIO_UART_BAUDRATE); /* once the ETS initialization is done we can start with our code as callback */ system_init_done_cb(system_init); /* keep wifi interface in null mode per default */ wifi_set_opmode_current (0); }
//meant to be called from user-defined preinit() void ESP8266WiFiGenericClass::preinitWiFiOff () { // https://github.com/esp8266/Arduino/issues/2111#issuecomment-224251391 // WiFi.persistent(false); // WiFi.mode(WIFI_OFF); // WiFi.forceSleepBegin(); //WiFi.mode(WIFI_OFF) equivalent: // datasheet: // Set Wi-Fi working mode to Station mode, SoftAP // or Station + SoftAP, and do not update flash // (not persistent) wifi_set_opmode_current(WIFI_OFF); //WiFi.forceSleepBegin(/*default*/0) equivalent: // sleep forever until wifi_fpm_do_wakeup() is called wifi_fpm_set_sleep_type(MODEM_SLEEP_T); wifi_fpm_open(); wifi_fpm_do_sleep(0xFFFFFFF); // use WiFi.forceSleepWake() to wake WiFi up }
static void ICACHE_FLASH_ATTR procTask(os_event_t *events) { system_os_post(procTaskPrio, 0, 0 ); CSTick( 0 ); TickAVRSoftSPI(0); if( events->sig == 0 && events->par == 0 ) { //Idle Event. struct station_config wcfg; char stret[256]; char *stt = &stret[0]; struct ip_info ipi; int stat = wifi_station_get_connect_status(); // printf( "STAT: %d\n", stat ); if( stat == STATION_WRONG_PASSWORD || stat == STATION_NO_AP_FOUND || stat == STATION_CONNECT_FAIL ) { wifi_set_opmode_current( 2 ); stt += ets_sprintf( stt, "Connection failed: %d\n", stat ); uart0_sendStr(stret); } if( stat == STATION_GOT_IP && !printed_ip ) { wifi_station_get_config( &wcfg ); wifi_get_ip_info(0, &ipi); stt += ets_sprintf( stt, "STAT: %d\n", stat ); stt += ets_sprintf( stt, "IP: %d.%d.%d.%d\n", (ipi.ip.addr>>0)&0xff,(ipi.ip.addr>>8)&0xff,(ipi.ip.addr>>16)&0xff,(ipi.ip.addr>>24)&0xff ); stt += ets_sprintf( stt, "NM: %d.%d.%d.%d\n", (ipi.netmask.addr>>0)&0xff,(ipi.netmask.addr>>8)&0xff,(ipi.netmask.addr>>16)&0xff,(ipi.netmask.addr>>24)&0xff ); stt += ets_sprintf( stt, "GW: %d.%d.%d.%d\n", (ipi.gw.addr>>0)&0xff,(ipi.gw.addr>>8)&0xff,(ipi.gw.addr>>16)&0xff,(ipi.gw.addr>>24)&0xff ); stt += ets_sprintf( stt, "WCFG: /%s/%s/\n", wcfg.ssid, wcfg.password ); uart0_sendStr(stret); printed_ip = 1; } }
void ap_start( void ) { if (wifi_get_opmode()<0x03) { wifi_set_opmode_current( STATIONAP_MODE ); struct softap_config softapConf; char* apssid[32]; char sta_mac[6] = {0}; wifi_get_macaddr(STATION_IF, sta_mac); os_sprintf(apssid, "%s_%x%x%x", DEFAULT_AP_SSID, sta_mac[3], sta_mac[4], sta_mac[5]); os_memcpy( &softapConf.ssid, apssid, 32 ); os_memcpy( &softapConf.password, "", 64 ); softapConf.ssid_len=os_strlen( &apssid ); //softapConf.channel= 6; softapConf.authmode= AUTH_OPEN; softapConf.max_connection= 2; softapConf.ssid_hidden= false; wifi_softap_set_config_current(&softapConf); wifi_softap_dhcps_start(); os_printf("Setup network is enabled.\n\r"); board_statusLed2(1); } }
void user_init( void ) { positiondebut = 0; positionfin = 1; positiondebutUDP = 0; positionfinUDP = 0; static struct station_config config; uart_div_modify( 0, UART_CLK_FREQ / ( 115200 ) ); os_printf( "%s\n", __FUNCTION__ ); uart_init(115200,9600); spi_slave_init(HSPI,SPI_BUFF); wifi_station_set_hostname( HOSTNAME ); wifi_set_opmode_current( STATIONAP_MODE ); gpio_init(); spi_gpio_init(); //os_printf sur Uart0 espconn_init() ; uart0_tx_buffer("init\n", 5); config.bssid_set = 1; os_memcpy( &config.ssid, SSID, 14 ); os_memcpy( &config.password, PASSWORD, 41); wifi_station_set_config( &config ); wifi_station_connect(); // wifi_set_event_handler_cb( wifi_callback ); shell_init(); //Démarage du client UDP user_set_station_config_udp(); MessageUDP.Status = E_ID; MessageSPi.Status = E_ID; system_os_task(all_recvTask,PRIO_SPI, all_recvTaskQueue, TASK_LENGHT); ///demo with a task to process the uart data system_os_task(uart_recvTask,PRIO_UART, uart_recvTaskQueue, TASK_LENGHT); // //demo with a task to process the spi }
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; }
int sj_wifi_setup_ap(const struct sys_config_wifi_ap *cfg) { struct softap_config ap_cfg; struct ip_info info; struct dhcps_lease dhcps; size_t pass_len = strlen(cfg->pass); size_t ssid_len = strlen(cfg->ssid); if (ssid_len > sizeof(ap_cfg.ssid) || pass_len > sizeof(ap_cfg.password)) { LOG(LL_ERROR, ("AP SSID or PASS too long")); return 0; } if (pass_len != 0 && pass_len < 8) { /* * If we don't check pwd len here and it will be less than 8 chars * esp will setup _open_ wifi with name ESP_<mac address here> */ LOG(LL_ERROR, ("AP password too short")); return 0; } /* If in STA-only mode, switch to AP. If in AP or AP+STA, keep it. */ if (wifi_get_opmode() == STATION_MODE) { wifi_set_opmode_current(SOFTAP_MODE); } memset(&ap_cfg, 0, sizeof(ap_cfg)); strncpy((char *) ap_cfg.ssid, cfg->ssid, sizeof(ap_cfg.ssid)); strncpy((char *) ap_cfg.password, cfg->pass, sizeof(ap_cfg.password)); ap_cfg.ssid_len = ssid_len; if (pass_len != 0) { ap_cfg.authmode = AUTH_WPA2_PSK; } ap_cfg.channel = cfg->channel; ap_cfg.ssid_hidden = (cfg->hidden != 0); ap_cfg.max_connection = cfg->max_connections; ap_cfg.beacon_interval = 100; /* ms */ LOG(LL_DEBUG, ("Setting up %s on channel %d", ap_cfg.ssid, ap_cfg.channel)); wifi_softap_set_config_current(&ap_cfg); LOG(LL_DEBUG, ("Restarting DHCP server")); wifi_softap_dhcps_stop(); /* * We have to set ESP's IP address explicitly also, GW IP has to be the * same. Using ap_dhcp_start as IP address for ESP */ info.netmask.addr = ipaddr_addr(cfg->netmask); info.ip.addr = ipaddr_addr(cfg->ip); info.gw.addr = ipaddr_addr(cfg->gw); wifi_set_ip_info(SOFTAP_IF, &info); dhcps.enable = 1; dhcps.start_ip.addr = ipaddr_addr(cfg->dhcp_start); dhcps.end_ip.addr = ipaddr_addr(cfg->dhcp_end); wifi_softap_set_dhcps_lease(&dhcps); /* Do not offer self as a router, we're not one. */ { int off = 0; wifi_softap_set_dhcps_offer_option(OFFER_ROUTER, &off); } wifi_softap_dhcps_start(); wifi_get_ip_info(SOFTAP_IF, &info); LOG(LL_INFO, ("WiFi AP: SSID %s, channel %d, IP " IPSTR "", ap_cfg.ssid, ap_cfg.channel, IP2STR(&info.ip))); return 1; }
/*JSON{ "type" : "staticmethod", "class" : "ESP8266WiFi", "name" : "connect", "generate" : "jswrap_ESP8266WiFi_connect", "params" : [ ["ssid","JsVar","The network id of the access point."], ["password","JsVar","The password to the access point"], ["gotIpCallback", "JsVar", "An optional callback invoked when we have an IP"] ] } * * Connect the station to an access point. */ void jswrap_ESP8266WiFi_connect( JsVar *jsv_ssid, //!< The SSID of the access point to connect. JsVar *jsv_password, //!< The password for the access point. JsVar *gotIpCallback //!< The Callback function to be called when we are connected. ) { os_printf("> jswrap_ESP8266WiFi_connect\n"); // Check that the ssid and password values aren't obviously in error. if (jsv_ssid == NULL || !jsvIsString(jsv_ssid)) { jsExceptionHere(JSET_ERROR, "No SSID."); return; } if (jsv_password == NULL || !jsvIsString(jsv_password)) { jsExceptionHere(JSET_ERROR, "No password."); return; } // Check that if a callback function was supplied that we actually have a callback function. if (gotIpCallback != NULL && !jsvIsUndefined(gotIpCallback) && !jsvIsFunction(gotIpCallback)) { gotIpCallback = NULL; jsExceptionHere(JSET_ERROR, "A callback function was supplied that is not a function."); return; } if (jsvIsUndefined(gotIpCallback) || jsvIsNull(gotIpCallback)) { gotIpCallback = NULL; } // Set the global which is the gotIP callback to null but first unlock it. if (g_jsGotIpCallback != NULL) { jsvUnLock(g_jsGotIpCallback); g_jsGotIpCallback = NULL; } // If we have a callback, save it for later invocation. if (gotIpCallback != NULL) { g_jsGotIpCallback = jsvLockAgainSafe(gotIpCallback); } // Debug // os_printf("jsGotIpCallback=%p\n", jsGotIpCallback); // Create strings from the JsVars for the ESP8266 API calls. char ssid[33]; int len = jsvGetString(jsv_ssid, ssid, sizeof(ssid)-1); ssid[len]='\0'; char password[65]; len = jsvGetString(jsv_password, password, sizeof(password)-1); password[len]='\0'; os_printf("> - ssid=%s, password=%s\n", ssid, password); // Set the WiFi mode of the ESP8266 wifi_set_opmode_current(STATION_MODE); struct station_config stationConfig; memset(&stationConfig, 0, sizeof(stationConfig)); os_strncpy((char *)stationConfig.ssid, ssid, 32); if (password != NULL) { os_strncpy((char *)stationConfig.password, password, 64); } else { os_strcpy((char *)stationConfig.password, ""); } // Set the WiFi configuration wifi_station_set_config(&stationConfig); uint8 wifiConnectStatus = wifi_station_get_connect_status(); os_printf(" - Current connect status: %s\n", wifiConnectStatusToString(wifiConnectStatus)); if (wifiConnectStatus == STATION_GOT_IP) { // See issue #618. There are currently three schools of thought on what should happen // when a connect is issued and we are already connected. // // Option #1 - Always perform a disconnect. // Option #2 - Perform a disconnect if the SSID or PASSWORD are different from current // Option #3 - Fail the connect if we are already connected. // #define ISSUE_618 1 #if ISSUE_618 == 1 wifi_station_disconnect(); #elif ISSUE_618 == 2 struct station_config existingConfig; wifi_station_get_config(&existingConfig); if (os_strncmp((char *)existingConfig.ssid, (char *)stationConfig.ssid, 32) == 0 && os_strncmp((char *)existingConfig.password, (char *)stationConfig.password, 64) == 0) { if (jsGotIpCallback != NULL) { JsVar *params[2]; params[0] = jsvNewFromInteger(STATION_GOT_IP); params[1] = jsvNewNull(); jsiQueueEvents(NULL, jsGotIpCallback, params, 2); } return; } else { wifi_station_disconnect(); } #elif ISSUE_618 == 3 // Add a return code to the function and return an already connected error. #endif } // Perform the network level connection. wifi_station_connect(); os_printf("< jswrap_ESP8266WiFi_connect\n"); }
int sj_wifi_disconnect(void) { /* disable any AP mode */ wifi_set_opmode_current(STATION_MODE); return wifi_station_disconnect(); }
/*JSON{ "type" : "staticmethod", "class" : "ESP8266WiFi", "name" : "stopAP", "generate" : "jswrap_ESP8266WiFi_stopAP" } * Stop being an access point. */ void jswrap_ESP8266WiFi_stopAP() { os_printf("Wifi: switching to Station mode."); wifi_set_opmode_current(STATION_MODE); }