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 @@
#include "co2_sensor.h"
#include "led_effects.h"
void wifiConnecting();
void wifiConnected();
void wifiFailed();
void apModeStarts();
void keepServicesAlive();
void checkFlashButton();
......
......@@ -67,9 +67,10 @@ void setup() {
web_config::initialize();
web_config::setWifiConnectingCallback(wifiConnecting);
web_config::setWifiConnectionCallback(wifiConnected);
web_config::setWifiFailCallback(wifiFailed);
web_config::setApModeCallback(apModeStarts);
pinMode(0, INPUT); // Flash button (used for forced calibration)
......@@ -160,8 +161,16 @@ void loop() {
/*****************************************************************
* 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() {
led_effects::showKITTWheel(color::green);
Serial.println();
Serial.print(F("WiFi - Connected to "));
Serial.print(WiFi.SSID());
......@@ -169,6 +178,7 @@ void wifiConnected() {
IPAddress address = WiFi.localIP();
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);
led_effects::showKITTWheel(color::green);
ntp::connect();
......@@ -184,11 +194,19 @@ void wifiConnected() {
void wifiFailed() {
// 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.println(config::selected_ssid());
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 *
*****************************************************************/
......
......@@ -9,6 +9,7 @@ namespace color {
const uint32_t blue = 0x0000FF;
const uint32_t black = 0x000000;
const uint32_t magenta = 0xFF00FF;
const uint32_t turquoise = 0x1CFF68;
}
namespace led_effects {
......
......@@ -19,7 +19,6 @@
#include "src/lib/IotWebConf/src/IotWebConfOptionalGroup.h"
//TODO: Convert all strings to F-strings
//TODO: Add callbacks for states, e.g. AP mode or connecting?
namespace web_config {
#if defined(ESP8266)
......@@ -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.
using namespace iotwebconf;
std::function<void()> _wifiConnectingCallback = nullptr;
std::function<void()> _apModeCallback= nullptr;
/**
* WiFi params
......@@ -160,6 +161,14 @@ namespace web_config {
iotWebConf->setWifiConnectionCallback(success_function);
}
void setWifiConnectingCallback(void (*fn)()) {
_wifiConnectingCallback = fn;
}
void setApModeCallback(void (*fn)()) {
_apModeCallback = fn;
}
void setWifiFailCallback(void (*fail_function)()) {
std::function<WifiAuthInfo* ()> fail_and_return_null = [fail_function]() {
fail_function();
......@@ -277,7 +286,6 @@ namespace web_config {
//NOTE: Can only work if wifi is on.
sensor_console::defineIntCommand("ap", [](int onOff) {
//TODO: Add On-board LED effects to show the changes?
if (onOff) {
Serial.print(F("Enable "));
} else {
......@@ -287,6 +295,15 @@ namespace web_config {
iotWebConf->forceApMode(onOff);
}, 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->skipApStartup();
......
......@@ -69,6 +69,8 @@ namespace web_config {
void initialize();
void setWifiConnectionCallback(void (*success_function)());
void setWifiFailCallback(void (*fail_function)());
void setWifiConnectingCallback(void (*connecting_function)());
void setApModeCallback(void (*ap_mode_function)());
void update();
#if defined(ESP8266)
......
......@@ -37,7 +37,7 @@ namespace wifi {
web_config::update();
sensor_console::checkSerialInput(); // To allow reset or ssid ... during startup
if (isAccessPoint()) {
led_effects::alert(0x1cff68);
led_effects::alert(color::turquoise);
} else if (connected()) {
break;
} else {
......
Supports Markdown
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