diff --git a/CO2_Gadget.ino b/CO2_Gadget.ino index ffe50b78..bc1b5f0b 100644 --- a/CO2_Gadget.ino +++ b/CO2_Gadget.ino @@ -476,8 +476,9 @@ void setup() { #ifdef SUPPORT_BLE initBLE(); #endif - initWifi(); initSensors(); + initWifi(); + wifiChanged = false; #ifdef SUPPORT_ESPNOW initESPNow(); #endif diff --git a/CO2_Gadget_BLE.h b/CO2_Gadget_BLE.h index 5b801d98..e505cf53 100644 --- a/CO2_Gadget_BLE.h +++ b/CO2_Gadget_BLE.h @@ -5,6 +5,8 @@ #include "Sensirion_Gadget_BLE.h" #include "WifiMultiLibraryWrapper.h" +// clang-format on + NimBLELibraryWrapper lib; WifiMultiLibraryWrapper wifi; DataProvider provider(lib, DataType::T_RH_CO2_ALT, true, false, false, &wifi); @@ -12,7 +14,7 @@ DataProvider provider(lib, DataType::T_RH_CO2_ALT, true, false, false, &wifi); #endif void initBLE() { -#ifdef SUPPORT_BLE +#ifdef SUPPORT_BLE if (activeBLE) { if (bleInitialized) { Serial.print( @@ -36,7 +38,7 @@ void initBLE() { void publishBLE() { #ifdef SUPPORT_BLE - if (activeBLE) { + if ((activeBLE) && (co2 > 0)) { provider.writeValueToCurrentSample(co2, SignalType::CO2_PARTS_PER_MILLION); provider.writeValueToCurrentSample(temp, SignalType::TEMPERATURE_DEGREES_CELSIUS); provider.writeValueToCurrentSample(hum, SignalType::RELATIVE_HUMIDITY_PERCENTAGE); diff --git a/CO2_Gadget_Preferences.h b/CO2_Gadget_Preferences.h index 9aa0ecfe..ce5c5c57 100644 --- a/CO2_Gadget_Preferences.h +++ b/CO2_Gadget_Preferences.h @@ -152,7 +152,9 @@ void initPreferences() { wifiPass.trim(); hostName.trim(); preferences.end(); + #ifdef DEBUG_PREFERENCES printPreferences(); + #endif } void putPreferences() { diff --git a/CO2_Gadget_WIFI.h b/CO2_Gadget_WIFI.h index a63c710f..85ea0ff3 100644 --- a/CO2_Gadget_WIFI.h +++ b/CO2_Gadget_WIFI.h @@ -10,7 +10,7 @@ // clang-format on #if !defined WIFI_SSID_CREDENTIALS || !defined WIFI_PW_CREDENTIALS -// If not using enviroment variables, you must fill data on file credentials.h.tpl and rename as credentials.h +// If not using enviroment variables, you must have a credentials.h file #include "credentials.h" #endif @@ -18,129 +18,239 @@ WiFiClient espClient; AsyncWebServer server(80); void printSmallChar(char c, int row) { - switch (c) { - case '0': - switch (row) { - case 0: Serial.print(" 000 "); break; - case 1: Serial.print("0 0"); break; - case 2: Serial.print("0 0"); break; - case 3: Serial.print("0 0"); break; - case 4: Serial.print(" 000 "); break; - } - break; - case '1': - switch (row) { - case 0: Serial.print(" 1 "); break; - case 1: Serial.print(" 11 "); break; - case 2: Serial.print(" 1 "); break; - case 3: Serial.print(" 1 "); break; - case 4: Serial.print(" 111 "); break; - } - break; - case '2': - switch (row) { - case 0: Serial.print(" 222 "); break; - case 1: Serial.print(" 2"); break; - case 2: Serial.print(" 22 "); break; - case 3: Serial.print(" 2 "); break; - case 4: Serial.print(" 2222"); break; - } - break; - case '3': - switch (row) { - case 0: Serial.print(" 333 "); break; - case 1: Serial.print(" 3"); break; - case 2: Serial.print(" 33 "); break; - case 3: Serial.print(" 3"); break; - case 4: Serial.print(" 333 "); break; - } - break; - case '4': - switch (row) { - case 0: Serial.print("4 4"); break; - case 1: Serial.print("4 4"); break; - case 2: Serial.print("44444"); break; - case 3: Serial.print(" 4"); break; - case 4: Serial.print(" 4"); break; - } - break; - case '5': - switch (row) { - case 0: Serial.print("55555"); break; - case 1: Serial.print("5 "); break; - case 2: Serial.print("555 "); break; - case 3: Serial.print(" 5"); break; - case 4: Serial.print("555 "); break; - } - break; - case '6': - switch (row) { - case 0: Serial.print(" 666 "); break; - case 1: Serial.print("6 "); break; - case 2: Serial.print("6666 "); break; - case 3: Serial.print("6 6"); break; - case 4: Serial.print(" 666 "); break; - } - break; - case '7': - switch (row) { - case 0: Serial.print("77777"); break; - case 1: Serial.print(" 7"); break; - case 2: Serial.print(" 7 "); break; - case 3: Serial.print(" 7 "); break; - case 4: Serial.print(" 7 "); break; - } - break; - case '8': - switch (row) { - case 0: Serial.print(" 888 "); break; - case 1: Serial.print("8 8"); break; - case 2: Serial.print(" 888 "); break; - case 3: Serial.print("8 8"); break; - case 4: Serial.print(" 888 "); break; - } - break; - case '9': - switch (row) { - case 0: Serial.print(" 999 "); break; - case 1: Serial.print("9 9"); break; - case 2: Serial.print(" 9999"); break; - case 3: Serial.print(" 9"); break; - case 4: Serial.print(" 999 "); break; - } - break; - case '.': - switch (row) { - case 0: Serial.print(" "); break; - case 1: Serial.print(" "); break; - case 2: Serial.print(" "); break; - case 3: Serial.print(" "); break; - case 4: Serial.print(" o "); break; - } - break; - default: - Serial.print(" "); // Default character for unhandled - break; - } + switch (c) { + case '0': + switch (row) { + case 0: + Serial.print(" 000 "); + break; + case 1: + Serial.print("0 0"); + break; + case 2: + Serial.print("0 0"); + break; + case 3: + Serial.print("0 0"); + break; + case 4: + Serial.print(" 000 "); + break; + } + break; + case '1': + switch (row) { + case 0: + Serial.print(" 1 "); + break; + case 1: + Serial.print(" 11 "); + break; + case 2: + Serial.print(" 1 "); + break; + case 3: + Serial.print(" 1 "); + break; + case 4: + Serial.print(" 111 "); + break; + } + break; + case '2': + switch (row) { + case 0: + Serial.print(" 222 "); + break; + case 1: + Serial.print(" 2"); + break; + case 2: + Serial.print(" 22 "); + break; + case 3: + Serial.print(" 2 "); + break; + case 4: + Serial.print(" 2222"); + break; + } + break; + case '3': + switch (row) { + case 0: + Serial.print(" 333 "); + break; + case 1: + Serial.print(" 3"); + break; + case 2: + Serial.print(" 33 "); + break; + case 3: + Serial.print(" 3"); + break; + case 4: + Serial.print(" 333 "); + break; + } + break; + case '4': + switch (row) { + case 0: + Serial.print("4 4"); + break; + case 1: + Serial.print("4 4"); + break; + case 2: + Serial.print("44444"); + break; + case 3: + Serial.print(" 4"); + break; + case 4: + Serial.print(" 4"); + break; + } + break; + case '5': + switch (row) { + case 0: + Serial.print("55555"); + break; + case 1: + Serial.print("5 "); + break; + case 2: + Serial.print("555 "); + break; + case 3: + Serial.print(" 5"); + break; + case 4: + Serial.print("555 "); + break; + } + break; + case '6': + switch (row) { + case 0: + Serial.print(" 666 "); + break; + case 1: + Serial.print("6 "); + break; + case 2: + Serial.print("6666 "); + break; + case 3: + Serial.print("6 6"); + break; + case 4: + Serial.print(" 666 "); + break; + } + break; + case '7': + switch (row) { + case 0: + Serial.print("77777"); + break; + case 1: + Serial.print(" 7"); + break; + case 2: + Serial.print(" 7 "); + break; + case 3: + Serial.print(" 7 "); + break; + case 4: + Serial.print(" 7 "); + break; + } + break; + case '8': + switch (row) { + case 0: + Serial.print(" 888 "); + break; + case 1: + Serial.print("8 8"); + break; + case 2: + Serial.print(" 888 "); + break; + case 3: + Serial.print("8 8"); + break; + case 4: + Serial.print(" 888 "); + break; + } + break; + case '9': + switch (row) { + case 0: + Serial.print(" 999 "); + break; + case 1: + Serial.print("9 9"); + break; + case 2: + Serial.print(" 9999"); + break; + case 3: + Serial.print(" 9"); + break; + case 4: + Serial.print(" 999 "); + break; + } + break; + case '.': + switch (row) { + case 0: + Serial.print(" "); + break; + case 1: + Serial.print(" "); + break; + case 2: + Serial.print(" "); + break; + case 3: + Serial.print(" "); + break; + case 4: + Serial.print(" o "); + break; + } + break; + default: + Serial.print(" "); // Default character for unhandled + break; + } } -void printLargeASCII(const char* text) { - for (int row = 0; row < 5; row++) { // 5 rows for each character - for (int i = 0; i < strlen(text); i++) { - printSmallChar(text[i], row); - Serial.print(" "); // Space between characters +void printLargeASCII(const char *text) { + for (int row = 0; row < 5; row++) { // 5 rows for each character + for (int i = 0; i < strlen(text); i++) { + printSmallChar(text[i], row); + Serial.print(" "); // Space between characters + } + Serial.println(); // New line after each row of characters } - Serial.println(); // New line after each row of characters - } } void onWifiSettingsChanged(std::string ssid, std::string password) { Serial.print("-->[WiFi] WifiSetup: SSID = "); Serial.print(ssid.c_str()); - #ifndef WIFI_PRIVACY +#ifndef WIFI_PRIVACY Serial.print(", Password = "); Serial.println(password.c_str()); - #endif +#endif WiFi.begin(ssid.c_str(), password.c_str()); } @@ -249,6 +359,8 @@ void printWiFiStatus() { // Print wifi status on serial monitor } void WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info) { +// #define DEBUG_WIFI_EVENTS +#ifdef DEBUG_WIFI_EVENTS Serial.printf("-->[WiFi-event] event: %d - ", event); switch (event) { @@ -331,6 +443,7 @@ void WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info) { default: break; } +#endif // DEBUG_WIFI_EVENTS } #ifdef SUPPORT_MDNS @@ -394,13 +507,16 @@ void WiFiStationGotIP(WiFiEvent_t event, WiFiEventInfo_t info) { WiFiConnectionRetries = 0; timeTroubledWIFI = 0; troubledMQTT = false; +#ifdef DEBUG_WIFI_EVENTS Serial.println("-->[WiFi-event] WiFi connected"); Serial.print("-->[WiFi-event] IP address: "); Serial.println(WiFi.localIP()); +#endif } void WiFiStationDisconnected(WiFiEvent_t event, WiFiEventInfo_t info) { ++WiFiConnectionRetries; +#ifdef DEBUG_WIFI_EVENTS Serial.println("-->[WiFi-event] Disconnected from WiFi access point"); Serial.print("-->[WiFi-event] WiFi lost connection. Reason: "); Serial.println(info.wifi_sta_disconnected.reason); @@ -408,14 +524,15 @@ void WiFiStationDisconnected(WiFiEvent_t event, WiFiEventInfo_t info) { Serial.print(WiFiConnectionRetries); Serial.print(" of "); Serial.println(maxWiFiConnectionRetries); +#endif if (WiFiConnectionRetries >= maxWiFiConnectionRetries) { disableWiFi(); troubledWIFI = true; timeTroubledWIFI = millis(); - Serial.printf( - "-->[WiFi-event] Not possible to connect to WiFi after %d tries. Will try later.\n", - WiFiConnectionRetries); +#ifdef DEBUG_WIFI_EVENTS + Serial.printf("-->[WiFi-event] Not possible to connect to WiFi after %d tries. Will try later.\n", WiFiConnectionRetries); +#endif } } @@ -535,11 +652,12 @@ void initWifi() { WiFi.onEvent(WiFiStationDisconnected, WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_DISCONNECTED); WiFi.onEvent(customWiFiEventHandler); - // Possible to optimize battery (further investigation needed) + // Possible to optimize battery? (further investigation needed) // WiFi.setSleep(true); // WiFi.setSleep(WIFI_PS_NONE); - Serial.print("-->[WiFi] Connecting to WiFi"); + String connectMessage = "-->[WiFi] Connecting to WiFi (SSID: " + String(wifiSSID) + ")\n"; + Serial.print(connectMessage); WiFi.begin(wifiSSID.c_str(), wifiPass.c_str()); // Wait for connection @@ -548,7 +666,7 @@ void initWifi() { if (TimePeriodIsOver(MyTestTimer, 500)) { // once every 500 miliseconds Serial.print("."); // print a dot WiFiConnectionRetries++; - if (WiFiConnectionRetries > maxWiFiConnectionRetries) { // after 30 dots = 15 seconds restart + if (WiFiConnectionRetries > maxWiFiConnectionRetries) { // after maxWiFiConnectionRetries dots Serial.println(); Serial.print("not connected "); } @@ -597,8 +715,19 @@ void initWifi() { void wifiClientLoop() { if (activeWIFI && troubledWIFI && (millis() - timeTroubledWIFI >= timeToRetryTroubledWIFI * 1000)) { - initWifi(); + initWifi(); } + + // This is a workaround until I can directly determine whether the Wi-Fi data has been changed via BLE + // Only checks for SSID changed (not password) + if (WiFi.SSID() != wifiSSID) { + Serial.println("-->[WiFi] Wi-Fi SSID changed. Old SSID: " + wifiSSID + ", new SSID: " + WiFi.SSID()); + wifiSSID = WiFi.SSID(); + putPreferences(); + // initWifi(); + wifiChanged = true; + } + if (wifiChanged) { wifiChanged = false; initWifi(); diff --git a/platformio.ini b/platformio.ini index 777e5db8..b2ff9711 100644 --- a/platformio.ini +++ b/platformio.ini @@ -58,7 +58,7 @@ build_flags = -D MQTT_BROKER_SERVER="\"192.168.1.145"\" -D CO2_GADGET_VERSION="\"0.8."\" - -D CO2_GADGET_REV="\"088"\" + -D CO2_GADGET_REV="\"090-development"\" -D CORE_DEBUG_LEVEL=0 -DNEOPIXEL_PIN=26 ; Pinnumber for button for down/next and back / exit actions -DNEOPIXEL_COUNT=16 ; How many neopixels to control