Skip to content

Commit

Permalink
Add postDataToAirGradient checkbox to WiFi Manager
Browse files Browse the repository at this point in the history
  • Loading branch information
pnt325 committed Apr 10, 2024
1 parent b219521 commit c28a937
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 4 deletions.
2 changes: 1 addition & 1 deletion examples/OneOpenAir/OneOpenAir.ino
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ static AirGradient *ag;
static OledDisplay oledDisplay(configuration, measurements, Serial);
static StateMachine stateMachine(oledDisplay, Serial, measurements,
configuration);
static WifiConnector wifiConnector(oledDisplay, Serial, stateMachine);
static WifiConnector wifiConnector(oledDisplay, Serial, stateMachine, configuration);
static OpenMetrics openMetrics(measurements, configuration, wifiConnector,
apiClient);
static LocalServer localServer(Serial, openMetrics, measurements, configuration,
Expand Down
10 changes: 10 additions & 0 deletions src/AgConfigure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -728,3 +728,13 @@ void Configuration::jsonInvalid(void) {
}

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));
}
}
1 change: 1 addition & 0 deletions src/AgConfigure.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class Configuration : public PrintLog {
String getModel(void);
bool isUpdated(void);
String getFailedMesage(void);
void setPostToAirGradient(bool enable);
};

#endif /** _AG_CONFIG_H_ */
13 changes: 11 additions & 2 deletions src/AgWiFiConnector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ void WifiConnector::setAirGradient(AirGradient *ag) { this->ag = ag; }
* @param log Stream
* @param sm StateMachine
*/
WifiConnector::WifiConnector(OledDisplay &disp, Stream &log, StateMachine &sm)
: PrintLog(log, "WifiConnector"), disp(disp), sm(sm) {}
WifiConnector::WifiConnector(OledDisplay &disp, Stream &log, StateMachine &sm,Configuration& config)
: PrintLog(log, "WifiConnector"), disp(disp), sm(sm), config(config) {}
#else
WifiConnector::WifiConnector(Stream &log) : PrintLog(log, "WiFiConnector") {}
#endif
Expand Down Expand Up @@ -61,6 +61,11 @@ bool WifiConnector::connect(void) {
ssid = "AG-" + String(ESP.getChipId(), HEX);
#endif
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);

#ifdef ESP32
Expand Down Expand Up @@ -134,6 +139,10 @@ bool WifiConnector::connect(void) {
} else {
hasConfig = true;
logInfo("WiFi Connected: " + WiFi.SSID() + " IP: " + localIpStr());

String result = String(postToAg.getValue());
logInfo("Post to AirGradient Configure: " + result);
config.setPostToAirGradient(result == "T");
}
#else
_wifiProcess();
Expand Down
4 changes: 3 additions & 1 deletion src/AgWiFiConnector.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "AgOledDisplay.h"
#include "AgStateMachine.h"
#include "AirGradient.h"
#include "AgConfigure.h"
#include "Main/PrintLog.h"

#include <Arduino.h>
Expand All @@ -14,6 +15,7 @@ class WifiConnector : public PrintLog {
#ifdef ESP32
OledDisplay &disp;
StateMachine &sm;
Configuration &config;
#else
void displayShowText(String ln1, String ln2, String ln3);
#endif
Expand All @@ -27,7 +29,7 @@ class WifiConnector : public PrintLog {
public:
void setAirGradient(AirGradient *ag);
#ifdef ESP32
WifiConnector(OledDisplay &disp, Stream &log, StateMachine &sm);
WifiConnector(OledDisplay &disp, Stream &log, StateMachine &sm, Configuration& config);
#else
WifiConnector(Stream &log);
#endif
Expand Down

0 comments on commit c28a937

Please sign in to comment.