Esempio n. 1
0
void  WIFI_CONFIG::Safe_Setup()
{
		#ifdef CAPTIVE_PORTAL_FEATURE
		dnsServer.stop();
		delay(100);
		#endif
		WiFi.disconnect();
		//setup Soft AP
		WiFi.mode(WIFI_AP);
		IPAddress local_ip (DEFAULT_IP_VALUE[0],DEFAULT_IP_VALUE[1],DEFAULT_IP_VALUE[2],DEFAULT_IP_VALUE[3]);
		IPAddress gateway (DEFAULT_GATEWAY_VALUE[0],DEFAULT_GATEWAY_VALUE[1],DEFAULT_GATEWAY_VALUE[2],DEFAULT_GATEWAY_VALUE[3]);
		IPAddress subnet (DEFAULT_MASK_VALUE[0],DEFAULT_MASK_VALUE[1],DEFAULT_MASK_VALUE[2],DEFAULT_MASK_VALUE[3]);
		String ssid = FPSTR(DEFAULT_SSID);
		String pwd = FPSTR(DEFAULT_PASSWORD);
		WiFi.softAP(ssid.c_str(),pwd.c_str());
		delay(500);
		WiFi.softAPConfig( local_ip,  gateway,  subnet);
		delay(1000);
		Serial.println(F("M117 Safe mode started"));
}
Esempio n. 2
0
//Read configuration settings and apply them
bool WIFI_CONFIG::Setup()
{
    char pwd[MAX_PASSWORD_LENGTH+1];
    char sbuf[MAX_SSID_LENGTH+1];
    char hostname [MAX_HOSTNAME_LENGTH+1];
    int wstatus;
    IPAddress currentIP;
    byte bflag=0;

    //set the sleep mode
    if (!CONFIG::read_byte(EP_SLEEP_MODE, &bflag )) {
        return false;
    }
    wifi_set_sleep_type ((sleep_type)bflag);
    sleep_mode=bflag;
    //AP or client ?
    if (!CONFIG::read_byte(EP_WIFI_MODE, &bflag ) ||  !CONFIG::read_string(EP_SSID, sbuf , MAX_SSID_LENGTH) ||!CONFIG::read_string(EP_PASSWORD, pwd , MAX_PASSWORD_LENGTH)) {
        return false;
    }
    if (!CONFIG::read_string(EP_HOSTNAME, hostname , MAX_HOSTNAME_LENGTH)) {
        strcpy(hostname,get_default_hostname());
    }
    //disconnect if connected
    WiFi.disconnect();
    //this is AP mode
    if (bflag==AP_MODE) {
        //setup Soft AP
        WiFi.mode(WIFI_AP);
        WiFi.softAP(sbuf, pwd);
        //setup PHY_MODE
        if (!CONFIG::read_byte(EP_PHY_MODE, &bflag )) {
            return false;
        }
        wifi_set_phy_mode((phy_mode)bflag);
        //get current config
        struct softap_config apconfig;
        wifi_softap_get_config(&apconfig);
        //set the chanel
        if (!CONFIG::read_byte(EP_CHANNEL, &bflag )) {
            return false;
        }
        apconfig.channel=bflag;
        //set Authentification type
        if (!CONFIG::read_byte(EP_AUTH_TYPE, &bflag )) {
            return false;
        }
        apconfig.authmode=(AUTH_MODE)bflag;
        //set the visibility of SSID
        if (!CONFIG::read_byte(EP_SSID_VISIBLE, &bflag )) {
            return false;
        }
        apconfig.ssid_hidden=!bflag;
        //no need to add these settings to configuration just use default ones
        apconfig.max_connection=DEFAULT_MAX_CONNECTIONS;
        apconfig.beacon_interval=DEFAULT_BEACON_INTERVAL;
        //apply settings to current and to default
        if (!wifi_softap_set_config(&apconfig) || !wifi_softap_set_config_current(&apconfig)) {
            Serial.println(F("M117 Error Wifi AP!"));
            delay(1000);
        }
    } else {
        //setup station mode
        WiFi.mode(WIFI_STA);
        WiFi.begin(sbuf, pwd);
        delay(500);
        //setup PHY_MODE
        if (!CONFIG::read_byte(EP_PHY_MODE, &bflag )) {
            return false;
        }
        wifi_set_phy_mode((phy_mode)bflag);
        delay(500);
        byte i=0;
        //try to connect
        while (WiFi.status() != WL_CONNECTED && i<40) {
            switch(WiFi.status()) {
            case 1:
                Serial.print(FPSTR(M117_));
                Serial.println(F("No SSID found!"));
                break;

            case 4:
                Serial.print(FPSTR(M117_));
                Serial.println(F("No Connection!"));
                break;

            default:
                Serial.print(FPSTR(M117_));
                Serial.println(F("Connecting..."));
                break;
            }
            delay(500);
            i++;
        }
        if (WiFi.status() != WL_CONNECTED) {
            return false;
        }
        WiFi.hostname(hostname);
    }

    //DHCP or Static IP ?
    if (!CONFIG::read_byte(EP_IP_MODE, &bflag )) {
        return false;
    }
    if (bflag==STATIC_IP_MODE) {
        byte ip_buf[4];
        //get the IP
        if (!CONFIG::read_buffer(EP_IP_VALUE,ip_buf , IP_LENGTH)) {
            return false;
        }
        IPAddress local_ip (ip_buf[0],ip_buf[1],ip_buf[2],ip_buf[3]);
        //get the gateway
        if (!CONFIG::read_buffer(EP_GATEWAY_VALUE,ip_buf , IP_LENGTH)) {
            return false;
        }
        IPAddress gateway (ip_buf[0],ip_buf[1],ip_buf[2],ip_buf[3]);
        //get the mask
        if (!CONFIG::read_buffer(EP_MASK_VALUE,ip_buf , IP_LENGTH)) {
            return false;
        }
        IPAddress subnet (ip_buf[0],ip_buf[1],ip_buf[2],ip_buf[3]);
        //apply according active wifi mode
        if (wifi_get_opmode()==WIFI_AP || wifi_get_opmode()==WIFI_AP_STA) {
            WiFi.softAPConfig( local_ip,  gateway,  subnet);
        } else {
            WiFi.config( local_ip,  gateway,  subnet);
        }
    }
#ifdef MDNS_FEATURE
    // Set up mDNS responder:
    if (!mdns.begin(hostname)) {
        Serial.print(FPSTR(M117_));
        Serial.println(F("Error with mDNS!"));
        delay(1000);
    }
#endif
    //Get IP
    if (wifi_get_opmode()==WIFI_STA) {
        currentIP=WiFi.localIP();
    } else {
        currentIP=WiFi.softAPIP();
    }
    Serial.print(FPSTR(M117_));
    Serial.println(currentIP);
    return true;
}