Skip to content

Commit

Permalink
Better improv-WiFi support.
Browse files Browse the repository at this point in the history
  • Loading branch information
melkati committed Jan 29, 2024
1 parent 59e1832 commit 92f567e
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 57 deletions.
4 changes: 2 additions & 2 deletions CO2_Gadget.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -499,7 +499,7 @@ void setup() {
void loop() {
batteryLoop();
utilityLoop();
improvLoopNew();
improvLoop();
wifiClientLoop();
mqttClientLoop();
sensorsLoop();
Expand Down
47 changes: 10 additions & 37 deletions CO2_Gadget_Improv.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 5 additions & 0 deletions CO2_Gadget_Menu.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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
}
Expand Down
24 changes: 7 additions & 17 deletions CO2_Gadget_WIFI.h
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -611,6 +597,10 @@ void initWifi() {

void wifiClientLoop() {
if (activeWIFI && troubledWIFI && (millis() - timeTroubledWIFI >= timeToRetryTroubledWIFI * 1000)) {
initWifi();
}
if (wifiChanged) {
wifiChanged = false;
initWifi();
}
}
Expand Down
2 changes: 1 addition & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 92f567e

Please sign in to comment.