wifi_util.cpp 1.53 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.
19
  WiFi.mode(WIFI_STA); // Set ESP 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
  WiFi.setHostname(hostname.c_str());
#endif

Eric Duminil's avatar
Eric Duminil committed
26
  Serial.print(F("WiFi - Connecting to "));
27
28
29
30
31
  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++) {
32
    led_effects::showRainbowWheel();
33
34
35
    Serial.print(".");
  }
  if (WiFi.status() == WL_CONNECTED) {
36
    led_effects::showKITTWheel(color::green);
37
    Serial.println();
Eric Duminil's avatar
Eric Duminil committed
38
    Serial.print(F("WiFi - Connected! IP address: "));
39
40
    Serial.println(WiFi.localIP());
  } else {
Eric Duminil's avatar
TODO    
Eric Duminil committed
41
    //TODO: Allow sensor to work as an Access Point, in order to define SSID & password?
42
    led_effects::showKITTWheel(color::red);
Eric Duminil's avatar
Eric Duminil committed
43
    Serial.println(F("Connection to WiFi failed"));
44
45
  }
}