Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/Coscolin/CO2-Gadget
Browse files Browse the repository at this point in the history
  • Loading branch information
Coscolin committed Jan 30, 2024
2 parents 48899ca + 56fa712 commit 764f1fa
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 70 deletions.
10 changes: 8 additions & 2 deletions CO2_Gadget.ino
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ String mqttUser = "";
String mqttPass = "";
String wifiSSID = WIFI_SSID_CREDENTIALS;
String wifiPass = WIFI_PW_CREDENTIALS;
String wifiSSID2 = "";
String wifiPass2 = "";
String mDNSName = "Unset";
String MACAddress = "Unset";
uint8_t peerESPNowAddress[] = ESPNOW_PEER_MAC_ADDRESS;
Expand All @@ -34,6 +36,7 @@ bool activeBLE = true;
bool activeWIFI = true;
bool activeMQTT = true;
bool activeESPNOW = false;
bool activeOTA = false;
bool troubledWIFI = false; // There are problems connecting to WIFI. Temporary suspend WIFI
bool troubledMQTT = false; // There are problems connecting to MQTT. Temporary suspend MQTT
uint64_t timeTroubledWIFI = 0; // Time since WIFI is troubled
Expand All @@ -54,7 +57,6 @@ bool displayShowHumidity = true;
bool displayShowBattery = true;
bool displayShowCO2 = true;
bool displayShowPM25 = true;

bool debugSensors = false;
bool inMenu = false;
uint16_t measurementInterval = 10;
Expand All @@ -63,6 +65,7 @@ int8_t selectedCO2Sensor = -1;
bool outputsModeRelay = false;
uint8_t channelESPNow = 1;
uint16_t boardIdESPNow = 0;
uint64_t timeInitializationCompleted = 0;

// Variables for Battery reading
float battery_voltage = 0;
Expand Down Expand Up @@ -107,7 +110,10 @@ uint16_t co2RedRange = 1000;

#include <WiFi.h>
#include <WiFiMulti.h>
<<<<<<< HEAD

=======
>>>>>>> 56fa71210855b96b41690aad2566683b671c8fe7
#include <Wire.h>

#include "driver/adc.h"
Expand Down Expand Up @@ -496,7 +502,7 @@ void setup() {
}
WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, brown_reg_temp); // enable brownout detector
Serial.println("-->[STUP] Ready.");
delay(100);
timeInitializationCompleted = millis();
}

void loop() {
Expand Down
65 changes: 61 additions & 4 deletions CO2_Gadget_Menu.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ char tempMQTTUser[] = " ";
char tempMQTTPass[] = " ";
char tempWiFiSSID[] = " ";
char tempWiFiPasswrd[] = " ";
char tempWiFiSSID2[] = " ";
char tempWiFiPasswrd2[] = " ";
char tempHostName[] = " ";
char tempBLEDeviceId[] = " ";
char tempCO2Sensor[] = " ";
Expand Down Expand Up @@ -331,11 +333,8 @@ result doSetWiFiSSID(eventMask e, navNode &nav, prompt &item) {
Serial.println(e);
Serial.flush();
#endif
// Serial.printf("tempWiFiSSID: #%s#\n", tempWiFiSSID);
wifiSSID = String(tempWiFiSSID);
// Serial.printf("wifiSSID: #%s#\n", wifiSSID.c_str());
wifiSSID.trim();
// Serial.printf("wifiSSID: #%s#\n", wifiSSID.c_str());
return proceed;
}

Expand All @@ -350,6 +349,32 @@ result doSetWiFiPasswrd(eventMask e, navNode &nav, prompt &item) {
wifiPass.trim();
return proceed;
}
result doSetWiFiSSID2(eventMask e, navNode &nav, prompt &item) {
#ifdef DEBUG_ARDUINOMENU
Serial.printf("-->[MENU] Setting WiFi SSID to #%s#\n", tempWiFiSSID);
Serial.print(F("-->[MENU] action1 event:"));
Serial.println(e);
Serial.flush();
#endif
// Serial.printf("tempWiFiSSID: #%s#\n", tempWiFiSSID);
wifiSSID2 = String(tempWiFiSSID2);
// Serial.printf("wifiSSID: #%s#\n", wifiSSID.c_str());
wifiSSID2.trim();
// Serial.printf("wifiSSID: #%s#\n", wifiSSID.c_str());
return proceed;
}

result doSetWiFiPasswrd2(eventMask e, navNode &nav, prompt &item) {
#ifdef DEBUG_ARDUINOMENU
Serial.printf("-->[MENU] Setting WiFi Password to #%s#\n", tempWiFiPasswrd);
Serial.print(F("-->[MENU] action1 event:"));
Serial.println(e);
Serial.flush();
#endif
wifiPass2 = String(tempWiFiPasswrd2);
wifiPass2.trim();
return proceed;
}

result doSetHostName(eventMask e, navNode &nav, prompt &item) {
#ifdef DEBUG_ARDUINOMENU
Expand All @@ -367,12 +392,32 @@ TOGGLE(activeWIFI, activeWIFIMenu, "WIFI Enable: ", doNothing,noEvent, wrapStyle
,VALUE("ON", true, doSetActiveWIFI, exitEvent)
,VALUE("OFF", false, doSetActiveWIFI, exitEvent));

#ifdef SUPPORT_OTA
result doSetActiveOTA(eventMask e, navNode &nav, prompt &item) {
return proceed;
}

TOGGLE(activeOTA, activeOTAMenu, "OTA Enable: ", doNothing,noEvent, wrapStyle
,VALUE("ON", true, doSetActiveOTA, exitEvent)
,VALUE("OFF", false, doSetActiveOTA, exitEvent));
#endif

MENU(wifiConfigMenu, "WIFI Config", doNothing, noEvent, wrapStyle
,SUBMENU(activeWIFIMenu)
<<<<<<< HEAD
,SUBMENU(searchWIFIMenu)
,EDIT("SSID", tempWiFiSSID, ssidChars, doSetWiFiSSID, exitEvent, wrapStyle)
,EDIT("Pass:", tempWiFiPasswrd, allChars, doSetWiFiPasswrd, exitEvent, wrapStyle)
=======
,EDIT("SSID1", tempWiFiSSID, ssidChars, doSetWiFiSSID, exitEvent, wrapStyle)
,EDIT("PASS1:", tempWiFiPasswrd, allChars, doSetWiFiPasswrd, exitEvent, wrapStyle)
,EDIT("SSID2", tempWiFiSSID2, ssidChars, doSetWiFiSSID2, exitEvent, wrapStyle)
,EDIT("PASS2:", tempWiFiPasswrd2, allChars, doSetWiFiPasswrd2, exitEvent, wrapStyle)
>>>>>>> 56fa71210855b96b41690aad2566683b671c8fe7
,EDIT("Host:", tempHostName, allChars, doSetHostName, exitEvent, wrapStyle)
#ifdef SUPPORT_OTA
,SUBMENU(activeOTAMenu)
#endif
,EXIT("<Back"));


Expand Down Expand Up @@ -927,9 +972,11 @@ void loadTempArraysWithActualValues() {
#endif

copyStringToCharArray(rightPad(wifiSSID, 30), tempWiFiSSID, 30, "tempWiFiSSID");
copyStringToCharArray(rightPad(wifiSSID2, 30), tempWiFiSSID2, 30, "tempWiFiSSID2");

#ifdef WIFI_PRIVACY
copyStringToCharArray(rightPad(" ", 30), tempWiFiPasswrd, 30, "tempWiFiPasswrd");
copyStringToCharArray(rightPad(" ", 30), tempWiFiPasswrd2, 30, "tempWiFiPasswrd2");
#else
copyStringToCharArray(rightPad(wifiPass, 30), tempWiFiPasswrd, 30, "tempWiFiPasswrd");
#endif
Expand Down Expand Up @@ -1000,8 +1047,18 @@ void menuLoop() {
}
}

if (millis() < timeInitializationCompleted + 5000) { // Wait 10 seconds before starting the menu to avoid issues with Improv-WiFi
return;
}

if (activeWIFI) {
activeMQTTMenu[0].enable();
} else {
activeMQTTMenu[0].disable();
}

#if defined(SUPPORT_TFT)
if (wifiChanged) {
if ((wifiChanged) && (!inMenu)) {
wifiChanged = false;
tft.fillScreen(TFT_BLACK);
}
Expand Down
5 changes: 5 additions & 0 deletions CO2_Gadget_Partitions-no_ota.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Extendend to 0x2D0000 for the program
# Name, Type, SubType, Offset, Size, Flags
nvs, data, nvs, 0x9000, 0x5000,
app0, app, ota_0, 0x10000, 0x2D0000,
spiffs, data, spiffs, 0x3D0000, 0x30000,
8 changes: 7 additions & 1 deletion CO2_Gadget_Preferences.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ void printPreferences() {
Serial.printf("-->[PREF] activeBLE is:\t#%s# (%d)\n", ((activeBLE) ? "Enabled" : "Disabled"), activeBLE);
Serial.printf("-->[PREF] activeWIFI is:\t#%s# (%d)\n", ((activeWIFI) ? "Enabled" : "Disabled"), activeWIFI);
Serial.printf("-->[PREF] activeMQTT is:\t#%s# (%d)\n", ((activeMQTT) ? "Enabled" : "Disabled"), activeMQTT);
Serial.printf("-->[PREF] activeESPNOW is:\t#%s# (%d)\n", ((activeMQTT) ? "Enabled" : "Disabled"), activeESPNOW);
Serial.printf("-->[PREF] activeESPNOW is:\t#%s# (%d)\n", ((activeESPNOW) ? "Enabled" : "Disabled"), activeESPNOW);
Serial.printf("-->[PREF] activeOTA is:\t#%s# (%d)\n", ((activeOTA) ? "Enabled" : "Disabled"), activeOTA);
Serial.printf("-->[PREF] rootTopic:\t#%s#\n", rootTopic.c_str());
Serial.printf("-->[PREF] batDischgd:\t #%d#\n", batteryDischargedMillivolts);
Serial.printf("-->[PREF] batChargd:\t #%d#\n", batteryFullyChargedMillivolts);
Expand Down Expand Up @@ -85,6 +86,7 @@ void initPreferences() {
activeWIFI = preferences.getBool("activeWIFI", true);
activeMQTT = preferences.getBool("activeMQTT", false);
activeESPNOW = preferences.getBool("activeESPNOW", false);
activeOTA = preferences.getBool("activeOTA", false);
rootTopic = preferences.getString("rootTopic", rootTopic);
mqttClientId = preferences.getString("mqttClientId", mqttClientId);
mqttBroker = preferences.getString("mqttBroker", mqttBroker).c_str();
Expand Down Expand Up @@ -183,6 +185,7 @@ void putPreferences() {
preferences.putBool("activeWIFI", activeWIFI);
preferences.putBool("activeMQTT", activeMQTT);
preferences.putBool("activeESPNOW", activeESPNOW);
preferences.putBool("activeOTA", activeOTA);
preferences.putString("rootTopic", rootTopic);
preferences.putUInt("batDischgd", batteryDischargedMillivolts);
preferences.putUInt("batChargd", batteryFullyChargedMillivolts);
Expand Down Expand Up @@ -237,6 +240,7 @@ String getPreferencesAsJson() {
doc["activeWIFI"] = preferences.getBool("activeWIFI", false);
doc["activeMQTT"] = preferences.getBool("activeMQTT", false);
doc["activeESPNOW"] = preferences.getBool("activeESPNOW", false);
doc["activeOTA"] = preferences.getBool("activeOTA", false);
doc["rootTopic"] = preferences.getString("rootTopic", rootTopic);
doc["batDischgd"] = preferences.getInt("batDischgd", 3500);
doc["batChargd"] = preferences.getInt("batChargd", 4200);
Expand Down Expand Up @@ -293,6 +297,7 @@ String getActualSettingsAsJson() {
doc["activeWIFI"] = activeWIFI;
doc["activeMQTT"] = activeMQTT;
doc["activeESPNOW"] = activeESPNOW;
doc["activeOTA"] = activeOTA;
doc["rootTopic"] = rootTopic;
doc["batDischgd"] = batteryDischargedMillivolts;
doc["batChargd"] = batteryFullyChargedMillivolts;
Expand Down Expand Up @@ -379,6 +384,7 @@ bool handleSavePreferencesfromJSON(String jsonPreferences) {
activeWIFI = JsonDocument["activeWIFI"];
activeMQTT = JsonDocument["activeMQTT"];
activeESPNOW = JsonDocument["activeESPNOW"];
activeOTA = JsonDocument["activeOTA"];
rootTopic = JsonDocument["rootTopic"].as<String>().c_str();
batteryDischargedMillivolts = JsonDocument["batDischgd"];
batteryFullyChargedMillivolts = JsonDocument["batChargd"];
Expand Down
59 changes: 48 additions & 11 deletions CO2_Gadget_WIFI.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ WiFiClient espClient;
WiFiMulti wifiMulti;
AsyncWebServer server(80);

// WiFi connect timeout per AP. Increase when connecting takes longer.
const uint32_t connectTimeoutMs = 10000;

void printSmallChar(char c, int row) {
switch (c) {
case '0':
Expand Down Expand Up @@ -635,18 +638,20 @@ boolean TimePeriodIsOver(unsigned long &startOfPeriod, unsigned long TimePeriod)
unsigned long MyTestTimer = 0; // Timer-variables MUST be of type unsigned long

void initWifi() {
if (wifiSSID == "") {
activeWIFI = false;
}
if (activeWIFI) {
wifiChanged = true;
troubledWIFI = false;
WiFiConnectionRetries = 0;
displayNotification("Init WiFi", notifyInfo);
Serial.print("-->[WiFi] Initializing WiFi...\n");
WiFi.disconnect(true); // disconnect form wifi to set new wifi connection
delay(500);
Serial.println("-->[WiFi] Initializing WiFi...");
// WiFi.disconnect(true); // disconnect form wifi to set new wifi connection
// delay(500);
WiFi.mode(WIFI_STA);
WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE);
Serial.printf("-->[WiFi] Setting hostname %s: %d\n", hostName.c_str(),
WiFi.setHostname(hostName.c_str()));
Serial.printf("-->[WiFi] Setting hostname %s: %d\n", hostName.c_str(), WiFi.setHostname(hostName.c_str()));

WiFi.onEvent(WiFiStationConnected, WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_CONNECTED);
WiFi.onEvent(WiFiStationGotIP, WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_GOT_IP);
Expand All @@ -657,10 +662,39 @@ void initWifi() {
// WiFi.setSleep(true);
// WiFi.setSleep(WIFI_PS_NONE);

String connectMessage = "-->[WiFi] Connecting to WiFi (SSID: " + String(wifiSSID) + ")\n";
String connectMessage = "-->[WiFi] Connecting to WiFi (SSID: " + String(wifiSSID) + " - PASS: " + String(wifiPass)
+" OR SSID2: " + String(wifiSSID2) + " - PASS2: " + String(wifiPass2) +")\n";
Serial.print(connectMessage);
WiFi.begin(wifiSSID.c_str(), wifiPass.c_str());

// WiFi.begin(wifiSSID.c_str(), wifiPass.c_str());

if(wifiSSID != "") wifiMulti.addAP(wifiSSID.c_str(), wifiPass.c_str());
if(wifiSSID2 != "") wifiMulti.addAP(wifiSSID2.c_str(), wifiPass2.c_str());

// WiFi.scanNetworks will return the number of networks found
/*
Serial.println("Scanning WiFi networks...");
int n = WiFi.scanNetworks();
if (n == 0) {
Serial.println("no networks found");
} else {
Serial.print(n);
Serial.println(" networks found");
for (int i = 0; i < n; ++i) {
// Print SSID and RSSI for each network found
Serial.print(i + 1);
Serial.print(": ");
Serial.print(WiFi.SSID(i));
Serial.print(" (");
Serial.print(WiFi.RSSI(i));
Serial.print(")");
Serial.println((WiFi.encryptionType(i) == WIFI_AUTH_OPEN)?" ":"*");
delay(10);
}
}
*/
// Connect to Wi-Fi using wifiMulti (connects to the SSID with strongest connection)
Serial.println("Connecting Wifi...");
wifiMulti.run(connectTimeoutMs);
// Wait for connection
while (WiFi.status() != WL_CONNECTED && WiFiConnectionRetries < maxWiFiConnectionRetries) {
yield(); // very important to execute yield to make it work
Expand All @@ -673,6 +707,7 @@ void initWifi() {
}
}
}

if ((WiFiConnectionRetries > maxWiFiConnectionRetries) && (WiFi.status() != WL_CONNECTED)) {
disableWiFi();
troubledWIFI = true;
Expand Down Expand Up @@ -721,23 +756,25 @@ void wifiClientLoop() {

// 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) {
if ((WiFi.SSID() != wifiSSID) && (!inMenu)) {
Serial.println("-->[WiFi] Wi-Fi SSID changed. Old SSID: " + wifiSSID + ", new SSID: " + WiFi.SSID());
wifiSSID = WiFi.SSID();
putPreferences();
// initWifi();
wifiChanged = true;
}

if (wifiChanged) {
if ((wifiChanged) && (!inMenu)) {
wifiChanged = false;
initWifi();
}
}

void OTALoop() {
#ifdef SUPPORT_OTA
AsyncElegantOTA.loop();
if ((activeWIFI) && (activeOTA) && (!troubledWIFI) && (WiFi.status() == WL_CONNECTED)) {
AsyncElegantOTA.loop();
}
#endif
}

Expand Down
Loading

0 comments on commit 764f1fa

Please sign in to comment.