Add postDataToAirGradient checkbox to WiFi Manager

This commit is contained in:
Phat Nguyen
2024-04-11 06:33:56 +07:00
parent b2195219ab
commit c28a937384
5 changed files with 26 additions and 4 deletions

View File

@ -82,7 +82,7 @@ static AirGradient *ag;
static OledDisplay oledDisplay(configuration, measurements, Serial); static OledDisplay oledDisplay(configuration, measurements, Serial);
static StateMachine stateMachine(oledDisplay, Serial, measurements, static StateMachine stateMachine(oledDisplay, Serial, measurements,
configuration); configuration);
static WifiConnector wifiConnector(oledDisplay, Serial, stateMachine); static WifiConnector wifiConnector(oledDisplay, Serial, stateMachine, configuration);
static OpenMetrics openMetrics(measurements, configuration, wifiConnector, static OpenMetrics openMetrics(measurements, configuration, wifiConnector,
apiClient); apiClient);
static LocalServer localServer(Serial, openMetrics, measurements, configuration, static LocalServer localServer(Serial, openMetrics, measurements, configuration,

View File

@ -728,3 +728,13 @@ void Configuration::jsonInvalid(void) {
} }
String Configuration::getFailedMesage(void) { return failedMessage; } String Configuration::getFailedMesage(void) { return failedMessage; }
void Configuration::setPostToAirGradient(bool enable) {
if (enable != config.postDataToAirGradient) {
config.postDataToAirGradient = enable;
logInfo("postDataToAirGradient set to: " + String(enable));
saveConfig();
} else {
logInfo("postDataToAirGradient: Ignored set to " + String(enable));
}
}

View File

@ -74,6 +74,7 @@ public:
String getModel(void); String getModel(void);
bool isUpdated(void); bool isUpdated(void);
String getFailedMesage(void); String getFailedMesage(void);
void setPostToAirGradient(bool enable);
}; };
#endif /** _AG_CONFIG_H_ */ #endif /** _AG_CONFIG_H_ */

View File

@ -21,8 +21,8 @@ void WifiConnector::setAirGradient(AirGradient *ag) { this->ag = ag; }
* @param log Stream * @param log Stream
* @param sm StateMachine * @param sm StateMachine
*/ */
WifiConnector::WifiConnector(OledDisplay &disp, Stream &log, StateMachine &sm) WifiConnector::WifiConnector(OledDisplay &disp, Stream &log, StateMachine &sm,Configuration& config)
: PrintLog(log, "WifiConnector"), disp(disp), sm(sm) {} : PrintLog(log, "WifiConnector"), disp(disp), sm(sm), config(config) {}
#else #else
WifiConnector::WifiConnector(Stream &log) : PrintLog(log, "WiFiConnector") {} WifiConnector::WifiConnector(Stream &log) : PrintLog(log, "WiFiConnector") {}
#endif #endif
@ -61,6 +61,11 @@ bool WifiConnector::connect(void) {
ssid = "AG-" + String(ESP.getChipId(), HEX); ssid = "AG-" + String(ESP.getChipId(), HEX);
#endif #endif
WIFI()->setConfigPortalTimeout(WIFI_CONNECT_COUNTDOWN_MAX); WIFI()->setConfigPortalTimeout(WIFI_CONNECT_COUNTDOWN_MAX);
WiFiManagerParameter lineBreak("<p>Check to enabled post data to AirGradient cloud</p>");
WIFI()->addParameter(&lineBreak);
WiFiManagerParameter postToAg("chbPostToAg", "Post To AirGradient", "T", 2, "type=\"checkbox\" ", WFM_LABEL_AFTER);
WIFI()->addParameter(&postToAg);
WIFI()->autoConnect(ssid.c_str(), WIFI_HOTSPOT_PASSWORD_DEFAULT); WIFI()->autoConnect(ssid.c_str(), WIFI_HOTSPOT_PASSWORD_DEFAULT);
#ifdef ESP32 #ifdef ESP32
@ -134,6 +139,10 @@ bool WifiConnector::connect(void) {
} else { } else {
hasConfig = true; hasConfig = true;
logInfo("WiFi Connected: " + WiFi.SSID() + " IP: " + localIpStr()); logInfo("WiFi Connected: " + WiFi.SSID() + " IP: " + localIpStr());
String result = String(postToAg.getValue());
logInfo("Post to AirGradient Configure: " + result);
config.setPostToAirGradient(result == "T");
} }
#else #else
_wifiProcess(); _wifiProcess();

View File

@ -4,6 +4,7 @@
#include "AgOledDisplay.h" #include "AgOledDisplay.h"
#include "AgStateMachine.h" #include "AgStateMachine.h"
#include "AirGradient.h" #include "AirGradient.h"
#include "AgConfigure.h"
#include "Main/PrintLog.h" #include "Main/PrintLog.h"
#include <Arduino.h> #include <Arduino.h>
@ -14,6 +15,7 @@ private:
#ifdef ESP32 #ifdef ESP32
OledDisplay &disp; OledDisplay &disp;
StateMachine &sm; StateMachine &sm;
Configuration &config;
#else #else
void displayShowText(String ln1, String ln2, String ln3); void displayShowText(String ln1, String ln2, String ln3);
#endif #endif
@ -27,7 +29,7 @@ private:
public: public:
void setAirGradient(AirGradient *ag); void setAirGradient(AirGradient *ag);
#ifdef ESP32 #ifdef ESP32
WifiConnector(OledDisplay &disp, Stream &log, StateMachine &sm); WifiConnector(OledDisplay &disp, Stream &log, StateMachine &sm, Configuration& config);
#else #else
WifiConnector(Stream &log); WifiConnector(Stream &log);
#endif #endif