Add DiyProIndoorV4_2.ino example

This commit is contained in:
Phat Nguyen
2024-06-15 15:40:50 +07:00
parent 227a4f76f7
commit bd237ed95d
14 changed files with 1214 additions and 78 deletions

View File

@ -43,7 +43,7 @@ JSON_PROP_DEF(ledBarTestRequested);
JSON_PROP_DEF(offlineMode);
#define jprop_model_default ""
#define jprop_country_default ""
#define jprop_country_default "TH"
#define jprop_pmStandard_default getPMStandardString(false)
#define jprop_ledBarMode_default getLedBarModeName(LedBarMode::LedBarModeCO2)
#define jprop_abcDays_default 8
@ -614,15 +614,18 @@ bool Configuration::parse(String data, bool isLocal) {
}
}
if (JSON.typeof_(root["targetFirmware"]) == "string") {
String newVer = root["targetFirmware"];
String curVer = String(GIT_VERSION);
if (curVer != newVer) {
logInfo("Detected new firmware version: " + newVer);
otaNewFirmwareVersion = newVer;
udpated = true;
} else {
otaNewFirmwareVersion = String("");
if (ag->getBoardType() == ONE_INDOOR ||
ag->getBoardType() == OPEN_AIR_OUTDOOR) {
if (JSON.typeof_(root["targetFirmware"]) == "string") {
String newVer = root["targetFirmware"];
String curVer = String(GIT_VERSION);
if (curVer != newVer) {
logInfo("Detected new firmware version: " + newVer);
otaNewFirmwareVersion = newVer;
udpated = true;
} else {
otaNewFirmwareVersion = String("");
}
}
}

View File

@ -42,6 +42,7 @@ public:
bool hasSensorS8 = true;
bool hasSensorPMS1 = true;
bool hasSensorPMS2 = true;
bool hasSensorPMS = true;
bool hasSensorSGP = true;
bool hasSensorSHT = true;

View File

@ -224,7 +224,7 @@ void StateMachine::co2Calibration(void) {
/** Count down to 0 then start */
for (int i = 0; i < SENSOR_CO2_CALIB_COUNTDOWN_MAX; i++) {
if (ag->isOne()) {
if (ag->isOne() || (ag->getBoardType() == DIY_PRO_INDOOR_V4_2)) {
String str =
"after " + String(SENSOR_CO2_CALIB_COUNTDOWN_MAX - i) + " sec";
disp.setText("Start CO2 calib", str.c_str(), "");
@ -236,13 +236,13 @@ void StateMachine::co2Calibration(void) {
}
if (ag->s8.setBaselineCalibration()) {
if (ag->isOne()) {
if (ag->isOne() || (ag->getBoardType() == DIY_PRO_INDOOR_V4_2)) {
disp.setText("Calibration", "success", "");
} else {
logInfo("CO2 Calibration: success");
}
delay(1000);
if (ag->isOne()) {
if (ag->isOne() || (ag->getBoardType() == DIY_PRO_INDOOR_V4_2)) {
disp.setText("Wait for", "calib finish", "...");
} else {
logInfo("CO2 Calibration: Wait for calibration finish...");
@ -254,7 +254,7 @@ void StateMachine::co2Calibration(void) {
delay(1000);
count++;
}
if (ag->isOne()) {
if (ag->isOne() || (ag->getBoardType() == DIY_PRO_INDOOR_V4_2)) {
String str = "after " + String(count);
disp.setText("Calib finish", str.c_str(), "sec");
} else {
@ -262,7 +262,7 @@ void StateMachine::co2Calibration(void) {
}
delay(2000);
} else {
if (ag->isOne()) {
if (ag->isOne() || (ag->getBoardType() == DIY_PRO_INDOOR_V4_2)) {
disp.setText("Calibration", "failure!!!", "");
} else {
logInfo("CO2 Calibration: failure!!!");
@ -399,7 +399,7 @@ StateMachine::~StateMachine() {}
*/
void StateMachine::displayHandle(AgStateMachineState state) {
// Ignore handle if not ONE_INDOOR board
if (!ag->isOne()) {
if (!(ag->isOne() || (ag->getBoardType() == DIY_PRO_INDOOR_V4_2))) {
if (state == AgStateMachineCo2Calibration) {
co2Calibration();
}

View File

@ -19,8 +19,8 @@ String Measurements::toString(bool localServer, AgFirmwareMode fwMode, int rssi,
}
}
if (ag->isOne()) {
if (config->hasSensorPMS1) {
if (ag->isOne() || (ag->getBoardType() == DIY_PRO_INDOOR_V4_2)) {
if (config->hasSensorPMS1 || config->hasSensorPMS) {
if (this->pm01_1 >= 0) {
root["pm01"] = this->pm01_1;
}

View File

@ -13,7 +13,6 @@
*/
void WifiConnector::setAirGradient(AirGradient *ag) { this->ag = ag; }
#ifdef ESP32
/**
* @brief Construct a new Ag Wi Fi Connector:: Ag Wi Fi Connector object
*
@ -24,9 +23,10 @@ void WifiConnector::setAirGradient(AirGradient *ag) { this->ag = ag; }
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
// #ifdef ESP8266
// WifiConnector::WifiConnector(Stream &log) : PrintLog(log, "WiFiConnector") {}
// #endif
WifiConnector::~WifiConnector() {}
@ -49,20 +49,18 @@ bool WifiConnector::connect(void) {
WIFI()->setConnectTimeout(15);
WIFI()->setTimeout(WIFI_CONNECT_COUNTDOWN_MAX);
#ifdef ESP32
WIFI()->setAPCallback([this](WiFiManager *obj) { _wifiApCallback(); });
WIFI()->setSaveConfigCallback([this]() { _wifiSaveConfig(); });
WIFI()->setSaveParamsCallback([this]() { _wifiSaveParamCallback(); });
WIFI()->setConfigPortalTimeoutCallback([this](){});
if (ag->isOne()) {
WIFI()->setConfigPortalTimeoutCallback([this]() {});
if (ag->isOne() || (ag->getBoardType() == DIY_PRO_INDOOR_V4_2)) {
disp.setText("Connecting to", "WiFi", "...");
} else {
logInfo("Connecting to WiFi...");
}
ssid = "airgradient-" + ag->deviceId();
#else
ssid = "AG-" + String(ESP.getChipId(), HEX);
#endif
// ssid = "AG-" + String(ESP.getChipId(), HEX);
WIFI()->setConfigPortalTimeout(WIFI_CONNECT_COUNTDOWN_MAX);
WiFiManagerParameter postToAg("chbPostToAg",
@ -78,6 +76,8 @@ bool WifiConnector::connect(void) {
WIFI()->autoConnect(ssid.c_str(), WIFI_HOTSPOT_PASSWORD_DEFAULT);
logInfo("Wait for configure portal");
#ifdef ESP32
// Task handle WiFi connection.
xTaskCreate(
@ -139,10 +139,14 @@ bool WifiConnector::connect(void) {
delay(1); // avoid watchdog timer reset.
}
#else
_wifiProcess();
#endif
/** Show display wifi connect result failed */
if (WiFi.isConnected() == false) {
sm.handleLeds(AgStateMachineWiFiManagerConnectFailed);
if (ag->isOne()) {
if (ag->isOne() || ag->getBoardType() == DIY_PRO_INDOOR_V4_2) {
sm.displayHandle(AgStateMachineWiFiManagerConnectFailed);
}
delay(6000);
@ -160,9 +164,7 @@ bool WifiConnector::connect(void) {
}
hasPortalConfig = false;
}
#else
_wifiProcess();
#endif
return true;
}
@ -179,20 +181,20 @@ void WifiConnector::disconnect(void) {
#ifdef ESP32
#else
void WifiConnector::displayShowText(String ln1, String ln2, String ln3) {
char buf[9];
ag->display.clear();
// void WifiConnector::displayShowText(String ln1, String ln2, String ln3) {
// char buf[9];
// ag->display.clear();
ag->display.setCursor(1, 1);
ag->display.setText(ln1);
ag->display.setCursor(1, 19);
ag->display.setText(ln2);
ag->display.setCursor(1, 37);
ag->display.setText(ln3);
// ag->display.setCursor(1, 1);
// ag->display.setText(ln1);
// ag->display.setCursor(1, 19);
// ag->display.setText(ln2);
// ag->display.setCursor(1, 37);
// ag->display.setText(ln3);
ag->display.show();
delay(100);
}
// ag->display.show();
// delay(100);
// }
#endif
/**
@ -205,7 +207,6 @@ bool WifiConnector::wifiClientConnected(void) {
return WiFi.softAPgetStationNum() ? true : false;
}
#ifdef ESP32
/**
* @brief Handle WiFiManage softAP setup completed callback
*
@ -247,7 +248,7 @@ bool WifiConnector::_wifiConfigPortalActive(void) {
return WIFI()->getConfigPortalActive();
}
void WifiConnector::_wifiTimeoutCallback(void) { connectorTimeout = true; }
#endif
/**
* @brief Process WiFiManager connection
*
@ -256,34 +257,66 @@ void WifiConnector::_wifiProcess() {
#ifdef ESP32
WIFI()->process();
#else
int count = WIFI_CONNECT_COUNTDOWN_MAX;
displayShowText(String(WIFI_CONNECT_COUNTDOWN_MAX) + " sec", "SSID:", ssid);
/** Wait for WiFi connect and show LED, display status */
uint32_t dispPeriod = millis();
uint32_t ledPeriod = millis();
bool clientConnectChanged = false;
AgStateMachineState stateOld = sm.getDisplayState();
while (WIFI()->getConfigPortalActive()) {
WIFI()->process();
uint32_t lastTime = millis();
uint32_t ms = (uint32_t)(millis() - lastTime);
if (ms >= 1000) {
lastTime = millis();
if (WiFi.isConnected() == false) {
/** Display countdown */
uint32_t ms;
if (ag->isOne() || (ag->getBoardType() == DIY_PRO_INDOOR_V4_2)) {
ms = (uint32_t)(millis() - dispPeriod);
if (ms >= 1000) {
dispPeriod = millis();
sm.displayHandle();
logInfo("displayHandle state: " + String(sm.getDisplayState()));
} else {
if (stateOld != sm.getDisplayState()) {
stateOld = sm.getDisplayState();
sm.displayHandle();
}
}
}
displayShowText(String(count) + " sec", "SSID:", ssid);
/** LED animations */
ms = (uint32_t)(millis() - ledPeriod);
if (ms >= 100) {
ledPeriod = millis();
sm.handleLeds();
}
count--;
// Timeout
if (count == 0) {
break;
/** Check for client connect to change led color */
bool clientConnected = wifiClientConnected();
if (clientConnected != clientConnectChanged) {
clientConnectChanged = clientConnected;
if (clientConnectChanged) {
sm.handleLeds(AgStateMachineWiFiManagerPortalActive);
} else {
sm.ledAnimationInit();
sm.handleLeds(AgStateMachineWiFiManagerMode);
if (ag->isOne()) {
sm.displayHandle(AgStateMachineWiFiManagerMode);
}
}
}
}
delay(1);
}
if (!WiFi.isConnected()) {
displayShowText("Booting", "offline", "mode");
Serial.println("failed to connect and hit timeout");
delay(2500);
} else {
hasConfig = true;
}
// TODO This is for basic
// if (!WiFi.isConnected()) {
// disp.setText("Booting", "offline", "mode");
// Serial.println("failed to connect and hit timeout");
// delay(2500);
// } else {
// hasConfig = true;
// }
#endif
}
@ -344,7 +377,7 @@ String WifiConnector::localIpStr(void) { return WiFi.localIP().toString(); }
/**
* @brief Get status that wifi has configurated
*
*
* @return true Configurated
* @return false Not Configurated
*/
@ -357,8 +390,8 @@ bool WifiConnector::hasConfigurated(void) {
/**
* @brief Get WiFi connection porttal timeout.
*
* @return true
* @return false
*
* @return true
* @return false
*/
bool WifiConnector::isConfigurePorttalTimeout(void) { return connectorTimeout; }

View File

@ -12,13 +12,10 @@
class WifiConnector : public PrintLog {
private:
AirGradient *ag;
#ifdef ESP32
OledDisplay &disp;
StateMachine &sm;
Configuration &config;
#else
void displayShowText(String ln1, String ln2, String ln3);
#endif
String ssid;
void *wifi = NULL;
bool hasConfig;
@ -30,23 +27,21 @@ private:
public:
void setAirGradient(AirGradient *ag);
#ifdef ESP32
WifiConnector(OledDisplay &disp, Stream &log, StateMachine &sm, Configuration& config);
#else
WifiConnector(Stream &log);
#ifdef ESP8266
// WifiConnector(Stream &log);
#endif
~WifiConnector();
bool connect(void);
void disconnect(void);
void handle(void);
#ifdef ESP32
void _wifiApCallback(void);
void _wifiSaveConfig(void);
void _wifiSaveParamCallback(void);
bool _wifiConfigPortalActive(void);
void _wifiTimeoutCallback(void);
#endif
void _wifiProcess();
bool isConnected(void);
void reset(void);

View File

@ -14,6 +14,8 @@ const char *AgFirmwareModeName(AgFirmwareMode mode) {
return "0-1PS";
case FW_MODE_O_1P:
return "O-1P";
case FW_MODE_I_8PSL:
return "I-8PSL";
default:
break;
}

View File

@ -101,6 +101,7 @@ enum AgFirmwareMode {
FW_MODE_O_1PP, /** PMS5003T_1, PMS5003T_2 */
FW_MODE_O_1PS, /** PMS5003T, S8 */
FW_MODE_O_1P, /** PMS5003T */
FW_MODE_I_8PSL, /** DIY_PRO 4.2 */
};
const char *AgFirmwareModeName(AgFirmwareMode mode);

View File

@ -116,6 +116,9 @@ void LedBar::setColor(uint8_t red, uint8_t green, uint8_t blue) {
*/
void LedBar::show(void) {
// Ignore update the LED if LED bar disabled
if(this->isBegin() == false) {
return;
}
if (enabled == false) {
return;
}