Created
December 9, 2018 09:22
-
-
Save artistio/05e6a525096097785ab02c9f8676afd3 to your computer and use it in GitHub Desktop.
Fungsi wrapper untuk memulai koneksi WiFi di NodeMCU
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* File startWiFi.ino | |
* | |
* int startWiFi(char* ssid, char* pass) | |
* | |
* Fungsi: Memulai koneksi ke WiFi sesuai dengan ssid dan pass | |
* | |
* Return Value: | |
* - 0: Koneksi sukses. Hasil koneksi akan terbaca di Serial Monitor | |
* - 1: Koneksi gagal. Hasil koneksi akan terbaca di Serial Monitor | |
* | |
* Hak cipta (c) 2016 x.benny.id. | |
* https://x.benny.id | |
* | |
* Lisensi Creative Commons Atribusi-NonKomersial-BerbagiSerupa 4.0 Internasional | |
*/ | |
int startWiFi(char* ssid, char* pass) { | |
int waktuTunggu = 5; // Waktu tunggu koneksi WiFi dalam detik | |
WiFi.mode(WIFI_STA); | |
WiFi.begin(ssid, pass); | |
unsigned long wifiConnectStart = millis(); | |
while (WiFi.status() != WL_CONNECTED) { | |
// Check to see if | |
if (WiFi.status() == WL_CONNECT_FAILED) { | |
// Koneksi gagal karena SSID dan Password tidah sesuai | |
Serial.println("ERROR: Koneksi WiFi gagal. Mohon periksa konfigurasi WiFi: "); | |
Serial.println(); | |
Serial.print("SSID: "); | |
Serial.println(ssid); | |
Serial.print("Password: "); | |
Serial.println(pass); | |
Serial.println(); | |
return(1); | |
} | |
delay(500); | |
Serial.print("Status Koneksi WiFi: "); | |
Serial.println(WiFi.status()); | |
if(millis() - wifiConnectStart > (waktuTunggu * 1000)) { | |
// Koneksi gagal setelah waktu tunggu | |
Serial.println("Koneksi Gagal, WiFi Access Point tidak merespon"); | |
Serial.println("Mohon ulangi lagi"); | |
return(1); | |
} | |
} | |
// Koneksi sukses | |
Serial.println(); | |
Serial.print("Terhubung dengan WiFi "); | |
Serial.println(ssid); | |
Serial.print("IP address: "); | |
Serial.println(WiFi.localIP()); | |
return(0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment