diff --git a/CO2_Gadget.ino b/CO2_Gadget.ino index 81ab73cf..ffe50b78 100644 --- a/CO2_Gadget.ino +++ b/CO2_Gadget.ino @@ -27,7 +27,6 @@ String wifiSSID = WIFI_SSID_CREDENTIALS; String wifiPass = WIFI_PW_CREDENTIALS; String mDNSName = "Unset"; String MACAddress = "Unset"; -// String peerESPNow = ESPNOW_PEER_MAC_ADDRESS; uint8_t peerESPNowAddress[] = ESPNOW_PEER_MAC_ADDRESS; // Communication options @@ -44,6 +43,7 @@ uint64_t timeToRetryTroubledMQTT = 900; // Time in seconds to retry MQTT connec uint16_t WiFiConnectionRetries = 0; uint16_t maxWiFiConnectionRetries = 20; bool mqttDiscoverySent = false; +bool wifiChanged = false; // Display and menu options uint16_t DisplayBrightness = 100; @@ -499,7 +499,7 @@ void setup() { void loop() { batteryLoop(); utilityLoop(); - improvLoopNew(); + improvLoop(); wifiClientLoop(); mqttClientLoop(); sensorsLoop(); diff --git a/CO2_Gadget_Improv.h b/CO2_Gadget_Improv.h index 766910ab..92f8ead3 100644 --- a/CO2_Gadget_Improv.h +++ b/CO2_Gadget_Improv.h @@ -13,67 +13,40 @@ #include "ImprovWiFiLibrary.h" ImprovWiFi improvSerial(&Serial); -char linebuf[80]; -int charcount = 0; - -void blink_led(int d, int times) { - // for (int j = 0; j < times; j++) - // { - // digitalWrite(LED_BUILTIN, HIGH); - // delay(d); - // digitalWrite(LED_BUILTIN, LOW); - // delay(d); - // } -} - void onImprovWiFiErrorCb(ImprovTypes::Error err) { - server.end(); Serial.println("-->[IMPR] Error: " + String(err)); - blink_led(2000, 3); } void onImprovWiFiConnectedCb(const char *ssid, const char *password) { Serial.println("-->[IMPR] Connected to: " + String(ssid)); + wifiChanged = true; + activeWIFI = true; wifiSSID = ssid; wifiPass = password; putPreferences(); - blink_led(100, 3); } -// bool connectWifi(const char *ssid, const char *password) { -// WiFi.begin(ssid, password); - -// while (!improvSerial.isConnected()) { -// blink_led(500, 1); -// } - -// return true; -// } - void initImprov() { char version[100]; // Ajusta el tamaño según tus necesidades sprintf(version, "CO2 Gadget Version: %s%s Flavour: %s\n", CO2_GADGET_VERSION, CO2_GADGET_REV, FLAVOUR); - - // pinMode(LED_BUILTIN, OUTPUT); Serial.println("-->[IMPR] Init Improv"); + #if defined(CONFIG_IDF_TARGET_ESP32) improvSerial.setDeviceInfo(ImprovTypes::ChipFamily::CF_ESP32, "CO2-Gadget-Beta-Desarrollo", version, "CO2-Gadget", "http://{LOCAL_IPV4}/preferences.html"); + #elif defined(CONFIG_IDF_TARGET_ESP32S3) + improvSerial.setDeviceInfo(ImprovTypes::ChipFamily::CF_ESP32_S3, "CO2-Gadget-Beta-Desarrollo", version, "CO2-Gadget", "http://{LOCAL_IPV4}/preferences.html"); + #endif + improvSerial.onImprovError(onImprovWiFiErrorCb); improvSerial.onImprovConnected(onImprovWiFiConnectedCb); - // improvSerial.setCustomConnectWiFi(connectWifi); // Optional - - blink_led(100, 5); + // improvSerial.setCustomConnectWiFi(initWifi); // Optional } -void improvLoopNew() { +void improvLoop() { if (!inMenu) { - if (Serial.peek() != -1 && Serial.peek() != 0x2A) { + if (Serial.available() && Serial.peek() != 0x2A) { improvSerial.handleSerial(); } } } -void improvLoop() { - if (!inMenu) improvSerial.handleSerial(); -} - #endif // CO2_Gadget_Improv_h \ No newline at end of file diff --git a/CO2_Gadget_Menu.h b/CO2_Gadget_Menu.h index 0db05002..c56455fd 100644 --- a/CO2_Gadget_Menu.h +++ b/CO2_Gadget_Menu.h @@ -314,6 +314,7 @@ result doSetActiveWIFI(eventMask e, navNode &nav, prompt &item) { #endif } else { initWifi(); + nav.target-> dirty = true; activeMQTT = preferences.getBool("activeMQTT", false); if ((activeMQTT) && (WiFi.isConnected())) { initMQTT(); @@ -999,6 +1000,10 @@ void menuLoop() { } #if defined(SUPPORT_TFT) + if (wifiChanged) { + wifiChanged = false; + tft.fillScreen(TFT_BLACK); + } if (inMenu) { nav.poll(); // this device only draws when needed } diff --git a/CO2_Gadget_WIFI.h b/CO2_Gadget_WIFI.h index 6ca472be..a63c710f 100644 --- a/CO2_Gadget_WIFI.h +++ b/CO2_Gadget_WIFI.h @@ -137,8 +137,10 @@ void printLargeASCII(const char* text) { void onWifiSettingsChanged(std::string ssid, std::string password) { Serial.print("-->[WiFi] WifiSetup: SSID = "); Serial.print(ssid.c_str()); + #ifndef WIFI_PRIVACY Serial.print(", Password = "); Serial.println(password.c_str()); + #endif WiFi.begin(ssid.c_str(), password.c_str()); } @@ -516,6 +518,7 @@ unsigned long MyTestTimer = 0; // Timer-variables MUST be of type unsigned long void initWifi() { if (activeWIFI) { + wifiChanged = true; troubledWIFI = false; WiFiConnectionRetries = 0; displayNotification("Init WiFi", notifyInfo); @@ -563,23 +566,6 @@ void initWifi() { return; } - // while (WiFi.status() != WL_CONNECTED) { - // WiFiConnectionRetries++; - // Serial.print("."); - // delay(1000); - // if ((WiFiConnectionRetries >= maxWiFiConnectionRetries) && (WiFi.status() != WL_CONNECTED)) { - // disableWiFi(); - // troubledWIFI = true; - // timeTroubledWIFI = millis(); - // Serial.printf( - // "-->[WiFi-event] Not possible to connect to WiFi after %d tries. Will try later.\n", - // WiFiConnectionRetries); - // } - // if (troubledWIFI) { - // return; - // } - // } - Serial.println(""); Serial.print("-->[WiFi] MAC: "); Serial.println(MACAddress); @@ -611,6 +597,10 @@ void initWifi() { void wifiClientLoop() { if (activeWIFI && troubledWIFI && (millis() - timeTroubledWIFI >= timeToRetryTroubledWIFI * 1000)) { + initWifi(); + } + if (wifiChanged) { + wifiChanged = false; initWifi(); } } diff --git a/platformio.ini b/platformio.ini index b8324a20..777e5db8 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="\"087"\" + -D CO2_GADGET_REV="\"088"\" -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