diff --git a/CO2-Ampel_Plus/CO2-Ampel_Plus.ino b/CO2-Ampel_Plus/CO2-Ampel_Plus.ino index d095313..0afe8fa 100644 --- a/CO2-Ampel_Plus/CO2-Ampel_Plus.ino +++ b/CO2-Ampel_Plus/CO2-Ampel_Plus.ino @@ -40,8 +40,11 @@ Button modeButton(BUTTON_PIN); int wifi_reconnect_attemps = WIFI_RECONNECT_ATTEMPTS; void setup() { + + + #if DEBUG_LOG > 0 - while (!Serial) { + while (!Serial) { ; // wait for serial port to connect. } #endif @@ -77,7 +80,9 @@ void setup() { * Factory Reset when button is pressed while reset */ if (!config_is_initialized() || modeButton.isPressed()) { +#if DEBUG_LOG > 0 Serial.println("Loading factory defaults"); +#endif led_off(); led_set_color(LED_RED); led_update(); @@ -85,8 +90,10 @@ void setup() { config_set_factory_defaults(); led_off(); } +#if DEBUG_LOG > 0 Serial.println("Setup complete!"); Serial.println("------------------------"); +#endif } void loop() { @@ -100,18 +107,25 @@ void loop() { switch (wifi_state) { case WIFI_MODE_AP_INIT: // Create an Access Point +#if DEBUG_LOG > 0 Serial.println("Creating Access Point"); +#endif wifi_ap_create(); wifi_state = WIFI_MODE_AP_LISTEN; +#if DEBUG_LOG > 0 Serial.println("------------------------"); +#endif break; case WIFI_MODE_WPA_CONNECT: // Connect to WiFi device_config_t cfg = config_get_values(); + +#if DEBUG_LOG > 0 Serial.print("Connecting to SSID "); Serial.print(cfg.wifi_ssid); Serial.println(" Wifi"); +#endif if (strlen(cfg.wifi_ssid) != 0) { if (wifi_wpa_connect() == WL_CONNECTED) { wifi_state = WIFI_MODE_WPA_LISTEN; @@ -122,21 +136,22 @@ void loop() { Serial.println("No WiFi SSID Configured."); wifi_state = WIFI_MODE_NOT_CONECTED; } +#if DEBUG_LOG > 0 Serial.println("------------------------"); Serial.println("Start measurement"); Serial.println(""); +#endif break; } - - if (!wifi_is_connected()) { wifi_state = WIFI_MODE_WPA_CONNECT; } + mqtt_loop(); - - wifi_handle_client(); sensor_handler(); sensor_handle_brightness(); + wifi_handle_client(); + } diff --git a/CO2-Ampel_Plus/Config.h b/CO2-Ampel_Plus/Config.h index b9cd2ce..7caf11f 100644 --- a/CO2-Ampel_Plus/Config.h +++ b/CO2-Ampel_Plus/Config.h @@ -1,7 +1,7 @@ #ifndef CONFIG_H #define CONFIG_H -#define VERSION "v3.0.0" +#define VERSION "v3.0.1" #define DEBUG_LOG 0 // 1 = Enable debug log @@ -103,15 +103,13 @@ #define WIFI_MODE_NOT_CONECTED 87 #define WIFI_RECONNECT_ATTEMPTS 10 -// Required for WiFiWebServer lib -#define USE_WIFI_NINA false -#define USE_WIFI101 true -#define DEBUG_WIFI_WEBSERVER_PORT Serial // WIFI_LOGLEVEL below 4 causes EMPTY HTTP RESPONSE // see: https://github.com/khoih-prog/WiFiWebServer/issues/3 + +#define DEBUG_WIFI_WEBSERVER_PORT Serial #define _WIFI_LOGLEVEL_ 4 -#define _WIFININA_LOGLEVEL_ 3 +#define _WIFININA_LOGLEVEL_ 0 #define USE_WIFI_NINA false #define USE_WIFI101 true diff --git a/CO2-Ampel_Plus/HTMLStatic.h b/CO2-Ampel_Plus/HTMLStatic.h index 5e3f577..503bcb0 100644 --- a/CO2-Ampel_Plus/HTMLStatic.h +++ b/CO2-Ampel_Plus/HTMLStatic.h @@ -26,13 +26,20 @@ const char settings_header_html[] PROGMEM = R"=====( CO2 Ampel Wifi Settings - +

Settings

)====="; + + const char settings_footer_html[] PROGMEM = R"=====(
@@ -65,12 +72,13 @@ Password:
CO2 Ampel Wifi AP - - +)====="; + +const char root_footer_html[] PROGMEM = R"=====(

CO2 Ampel State

@@ -79,5 +87,4 @@ const char root_html[] PROGMEM = R"=====( Waiting for initial state...
- )====="; diff --git a/CO2-Ampel_Plus/NetworkManager.cpp b/CO2-Ampel_Plus/NetworkManager.cpp index 28329a8..bc2c21d 100644 --- a/CO2-Ampel_Plus/NetworkManager.cpp +++ b/CO2-Ampel_Plus/NetworkManager.cpp @@ -26,7 +26,9 @@ bool isAuthenticated(){ if (server.hasHeader(F("Cookie"))){ String cookie = server.header(F("Cookie")); if (cookie.indexOf(F("CO2SESSIONID=1")) != -1){ +#if DEBUG_LOG > 0 Serial.println(F("Authentication Successful")); +#endif return true; } } @@ -34,7 +36,18 @@ bool isAuthenticated(){ } void handleRoot(){ - server.send(200, F("text/html"), root_html); + server.sendContent(root_header_html); + + server.sendContent(""); + + server.sendContent(root_footer_html); + server.sendContent(""); + } void handleNotFound() { @@ -153,6 +166,8 @@ void handleSettings(){ */ server.sendContent(settings_header_html); + server.sendContent(css_styles_general); + server.sendContent(settings_middle_html); server.sendContent(F("")); server.sendContent(F(" 0 Serial.println("Creating access point failed"); +#endif while (true) { led_failure(LED_COLOR_WIFI_FAILURE); } @@ -395,7 +414,9 @@ int wifi_wpa_connect() { // check for the presence of the shield: if (WiFi.status() == WL_NO_SHIELD) { +#if DEBUG_LOG > 0 Serial.println("WiFi shield not present"); +#endif // don't continue while (true) { led_failure(LED_COLOR_WIFI_FAILURE); @@ -411,6 +432,8 @@ int wifi_wpa_connect() { } if (WiFi.status() != WL_CONNECTED) { + +#if DEBUG_LOG > 0 if (strlen(cfg.wifi_ssid) == 0) { Serial.println("No SSID and Password set. Wifi connection failed"); } else { @@ -418,6 +441,8 @@ int wifi_wpa_connect() { Serial.print(cfg.wifi_ssid); Serial.println(" failed"); } +#endif + } else { init_webserver_routes(wifi_status); print_wifi_status(); @@ -430,6 +455,7 @@ int wifi_wpa_connect() { void print_wifi_status() { // print the SSID of the network you're attached to: +#if DEBUG_LOG > 0 Serial.print("SSID: "); Serial.println(WiFi.SSID()); @@ -443,9 +469,11 @@ void print_wifi_status() { Serial.print("Signal strength (RSSI): "); Serial.print(rssi); Serial.println(" dBm"); +#endif } void print_mac_address(byte mac[]) { +#if DEBUG_LOG > 0 for (int i = 5; i >= 0; i--) { if (mac[i] < 16) { Serial.print("0"); @@ -456,28 +484,30 @@ void print_mac_address(byte mac[]) { } } Serial.println(); +#endif } void wifi_handle_client() { - // compare the previous status to the current status - if (wifi_status != WiFi.status()) { - // it has changed update the variable - wifi_status = WiFi.status(); - - if (wifi_status == WL_AP_CONNECTED) { - // a device has connected to the AP - Serial.println(F("Device connected to AP")); - } else { - // a device has disconnected from the AP, and we are back in listening - // mode - Serial.println(F("Device disconnected from AP")); + if (!ap_is_active()){ + // compare the previous status to the current status + if (wifi_status != WiFi.status()) { + // it has changed update the variable + wifi_status = WiFi.status(); + if (wifi_status == WL_AP_CONNECTED) { + // a device has connected to the AP + Serial.println(F("Device connected to AP")); + } else { + // a device has disconnected from the AP, and we are back in listening + // mode + Serial.println(F("Device disconnected from AP")); + } } } - + server.handleClient(); - delay(15); + delay(100); // WiFiClient client = server.available(); // listen for incoming clients