wifi_util.cpp 2.07 KB
Newer Older
1
2
#include "wifi_util.h"

Eric Duminil's avatar
Eric Duminil committed
3
#include "web_config.h"
4
#include "util.h"
Eric Duminil's avatar
Eric Duminil committed
5
#include "ntp.h"
Eric Duminil's avatar
Eric Duminil committed
6
7
8
#include "led_effects.h"
#include "sensor_console.h"

Eric Duminil's avatar
Eric Duminil committed
9
10
11
12
13
14
#if defined(ESP8266)
#  include <ESP8266WiFi.h>
#elif defined(ESP32)
#  include <WiFi.h>
#endif

Eric Duminil's avatar
Eric Duminil committed
15
16
namespace wifi {
  char local_ip[16]; // "255.255.255.255\0"
Eric Duminil's avatar
Eric Duminil committed
17

Eric Duminil's avatar
Eric Duminil committed
18
19
20
21
  bool connected() {
    return WiFi.status() == WL_CONNECTED;
  }

Eric Duminil's avatar
Doc    
Eric Duminil committed
22
23
24
25
26
27
28
29
30
  /*
   * Connection attempt, called in blocking mode by setup(). This way, LED effects can be shown
   * without needing callbacks, but only during wifi_timeout seconds.
   * If connection fails, access point will be started indefinitely, and corresponding
   * LED effects will be shown during 5 seconds.
   *
   * Afterwards, the non-blocking web_config::update() will be called inside loop, and the ampel
   * can display CO2 levels.
   */
Eric Duminil's avatar
Eric Duminil committed
31
  void tryConnection() {
Eric Duminil's avatar
Doc    
Eric Duminil committed
32
    for (int i = 0; i <= config::wifi_timeout + 5; i++) {
Eric Duminil's avatar
Eric Duminil committed
33
      web_config::update();
34
35
36
      if (WiFi.getMode() == WIFI_AP){
        led_effects::alert(0x1cff68);
      } else if (connected()) {
37
38
39
40
        break;
      } else {
        led_effects::showRainbowWheel();
      }
41
      Serial.print(".");
Eric Duminil's avatar
Eric Duminil committed
42
    }
43
    Serial.println();
Eric Duminil's avatar
Eric Duminil committed
44
45
  }

Eric Duminil's avatar
Eric Duminil committed
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
  void scanNetworks() {
    Serial.println();
    Serial.println(F("WiFi - Scanning..."));
    bool async = false;
    bool showHidden = true;
    int n = WiFi.scanNetworks(async, showHidden);
    for (int i = 0; i < n; ++i) {
      Serial.print(F("  * '"));
      Serial.print(WiFi.SSID(i));
      Serial.print(F("' ("));
      int16_t quality = 2 * (100 + WiFi.RSSI(i));
      Serial.print(util::min(util::max(quality, 0), 100));
      Serial.println(F(" %)"));
    }
    Serial.println(F("Done!"));
    Serial.println();
  }

64
65
66
67
  void showLocalIp() {
    Serial.print(F("WiFi - Local IP : "));
    Serial.println(wifi::local_ip);
    Serial.print(F("WiFi - SSID : "));
Eric Duminil's avatar
Rename    
Eric Duminil committed
68
    Serial.println(config::selected_ssid());
69
  }
Eric Duminil's avatar
Eric Duminil committed
70

Eric Duminil's avatar
Eric Duminil committed
71
  void defineCommands() {
72
73
    sensor_console::defineCommand("wifi_scan", scanNetworks, F("(Scans available WiFi networks)"));
    sensor_console::defineCommand("local_ip", showLocalIp, F("(Displays local IP and current SSID)"));
74
75
  }
}