wifi_util.cpp 1.43 KB
Newer Older
1
2
3
4
5
6
7
8
#include "wifi_util.h"

namespace config {
  // WiFi config. See 'config.h' if you want to modify those values.
  const char *wifi_ssid = WIFI_SSID;
  const char *wifi_password = WIFI_PASSWORD;

#ifdef WIFI_TIMEOUT
Eric Duminil's avatar
Eric Duminil committed
9
  const uint8_t wifi_timeout = WIFI_TIMEOUT; // [s] Will try to connect during wifi_timeout seconds before failing.
10
11
12
13
14
15
16
17
18
#else
  const uint8_t wifi_timeout = 60;  // [s] Will try to connect during wifi_timeout seconds before failing.
#endif
}

// Initialize Wi-Fi
void WiFiConnect(const String &hostname) {
  //NOTE: WiFi Multi could allow multiple SSID and passwords.
  WiFi.persistent(false); // Don't write user & password to Flash.
Eric Duminil's avatar
Eric Duminil committed
19
  WiFi.mode(WIFI_STA); // Set ESP8266 to be a WiFi-client only
20
21
#if defined(ESP8266)
    WiFi.hostname(hostname);
Eric Duminil's avatar
Eric Duminil committed
22
#elif defined(ESP32)
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
  WiFi.setHostname(hostname.c_str());
#endif

  Serial.print("\nConnecting to ");
  Serial.println(config::wifi_ssid);
  WiFi.begin(config::wifi_ssid, config::wifi_password);

  // Wait for connection, at most wifi_timeout seconds
  for (int i = 0; i <= config::wifi_timeout && (WiFi.status() != WL_CONNECTED); i++) {
    LedEffects::showRainbowWheel();
    Serial.print(".");
  }
  if (WiFi.status() == WL_CONNECTED) {
    LedEffects::showKITTWheel(color::green);
    Serial.println();
    Serial.print("\nWiFi connected, IP address: ");
    Serial.println(WiFi.localIP());
  } else {
    LedEffects::showKITTWheel(color::red);
    Serial.println("\nConnection to WiFi failed");
  }
}