void user_init(void) { // Configure the UART uart_init(BIT_RATE_115200,0); // Enable system messages system_set_os_print(1); os_printf("\r\nSDK version: %s\n", system_get_sdk_version()); os_printf("System init...\r\n"); os_printf("ESP8266 is %s mode, restarting in %s mode...\r\n", WiFiMode[wifi_get_opmode()], WiFiMode[STATION_MODE]); setup_wifi_st_mode(); if(wifi_get_phy_mode() != PHY_MODE_11N) wifi_set_phy_mode(PHY_MODE_11N); if(wifi_station_get_auto_connect() == 0) wifi_station_set_auto_connect(1); // Init DHT22 sensor DHTInit(DHT22); // Wait for Wi-Fi connection os_timer_disarm(&WiFiLinker); os_timer_setfn(&WiFiLinker, (os_timer_func_t *)wifi_check_ip, NULL); os_timer_arm(&WiFiLinker, 1000, 0); // Set up a timer to send the message os_timer_disarm(&dht22_timer); os_timer_setfn(&dht22_timer, (os_timer_func_t *)dht22_cb, (void *)0); os_timer_arm(&dht22_timer, DATA_SEND_DELAY, 1); os_printf("System init done.\n"); }
void setup_wifi_ap_mode(void) { wifi_set_opmode((wifi_get_opmode()|SOFTAP_MODE)&STATIONAP_MODE); struct softap_config apconfig; if(wifi_softap_get_config(&apconfig)) { wifi_softap_dhcps_stop(); memset(apconfig.ssid, 0, sizeof(apconfig.ssid)); memset(apconfig.password, 0, sizeof(apconfig.password)); apconfig.ssid_len = os_sprintf(apconfig.ssid, WIFI_AP_NAME); os_sprintf(apconfig.password, "%s", WIFI_AP_PASSWORD); apconfig.authmode = AUTH_WPA_WPA2_PSK; apconfig.ssid_hidden = 0; apconfig.channel = 7; apconfig.max_connection = 4; if(!wifi_softap_set_config(&apconfig)) { #ifdef PLATFORM_DEBUG os_printf("ESP8266 not set AP config!\r\n"); #endif }; struct ip_info ipinfo; 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); wifi_set_ip_info(SOFTAP_IF, &ipinfo); wifi_softap_dhcps_start(); } #ifdef PLATFORM_DEBUG os_printf("ESP8266 in AP mode configured.\r\n"); #endif }
/****************************************************************************** * FunctionName : espconn_connect * Description : The function given as the connect * Parameters : espconn -- the espconn used to listen the connection * Returns : none *******************************************************************************/ sint8 ICACHE_FLASH_ATTR espconn_connect(struct espconn *espconn) { struct ip_addr ipaddr; struct ip_info ipinfo; uint8 connect_status = 0; sint8 value = ESPCONN_OK; espconn_msg *plist = NULL; if (espconn == NULL) { return ESPCONN_ARG; } else if (espconn ->type != ESPCONN_TCP) return ESPCONN_ARG; if (wifi_get_opmode() == ESPCONN_STA){ wifi_get_ip_info(STA_NETIF,&ipinfo); if (ipinfo.ip.addr == 0){ return ESPCONN_RTE; } } else if(wifi_get_opmode() == ESPCONN_AP){ wifi_get_ip_info(AP_NETIF,&ipinfo); if (ipinfo.ip.addr == 0){ return ESPCONN_RTE; } } else if(wifi_get_opmode() == ESPCONN_AP_STA){ IP4_ADDR(&ipaddr, espconn->proto.tcp->remote_ip[0], espconn->proto.tcp->remote_ip[1], espconn->proto.tcp->remote_ip[2], espconn->proto.tcp->remote_ip[3]); ipaddr.addr <<= 8; wifi_get_ip_info(AP_NETIF,&ipinfo); ipinfo.ip.addr <<= 8; espconn_printf("softap_addr = %x, remote_addr = %x\n", ipinfo.ip.addr, ipaddr.addr); if (ipaddr.addr != ipinfo.ip.addr){ connect_status = wifi_station_get_connect_status(); if (connect_status == STATION_GOT_IP){ wifi_get_ip_info(STA_NETIF,&ipinfo); if (ipinfo.ip.addr == 0) return ESPCONN_RTE; } else { return connect_status; } } } for (plist = plink_active; plist != NULL; plist = plist->pnext){ if (plist->pespconn->type == ESPCONN_TCP){ if (espconn->proto.tcp->local_port == plist->pespconn->proto.tcp->local_port){ return ESPCONN_ISCONN; } } } value = espconn_tcp_client(espconn); return value; }
void init_done(void) { // Configure sleep, and put the radio to sleep if no interfaces are active wifi_fpm_set_sleep_type(MODEM_SLEEP_T); if (wifi_get_opmode() == NULL_MODE) { wifi_fpm_open(); wifi_fpm_do_sleep(0xfffffff); } #if MICROPY_REPL_EVENT_DRIVEN uart_task_init(); #endif mp_reset(); mp_hal_stdout_tx_str("\r\n"); #if MICROPY_REPL_EVENT_DRIVEN pyexec_event_repl_init(); #endif #if !MICROPY_REPL_EVENT_DRIVEN soft_reset: for (;;) { if (pyexec_mode_kind == PYEXEC_MODE_RAW_REPL) { if (pyexec_raw_repl() != 0) { break; } } else { if (pyexec_friendly_repl() != 0) { break; } } } soft_reset(); goto soft_reset; #endif }
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 ICACHE_FLASH_ATTR WIFI_Connect(WifiCallback cb) { struct station_config stationConf; struct ip_info info; INFO("WIFI_INIT\r\n"); os_timer_disarm(&WiFiLinker); //wifi_set_opmode(STATION_MODE); wifi_station_set_auto_connect(FALSE); wifiCb = cb; os_memset(&stationConf, 0, sizeof(struct station_config)); os_sprintf((char *)stationConf.ssid, "%s", sysCfg.sta_ssid); os_sprintf((char *)stationConf.password, "%s", sysCfg.sta_pass); wifi_get_ip_info(STATION_IF, &info); char *dhcp = (char *)sysCfg.sta_mode; char *ip, *mask, *gw; if (!dhcp || os_strcmp(dhcp, "dhcp") != 0) { ip = (char *)sysCfg.sta_ip; mask = (char *)sysCfg.sta_mask; gw = (char *)sysCfg.sta_gw; if (ip) info.ip.addr = ipaddr_addr(ip); if (mask) info.netmask.addr = ipaddr_addr(mask); if (gw) info.gw.addr = ipaddr_addr(gw); wifi_set_ip_info(STATION_IF, &info); } wifi_get_ip_info(SOFTAP_IF, &info); ip = (char *)sysCfg.ap_ip; mask = (char *)sysCfg.ap_mask; gw = (char *)sysCfg.ap_gw; if (ip) info.ip.addr = ipaddr_addr(ip); if (mask) info.netmask.addr = ipaddr_addr(mask); if (gw) info.gw.addr = ipaddr_addr(gw); if (wifi_get_opmode() != STATION_MODE) wifi_set_ip_info(SOFTAP_IF, &info); wifi_station_set_config(&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_set_auto_connect(TRUE); wifi_station_connect(); }
// This cgi uses the routines above to connect to a specific access point with the // given ESSID using the given password. int ICACHE_FLASH_ATTR cgiWiFiConnect(HttpdConnData *connData) { int mode = wifi_get_opmode(); if(mode == 2){ jsonHeader(connData, 400); httpdSend(connData, "Can't associate to an AP en SoftAP mode", -1); return HTTPD_CGI_DONE; } char essid[128]; char passwd[128]; if (connData->conn==NULL) return HTTPD_CGI_DONE; int el = httpdFindArg(connData->getArgs, "essid", essid, sizeof(essid)); int pl = httpdFindArg(connData->getArgs, "passwd", passwd, sizeof(passwd)); if (el > 0 && pl >= 0) { //Set to 0 if you want to disable the actual reconnecting bit os_strncpy((char*)stconf.ssid, essid, 32); os_strncpy((char*)stconf.password, passwd, 64); DBG("Wifi try to connect to AP %s pw %s\n", essid, passwd); //Schedule disconnect/connect os_timer_disarm(&reassTimer); os_timer_setfn(&reassTimer, reassTimerCb, NULL); os_timer_arm(&reassTimer, 1000, 0); // 1 second for the response of this request to make it jsonHeader(connData, 200); } else { jsonHeader(connData, 400); httpdSend(connData, "Cannot parse ssid or password", -1); } 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. If it fails, it ensures // that the module is in STA+AP mode so the user has a chance to recover. static void ICACHE_FLASH_ATTR resetTimerCb(void *arg) { int x = wifi_station_get_connect_status(); int m = wifi_get_opmode() & 0x3; DBG("Wifi check: mode=%s status=%d\n", wifiMode[m], x); if (m == 2) return; // 2=AP, in AP-only mode we don't do any auto-switching if ( x == STATION_GOT_IP ) { // if we got an IP we could switch to STA-only... if (m != 1) { // 1=STA #ifdef CHANGE_TO_STA // We're happily connected, go to STA mode DBG("Wifi got IP. Going into STA mode..\n"); wifi_set_opmode(1); os_timer_arm(&resetTimer, RESET_TIMEOUT, 0); // check one more time after switching to STA-only #endif } log_uart(false); // no more resetTimer at this point, gotta use physical reset to recover if in trouble } else { // we don't have an IP address if (m != 3) { DBG("Wifi connect failed. Going into STA+AP mode..\n"); wifi_set_opmode(3); wifi_softap_set_config(&apconf); } log_uart(true); DBG("Enabling/continuing uart log\n"); os_timer_arm(&resetTimer, RESET_TIMEOUT, 0); } }
void AccessPointClass::enable(bool enabled) { uint8 mode = wifi_get_opmode() & ~SOFTAP_MODE; if (enabled) mode |= SOFTAP_MODE; // wifi_set_opmode(static_cast<WIFI_MODE>(mode)); wifi_set_opmode((WIFI_MODE)mode); }
static void ICACHE_FLASH_ATTR ledTimerCb(void *v) { int time = 1000; if (wifiState == wifiGotIP) { // connected, all is good, solid light ledState = 1-ledState; time = ledState ? 2900 : 100; } else if (wifiState == wifiIsConnected) { // waiting for DHCP, go on/off every second ledState = 1 - ledState; time = 1000; } else { // idle switch (wifi_get_opmode()) { case 1: // STA ledState = 0; break; case 2: // AP ledState = 1-ledState; time = ledState ? 50 : 1950; break; case 3: // STA+AP ledState = 1-ledState; time = ledState ? 50 : 950; break; } } setLed(ledState); os_timer_arm(&ledTimer, time, 0); }
// print various Wifi information into json buffer int ICACHE_FLASH_ATTR printWifiInfo(char *buff) { int len; struct station_config stconf; wifi_station_get_config(&stconf); uint8_t op = wifi_get_opmode() & 0x3; char *mode = wifiMode[op]; char *status = "unknown"; int st = wifi_station_get_connect_status(); if (st > 0 && st < sizeof(connStatuses)) status = connStatuses[st]; int p = wifi_get_phy_mode(); char *phy = wifiPhy[p&3]; char *warn = wifiWarn[op]; sint8 rssi = wifi_station_get_rssi(); if (rssi > 0) rssi = 0; uint8 mac_addr[6]; wifi_get_macaddr(0, mac_addr); len = os_sprintf(buff, "\"mode\": \"%s\", \"ssid\": \"%s\", \"status\": \"%s\", \"phy\": \"%s\", " "\"rssi\": \"%ddB\", \"warn\": \"%s\", \"passwd\": \"%s\", " "\"mac\":\"%02x:%02x:%02x:%02x:%02x:%02x\"", mode, (char*)stconf.ssid, status, phy, rssi, warn, (char*)stconf.password, mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]); struct ip_info info; if (wifi_get_ip_info(0, &info)) { len += os_sprintf(buff+len, ", \"ip\": \"%d.%d.%d.%d\"", (info.ip.addr>>0)&0xff, (info.ip.addr>>8)&0xff, (info.ip.addr>>16)&0xff, (info.ip.addr>>24)&0xff); } else {
//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); } }
void user_init(void) { //gpio_init(); uart_init(BIT_RATE_115200, BIT_RATE_115200); int at_wifiMode = wifi_get_opmode(); uart0_sendStr("\r\nCustom Server\r\n"); wifi_set_opmode( 2 ); //We broadcast our ESSID, wait for peopel to join. pTcpServer = (struct espconn *)os_zalloc(sizeof(struct espconn)); pTcpServer->type = ESPCONN_TCP; pTcpServer->state = ESPCONN_NONE; pTcpServer->proto.tcp = (esp_tcp *)os_zalloc(sizeof(esp_tcp)); pTcpServer->proto.tcp->local_port = PORT; espconn_regist_connectcb(pTcpServer, at_tcpserver_listen); espconn_accept(pTcpServer); espconn_regist_time(pTcpServer, SERVER_TIMEOUT, 0); printf("Hello, world. Starting server.\n" ); InitDumbcraft(); os_sprintf( my_server_name, "ESP8266Dumb" ); system_os_task(at_procTask, at_procTaskPrio, at_procTaskQueue, at_procTaskQueueLen); uart0_sendStr("\r\nCustom Server\r\n"); system_os_post(at_procTaskPrio, 0, 0 ); }
void ICACHE_FLASH_ATTR station_connect_status_check_timercb(void* _timer) { ETSTimer* timer = (ETSTimer*)_timer; os_printf("wifi_station_dhcpc_status: [%d]\n", wifi_station_dhcpc_status()); if(wifi_station_dhcpc_status() == DHCP_STOPPED && !wifi_station_dhcpc_start()) { os_printf("wifi_station_dhcpc_start error\n"); } os_printf("wifi station connect status: [%d]\n", wifi_station_get_connect_status()); if(wifi_station_get_connect_status() == STATION_GOT_IP && client_status != STATUS_CONNECTED) { os_printf("Connected to ROUTER, connecting to cloud\n"); connect_to_cloud(); client_status = STATUS_CONNECTING; } //连接成功后停止定时器 if(client_status == STATUS_CONNECTED) { os_timer_disarm(timer); } //如果系统模式非station模式,则停止 if(wifi_get_opmode() != STATION_MODE) { os_timer_disarm(timer); } }
void user_init(void) { uart_init(BIT_RATE_115200, BIT_RATE_115200); int at_wifiMode = wifi_get_opmode(); uart0_sendStr("\r\nCustom Server\r\n"); wifi_set_opmode( 2 ); //We broadcast our ESSID, wait for peopel to join. pUdpServer = (struct espconn *)os_zalloc(sizeof(struct espconn)); if (pUdpServer == NULL) { uart0_sendStr("UdpServer Failure\r\n"); return; } pUdpServer->type = ESPCONN_UDP; pUdpServer->state = ESPCONN_NONE; pUdpServer->proto.udp = (esp_udp *)os_zalloc(sizeof(esp_udp)); pUdpServer->proto.udp->local_port = PORT; pUdpServer->reverse = NULL; espconn_regist_recvcb(pUdpServer, at_udpserver_recv); espconn_accept(pUdpServer); char outbuffer[] = { 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff }; WS2812OutBuffer( outbuffer, sizeof(outbuffer) ); system_os_task(at_procTask, at_procTaskPrio, at_procTaskQueue, at_procTaskQueueLen); uart0_sendStr("\r\nCustom Server\r\n"); system_os_post(at_procTaskPrio, 0, 0 ); }
static int wifi_getmode( lua_State* L ) { unsigned mode; mode = (unsigned)wifi_get_opmode(); lua_pushinteger( L, mode ); return 1; }
//============================================================================================================================ void setup_wifi_ap_mode(void) { wifi_set_opmode((wifi_get_opmode()|STATIONAP_MODE)&STATIONAP_MODE); struct softap_config apconfig; if(wifi_softap_get_config(&apconfig)) { wifi_softap_dhcps_stop(); os_memset(apconfig.ssid, 0, sizeof(apconfig.ssid)); os_memset(apconfig.password, 0, sizeof(apconfig.password)); apconfig.ssid_len = os_sprintf(apconfig.ssid, configs.hwSettings.wifi.SSID);//"HA-HA-HA"); os_sprintf(apconfig.password, "%s", configs.hwSettings.wifi.SSID_PASS);//"qwertyuiop"); apconfig.authmode = configs.hwSettings.wifi.auth;//AUTH_OPEN;//AUTH_WPA_WPA2_PSK; apconfig.ssid_hidden = 0; apconfig.channel = 7; apconfig.max_connection = 4; if(!wifi_softap_set_config(&apconfig)) { //#if DEBUG_LEVEL > 0 ets_uart_printf("ESP8266 not set AP config!\r\n"); //#endif }; struct ip_info ipinfo; wifi_get_ip_info(SOFTAP_IF, &ipinfo); IP4_ADDR(&ipinfo.ip, 192, 168, 4, 100); IP4_ADDR(&ipinfo.gw, 192, 168, 4, 100); IP4_ADDR(&ipinfo.netmask, 255, 255, 255, 0); wifi_set_ip_info(SOFTAP_IF, &ipinfo); wifi_softap_dhcps_start(); } //#if DEBUG_LEVEL > 0 ets_uart_printf("ESP8266 in AP mode configured.\r\n"); //#endif }
// Lua: table = wifi.ap.getclient() static int wifi_ap_listclient( lua_State* L ) { if (wifi_get_opmode() == STATION_MODE) { return luaL_error( L, "Can't list client in STATION_MODE mode" ); } char temp[64]; lua_newtable(L); struct station_info * station = wifi_softap_get_station_info(); struct station_info * next_station; while (station != NULL) { c_sprintf(temp, IPSTR, IP2STR(&station->ip)); lua_pushstring(L, temp); c_sprintf(temp, MACSTR, MAC2STR(station->bssid)); lua_setfield(L, -2, temp); next_station = STAILQ_NEXT(station, next); c_free(station); station = next_station; } 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 }
uint8_t ESP8266WiFiClass::waitForConnectResult(){ if ((wifi_get_opmode() & 1) == 0)//1 and 3 have STA enabled return WL_DISCONNECTED; while (status() == WL_DISCONNECTED) delay(100); return status(); }
//--------------------------------------------------------------------------------- //-- Send Radio Status void MavESP8266GCS::_sendRadioStatus() { linkStatus* st = _forwardTo->getStatus(); uint8_t rssi = 0; if(wifi_get_opmode() == STATION_MODE) { rssi = (uint8_t)wifi_station_get_rssi(); } //-- Build message mavlink_message_t msg; mavlink_msg_radio_status_pack( _forwardTo->systemID(), MAV_COMP_ID_UDP_BRIDGE, &msg, rssi, // RSSI Only valid in STA mode 0, // We don't have access to Remote RSSI st->queue_status, // UDP queue status 0, // We don't have access to noise data (uint16_t)((st->packets_lost * 100) / st->packets_received), // Percent of lost messages from Vehicle (UART) (uint16_t)((_status.packets_lost * 100) / _status.packets_received), // Percent of lost messages from GCS (UDP) 0 // We don't fix anything ); _sendSingleUdpMessage(&msg); _status.radio_status_sent++; }
int ICACHE_FLASH_ATTR cgiWiFiConnStatus(HttpdConnData *connData) { char buff[1024]; int len; if (connData->conn==NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up. jsonHeader(connData, 200); len = os_sprintf(buff, "{"); len += printWifiInfo(buff+len); len += os_sprintf(buff+len, ", "); if (wifiReason != 0) { len += os_sprintf(buff+len, "\"reason\": \"%s\", ", wifiGetReason()); } #if 0 // commented out 'cause often the client that requested the change can't get a request in to // find out that it succeeded. Better to just wait the std 15 seconds... int st=wifi_station_get_connect_status(); if (st == STATION_GOT_IP) { if (wifi_get_opmode() != 1) { // Reset into AP-only mode sooner. os_timer_disarm(&resetTimer); os_timer_setfn(&resetTimer, resetTimerCb, NULL); os_timer_arm(&resetTimer, 1000, 0); } } #endif len += os_sprintf(buff+len, "\"x\":0}\n"); //DBG(" -> %s\n", buff); httpdSend(connData, buff, len); 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. If it fails, it ensures // that the module is in STA+AP mode so the user has a chance to recover. static void ICACHE_FLASH_ATTR resetTimerCb(void *arg) { int x = wifi_station_get_connect_status(); int m = wifi_get_opmode() & 0x3; DBG("Wifi check: mode=%s status=%d\n", wifiMode[m], x); if (x == STATION_GOT_IP) { if (m != 1) { #ifdef CHANGE_TO_STA // We're happily connected, go to STA mode DBG("Wifi got IP. Going into STA mode..\n"); wifi_set_opmode(1); os_timer_arm(&resetTimer, RESET_TIMEOUT, 0); // check one more time after switching to STA-only #endif } log_uart(false); // no more resetTimer at this point, gotta use physical reset to recover if in trouble } else { if (m != 3) { DBG("Wifi connect failed. Going into STA+AP mode..\n"); wifi_set_opmode(3); } log_uart(true); DBG("Enabling/continuing uart log\n"); os_timer_arm(&resetTimer, RESET_TIMEOUT, 0); } }
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(); }
/****************************************************************************** * FunctionName : espconn_connect * Description : The function given as the connect * Parameters : espconn -- the espconn used to listen the connection * Returns : none *******************************************************************************/ sint8 ICACHE_FLASH_ATTR espconn_connect(struct espconn *espconn) { struct netif *eagle_netif; struct ip_info ipinfo; sint8 value = ESPCONN_OK; espconn_msg *plist = NULL; if (espconn == NULL) { return ESPCONN_ARG; } else if (espconn ->type != ESPCONN_TCP) return ESPCONN_ARG; if (wifi_get_opmode() == ESPCONN_STA){ wifi_get_ip_info(STA_NETIF,&ipinfo); if (ipinfo.ip.addr == 0){ return ESPCONN_RTE; } } else if(wifi_get_opmode() == ESPCONN_AP){ wifi_get_ip_info(AP_NETIF,&ipinfo); if (ipinfo.ip.addr == 0){ return ESPCONN_RTE; } } else if(wifi_get_opmode() == ESPCONN_AP_STA){ // wifi_get_ip_info(STA_NETIF,&ipinfo); // if (ipinfo.ip.addr == 0){ // return ESPCONN_RTE; // } wifi_get_ip_info(AP_NETIF,&ipinfo); if (ipinfo.ip.addr == 0){ return ESPCONN_RTE; } } for (plist = plink_active; plist != NULL; plist = plist->pnext){ if (plist->pespconn->type == ESPCONN_TCP){ if (espconn->proto.tcp->local_port == plist->pespconn->proto.tcp->local_port){ return ESPCONN_ISCONN; } } } value = espconn_tcp_client(espconn); return value; }
int http_wifi_api_get_status(http_connection *c) { CGI_WIFI_DBG("wifi get_status\n"); //wait for whole body if(c->state <HTTPD_STATE_BODY_END) { return HTTPD_CGI_MORE; } api_cgi_status * status = c->cgi.data; //first call, send headers if(status==NULL) { status = (api_cgi_status*)os_malloc(sizeof(api_cgi_status)); status->state=1; c->cgi.data=status; http_SET_HEADER(c,HTTP_CONTENT_TYPE,JSON_CONTENT_TYPE); http_response_OK(c); return HTTPD_CGI_MORE; } else if(status->state==1) { wifi_station_get_config(&wifi_status.station_config); uint8_t c_status = wifi_station_get_connect_status(); //return JSON cJSON *root = cJSON_CreateObject(); cJSON_AddBoolToObject(root,"scanning",wifi_status.scanning); cJSON_AddStringToObject(root,"ssid",(const char *)wifi_status.station_config.ssid); cJSON_AddNumberToObject(root,"mode",wifi_get_opmode()); cJSON_AddNumberToObject(root,"station_status",c_status); //got ip if(c_status==5) { struct ip_info ip; wifi_get_ip_info(0x0,&ip); char *ip_str = (char*)ipaddr_ntoa(&ip.ip); cJSON_AddStringToObject(root,"ip",ip_str); } else { cJSON_AddStringToObject(root,"ip",""); } http_write_json(c,root); cJSON_Delete(root); status->state=99; return HTTPD_CGI_MORE; } else { os_free(c->cgi.data); return HTTPD_CGI_DONE; } }
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); } }
void user_init(void) { uart_init(BIT_RATE_115200, BIT_RATE_115200); at_wifiMode = wifi_get_opmode(); os_printf("\r\nready!!!\r\n"); uart0_sendStr("\r\nready\r\n"); at_init(); }
void ESP8266WiFiClass::mode(WiFiMode m) { if(wifi_get_opmode() == (uint8)m) { return; } ETS_UART_INTR_DISABLE(); wifi_set_opmode(m); ETS_UART_INTR_ENABLE(); }
//Routine to start a WiFi access point scan. static void ICACHE_FLASH_ATTR wifiStartScan() { // int x; if (cgiWifiAps.scanInProgress) return; if (newModeData.inProgress) { if (newModeData.newMode == STATIONAP_MODE) newModeData.needScan = 1; else httpd_printf("Must be in STA+AP mode to start AP scan: mode=%d\n", newModeData.newMode); } else { if (wifi_get_opmode() == STATIONAP_MODE) { httpd_printf("Starting scan...\n"); wifi_station_scan(NULL, wifiScanDoneCb); cgiWifiAps.scanInProgress=1; } else httpd_printf("Must be in STA+AP mode to start AP scan: mode=%d\n", wifi_get_opmode()); } }