Skip to content

Commit

Permalink
Add web based configuration at http://ip/preferences.html (WIP) (#117) (
Browse files Browse the repository at this point in the history
#118)

* Update CO2_GADGET_REV to "014-web-config-new" in platformio.ini

* Add preferences page and handler for saving preferences

* Add onload function to fetch CO2, temperature, and humidity data

* Display Git HEAD in setup (Add .py and platformio_extra_configs.ini to .gitignore)

* Modify preferences.html

* Load preferences form (first version, non functional)

* Add ArduinoJson library and implement API endpoint to get preferences as JSON

* Preferences received on Save Preferences from configuration web page

* Update CO2_GADGET_REV to "020-web-config-new" in platformio.ini

* Update CO2_GADGET_REV to "024-web-config-new"

* Enable mDNS support for WiFi

* Increase size of DynamicJsonDocument in getPreferencesAsJson() function

* First rude web-config working (WIP no tested. no error checking)

* Enable utilityLoop() function in main loop

* Update publish functions to use int64_t and fix MQTT publish calls

* Add Restart ESP32 button to settings form. CSS styles

* Fix altitude misspelling

* Fix tempOffset assignment and update sensors

* Update vRef and battery voltage calculation on change from web settings page

* Add flash memory information to setup() function

* Fix utilityLoop()

---------

Co-authored-by: Mario Mariete <[email protected]>
  • Loading branch information
melkati and melkati authored Jan 14, 2024
1 parent 30cd603 commit f2d07c5
Show file tree
Hide file tree
Showing 11 changed files with 1,247 additions and 252 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
*.old
*.bin
*.ino.cpp
*.py
Bootlogo
platformio_extra_configs.ini
extra_envs.ini
credentials.h

Expand Down
80 changes: 51 additions & 29 deletions CO2_Gadget.ino
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ uint64_t lastTimeESPNowPublished = 0; // Time of last ESP-NOW transmission
// #include <WiFiUdp.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>

#include "AsyncJson.h"
#ifdef SUPPORT_OTA
#include <AsyncElegantOTA.h>
#endif
Expand All @@ -137,9 +139,32 @@ bool displayNotification(String notificationText, notificationTypes notification
/********* SETUP SENSORS *********/
/********* *********/
/*****************************************************************************************************/

#include <CO2_Gadget_Sensors.h>

/*****************************************************************************************************/
/********* *********/
/********* INCLUDE BATTERY FUNCTIONALITY *********/
/********* *********/
/*****************************************************************************************************/
uint16_t vRef = 1100;
uint16_t batteryDischargedMillivolts = 3500; // Voltage of battery when we consider it discharged (0%).
uint16_t batteryFullyChargedMillivolts = 4200; // Voltage of battery when it is considered fully charged (100%).
#include "CO2_Gadget_Battery.h"

/*****************************************************************************************************/
/********* *********/
/********* SETUP NEOPIXEL (ES2812b AND OTHERS) LED FUNCTIONALITY *********/
/********* *********/
/*****************************************************************************************************/
#include "CO2_Gadget_Neopixel.h"

/*****************************************************************************************************/
/********* *********/
/********* FUNCTIONALITY TO STORE PREFERENCES IN NON VOLATILE MEMORY *********/
/********* *********/
/*****************************************************************************************************/
#include "CO2_Gadget_Preferences.h"

/*****************************************************************************************************/
/********* *********/
/********* INCLUDE WIFI FUNCTIONALITY *********/
Expand Down Expand Up @@ -179,16 +204,6 @@ bool displayNotification(String notificationText, notificationTypes notification
#include <AsyncElegantOTA.h>
#endif

/*****************************************************************************************************/
/********* *********/
/********* INCLUDE BATTERY FUNCTIONALITY *********/
/********* *********/
/*****************************************************************************************************/
uint16_t vRef = 1100;
uint16_t batteryDischargedMillivolts = 3500; // Voltage of battery when we consider it discharged (0%).
uint16_t batteryFullyChargedMillivolts = 4200; // Voltage of battery when it is considered fully charged (100%).
#include "CO2_Gadget_Battery.h"

/*****************************************************************************************************/
/********* *********/
/********* INCLUDE OLED DISPLAY FUNCTIONALITY (UNFINISHED WIP) *********/
Expand All @@ -207,20 +222,6 @@ uint16_t batteryFullyChargedMillivolts = 4200; // Voltage of battery when it is
#include "CO2_Gadget_TFT.h"
#endif

/*****************************************************************************************************/
/********* *********/
/********* SETUP NEOPIXEL (ES2812b AND OTHERS) LED FUNCTIONALITY *********/
/********* *********/
/*****************************************************************************************************/
#include "CO2_Gadget_Neopixel.h"

/*****************************************************************************************************/
/********* *********/
/********* FUNCTIONALITY TO STORE PREFERENCES IN NON VOLATILE MEMORY *********/
/********* *********/
/*****************************************************************************************************/
#include "CO2_Gadget_Preferences.h"

/*****************************************************************************************************/
/********* *********/
/********* INCLUDE MENU FUNCIONALITY *********/
Expand Down Expand Up @@ -381,34 +382,55 @@ void batteryLoop() {
}

void utilityLoop() {
const int16_t actualCPUFrequency = 0;
static float lastCheckedVoltage = 0;
int16_t actualCPUFrequency = getCpuFrequencyMhz();

if (battery_voltage > 4.5 && actualCPUFrequency != 240) {
Serial.printf("-->[BATT] Battery voltage: %.2fV. Increasing CPU frequency to 240MHz\n", battery_voltage);
Serial.flush();
Serial.end();
setCpuFrequencyMhz(240); // High CPU frequency when working on USB power
Serial.begin(115200);
} else if (actualCPUFrequency != 80) {
lastCheckedVoltage = battery_voltage;
} else if (battery_voltage < 4.5 && actualCPUFrequency != 80) {
Serial.printf("-->[BATT] Battery voltage: %.2fV. Decreasing CPU frequency to 80MHz\n", battery_voltage);
Serial.flush();
Serial.end();
setCpuFrequencyMhz(80); // Lower CPU frequency to reduce power consumption
Serial.begin(115200);
lastCheckedVoltage = battery_voltage;
} else if (battery_voltage != lastCheckedVoltage) {
// The voltage has changed, but the CPU frequency is already at the desired value.
// Handle any additional actions needed in this case.
lastCheckedVoltage = battery_voltage;
}
}


// application entry point
void setup() {
uint32_t brown_reg_temp = READ_PERI_REG(RTC_CNTL_BROWN_OUT_REG); // save WatchDog register
WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0); // disable brownout detector
setCpuFrequencyMhz(80); // Lower CPU frecuency to reduce power consumption
Serial.begin(115200);
delay(100);
delay(50);
#ifdef AUTO_VERSION
Serial.printf("\n-->[STUP] CO2 Gadget Version: %s%s Flavour: %s (Git HEAD: %s)\n", CO2_GADGET_VERSION, CO2_GADGET_REV, FLAVOUR, AUTO_VERSION);
#else
Serial.printf("\n-->[STUP] CO2 Gadget Version: %s%s Flavour: %s\n", CO2_GADGET_VERSION, CO2_GADGET_REV, FLAVOUR);
#endif
Serial.printf("-->[STUP] Version compiled: %s at %s\n", __DATE__, __TIME__);
Serial.printf("-->[STUP] Total heap: %d\n", ESP.getHeapSize());
Serial.printf("-->[STUP] Free heap: %d\n", ESP.getFreeHeap());
Serial.printf("-->[STUP] Total PSRAM: %d\n", ESP.getPsramSize());
Serial.printf("-->[STUP] Free PSRAM: %d\n", ESP.getFreePsram());

// Get the size of the flash memory
uint32_t flash_size = ESP.getFlashChipSize();
Serial.printf("-->[STUP] Flash size: %d\n", flash_size);
Serial.printf("-->[STUP] Flash speed: %d\n", ESP.getFlashChipSpeed());
Serial.printf("-->[STUP] Flash mode: %d\n", ESP.getFlashChipMode());

Serial.printf("-->[STUP] Starting up...\n\n");

initPreferences();
Expand Down Expand Up @@ -439,7 +461,7 @@ void loop() {
mqttClientLoop();
sensorsLoop();
readBatteryVoltage();
// utilityLoop();
utilityLoop();
outputsLoop();
processPendingCommands();
readingsLoop();
Expand Down
10 changes: 5 additions & 5 deletions CO2_Gadget_MQTT.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,15 @@ void callbackMQTT(char *topic, byte *message, unsigned int length) {
}
}

void publishIntMQTT(String topic, int16_t payload) {
void publishIntMQTT(String topic, int64_t payload) {
#ifdef SUPPORT_MQTT
dtostrf(payload, 0, 0, charPublish);
topic = rootTopic + topic;
if (!inMenu) {
Serial.printf("-->[MQTT] Publishing %d to ", payload);
Serial.println("topic: " + topic);
mqttClient.publish((topic).c_str(), charPublish);
}
mqttClient.publish((topic).c_str(), charPublish);
#endif
}

Expand All @@ -106,8 +106,8 @@ void publishFloatMQTT(String topic, float payload) {
if (!inMenu) {
Serial.printf("-->[MQTT] Publishing %.0f to ", payload);
Serial.println("topic: " + topic);
mqttClient.publish((topic).c_str(), charPublish);
}
mqttClient.publish((topic).c_str(), charPublish);
#endif
}

Expand All @@ -117,8 +117,8 @@ void publishStrMQTT(String topic, String payload) {
if (!inMenu) {
Serial.printf("-->[MQTT] Publishing %s to ", payload.c_str());
Serial.println("topic: " + topic);
mqttClient.publish(topic.c_str(), payload.c_str());
}
mqttClient.publish(topic.c_str(), payload.c_str());
#endif
}

Expand All @@ -127,8 +127,8 @@ void publishStrDiscoveryMQTT(String topic, String payload, int qos) {
if (!inMenu) {
// Serial.printf("-->[MQTT] Publishing discovery %s to ", payload.c_str());
// Serial.println("topic: " + topic);
mqttClient.publish(topic.c_str(), payload.c_str(), true);
}
mqttClient.publish(topic.c_str(), payload.c_str(), true);
#endif
}

Expand Down
2 changes: 1 addition & 1 deletion CO2_Gadget_Menu.h
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ MENU(CO2SensorConfigMenu, "CO2 Sensor", doNothing, noEvent, wrapStyle
,SUBMENU(CO2SensorChooseMenu)
,SUBMENU(autoSelfCalibrationMenu)
,FIELD(ambientPressureValue, "Pres. Comp.", "mbar", 0, 2000, 10, 10, doNothing, noEvent, noStyle)
,FIELD(altidudeMeters, "Altitude", "mtrs", 0, 9999, 10, 10, doNothing, noEvent, noStyle)
,FIELD(altitudeMeters, "Altitude", "mtrs", 0, 9999, 10, 10, doNothing, noEvent, noStyle)
,FIELD(co2OrangeRange, "Orange", "ppm", 400, 2000, 10, 10, doNothing, noEvent, noStyle)
,FIELD(co2RedRange, "Red", "ppm", 400, 2000, 10, 10, doNothing, noEvent, noStyle)
,SUBMENU(debugSensorsMenu)
Expand Down
Loading

0 comments on commit f2d07c5

Please sign in to comment.