123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410 |
- int WlanStatus() {
- switch (WiFi.status()) {
- case WL_CONNECTED:
- # if _MONITOR>=1
- if ( debug>=1 ) {
- mPrint("A WlanStatus:: CONNECTED to " + String(WiFi.SSID()));
- }
- # endif
- WiFi.setAutoReconnect(true);
- return(1);
- break;
-
-
- case WL_DISCONNECTED:
- # if _MONITOR>=1
- if ( debug>=0 ) {
- mPrint("A WlanStatus:: DISCONNECTED, IP=" + String(WiFi.localIP().toString()));
- }
- # endif
-
-
- delay(1);
-
- return(0);
- break;
-
- case WL_IDLE_STATUS:
- # if _MONITOR>=1
- if ( debug>=0 ) {
- mPrint("A WlanStatus:: IDLE");
- }
- # endif
- break;
-
-
-
- case WL_NO_SSID_AVAIL:
- # if _MONITOR>=1
- if ( debug>=0 )
- mPrint("WlanStatus:: NO SSID");
- # endif
- break;
-
- case WL_CONNECT_FAILED:
- # if _MONITOR>=1
- if ( debug>=0 )
- mPrint("A WlanStatus:: FAILED");
- # endif
- break;
-
-
- case WL_SCAN_COMPLETED:
- # if _MONITOR>=1
- if ( debug>=0 )
- mPrint("A WlanStatus:: SCAN COMPLETE");
- # endif
- break;
-
-
- case WL_CONNECTION_LOST:
- # if _MONITOR>=1
- if ( debug>=0 )
- mPrint("A WlanStatus:: Connection LOST");
- # endif
- break;
-
-
-
- case WL_NO_SHIELD:
- # if _MONITOR>=1
- if ( debug>=0 )
- Serial.println(F("A WlanStatus:: WL_NO_SHIELD"));
- # endif
- break;
-
- default:
- # if _MONITOR>=1
- if ( debug>=0 ) {
- mPrint("A WlanStatus Error:: code=" + String(WiFi.status()));
- }
- # endif
- break;
- }
- return(-1);
-
- }
- int WlanReadWpa() {
-
- readConfig(CONFIGFILE, &gwayConfig);
- if (gwayConfig.sf != (uint8_t) 0) sf = (sf_t) gwayConfig.sf;
- ifreq = gwayConfig.ch;
- debug = gwayConfig.debug;
- pdebug = gwayConfig.pdebug;
- _cad = gwayConfig.cad;
- _hop = gwayConfig.hop;
- gwayConfig.boots++;
-
- #if GATEWAYNODE==1
- if (gwayConfig.fcnt != (uint8_t) 0) frameCount = gwayConfig.fcnt+10;
- #endif
-
- #if _WIFIMANAGER==1
- String ssid=gwayConfig.ssid;
- String pass=gwayConfig.pass;
- char ssidBuf[ssid.length()+1];
- ssid.toCharArray(ssidBuf,ssid.length()+1);
- char passBuf[pass.length()+1];
- pass.toCharArray(passBuf,pass.length()+1);
- Serial.print(F("WlanReadWpa: ")); Serial.print(ssidBuf); Serial.print(F(", ")); Serial.println(passBuf);
-
- strcpy(wpa[0].login, ssidBuf);
- strcpy(wpa[0].passw, passBuf);
-
- Serial.print(F("WlanReadWpa: <"));
- Serial.print(wpa[0].login);
- Serial.print(F(">, <"));
- Serial.print(wpa[0].passw);
- Serial.println(F(">"));
- #endif
- }
- #if _WIFIMANAGER==1
- int WlanWriteWpa( char* ssid, char *pass) {
- # if _MONITOR>=1
- if (( debug >=0 ) && ( pdebug & P_MAIN )) {
- mPrint("WlanWriteWpa:: ssid="+mPrint(ssid)+", pass="+mPrint(pass));
- }
- # endif
-
- String s((char *) ssid);
- gwayConfig.ssid = s;
-
- String p((char *) pass);
- gwayConfig.pass = p;
- # if GATEWAYNODE==1
- gwayConfig.fcnt = frameCount;
- # endif
- gwayConfig.ch = ifreq;
- gwayConfig.sf = sf;
- gwayConfig.cad = _cad;
- gwayConfig.hop = _hop;
-
- writeConfig( CONFIGFILE, &gwayConfig);
- return 1;
- }
- #endif
- int WlanConnect(int maxTry) {
-
- #if _WIFIMANAGER==1
- WiFiManager wifiManager;
- #endif
- unsigned char agains = 0;
- unsigned char wpa_index = (_WIFIMANAGER >0 ? 0 : 1);
-
-
-
- if (maxTry==0) {
- # if _MONITOR>=1
- mPrint("WlanConnect:: Init para 0");
- # endif
- WiFi.persistent(false);
- WiFi.mode(WIFI_OFF);
- if (gwayConfig.ssid.length() >0) {
- WiFi.begin(gwayConfig.ssid.c_str(), gwayConfig.pass.c_str());
- delay(100);
- }
- }
-
-
-
-
- int i=0;
- while ( (WiFi.status() != WL_CONNECTED) && ( i<= maxTry ) )
- {
-
- for (int j=wpa_index; (j< (sizeof(wpa)/sizeof(wpa[0]))) && (WiFi.status() != WL_CONNECTED ); j++)
- {
-
- char *ssid = wpa[j].login;
- char *password = wpa[j].passw;
- #if _DUSB>=1
- if (debug>=0) {
- Serial.print(i);
- Serial.print(':');
- Serial.print(j);
- Serial.print(':');
- Serial.print(sizeof(wpa)/sizeof(wpa[0]));
- Serial.print(F(". WiFi connect SSID="));
- Serial.print(ssid);
- if ( debug>=1 ) {
- Serial.print(F(", pass="));
- Serial.print(password);
- }
- Serial.println();
- }
- #endif
-
- gwayConfig.wifis++;
- WiFi.mode(WIFI_STA);
- delay(1000);
- WiFi.begin(ssid, password);
- delay(8000);
-
-
-
-
-
- int stat = WlanStatus();
- if ( stat == 1) {
- writeGwayCfg(CONFIGFILE);
- return(1);
- }
-
-
-
- agains=1;
- while (((WiFi.status()) != WL_CONNECTED) && (agains < 10)) {
- agains++;
- delay(agains*500);
- # if _DUSB>=1
- if ( debug>=0 ) {
- Serial.print(".");
- }
- # endif
- }
- # if _DUSB>=1
- Serial.println();
- # endif
-
-
-
- WiFi.persistent(false);
- WiFi.mode(WIFI_OFF);
- }
-
- i++;
- }
-
-
-
- if (WiFi.status() != WL_CONNECTED) {
- #if _WIFIMANAGER==1
- # if _MONITOR>=1
- if (debug>=1) {
- mPrint("Starting Access Point Mode"));
- mPrint("Connect Wifi to accesspoint: "+mPrint(AP_NAME)+" and connect to IP: 192.168.4.1");
- }
- # endif
- wifiManager.autoConnect(AP_NAME, AP_PASSWD );
-
-
-
-
-
-
-
- struct station_config sta_conf;
- wifi_station_get_config(&sta_conf);
-
- WlanWriteWpa((char *)sta_conf.ssid, (char *)sta_conf.password);
- #else
- # if _MONITOR>=1
- if (debug>=0) {
- mPrint("WlanConnect:: Not connected, WLAN retry="+String(i)+", stat="+String(WiFi.status()) );
- }
- # endif
- return(-1);
- #endif
- }
- yield();
- return(1);
- }
- IPAddress resolveHost(String svrName)
- {
- IPAddress svrIP;
-
- # if MONITOR>=1
- mPrint("Server " + String(svrName));
- # endif
- if (svrName.endsWith(".local")) {
- # if ESP32_ARCH==1
- svrName=svrName.substring(0,svrName.length()-6);
- svrIP = MDNS.queryHost(svrName);
- for (byte i=0; i<5; i++) {
- svrIP = MDNS.queryHost(svrName);
- if (svrIP.toString() != "0.0.0.0") break;
- # if (_MONITOR>=1)
- mPrint("ReTrying to resolve with mDNS");
- # endif
- die("resolveHost:: ERROR hostByName .local");
- delay(1000);
- }
- # else
- char cc[svrName.length() +1 ];
- strcpy(cc, svrName.c_str());
- if (!WiFi.hostByName(cc, svrIP))
- {
- die("resolveHost:: ERROR hostByName="+ String(cc));
- };
- # endif
- }
- else
- {
- char cc[svrName.length() +1 ];
- strcpy(cc, svrName.c_str());
- if (!WiFi.hostByName(cc, svrIP))
- {
- die("resolveHost:: ERROR hostByName="+ String(cc));
- };
- }
- return svrIP;
- }
|