losungepaper/wifi.ino

38 lines
1.1 KiB
Arduino
Raw Normal View History

2022-08-21 15:28:52 +00:00
#include <WiFi.h>
2022-08-21 17:21:56 +00:00
#include "time.h"
2022-08-21 15:28:52 +00:00
void blinkLED(int pin) {
pinMode(pin, OUTPUT);
delay(500);
digitalWrite(pin,HIGH);
delay(500);
digitalWrite(pin,LOW);
delay(500);
digitalWrite(pin,HIGH);
delay(500);
digitalWrite(pin,LOW);
}
2022-08-22 07:20:39 +00:00
bool connectWiFi() { // connect to the wifi with the above defined credentials
if(WiFi.status() == WL_CONNECTED) { return true; } // return if not connected
2022-08-21 15:28:52 +00:00
2022-08-21 19:12:34 +00:00
Serial.print("[WiFi] Connecting to WiFi...");
2022-08-21 15:28:52 +00:00
WiFi.begin(wiFiSSID, wiFiPSK);
2022-08-21 19:12:34 +00:00
while(WiFi.status() != WL_CONNECTED) {
delay(2000); Serial.printf(".");
if(WiFi.status() == WL_NO_SSID_AVAIL) {
2022-08-22 07:20:39 +00:00
Serial.println("\n[WiFi] Failed to connect to WiFi. Reason: WL_NO_SSID_AVAIL");
blinkLED(LED_ON_WIFI_ERROR);
2022-08-22 07:20:39 +00:00
return false;
2022-08-21 19:12:34 +00:00
} else if(WiFi.status() == WL_CONNECT_FAILED) {
2022-08-22 07:20:39 +00:00
Serial.println("\n[WiFi] Failed to connect to WiFi. Reason: WL_CONNECT_FAILED");
blinkLED(LED_ON_WIFI_ERROR);
2022-08-22 07:20:39 +00:00
return false;
2022-08-21 19:12:34 +00:00
}
}
Serial.printf("\n[WiFi] Connected to WiFi \"%s\", got IP-Adress ", wiFiSSID);
2022-08-21 15:28:52 +00:00
Serial.println(WiFi.localIP());
2022-08-22 07:20:39 +00:00
return true;
2022-08-21 15:28:52 +00:00
}