Exemplo n.º 1
0
ICACHE_FLASH_ATTR static v7_val_t Wifi_setup(struct v7 *v7, v7_val_t this_obj,
                                             v7_val_t args) {
  struct station_config stationConf;
  v7_val_t ssidv = v7_array_get(v7, args, 0);
  v7_val_t passv = v7_array_get(v7, args, 1);
  const char *ssid, *pass;
  size_t ssid_len, pass_len;
  int res;

  if (!v7_is_string(ssidv) || !v7_is_string(passv)) {
    printf("ssid/pass are not strings\n");
    return v7_create_undefined();
  }

  wifi_station_disconnect();

  ssid = v7_to_string(v7, &ssidv, &ssid_len);
  pass = v7_to_string(v7, &passv, &pass_len);

  stationConf.bssid_set = 0;
  strncpy((char *) &stationConf.ssid, ssid, 32);
  strncpy((char *) &stationConf.password, pass, 64);

  res = v7_create_boolean(wifi_station_set_config(&stationConf));
  if (!res) {
    printf("Failed to set station config\n");
    return v7_create_boolean(0);
  }

  return v7_create_boolean(wifi_station_connect());
}
Exemplo n.º 2
0
void ICACHE_FLASH_ATTR smartConfig_done(sc_status status, void *pdata) {
	switch (status) {
	case SC_STATUS_WAIT:
		os_printf("SC_STATUS_WAIT\n");
		break;
	case SC_STATUS_FIND_CHANNEL:
		os_printf("SC_STATUS_FIND_CHANNEL\n");
		break;
	case SC_STATUS_GETTING_SSID_PSWD:
		os_printf("SC_STATUS_GETTING_SSID_PSWD\n");
		break;
	case SC_STATUS_LINK:
		os_printf("SC_STATUS_LINK\n");
		struct station_config *sta_conf = pdata;
		wifi_station_set_config(sta_conf);
		INFOP("Connected to %s (%s) %d", sta_conf->ssid, sta_conf->password, sta_conf->bssid_set);
		strcpy(sysCfg.sta_ssid, sta_conf->ssid);
		strcpy(sysCfg.sta_pwd, sta_conf->password);
		wifi_station_disconnect();
		wifi_station_connect();
		break;
	case SC_STATUS_LINK_OVER:
		os_printf("SC_STATUS_LINK_OVER\n");
		smartconfig_stop();
		checkSmartConfig(SC_HAS_STOPPED);
		sysCfg.sta_ssid;
		break;
	}
}
Exemplo n.º 3
0
/**
  * @brief  Transparent data through ip.
  * @param  arg: no used
  * @retval None
  */
void ICACHE_FLASH_ATTR
at_japChack(void *arg)
{
  static uint8_t chackTime = 0;
  uint8_t japState;

  os_timer_disarm(&at_japDelayChack);
  chackTime++;
  japState = wifi_station_get_connect_status();
  if(japState == STATION_GOT_IP)
  {
    chackTime = 0;
    at_backOk;
    specialAtState = TRUE;
    at_state = at_statIdle;
    return;
  }
  else if(chackTime >= 7)
  {
    wifi_station_disconnect();
    chackTime = 0;
    uart0_sendStr("\r\nFAIL\r\n");
    specialAtState = TRUE;
    at_state = at_statIdle;
    return;
  }
  os_timer_arm(&at_japDelayChack, 2000, 0);

}
Exemplo n.º 4
0
void wifiConnectTimerCb(void *arg)
{
	/* Do we have an IP already? */
	int status = wifi_station_get_connect_status();
	if( status != STATION_GOT_IP)
	{
		/* No, then connect to the WiFi station */
		wifi_station_disconnect();

		os_printf("Trying to connect to %s\n", stconf.ssid );

		/* connect to a WiFi station */
		wifi_station_connect();
	}
	else
	{
		/* yes, then disable the timer WiFi setup timer*/
		os_timer_disarm(&wifi_setup_timer);

		/* enable POST timer in periodic mode */
		os_timer_disarm((ETSTimer*)&post_timer);
		os_timer_setfn((ETSTimer*)&post_timer, (os_timer_func_t *) startHttpRequestTimerCallback, NULL);
		os_timer_arm((ETSTimer*)&post_timer, 10000, 0); // thingspeak needs minimum 15 sec delay between updates
	}
}
Exemplo n.º 5
0
void tcp_disconnected( void *arg )
{
    struct espconn *conn = arg;
    
    os_printf( "%s\n", __FUNCTION__ );
    wifi_station_disconnect();
}
/******************************************************************************
 * FunctionName : esptouch_FailCb
 * Description  : esp-touch fail callback
*******************************************************************************/
void ICACHE_FLASH_ATTR
    esptouch_FailCb(void* data)
{   
    wifi_station_disconnect();
    smartconfig_stop();
    //wifi_set_opmode(STATIONAP_MODE);
    
    SC_INFO("ESP-TOUCH FAIL \r\n");
    os_timer_disarm(&esptouch_tout_t);
    #if LIGHT_DEVICE
    light_shadeStart(HINT_RED,2000,0,1,NULL);
    #endif
    
    SC_INFO("ENABLE LIGHT ACTION(ESP-NOW)");
    os_printf("debug: channel:%d\r\n",wifi_get_channel());
#if ESP_NOW_SUPPORT
    light_EspnowInit();
#endif

#if ESP_MESH_SUPPORT
    SC_INFO("RESTART MESH NOW...\r\n");
    #if LIGHT_DEVICE
    light_hint_stop(HINT_RED);
    #endif
    //user_MeshStart();
    wifi_RestartMeshScan(5000);
#else
	SC_INFO("ESP-TOUCH FAIL, OPEN WEBSERVER NOW");
	mesh_SetSoftap();//check
#endif
}
Exemplo n.º 7
0
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;
}
Exemplo n.º 8
0
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 restoreFactorySettings() {
	wifi_station_disconnect();
	wifi_set_opmode(0x3); //reset to STA+AP mode

	struct softap_config apConfig;
	wifi_softap_get_config(&apConfig);
	os_strncpy((char*)apConfig.ssid, "smartswitch", 18);
	apConfig.authmode = 0; //Disable security
	wifi_softap_set_config(&apConfig);

	struct station_config stconf;
	os_strncpy((char*)stconf.ssid, "", 2);
	os_strncpy((char*)stconf.password, "", 2);
	wifi_station_set_config(&stconf);

	OLED_CLS();
	OLED_Print(2, 0, "RESET", 1);
	os_printf("Reset completed. Restarting system...\n");

	ioOutput(0, GPIO_OUTPUT1);
	ioOutput(0, GPIO_OUTPUT2);

	while (!GPIO_INPUT_GET(GPIO_BUTTON1)) { os_printf("."); };
	while (!GPIO_INPUT_GET(GPIO_BUTTON2)) { os_printf("."); };

	system_restart();
}
Exemplo n.º 10
0
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
}
Exemplo n.º 11
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();
}
Exemplo n.º 12
0
/**
 * @brief Initialize the CPU, the board and the peripherals
 *
 * This function is called by ESP8266 SDK when all system initializations
 * has been finished.
 */
void system_init(void)
{
    LOG_INFO("\nStarting ESP8266 CPU with ID: %08x", system_get_chip_id());
    LOG_INFO("\nSDK Version %s\n\n", system_get_sdk_version());

    /* avoid reconnection all the time */
    wifi_station_disconnect();

    /* set exception handlers */
    init_exceptions ();

    /* init random number generator */
    srand(hwrand());

    /* init flash drive */
    extern void flash_drive_init (void);
    flash_drive_init();

    /* trigger static peripheral initialization */
    periph_init();

    /* trigger board initialization */
    board_init();

    /* print the board config */
    board_print_config();
}
Exemplo n.º 13
0
static void reset_slave() {
	init_slave = 0;
	chackTime = 0;
	ip_connected = 0;
	wifi_station_disconnect();
	os_timer_disarm(&at_japDelayChack);
}
Exemplo n.º 14
0
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;
}
Exemplo n.º 15
0
/**
  * @brief  Transparent data through ip.
  * @param  arg: no used
  * @retval None
  */
static void ICACHE_FLASH_ATTR
at_japChack(void *arg)
{

  uint8_t japState;
  char temp[32];
  os_printf("Checking if we have ip...\r\n");
  os_timer_disarm(&at_japDelayChack);
  chackTime++;
  japState = wifi_station_get_connect_status();
  if(japState == STATION_GOT_IP)
  {
	os_printf("YEAHHH we have ip...\r\n");
    chackTime = 0;
    connect_attempts = 0;
    if(ip_connected == 0) {
    	initer_slave();
    	ip_connected = 1;
    }
  }
  else if(chackTime >= 10)
  {
	ip_connected = 0;
    wifi_station_disconnect();
    os_sprintf(temp,"+CWJAP:%d\r\n",japState);
    os_printf(temp);
    os_printf("\r\nFAIL\r\n");
    reset_slave();
    return;
  }
  //scan_slave();
  os_timer_arm(&at_japDelayChack, 2000, 0);
  os_printf("ip check done\r\n");
}
Exemplo n.º 16
0
/**
  * @brief  Setup commad of join to wifi ap.
  * @param  id: commad id number
  * @param  pPara: AT input param
  * @retval None
  */
static void ICACHE_FLASH_ATTR wifi_connect(char * ssid, char * password, char * bssid)
{
	char temp[64];
	struct station_config stationConf;
	int8_t len;
	connect_attempts++;
	//wifi_station_get_config(&stationConf);
	os_bzero(&stationConf, sizeof(struct station_config));
	os_memcpy(&stationConf.ssid, ssid, os_strlen(ssid));
	//os_memcpy(&stationConf.password, password, os_strlen(password));
	os_memcpy(&stationConf.bssid, bssid, 6);
	stationConf.bssid_set = 1;
    wifi_station_disconnect();
    os_printf("stationConf.ssid: -%s-\r\n", stationConf.ssid);
    os_printf("stationConf.password: -%s-\r\n", stationConf.password);

    ETS_UART_INTR_DISABLE();
    wifi_station_set_config(&stationConf);
    ETS_UART_INTR_ENABLE();
    wifi_station_connect();
    os_timer_disarm(&at_japDelayChack);
    os_timer_setfn(&at_japDelayChack, (os_timer_func_t *)at_japChack, NULL);
    os_timer_arm(&at_japDelayChack, 3000, 0);

}
Exemplo n.º 17
0
bool StationClass::setIP(IPAddress address, IPAddress netmask, IPAddress gateway)
{
	if (System.isReady())
	{
		debugf("IP can be changed only in init() method");
		return false;
	}

	wifi_station_disconnect();
//	wifi_station_dhcpc_stop();
	struct ip_info ipinfo;
	wifi_get_ip_info(STATION_IF, &ipinfo);
	ipinfo.ip = address;
	ipinfo.netmask = netmask;
	ipinfo.gw = gateway;
	if (wifi_set_ip_info(STATION_IF, &ipinfo))
		debugf("AP IP succesfully updated");
	else
	{
		debugf("AP IP can't be updated");
		enableDHCP(true);
	}
	wifi_station_connect();
	//wifi_station_dhcpc_start();
	return true;
}
Exemplo n.º 18
0
/**
  * @brief  Execution commad of quit wifi ap.
  * @param  id: commad id number
  * @retval None
  */
void ICACHE_FLASH_ATTR
at_exeCmdCwqap(uint8_t id)
{
  wifi_station_disconnect();
  mdState = m_wdact;
  at_backOk;
}
Exemplo n.º 19
0
bool ICACHE_FLASH_ATTR checkSmartConfig(enum SmartConfigAction action) {
	static doingSmartConfig = false;

	switch (action) {
	case SC_CHECK:
		break;
	case SC_HAS_STOPPED:
		os_printf("Finished smartConfig\n");
		stopFlash();
		doingSmartConfig = false;
		MQTT_Connect(&mqttClient);
		break;
	case SC_TOGGLE:
		if (doingSmartConfig) {
			os_printf("Stop smartConfig\n");
			stopFlash();
			smartconfig_stop();
			doingSmartConfig = false;
			wifi_station_disconnect();
			wifi_station_connect();
		} else {
			os_printf("Start smartConfig\n");
			MQTT_Disconnect(&mqttClient);
			mqttConnected = false;
			startFlash(100, true);
			doingSmartConfig = true;
			smartconfig_start(smartConfig_done, true);
		}
		break;
	}
	return doingSmartConfig;
}
Exemplo n.º 20
0
void ICACHE_FLASH_ATTR
smartconfig_done(sc_status status, void *pdata)
{
    switch(status) {
        case SC_STATUS_WAIT:
            printf("SC_STATUS_WAIT\n");
            break;
        case SC_STATUS_FIND_CHANNEL:
            printf("SC_STATUS_FIND_CHANNEL\n");
            break;
        case SC_STATUS_GETTING_SSID_PSWD:
            printf("SC_STATUS_GETTING_SSID_PSWD\n");
            break;
        case SC_STATUS_LINK:
            printf("SC_STATUS_LINK\n");
            struct station_config *sta_conf = pdata;
	
	        wifi_station_set_config(sta_conf);
	        wifi_station_disconnect();
	        wifi_station_connect();
            break;
        case SC_STATUS_LINK_OVER:
            printf("SC_STATUS_LINK_OVER\n");
            if (SC_Type == SC_TYPE_ESPTOUCH) {
                uint8 phone_ip[4] = {0};

                memcpy(phone_ip, (uint8*)pdata, 4);
                printf("Phone ip: %d.%d.%d.%d\n",phone_ip[0],phone_ip[1],phone_ip[2],phone_ip[3]);
            }
            smartconfig_stop();
            break;
    }
	
}
Exemplo n.º 21
0
// Lua: wifi.sta.config(ssid, password)
static int wifi_station_config( lua_State* L )
{
  size_t sl, pl;
  struct station_config sta_conf;
  int i;
  const char *ssid = luaL_checklstring( L, 1, &sl );
  if (sl>32 || ssid == NULL)
    return luaL_error( L, "ssid:<32" );
  const char *password = luaL_checklstring( L, 2, &pl );
  if (pl>64 || password == NULL)
    return luaL_error( L, "pwd:<64" );

  c_memset(sta_conf.ssid, 0, 32);
  c_memset(sta_conf.password, 0, 64);
  c_memset(sta_conf.bssid, 0, 6);
  c_memcpy(sta_conf.ssid, ssid, sl);
  c_memcpy(sta_conf.password, password, pl);
  sta_conf.bssid_set = 0;

  NODE_DBG(sta_conf.ssid);
  NODE_DBG(" %d\n", sl);
  NODE_DBG(sta_conf.password);
  NODE_DBG(" %d\n", pl);

  wifi_station_set_config(&sta_conf);
  wifi_station_set_auto_connect(true);
  wifi_station_disconnect();
  wifi_station_connect();
  // station_check_connect(0);
  return 0;  
}
Exemplo n.º 22
0
int ESP8266WiFiClass::disconnect(bool wifioff)
{
    struct station_config conf;
    *conf.ssid = 0;
    *conf.password = 0;
    ETS_UART_INTR_DISABLE();
    if (_persistent)
        wifi_station_set_config(&conf);
    else
        wifi_station_set_config_current(&conf);
    wifi_station_disconnect();
    ETS_UART_INTR_ENABLE();

    if(wifioff) {
        _useClientMode = false;

        if(_useApMode) {
            // turn on AP
            _mode(WIFI_AP);
        } else {
            // turn wifi off
            _mode(WIFI_OFF);
        }
    }

    return 0;
}
Exemplo n.º 23
0
void dns_done( const char *name, ip_addr_t *ipaddr, void *arg )
{
    struct espconn *conn = arg;
    os_printf( "%s\n", __FUNCTION__ );

    if ( ipaddr == NULL)
    {
        os_printf("DNS lookup failed\n");
        wifi_station_disconnect();
    }
    else
    {
        os_printf("Connecting...\n" );
        
        conn->type = ESPCONN_TCP;
        conn->state = ESPCONN_NONE;
        conn->proto.tcp=&threatbutt_tcp;
        conn->proto.tcp->local_port = espconn_port();
        conn->proto.tcp->remote_port = 1234;
        os_memcpy(conn->proto.tcp->remote_ip, &ipaddr->addr, 4 );
        
        espconn_regist_connectcb( conn, tcp_connected );
        //espconn_regist_disconcb( conn, tcp_disconnected );
        
        espconn_connect( conn );
    }
}
Exemplo n.º 24
0
ICACHE_FLASH_ATTR static v7_val_t Wifi_disconnect(struct v7 *v7,
                                                  v7_val_t this_obj,
                                                  v7_val_t args) {
  (void) v7;
  (void) this_obj;
  (void) args;
  return v7_create_boolean(wifi_station_disconnect());
}
Exemplo n.º 25
0
void ESP8266WiFiClass::_smartConfigDone(void* result)
{
    station_config* sta_conf = reinterpret_cast<station_config*>(result);
  
    wifi_station_set_config(sta_conf);
    wifi_station_disconnect();
    wifi_station_connect();
}
Exemplo n.º 26
0
/**
 * will force a disconnect an then start reconnecting to AP
 * @return ok
 */
bool ESP8266WiFiSTAClass::reconnect() {
    if((WiFi.getMode() & WIFI_STA) != 0) {
        if(wifi_station_disconnect()) {
            return wifi_station_connect();
        }
    }
    return false;
}
Exemplo n.º 27
0
void ICACHE_FLASH_ATTR
smartconfig_done(void *data)
{
	struct station_config *sta_conf = data;

	wifi_station_set_config(sta_conf);
	wifi_station_disconnect();
	wifi_station_connect();
}
Exemplo n.º 28
0
// setMode must be called with Client or ClientAndAccessPoint
void STC_FLASHMEM WiFi::configureClient(char const *SSID, char const *securityKey) {
  station_config config = {0};
  f_strcpy((char *)config.ssid, SSID);
  f_strcpy((char *)config.password, securityKey);
  Critical critical;
  wifi_station_disconnect();
  wifi_station_set_config(&config);
  wifi_station_connect();
}
Exemplo n.º 29
0
void ICACHE_FLASH_ATTR user_init(void) {
	stdout_init();
	gpio_init();
	wifi_station_disconnect();
	wifi_station_set_auto_connect(false);
	wifi_station_set_reconnect_policy(false);

	system_init_done_cb(&initDone_cb);
}
Exemplo n.º 30
0
// Callback actually doing reassociation
static void ICACHE_FLASH_ATTR reassTimerCb(void *arg) {
	os_printf("Wifi changing association\n");
	wifi_station_disconnect();
	wifi_station_set_config(&stconf);
	wifi_station_connect();
	// Schedule check
	os_timer_disarm(&resetTimer);
	os_timer_setfn(&resetTimer, resetTimerCb, NULL);
	os_timer_arm(&resetTimer, RESET_TIMEOUT, 0);
}