Exemple #1
0
void softap_start()
{
        char sofap_mac[6] = {0x16, 0x34, 0x56, 0x78, 0x90, 0xab};
        char sta_mac[6] = {0x12, 0x34, 0x56, 0x78, 0x90, 0xab};
        struct ip_info info;

        wifi_set_macaddr(SOFTAP_IF, sofap_mac);
        wifi_set_macaddr(STATION_IF, sta_mac);

        IP4_ADDR(&info.ip, 192, 168, 3, 200);
        IP4_ADDR(&info.gw, 192, 168, 3, 1);
        IP4_ADDR(&info.netmask, 255, 255, 255, 0);
        wifi_set_ip_info(STATION_IF, &info);

        IP4_ADDR(&info.ip, 10, 10, 10, 1);
        IP4_ADDR(&info.gw, 10, 10, 10, 1);
        IP4_ADDR(&info.netmask, 255, 255, 255, 0);
        wifi_set_ip_info(SOFTAP_IF, &info);

	ets_uart_printf("Starting SoftAP\n");
}
 void STC_FLASHMEM IP::configureStatic(WiFi::Network network, char const* IP, char const* netmask, char const* gateway)
 {
     ip_info info;
     info.ip.addr      = ipaddr_addr(APtr<char>(f_strdup(IP)).get());
     info.netmask.addr = ipaddr_addr(APtr<char>(f_strdup(netmask)).get());
     info.gw.addr      = ipaddr_addr(APtr<char>(f_strdup(gateway)).get());
     
     Critical critical;
     wifi_station_dhcpc_stop();
     wifi_softap_dhcps_stop();
     wifi_set_ip_info(network, &info);
 }
// You will have to set the DNS-Server manually later since this will not enable DHCP2
void ESP8266WiFiClass::config(IPAddress local_ip, IPAddress gateway, IPAddress subnet)
{
    struct ip_info info;
    info.ip.addr = static_cast<uint32_t>(local_ip);
    info.gw.addr = static_cast<uint32_t>(gateway);
    info.netmask.addr = static_cast<uint32_t>(subnet);

    wifi_station_dhcpc_stop();
    wifi_set_ip_info(STATION_IF, &info);

    _useStaticIp = true;
}
Exemple #4
0
// Access point IP setup
void ICACHE_FLASH_ATTR setup_ap_ip() {
    struct ip_info ipinfo;
    wifi_softap_dhcps_stop();
    //wifi_get_ip_info(SOFTAP_IF, &ipinfo);
    IP4_ADDR(&ipinfo.ip, 10, 10, 10, 1); // Self IP
    IP4_ADDR(&ipinfo.gw, 10, 10, 10, 1); // Gateway IP (also self)
    IP4_ADDR(&ipinfo.netmask, 255, 255, 255, 0); // Netmask (class C /24)
    //IP4_ADDR(&dhcp_lease.start_ip, 10, 10, 10, 10);
    //IP4_ADDR(&dhcp_lease.end_ip, 10, 10, 10, 100);
    //wifi_softap_set_dhcps_lease(&dhcp_lease);
    wifi_set_ip_info(SOFTAP_IF, &ipinfo);
    wifi_softap_dhcps_start();
    //print("Set IP info");
}
void ESP8266WiFiClass::config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns)
{
    struct ip_info info;
    info.ip.addr = static_cast<uint32_t>(local_ip);
    info.gw.addr = static_cast<uint32_t>(gateway);
    info.netmask.addr = static_cast<uint32_t>(subnet);

    wifi_station_dhcpc_stop();
    wifi_set_ip_info(STATION_IF, &info);

    // Set DNS-Server
    ip_addr_t d;
    d.addr = static_cast<uint32_t>(dns);
    dns_setserver(0,&d);

    _useStaticIp = true;
}
void ICACHE_FLASH_ATTR set_softap_mode()
{
	os_printf("run in wifi_boardcast mode\n");
	if(!wifi_set_opmode(SOFTAP_MODE)) {
		os_printf("wifi set opmode to softap error\n");
		//可以停机了。。。
	}
	os_printf("wifi set opmode softap ok\n");

	struct softap_config apconfig;
	memset(&apconfig, 0, sizeof(struct softap_config));
	os_strcpy(apconfig.ssid, DEFAULT_SSID);
	os_strcpy(apconfig.password, DEFAULT_SSID_PWD);
	apconfig.ssid_len = 0;
	apconfig.authmode = AUTH_WPA_WPA2_PSK;
	apconfig.ssid_hidden = 0;
	apconfig.max_connection = 5;
	apconfig.beacon_interval = 100;

	if (!wifi_softap_set_config(&apconfig)) {
		os_printf("[%s] [%s] ERROR\n", __func__,
				"wifi_softap_set_config");
	}
	os_printf("wifi_softap_set_config success\n");

	struct ip_info ipinfo;

    ipinfo.gw.addr = ipaddr_addr(DEFAULT_GWADDR);
	ipinfo.ip.addr = ipaddr_addr(DEFAULT_GWADDR);
	ipinfo.netmask.addr = ipaddr_addr("255.255.255.0");

	if(!wifi_set_ip_info(SOFTAP_IF, &ipinfo)) {
		os_printf("wifi_set_ip_info error\n");
		//my god...
	}

	struct dhcps_lease please;
	please.start_ip.addr = ipaddr_addr(DHCP_BEGIN_ADDR);
	please.end_ip.addr = ipaddr_addr(DHCP_END_ADDR);

	if(!wifi_softap_set_dhcps_lease(&please)) {
		os_printf("wifi_softap_set_dhcps_lease error\n");
		//unknown...
	}
	os_printf("wifi_softap_dhcps config lease ok\n");
}
LOCAL void ICACHE_FLASH_ATTR setup_ap_mode() {
	wifi_station_disconnect();
	wifi_station_dhcpc_stop();

	wifi_set_opmode(SOFTAP_MODE);

	struct softap_config apconfig;

	if (wifi_softap_get_config(&apconfig)) {
		wifi_softap_dhcps_stop();
		char macaddr[6];
		wifi_get_macaddr(SOFTAP_IF, macaddr);

		os_memset(apconfig.ssid, 0, sizeof(apconfig.ssid));
		os_memset(apconfig.password, 0, sizeof(apconfig.password));
		apconfig.ssid_len = os_sprintf(apconfig.ssid, "MacGyver-IoT_%02x%02x%02x%02x%02x%02x", MAC2STR(macaddr));
		//os_sprintf(apconfig.password, "%02x%02x%02x%02x%02x%02x", MAC2STR(macaddr)); // 18fe349bc6b6
		//os_sprintf(apconfig.password, "test"); // 18fe349bc6b6
		//apconfig.authmode = AUTH_WPA_WPA2_PSK; // AUTH_OPEN
		apconfig.authmode = AUTH_OPEN;
		apconfig.ssid_hidden = 0;
		apconfig.channel = 7;
		apconfig.max_connection = 10;

		if (!wifi_softap_set_config(&apconfig)) {
			// CTRL not set AP config!
		}

		struct ip_info ipinfo;
		if (wifi_get_ip_info(SOFTAP_IF, &ipinfo)) {
			IP4_ADDR(&ipinfo.ip, 192, 168, 4, 1);
			IP4_ADDR(&ipinfo.gw, 192, 168, 4, 1);
			IP4_ADDR(&ipinfo.netmask, 255, 255, 255, 0);
			if (!wifi_set_ip_info(SOFTAP_IF, &ipinfo)) {
				// CTRL not set IP config!
			}
		}

		wifi_softap_dhcps_start();

	}

	if (wifi_get_phy_mode() != PHY_MODE_11N)
		wifi_set_phy_mode(PHY_MODE_11N);

}
Exemple #8
0
bool AccessPointClass::setIP(IPAddress address)
{
	/*
	if (System.isReady())
	{
		debugf("IP can be changed only in init() method");
		return false;
	}
	*/

	wifi_softap_dhcps_stop();
	struct ip_info ipinfo;
	wifi_get_ip_info(SOFTAP_IF, &ipinfo);
	ipinfo.ip = address;
	ipinfo.gw = address;
	IP4_ADDR(&ipinfo.netmask, 255, 255, 255, 0);
	wifi_set_ip_info(SOFTAP_IF, &ipinfo);
	wifi_softap_dhcps_start();
	return true;
}
Exemple #9
0
void setup_wifi_ap_mode(void)
{
wifi_set_opmode((wifi_get_opmode() | SOFTAP_MODE));
//wifi_set_opmode(STATIONAP_MODE);

struct softap_config apconfig;
if(wifi_softap_get_config(&apconfig))
	{
	INFO("In softw AP Setup\r\n");
	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_OPEN; // AUTH_WPA_WPA2_PSK - for protected AP
	apconfig.ssid_hidden = 0;
	apconfig.channel = 7;
	apconfig.max_connection = 4;
	if(!wifi_softap_set_config(&apconfig))
		{
		INFO("\r\n === ESP8266 not set AP config!\r\n");
		}
	else
		INFO("\r == ESP Set to AP Mode for Setup - 192.168.4.1\r");

	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();
	}
else
	{
	INFO("\r\n === Problems setting AP Mode\r\n");
	}

INFO("\r\n === ESP8266 in AP mode configured.\r\n");
}
Exemple #10
0
static void ICACHE_FLASH_ATTR _network_config_address(void) {
#if CONFIG_USE_DHCP
  #error Not implemented yet!
#else
    wifi_set_opmode(STATIONAP_MODE);
    uint8_t ret;
    ret = wifi_station_dhcpc_stop();
    if (!ret)
        os_printf("dhcpc stop failed\r\n");
    ret = ipaddr_aton(CONFIG_NETWORK_IP_ADDRESS, &ip_info.ip);
    if (!ret)
        os_printf("network IP address parse failed\r\n");
    ret = ipaddr_aton(CONFIG_NETWORK_NETMASK, &ip_info.netmask);
    if (!ret)
        os_printf("network mask parse failed\r\n");
    ret = ipaddr_aton(CONFIG_NETWORK_GATEWAY, &ip_info.gw);
    if (!ret)
        os_printf("network gateway parse failed\r\n");
    wifi_set_ip_info(STATION_IF, &ip_info);
#endif
}
Exemple #11
0
void user_init()
{
#ifdef ESP8266_GDBSTUB
	gdbstub_init();
#endif

	//Uncomment the line below if you want to step through the initialization function in the debugger without getting a reset from a watchdog.
	//system_soft_wdt_stop();
	struct ip_info info;
	struct softap_config cfg;
	wifi_softap_get_config(&cfg);
	strcpy((char *)cfg.ssid, "$$com.sysprogs.esp8266.http.ssid$$");
	cfg.ssid_len = strlen((char*)cfg.ssid);
	wifi_softap_set_config_current(&cfg);
	wifi_set_opmode(SOFTAP_MODE);
	
	wifi_softap_dhcps_stop();
	IP4_ADDR(&info.ip, 192, 168, $$com.sysprogs.esp8266.http.subnet$$, 1);
	IP4_ADDR(&info.gw, 192, 168, $$com.sysprogs.esp8266.http.subnet$$, 1);
	IP4_ADDR(&info.netmask, 255, 255, 255, 0);
	wifi_set_ip_info(SOFTAP_IF, &info);
	dhcps_lease_test();
	wifi_softap_dhcps_start();
	
    static esp_tcp httpdTcp;
    httpdTcp.local_port = 80;
    static struct espconn httpdConn = { .type = ESPCONN_TCP, .state = ESPCONN_NONE };
	httpdConn.proto.tcp = &httpdTcp;
    
    static esp_tcp upgradeTcp;
    upgradeTcp.local_port = 88;
    static struct espconn upgradeConn = { .type = ESPCONN_TCP, .state = ESPCONN_NONE };
    upgradeConn.proto.tcp = &upgradeTcp;

	espconn_regist_connectcb(&httpdConn, httpdConnectCb);
	espconn_accept(&httpdConn);
    
    espconn_regist_connectcb(&upgradeConn, upgradeCb);
    espconn_accept(&upgradeConn);
}
Exemple #12
0
// Lua: ip = wifi.xx.setip()
static int wifi_setip( lua_State* L, uint8_t mode )
{
  struct ip_info pTempIp;
  wifi_get_ip_info(mode, &pTempIp);

  if (!lua_istable(L, 1))
    return luaL_error( L, "wrong arg type" );
  uint32_t ip = parse_key(L, "ip");
  if(ip!=0) 
    pTempIp.ip.addr = ip;

  ip = parse_key(L, "netmask");
  if(ip!=0) 
    pTempIp.netmask.addr = ip;

  ip = parse_key(L, "gateway");
  if(ip!=0) 
    pTempIp.gw.addr = ip;
  wifi_station_dhcpc_stop();
  lua_pushboolean(L,wifi_set_ip_info(mode, &pTempIp));
  return 1;  
}
Exemple #13
0
// configure Wifi, specifically DHCP vs static IP address based on flash config
void ICACHE_FLASH_ATTR configWifiIP() {
  if (flashConfig.staticip == 0) {
    // let's DHCP!
    wifi_station_set_hostname(flashConfig.hostname);
    if (wifi_station_dhcpc_status() == DHCP_STARTED)
      wifi_station_dhcpc_stop();
    wifi_station_dhcpc_start();
    DBG("Wifi uses DHCP, hostname=%s\n", flashConfig.hostname);
  } else {
    // no DHCP, we got static network config!
    wifi_station_dhcpc_stop();
    struct ip_info ipi;
    ipi.ip.addr = flashConfig.staticip;
    ipi.netmask.addr = flashConfig.netmask;
    ipi.gw.addr = flashConfig.gateway;
    wifi_set_ip_info(0, &ipi);
    DBG("Wifi uses static IP %d.%d.%d.%d\n", IP2STR(&ipi.ip.addr));
  }
#ifdef DEBUGIP
  debugIP();
#endif
}
Exemple #14
0
void ICACHE_FLASH_ATTR wifi_init() {
	/*** Connect to WiFi ***/
	wifi_set_opmode(STATION_MODE);
	wifi_station_disconnect();

	char ssid[32] = WIFI_SSID;
	char pass[64] = WIFI_PASS;
	struct station_config sta_conf;
	sta_conf.bssid_set = 0;
	os_memcpy(&sta_conf.ssid, ssid, 32);
	os_memcpy(&sta_conf.password, pass, 64);

	wifi_station_set_config(&sta_conf);
	wifi_station_connect();

	/*** Network configuration ***/
	wifi_station_dhcpc_stop();

	struct ip_info ip_conf;
	ip_conf.ip.addr = string_to_ip(IP_ADDR);
	ip_conf.netmask.addr = string_to_ip(IP_SUBNET);
	wifi_set_ip_info(STATION_IF, &ip_conf);
}
Exemple #15
0
void RAMFUNC user_init(void)
{
#ifdef ESP32_GDBSTUB
	gdbstub_init();
#endif

	struct ip_info info;
	struct softap_config cfg;
	wifi_softap_get_config(&cfg);
	strcpy((char *)cfg.ssid, "$$com.sysprogs.esp32.http.ssid$$");
	cfg.ssid_len = strlen((char*)cfg.ssid);
	wifi_softap_set_config_current(&cfg);
	wifi_set_opmode(SOFTAP_MODE);
	
	wifi_softap_dhcps_stop();
	IP4_ADDR(&info.ip, 192, 168, $$com.sysprogs.esp32.http.subnet$$, 1);
	IP4_ADDR(&info.gw, 192, 168, $$com.sysprogs.esp32.http.subnet$$, 1);
	IP4_ADDR(&info.netmask, 255, 255, 255, 0);
	wifi_set_ip_info(SOFTAP_IF, &info);
	dhcps_lease_test();
	wifi_softap_dhcps_start();

	xTaskCreate(ServerTask, "Server", 256, NULL, 2, NULL);
}
Exemple #16
0
/**
 * Configure access point
 * @param local_ip      access point IP
 * @param gateway       gateway IP
 * @param subnet        subnet mask
 */
bool ESP8266WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet) {
    DEBUG_WIFI("[APConfig] local_ip: %s gateway: %s subnet: %s\n", local_ip.toString().c_str(), gateway.toString().c_str(), subnet.toString().c_str());
    if(!WiFi.enableAP(true)) {
        // enable AP failed
        DEBUG_WIFI("[APConfig] enableAP failed!\n");
        return false;
    }
    bool ret = true;

    struct ip_info info;
    info.ip.addr = static_cast<uint32_t>(local_ip);
    info.gw.addr = static_cast<uint32_t>(gateway);
    info.netmask.addr = static_cast<uint32_t>(subnet);

    if(!wifi_softap_dhcps_stop()) {
        DEBUG_WIFI("[APConfig] wifi_softap_dhcps_stop failed!\n");
    }

    if(!wifi_set_ip_info(SOFTAP_IF, &info)) {
        DEBUG_WIFI("[APConfig] wifi_set_ip_info failed!\n");
        ret = false;
    }

    struct dhcps_lease dhcp_lease;
    IPAddress ip = local_ip;
    ip[3] += 99;
    dhcp_lease.start_ip.addr = static_cast<uint32_t>(ip);
    DEBUG_WIFI("[APConfig] DHCP IP start: %s\n", ip.toString().c_str());

    ip[3] += 100;
    dhcp_lease.end_ip.addr = static_cast<uint32_t>(ip);
    DEBUG_WIFI("[APConfig] DHCP IP end: %s\n", ip.toString().c_str());

    if(!wifi_softap_set_dhcps_lease(&dhcp_lease)) {
        DEBUG_WIFI("[APConfig] wifi_set_ip_info failed!\n");
        ret = false;
    }

    // set lease time to 720min --> 12h
    if(!wifi_softap_set_dhcps_lease_time(720)) {
        DEBUG_WIFI("[APConfig] wifi_softap_set_dhcps_lease_time failed!\n");
        ret = false;
    }

    uint8 mode = 1;
    if(!wifi_softap_set_dhcps_offer_option(OFFER_ROUTER, &mode)) {
        DEBUG_WIFI("[APConfig] wifi_softap_set_dhcps_offer_option failed!\n");
        ret = false;
    }

    if(!wifi_softap_dhcps_start()) {
        DEBUG_WIFI("[APConfig] wifi_softap_dhcps_start failed!\n");
        ret = false;
    }

    // check config
    if(wifi_get_ip_info(SOFTAP_IF, &info)) {
        if(info.ip.addr == 0x00000000) {
            DEBUG_WIFI("[APConfig] IP config Invalid?!\n");
            ret = false;
        } else if(local_ip != info.ip.addr) {
            ip = info.ip.addr;
            DEBUG_WIFI("[APConfig] IP config not set correct?! new IP: %s\n", ip.toString().c_str());
            ret = false;
        }
    } else {
        DEBUG_WIFI("[APConfig] wifi_get_ip_info failed!\n");
        ret = false;
    }

    return ret;
}
void ICACHE_FLASH_ATTR user_init()
{
	uart_init(BIT_RATE_115200, BIT_RATE_115200);
	os_delay_us(100);

	#ifdef PLATFORM_DEBUG
	ets_uart_printf("ESP8266 platform starting...\r\n");
	#endif
    
	struct softap_config apConfig;
	struct ip_info ipinfo;
	char ssid[33];
	char password[33];
	char macaddress[17];
	char info[150];

	if(wifi_get_opmode() != SOFTAP_MODE)
	{
		#ifdef PLATFORM_DEBUG
		ets_uart_printf("ESP8266 not in SOFTAP mode, restarting in SOFTAP mode...\r\n");
		#endif
		wifi_set_opmode(SOFTAP_MODE);
		//after esp_iot_sdk_v0.9.2, need not to restart
		//system_restart();
	}

	IP4_ADDR(&ipinfo.ip, 10, 10, 10, 1);
	IP4_ADDR(&ipinfo.gw, 10, 10, 10, 1);
	IP4_ADDR(&ipinfo.netmask, 255, 255, 255, 0);
	wifi_set_ip_info(SOFTAP_IF, &ipinfo);

	wifi_get_macaddr(SOFTAP_IF, macaddr);
	wifi_softap_get_config(&apConfig);
	os_memset(apConfig.ssid, 0, sizeof(apConfig.ssid));
	os_sprintf(ssid, "%s", WIFI_APSSID);
	os_memcpy(apConfig.ssid, ssid, os_strlen(ssid));
	if (wifi_get_opmode() == SOFTAP_MODE) 
	{
		#ifdef WIFI_APWPA
		os_memset(apConfig.password, 0, sizeof(apConfig.password));
		os_sprintf(password, "%s", WIFI_APPASSWORD);
		os_memcpy(apConfig.password, password, os_strlen(password));
		apConfig.authmode = AUTH_WPA_WPA2_PSK;
		#else
		apConfig.authmode = AUTH_OPEN;
		#endif
		apConfig.channel = 7;
		apConfig.max_connection = 255;
		apConfig.ssid_hidden = 0;
		wifi_softap_set_config(&apConfig);
	}

	#ifdef PLATFORM_DEBUG
	if (wifi_get_opmode() == SOFTAP_MODE) 
	{
		wifi_softap_get_config(&apConfig);
		os_sprintf(macaddress, MACSTR, MAC2STR(macaddr));
		os_sprintf(info,"OPMODE: %u, SSID: %s, PASSWORD: %s, CHANNEL: %d, AUTHMODE: %d, MACADDRESS: %s\r\n",
					wifi_get_opmode(),
					apConfig.ssid,
					apConfig.password,
					apConfig.channel,
					apConfig.authmode,
					macaddress);
		ets_uart_printf(info);
	}
	#endif

	BtnInit();

	#ifdef PLATFORM_DEBUG
	ets_uart_printf("ESP8266 platform started!\r\n");
	#endif
}
/******************************************************************************
 * FunctionName : user_esp_platform_init
 * Description  : device parame init based on espressif platform
 * Parameters   : none
 * Returns      : none
*******************************************************************************/
void ICACHE_FLASH_ATTR
user_esp_platform_init(void)
{

	os_sprintf(iot_version,"%s%d.%d.%dt%d(%s)",VERSION_TYPE,IOT_VERSION_MAJOR,\
	IOT_VERSION_MINOR,IOT_VERSION_REVISION,device_type,UPGRADE_FALG);
	os_printf("IOT VERSION = %s\n",iot_version);

	system_param_load(ESP_PARAM_START_SEC, 0, &esp_param, sizeof(esp_param));

	struct rst_info *rtc_info = system_get_rst_info();

	os_printf("reset reason: %x\n", rtc_info->reason);

	if (rtc_info->reason == REASON_WDT_RST ||
		rtc_info->reason == REASON_EXCEPTION_RST ||
		rtc_info->reason == REASON_SOFT_WDT_RST) {
		if (rtc_info->reason == REASON_EXCEPTION_RST) {
			os_printf("Fatal exception (%d):\n", rtc_info->exccause);
		}
		os_printf("epc1=0x%08x, epc2=0x%08x, epc3=0x%08x, excvaddr=0x%08x, depc=0x%08x\n",
				rtc_info->epc1, rtc_info->epc2, rtc_info->epc3, rtc_info->excvaddr, rtc_info->depc);
	}

	/***add by tzx for saving ip_info to avoid dhcp_client start****/
    struct dhcp_client_info dhcp_info;
    struct ip_info sta_info;
    system_rtc_mem_read(64,&dhcp_info,sizeof(struct dhcp_client_info));
	if(dhcp_info.flag == 0x01 ) {
		if (true == wifi_station_dhcpc_status())
		{
			wifi_station_dhcpc_stop();
		}
		sta_info.ip = dhcp_info.ip_addr;
		sta_info.gw = dhcp_info.gw;
		sta_info.netmask = dhcp_info.netmask;
		if ( true != wifi_set_ip_info(STATION_IF,&sta_info)) {
			os_printf("set default ip wrong\n");
		}
	}
    os_memset(&dhcp_info,0,sizeof(struct dhcp_client_info));
    system_rtc_mem_write(64,&dhcp_info,sizeof(struct rst_info));


#if AP_CACHE
    wifi_station_ap_number_set(AP_CACHE_NUMBER);
#endif

#if 0
    {
        char sofap_mac[6] = {0x16, 0x34, 0x56, 0x78, 0x90, 0xab};
        char sta_mac[6] = {0x12, 0x34, 0x56, 0x78, 0x90, 0xab};
        struct ip_info info;

        wifi_set_macaddr(SOFTAP_IF, sofap_mac);
        wifi_set_macaddr(STATION_IF, sta_mac);

        IP4_ADDR(&info.ip, 192, 168, 3, 200);
        IP4_ADDR(&info.gw, 192, 168, 3, 1);
        IP4_ADDR(&info.netmask, 255, 255, 255, 0);
        wifi_set_ip_info(STATION_IF, &info);

        IP4_ADDR(&info.ip, 10, 10, 10, 1);
        IP4_ADDR(&info.gw, 10, 10, 10, 1);
        IP4_ADDR(&info.netmask, 255, 255, 255, 0);
        wifi_set_ip_info(SOFTAP_IF, &info);
    }
#endif

    if (esp_param.activeflag != 1) {
#ifdef SOFTAP_ENCRYPT
        struct softap_config config;
        char password[33];
        char macaddr[6];

        wifi_softap_get_config(&config);
        wifi_get_macaddr(SOFTAP_IF, macaddr);

        os_memset(config.password, 0, sizeof(config.password));
        os_sprintf(password, MACSTR "_%s", MAC2STR(macaddr), PASSWORD);
        os_memcpy(config.password, password, os_strlen(password));
        config.authmode = AUTH_WPA_WPA2_PSK;

        wifi_softap_set_config(&config);
#endif
		
		wifi_station_set_hostname( HOST_NAME );
        wifi_set_opmode(STATION_MODE);
    }

#if SENSOR_DEVICE
    user_sensor_init(esp_param.activeflag);
#endif

#if 0
    if (wifi_get_opmode() != SOFTAP_MODE) {
        os_timer_disarm(&client_timer);
        os_timer_setfn(&client_timer, (os_timer_func_t *)user_esp_platform_check_ip, 1);
        os_timer_arm(&client_timer, 100, 0);
    }
	
	// 2015-12-27 
	single_key[0] = key_init_single( 12, PERIPHS_IO_MUX_MTDI_U, FUNC_GPIO12, NULL, GPIO_INTER);
	keys.key_num = 1;
	keys.single_key = single_key;
	key_init(&keys);
#endif
}
Exemple #19
0
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;
}
void wifi_init() {
	wifi_set_opmode(NULL_MODE); //Next time start up in NULL mode
	wifi_set_opmode_current(NULL_MODE);

	//Initialize station config parameters
	struct station_config stconf;
	memset(&stconf, 0, sizeof(stconf));
	strncpy((char *)stconf.ssid, szWifiSsid, sizeof(stconf.ssid));
	strncpy((char *)stconf.password, szWifiPassword, sizeof(stconf.password));
	stconf.ssid[sizeof(stconf.ssid)-1]='\0';
	stconf.password[sizeof(stconf.password)-1]='\0';

	//Initialize AP config parameters
	struct softap_config apconf;
	memset(&apconf, 0, sizeof(apconf));
	// SSID
	strncpy((char *)apconf.ssid, szWifiSsid, sizeof(apconf.ssid));
	apconf.ssid[sizeof(apconf.ssid)-1]='\0';
	// Password & encryption
	strncpy((char *)apconf.password, szWifiPassword, sizeof(apconf.password));
	apconf.password[sizeof(apconf.password)-1]='\0';
	if (strlen(szWifiPassword) >= 8) {
		apconf.authmode = AUTH_WPA_WPA2_PSK;
	} else {
		// if password <8 characters, don't use password.
		apconf.authmode = AUTH_WEP;
		memset(apconf.password, 0, sizeof(apconf.password));
	}
	apconf.max_connection = 255;
	apconf.beacon_interval = 100;

	wifi_set_opmode_current(nWifiMode);
	wifi_softap_set_config_current(&apconf);
	wifi_station_set_config_current(&stconf);

	struct ip_info ipinfo;
	memset(&ipinfo, 0, sizeof(ipinfo));
	ipinfo.ip.addr = aIP;
	ipinfo.netmask.addr = aNetmask;
	ipinfo.gw.addr = aGateway;

	if (nWifiMode == STATION_MODE) {
		wifi_station_dhcpc_stop();
		wifi_station_set_hostname(szHostname);
		if (bUseDhcp) {
			wifi_station_dhcpc_start();
		} else {
			wifi_set_ip_info(STATION_IF, &ipinfo);
		}
	} else {
		// DHCP Server should be stopped to change IP information
		wifi_softap_dhcps_stop();
		wifi_set_ip_info(SOFTAP_IF, &ipinfo);

		uint32_t aFirstIp = (aIP & aNetmask) | 1;
		uint32_t aLastIp = (aIP & aNetmask) | (-1 & ~aNetmask);
		struct dhcps_lease leases;
		memset(&leases, 0, sizeof(leases));
		// Determine whether the range before or after our IP is bigger
		if (aIP - aFirstIp > aLastIp - aIP) {
			leases.start_ip.addr = aFirstIp;
			leases.end_ip.addr = aIP - 1;
		} else {
			leases.start_ip.addr = aIP + 1;
			leases.end_ip.addr = aLastIp;
		}
		wifi_softap_set_dhcps_lease(&leases);
		uint8_t offer_router = 0;
		wifi_softap_set_dhcps_offer_option(OFFER_ROUTER, &offer_router);
		wifi_softap_dhcps_start();
	}
}
Exemple #21
0
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);

#define DEBUG 1
#ifdef DEBUG
	INFO("SSID: %s\tPWD: %s\tPASS: %s\n", stationConf.ssid, stationConf.password, sysCfg.sta_pass);
#endif
	wifi_get_ip_info(STATION_IF, &info);
	char *dhcp = (char *)sysCfg.sta_mode; 
	char *ip, *mask, *gw;
	if (!dhcp || 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);
	}

	/*
	 * We're not using AP mode in this particular application.
	 *
	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);
	 *
	 * -- End of AP mode --
	 */


	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);
	/*
	 * Reconnect is only in SDK 1.2 and newer.  :-(
	 *
	 * wifi_station_set_reconnect_policy(TRUE);	// After a hint from Pete Scargill.
	 */
	wifi_station_connect();
}
Exemple #22
0
/**
 * Change IP configuration settings disabling the dhcp client
 * @param local_ip   Static ip configuration
 * @param gateway    Static gateway configuration
 * @param subnet     Static Subnet mask
 * @param dns1       Static DNS server 1
 * @param dns2       Static DNS server 2
 */
bool ESP8266WiFiSTAClass::config(IPAddress local_ip, IPAddress arg1, IPAddress arg2, IPAddress arg3, IPAddress dns2) {

  if(!WiFi.enableSTA(true)) {
      return false;
  }

  //ESP argument order is: ip, gateway, subnet, dns1
  //Arduino arg order is:  ip, dns, gateway, subnet.

  //first, check whether dhcp should be used, which is when ip == 0 && gateway == 0 && subnet == 0.
  bool espOrderUseDHCP = (local_ip == 0U && arg1 == 0U && arg2 == 0U);
  bool arduinoOrderUseDHCP = (local_ip == 0U && arg2 == 0U && arg3 == 0U);
  if (espOrderUseDHCP || arduinoOrderUseDHCP) {
      _useStaticIp = false;
      wifi_station_dhcpc_start();
      return true;
  }

  //To allow compatibility, check first octet of 3rd arg. If 255, interpret as ESP order, otherwise Arduino order.
  IPAddress gateway = arg1;
  IPAddress subnet = arg2;
  IPAddress dns1 = arg3;

  if(subnet[0] != 255)
  {
    //octet is not 255 => interpret as Arduino order
    gateway = arg2;
    subnet = arg3[0] == 0 ? IPAddress(255,255,255,0) : arg3; //arg order is arduino and 4th arg not given => assign it arduino default
    dns1 = arg1;
  }

  // check whether all is IPv4 (or gateway not set)
  if (!(local_ip.isV4() && subnet.isV4() && (!gateway.isSet() || gateway.isV4()))) {
    return false;
  }

  //ip and gateway must be in the same subnet
  if((local_ip.v4() & subnet.v4()) != (gateway.v4() & subnet.v4())) {
    return false;
  }

  struct ip_info info;
  info.ip.addr = local_ip.v4();
  info.gw.addr = gateway.v4();
  info.netmask.addr = subnet.v4();

  wifi_station_dhcpc_stop();
  if(wifi_set_ip_info(STATION_IF, &info)) {
      _useStaticIp = true;
  } else {
      return false;
  }

  if(dns1.isSet()) {
      // Set DNS1-Server
      dns_setserver(0, dns1);
  }

  if(dns2.isSet()) {
      // Set DNS2-Server
      dns_setserver(1, dns2);
  }

  return true;
}
void ICACHE_FLASH_ATTR settings_apply( userSettings_t *settings )
{
  //Description
  if (!((settings->magic==0x42)&&(settings->version==SETTINGS_VERSION))) {
    os_printf("<ERROR: CAN NOT APPLY CORRUPT SETTINGS!>\n\r");
    return;
  }
  
  if (settings->connection_successful) {
    ap_stop();
  } else {
    ap_start();
  }
  
  //Device information
  wifi_station_set_hostname(settings->name);
  
  //Network
  struct station_config stationConf;
  wifi_station_get_config( &stationConf );
  stationConf.bssid_set = 0;
  os_memcpy( &stationConf.ssid, settings->ssid, 32 );
  os_memcpy( &stationConf.password, settings->password, 64 );
  wifi_station_set_config_current( &stationConf );
  os_printf("########################## Wifi config set to SSID '%s' and PASSWORD '%s'.\n\r", settings->ssid, settings->password);
  if (settings->enable_dhcp) {
    wifi_station_dhcpc_start();
  } else {
    wifi_station_dhcpc_stop();
    struct ip_info dhcpc_ip_info;
    IP4_ADDR(&dhcpc_ip_info.ip, settings->static_ip_1, settings->static_ip_2, settings->static_ip_3, settings->static_ip_4);
    IP4_ADDR(&dhcpc_ip_info.gw, settings->static_gateway_1, settings->static_gateway_2, settings->static_gateway_3, settings->static_gateway_4);
    IP4_ADDR(&dhcpc_ip_info.netmask, settings->static_netmask_1, settings->static_netmask_2, settings->static_netmask_3, settings->static_netmask_4);
    wifi_set_ip_info(STATION_IF, &dhcpc_ip_info);
  }
  wifi_station_disconnect();
  
  //MDNS
  if (settings->enable_mdns)
  {
    /*wifi_set_broadcast_if(STATIONAP_MODE);
    struct mdns_info *info = (struct mdns_info *) os_zalloc(sizeof(struct mdns_info));
    info->host_name = settings->mdns_hostname;
    info->ipAddr = station_ipconfig.ip.addr; //ESP8266 station IP
    info->server_name = "FirmwaRe MDNS";
    info->server_port = 80;
    info->txt_data[0] = “version = now”;
    info->txt_data[1] = “user1 = data1”;
    info->txt_data[2] = “user2 = data2”;
    espconn_mdns_init(info);*/
  }
  
  //NTP
  if (settings->enable_ntp)
  {
    //settings->ntpserver
    //settings->timezone
    //settings->enable_summertime
  }
  
  //Security
  /* settings->password; */

  //Digital outputs
  #if OUTPUT1>-1
    if (settings->bootstate&1){
      board_setOutput(OUTPUT1, true);
    } else {
      board_setOutput(OUTPUT1, false);
    }
  #endif
  #if OUTPUT2>-1
    if (settings->bootstate&2) {
      board_setOutput(OUTPUT2, true);
    } else {
      board_setOutput(OUTPUT2, false);
    }
  #endif
  #if OUTPUT3>-1
    if (settings->bootstate&4) {
      board_setOutput(OUTPUT3, true);
    } else {
      board_setOutput(OUTPUT3, false);
    }
  #endif
  #if OUTPUT4>-1
    if (settings->bootstate&8) {
      board_setOutput(OUTPUT4, true);
    } else {
      board_setOutput(OUTPUT4, false);
    }
  #endif
  #if OUTPUT5>-1
    if (settings->bootstate&16) {
      board_setOutput(OUTPUT5, true);
    } else {
      board_setOutput(OUTPUT5, false);
    }
  #endif
  #if OUTPUT6>-1
    if (settings->bootstate&32) {
      board_setOutput(OUTPUT6, true);
    } else {
      board_setOutput(OUTPUT6, false);
    }
  #endif
  #if OUTPUT7>-1
    if (settings->bootstate&64) {
      board_setOutput(OUTPUT7, true);
    } else {
      board_setOutput(OUTPUT7, false);
    }
  #endif
  #if OUTPUT8>-1
    if (settings->bootstate&128) {
      board_setOutput(OUTPUT8, true);
    } else {
      board_setOutput(OUTPUT8, false);
    }
  #endif

  //ESPLight
  setPWM(settings->bootstate_R, settings->bootstate_G, settings->bootstate_B);
  //settings->ledstrip_type;
  //settings->ledstrip_length;
  
  //PKA
  /* settings->pka_wb settings->pka_wb_time */
}