Commit d1200037 authored by Eric Duminil's avatar Eric Duminil
Browse files

Turn wifi on/off

parent 01724c50
Pipeline #5787 passed with stage
in 2 minutes and 24 seconds
...@@ -11,12 +11,10 @@ ...@@ -11,12 +11,10 @@
#include "csv_writer.h" #include "csv_writer.h"
#ifdef AMPEL_WIFI #include "wifi_util.h"
# include "wifi_util.h" #include "mqtt.h"
# include "mqtt.h" #ifdef AMPEL_HTTP
# ifdef AMPEL_HTTP # include "web_server.h"
# include "web_server.h"
# endif
#endif #endif
#include "lorawan.h" #include "lorawan.h"
......
...@@ -72,35 +72,35 @@ void setup() { ...@@ -72,35 +72,35 @@ void setup() {
web_config::initialize(); web_config::initialize();
#ifdef AMPEL_WIFI // TODO: What to do with those? Replace with webconf::wifi_active? if (config::wifi_active) {
web_config::setWifiConnectionCallback([]() { web_config::setWifiConnectionCallback([]() {
//TODO: Move somewhere else //TODO: Move somewhere else
led_effects::showKITTWheel(color::green); led_effects::showKITTWheel(color::green);
Serial.println(); Serial.println();
Serial.print(F("WiFi - Connected! IP address: ")); Serial.print(F("WiFi - Connected! IP address: "));
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);
ntp::initialize(); ntp::initialize();
if (config::mqtt_active()) { if (config::mqtt_active()) {
mqtt::initialize(ampel.sensorId); mqtt::initialize(ampel.sensorId);
} }
Serial.print(F("You can access this sensor via http://")); Serial.print(F("You can access this sensor via http://"));
Serial.print(ampel.sensorId); Serial.print(ampel.sensorId);
Serial.print(F(".local (might be unstable) or http://")); Serial.print(F(".local (might be unstable) or http://"));
Serial.println(WiFi.localIP()); Serial.println(WiFi.localIP());
}); });
web_config::setWifiFailCallback(failedConnection); web_config::setWifiFailCallback(failedConnection);
// web_config::setWifiConnectionFailedCallback([]() { // web_config::setWifiConnectionFailedCallback([]() {
// led_effects::showKITTWheel(color::red); // led_effects::showKITTWheel(color::red);
// Serial.println(F("Connection to WiFi failed")); // Serial.println(F("Connection to WiFi failed"));
// }); // });
#endif }
pinMode(0, INPUT); // Flash button (used for forced calibration) pinMode(0, INPUT); // Flash button (used for forced calibration)
...@@ -120,14 +120,14 @@ void setup() { ...@@ -120,14 +120,14 @@ void setup() {
csv_writer::initialize(ampel.sensorId); csv_writer::initialize(ampel.sensorId);
#ifdef AMPEL_WIFI if (config::wifi_active) {
wifi::defineCommands(); wifi::defineCommands();
# ifdef AMPEL_HTTP # ifdef AMPEL_HTTP
web_server::definePages(); web_server::definePages();
//TODO: Rename. Not just web_server //TODO: Rename. Not just web_server
// web_server::initialize(); // web_server::initialize();
# endif # endif
#endif }
#if defined(ESP32) #if defined(ESP32)
if (config::lorawan_active()) { if (config::lorawan_active()) {
...@@ -175,11 +175,9 @@ void loop() { ...@@ -175,11 +175,9 @@ void loop() {
csv_writer::logIfTimeHasCome(sensor::timestamp, sensor::co2, sensor::temperature, sensor::humidity); csv_writer::logIfTimeHasCome(sensor::timestamp, sensor::co2, sensor::temperature, sensor::humidity);
} }
#if defined(AMPEL_WIFI) if (config::wifi_active && config::mqtt_active()) {
if (config::mqtt_active()) {
mqtt::publishIfTimeHasCome(sensor::timestamp, sensor::co2, sensor::temperature, sensor::humidity); mqtt::publishIfTimeHasCome(sensor::timestamp, sensor::co2, sensor::temperature, sensor::humidity);
} }
#endif
#if defined(ESP32) #if defined(ESP32)
if (config::lorawan_active()) { if (config::lorawan_active()) {
...@@ -229,13 +227,13 @@ void checkFlashButton() { ...@@ -229,13 +227,13 @@ void checkFlashButton() {
} }
void keepServicesAlive() { void keepServicesAlive() {
#ifdef AMPEL_WIFI if (config::wifi_active) {
web_config::update(); web_config::update();
if (wifi::connected()) { if (wifi::connected()) {
ntp::update(); // NTP client has its own timer. It will connect to NTP server every 60s. ntp::update(); // NTP client has its own timer. It will connect to NTP server every 60s.
if (config::mqtt_active()) { if (config::mqtt_active()) {
mqtt::keepConnection(); // MQTT client has its own timer. It will keep alive every 15s. mqtt::keepConnection(); // MQTT client has its own timer. It will keep alive every 15s.
}
} }
} }
#endif
} }
...@@ -228,10 +228,19 @@ namespace web_config { ...@@ -228,10 +228,19 @@ namespace web_config {
Serial.println(F("Done!")); Serial.println(F("Done!"));
}, F("(resets the complete IotWeb config)")); }, F("(resets the complete IotWeb config)"));
#if !defined(AMPEL_WIFI) sensor_console::defineIntCommand("wifi", [](int32_t onOff) {
config::wifi_active = onOff;
iotWebConf->saveConfig();
Serial.print(F("WiFi - "));
Serial.println(onOff ? F("On!") : F("Off!"));
}, F("0/1 (Wifi On?)"));
iotWebConf->loadConfig(); iotWebConf->loadConfig();
return;
#endif if (!config::wifi_active) {
Serial.println(F("Wifi is off : no access point or connection. Use 'wifi 1' to turn on again!"));
return;
}
const int ONBOARD_LED_PIN = 2; const int ONBOARD_LED_PIN = 2;
# ifdef ESP8266 # ifdef ESP8266
...@@ -251,12 +260,6 @@ namespace web_config { ...@@ -251,12 +260,6 @@ namespace web_config {
//TODO: Remove AP Password config? //TODO: Remove AP Password config?
//TODO: Save LoRaWAN key if possible? //TODO: Save LoRaWAN key if possible?
Serial.print("WIFI ? : ");
Serial.println(ampelWifiParam.value());
iotWebConf->loadConfig();
Serial.print("WIFI ? : ");
Serial.println(ampelWifiParam.value());
iotWebConf->init(); iotWebConf->init();
//TODO: Authenticate only if required? //TODO: Authenticate only if required?
...@@ -288,6 +291,8 @@ namespace config { ...@@ -288,6 +291,8 @@ namespace config {
bool &auto_calibrate_sensor = web_config::autoCalibrateParam.value(); // [true / false] bool &auto_calibrate_sensor = web_config::autoCalibrateParam.value(); // [true / false]
float &temperature_offset = web_config::temperatureOffsetParam.value(); // [K] Sign isn't relevant. float &temperature_offset = web_config::temperatureOffsetParam.value(); // [K] Sign isn't relevant.
bool &wifi_active = web_config::ampelWifiParam.value();
// LEDs // LEDs
uint8_t &max_brightness = web_config::maxBrightnessParam.value(); uint8_t &max_brightness = web_config::maxBrightnessParam.value();
uint8_t &min_brightness = web_config::minBrightnessParam.value(); uint8_t &min_brightness = web_config::minBrightnessParam.value();
......
...@@ -18,6 +18,8 @@ namespace config { ...@@ -18,6 +18,8 @@ namespace config {
extern bool &auto_calibrate_sensor; // [true / false] extern bool &auto_calibrate_sensor; // [true / false]
extern float &temperature_offset; // [K] Sign isn't relevant. extern float &temperature_offset; // [K] Sign isn't relevant.
extern bool &wifi_active; // [true / false]
// LED // LED
extern uint8_t &max_brightness; extern uint8_t &max_brightness;
extern uint8_t &min_brightness; extern uint8_t &min_brightness;
......
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