#include "wifi_util.h" #include "web_config.h" #include "util.h" #include "ntp.h" #include "led_effects.h" #include "sensor_console.h" #if defined(ESP8266) # include #elif defined(ESP32) # include #endif namespace wifi { char local_ip[16]; // "255.255.255.255\0" bool connected() { return WiFi.status() == WL_CONNECTED; } bool isAccessPoint() { return WiFi.getMode() == WIFI_AP; } /* * 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. */ void tryConnection() { for (int i = 0; i <= config::wifi_timeout + 5; i++) { web_config::update(); sensor_console::checkSerialInput(); // To allow reset or ssid ... during startup if (isAccessPoint()) { led_effects::alert(color::turquoise); } else if (connected()) { break; } else { led_effects::showRainbowWheel(); } Serial.print("."); } Serial.println(); } 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(); } void showLocalIp() { Serial.print(F("WiFi - Local IP : ")); Serial.println(wifi::local_ip); Serial.print(F("WiFi - SSID : ")); Serial.println(config::selected_ssid()); } void defineCommands() { sensor_console::defineCommand("wifi_scan", scanNetworks, F("(Scans available WiFi networks)")); sensor_console::defineCommand("local_ip", showLocalIp, F("(Displays local IP and current SSID)")); //TODO: Add "update!" command? https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266httpUpdate/examples/httpUpdate/httpUpdate.ino } }