Commit 18a805bc authored by Eric Duminil's avatar Eric Duminil
Browse files

Adding callbacks for connecting and ap mode

parent 1264c29c
Pipeline #5991 passed with stage
in 2 minutes and 40 seconds
...@@ -21,8 +21,11 @@ ...@@ -21,8 +21,11 @@
#include "co2_sensor.h" #include "co2_sensor.h"
#include "led_effects.h" #include "led_effects.h"
void wifiConnecting();
void wifiConnected(); void wifiConnected();
void wifiFailed(); void wifiFailed();
void apModeStarts();
void keepServicesAlive(); void keepServicesAlive();
void checkFlashButton(); void checkFlashButton();
......
...@@ -67,9 +67,10 @@ void setup() { ...@@ -67,9 +67,10 @@ void setup() {
web_config::initialize(); web_config::initialize();
web_config::setWifiConnectingCallback(wifiConnecting);
web_config::setWifiConnectionCallback(wifiConnected); web_config::setWifiConnectionCallback(wifiConnected);
web_config::setWifiFailCallback(wifiFailed); web_config::setWifiFailCallback(wifiFailed);
web_config::setApModeCallback(apModeStarts);
pinMode(0, INPUT); // Flash button (used for forced calibration) pinMode(0, INPUT); // Flash button (used for forced calibration)
...@@ -160,8 +161,16 @@ void loop() { ...@@ -160,8 +161,16 @@ void loop() {
/***************************************************************** /*****************************************************************
* Callbacks * * Callbacks *
*****************************************************************/ *****************************************************************/
void wifiConnecting() {
Serial.print(F("WiFi - Trying to connect to "));
Serial.print(config::selected_ssid());
Serial.print(F(" for "));
Serial.print(config::wifi_timeout);
Serial.println(F("s."));
led_effects::showRainbowWheel();
}
void wifiConnected() { void wifiConnected() {
led_effects::showKITTWheel(color::green);
Serial.println(); Serial.println();
Serial.print(F("WiFi - Connected to ")); Serial.print(F("WiFi - Connected to "));
Serial.print(WiFi.SSID()); Serial.print(WiFi.SSID());
...@@ -169,6 +178,7 @@ void wifiConnected() { ...@@ -169,6 +178,7 @@ void wifiConnected() {
IPAddress address = WiFi.localIP(); IPAddress address = WiFi.localIP();
snprintf(wifi::local_ip, sizeof(wifi::local_ip), "%d.%d.%d.%d", address[0], address[1], address[2], address[3]); snprintf(wifi::local_ip, sizeof(wifi::local_ip), "%d.%d.%d.%d", address[0], address[1], address[2], address[3]);
Serial.println(wifi::local_ip); Serial.println(wifi::local_ip);
led_effects::showKITTWheel(color::green);
ntp::connect(); ntp::connect();
...@@ -184,11 +194,19 @@ void wifiConnected() { ...@@ -184,11 +194,19 @@ void wifiConnected() {
void wifiFailed() { void wifiFailed() {
// Ampel will go back to Access Point mode for AP_TIMEOUT seconds, and try connection again after // Ampel will go back to Access Point mode for AP_TIMEOUT seconds, and try connection again after
Serial.println();
Serial.print(F("WiFi - Could not connect to ")); Serial.print(F("WiFi - Could not connect to "));
Serial.println(config::selected_ssid()); Serial.println(config::selected_ssid());
led_effects::showKITTWheel(color::red); led_effects::showKITTWheel(color::red);
} }
void apModeStarts() {
Serial.print(F("WiFi - Starting Access Point mode ("));
Serial.print(config::ampel_name());
Serial.println(F(")."));
led_effects::alert(color::turquoise);
}
/***************************************************************** /*****************************************************************
* Helper functions * * Helper functions *
*****************************************************************/ *****************************************************************/
......
...@@ -9,6 +9,7 @@ namespace color { ...@@ -9,6 +9,7 @@ namespace color {
const uint32_t blue = 0x0000FF; const uint32_t blue = 0x0000FF;
const uint32_t black = 0x000000; const uint32_t black = 0x000000;
const uint32_t magenta = 0xFF00FF; const uint32_t magenta = 0xFF00FF;
const uint32_t turquoise = 0x1CFF68;
} }
namespace led_effects { namespace led_effects {
......
...@@ -19,7 +19,6 @@ ...@@ -19,7 +19,6 @@
#include "src/lib/IotWebConf/src/IotWebConfOptionalGroup.h" #include "src/lib/IotWebConf/src/IotWebConfOptionalGroup.h"
//TODO: Convert all strings to F-strings //TODO: Convert all strings to F-strings
//TODO: Add callbacks for states, e.g. AP mode or connecting?
namespace web_config { namespace web_config {
#if defined(ESP8266) #if defined(ESP8266)
...@@ -34,6 +33,8 @@ namespace web_config { ...@@ -34,6 +33,8 @@ namespace web_config {
const char config_version[IOTWEBCONF_CONFIG_VERSION_LENGTH] = AMPEL_CONFIG_VERSION; // -- Configuration specific key. The value should be modified if config structure was changed. const char config_version[IOTWEBCONF_CONFIG_VERSION_LENGTH] = AMPEL_CONFIG_VERSION; // -- Configuration specific key. The value should be modified if config structure was changed.
using namespace iotwebconf; using namespace iotwebconf;
std::function<void()> _wifiConnectingCallback = nullptr;
std::function<void()> _apModeCallback= nullptr;
/** /**
* WiFi params * WiFi params
...@@ -160,6 +161,14 @@ namespace web_config { ...@@ -160,6 +161,14 @@ namespace web_config {
iotWebConf->setWifiConnectionCallback(success_function); iotWebConf->setWifiConnectionCallback(success_function);
} }
void setWifiConnectingCallback(void (*fn)()) {
_wifiConnectingCallback = fn;
}
void setApModeCallback(void (*fn)()) {
_apModeCallback = fn;
}
void setWifiFailCallback(void (*fail_function)()) { void setWifiFailCallback(void (*fail_function)()) {
std::function<WifiAuthInfo* ()> fail_and_return_null = [fail_function]() { std::function<WifiAuthInfo* ()> fail_and_return_null = [fail_function]() {
fail_function(); fail_function();
...@@ -277,7 +286,6 @@ namespace web_config { ...@@ -277,7 +286,6 @@ namespace web_config {
//NOTE: Can only work if wifi is on. //NOTE: Can only work if wifi is on.
sensor_console::defineIntCommand("ap", [](int onOff) { sensor_console::defineIntCommand("ap", [](int onOff) {
//TODO: Add On-board LED effects to show the changes?
if (onOff) { if (onOff) {
Serial.print(F("Enable ")); Serial.print(F("Enable "));
} else { } else {
...@@ -287,6 +295,15 @@ namespace web_config { ...@@ -287,6 +295,15 @@ namespace web_config {
iotWebConf->forceApMode(onOff); iotWebConf->forceApMode(onOff);
}, F("0/1 (Enables/disables access point)")); }, F("0/1 (Enables/disables access point)"));
iotWebConf->setStateChangedCallback([](NetworkState in, NetworkState out){
if (out == NetworkState::Connecting && _wifiConnectingCallback != nullptr){
_wifiConnectingCallback();
} else if (out == NetworkState::ApMode && _apModeCallback != nullptr){
_apModeCallback();
}
});
iotWebConf->setWifiConnectionTimeoutMs(1000UL * config::wifi_timeout); iotWebConf->setWifiConnectionTimeoutMs(1000UL * config::wifi_timeout);
iotWebConf->skipApStartup(); iotWebConf->skipApStartup();
......
...@@ -69,6 +69,8 @@ namespace web_config { ...@@ -69,6 +69,8 @@ namespace web_config {
void initialize(); void initialize();
void setWifiConnectionCallback(void (*success_function)()); void setWifiConnectionCallback(void (*success_function)());
void setWifiFailCallback(void (*fail_function)()); void setWifiFailCallback(void (*fail_function)());
void setWifiConnectingCallback(void (*connecting_function)());
void setApModeCallback(void (*ap_mode_function)());
void update(); void update();
#if defined(ESP8266) #if defined(ESP8266)
......
...@@ -37,7 +37,7 @@ namespace wifi { ...@@ -37,7 +37,7 @@ namespace wifi {
web_config::update(); web_config::update();
sensor_console::checkSerialInput(); // To allow reset or ssid ... during startup sensor_console::checkSerialInput(); // To allow reset or ssid ... during startup
if (isAccessPoint()) { if (isAccessPoint()) {
led_effects::alert(0x1cff68); led_effects::alert(color::turquoise);
} else if (connected()) { } else if (connected()) {
break; break;
} else { } else {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment