Compare commits

...

1 Commits

Author SHA1 Message Date
samuelbles07
ba250787e6 WIP - trial energy saving 2025-10-13 02:58:11 +07:00
5 changed files with 99 additions and 22 deletions

View File

@@ -175,6 +175,9 @@ AgSchedule checkForUpdateSchedule(FIRMWARE_CHECK_FOR_UPDATE_MS, checkForFirmware
AgSchedule networkSignalCheckSchedule(10000, networkSignalCheck);
AgSchedule printMeasurementsSchedule(6000, printMeasurements);
static int pmsValueTaken = 0;
void setup() {
/** Serial for print debug message */
Serial.begin(115200);
@@ -275,6 +278,9 @@ void setup() {
}
configSchedule.setPeriod(CELLULAR_TRANSMISSION_INTERVAL);
transmissionSchedule.setPeriod(CELLULAR_TRANSMISSION_INTERVAL);
if (networkOption == UseCellular) {
// If using cellular re-set scheduler interval
configSchedule.setPeriod(CELLULAR_SERVER_CONFIG_SYNC_INTERVAL);
@@ -347,21 +353,24 @@ void loop() {
if (configuration.hasSensorSGP) {
tvocSchedule.run();
}
if (ag->isOne()) {
if (configuration.hasSensorPMS1) {
ag->pms5003.handle();
static bool pmsConnected = false;
if (pmsConnected != ag->pms5003.connected()) {
pmsConnected = ag->pms5003.connected();
Serial.printf("PMS sensor %s \n", pmsConnected?"connected":"removed");
if (pmsValueTaken < 60) {
if (ag->isOne()) {
if (configuration.hasSensorPMS1) {
ag->pms5003.handle();
static bool pmsConnected = false;
if (pmsConnected != ag->pms5003.connected()) {
pmsConnected = ag->pms5003.connected();
Serial.printf("PMS sensor %s \n", pmsConnected?"connected":"removed");
}
}
} else {
if (configuration.hasSensorPMS1) {
ag->pms5003t_1.handle();
}
if (configuration.hasSensorPMS2) {
ag->pms5003t_2.handle();
}
}
} else {
if (configuration.hasSensorPMS1) {
ag->pms5003t_1.handle();
}
if (configuration.hasSensorPMS2) {
ag->pms5003t_2.handle();
}
}
@@ -1239,6 +1248,16 @@ static void updateTvoc(void) {
}
static void updatePMS5003() {
pmsValueTaken++;
if (pmsValueTaken >= 60) {
if (pmsValueTaken == 60) {
ag->pms5003.sleep();
Serial.println("PMS go sleep");
}
return;
}
if (ag->pms5003.connected()) {
measurements.update(Measurements::PM01, ag->pms5003.getPm01Ae());
measurements.update(Measurements::PM25, ag->pms5003.getPm25Ae());
@@ -1461,6 +1480,11 @@ void sendDataToServer(void) {
} else if (networkOption == UseCellular) {
postUsingCellular(false);
}
pmsValueTaken = 0;
ag->pms5003.wakeUp();
ag->pms5003.activeMode();
Serial.println("run PMS again");
}
static void tempHumUpdate(void) {

View File

@@ -21,10 +21,15 @@ bool PMSBase::begin(Stream *stream) {
}
Serial.printf("cleared %d byte(s)\n", bytesCleared);
uint8_t command[] = {0x42, 0x4D, 0xE4, 0x00, 0x01, 0x01, 0x74};
size_t bytesWritten = stream->write(command, sizeof(command));
Serial.printf("%d byte(s) written\n", bytesWritten);
// explicitly put the sensor into active mode, this seems to be be needed for the Cubic PM2009X
Serial.printf("setting active mode\n");
uint8_t activeModeCommand[] = { 0x42, 0x4D, 0xE1, 0x00, 0x01, 0x01, 0x71 };
size_t bytesWritten = stream->write(activeModeCommand, sizeof(activeModeCommand));
uint8_t activeModeCommand[] = {0x42, 0x4D, 0xE1, 0x00, 0x01, 0x01, 0x71};
bytesWritten = stream->write(activeModeCommand, sizeof(activeModeCommand));
Serial.printf("%d byte(s) written\n", bytesWritten);
// Run and check sensor data for 4sec
@@ -314,7 +319,6 @@ int PMSBase::pm25ToAQI(int pm02) {
return 500;
}
/**
* @brief SLR correction for PM2.5
*

View File

@@ -48,7 +48,7 @@ private:
/** In normal package interval is 200-800ms, In case small changed on sensor
* it's will interval reach to 2.3sec
*/
const uint16_t READ_PACKGE_TIMEOUT = 3000; /** ms */
const uint16_t READ_PACKGE_TIMEOUT = 3000; /** ms */
const int failCountMax = 10;
int failCount = 0;
@@ -76,15 +76,15 @@ private:
uint16_t pms_count2_5;
uint16_t pms_count5_0;
uint16_t pms_count10;
int16_t pms_temp;
int16_t pms_temp;
uint16_t pms_hum;
uint8_t pms_errorCode;
uint8_t pms_firmwareVersion;
uint8_t pms_errorCode;
uint8_t pms_firmwareVersion;
int16_t toI16(const uint8_t *buf);
uint16_t toU16(const uint8_t *buf);
bool validate(const uint8_t *buf);
void parse(const uint8_t* buf);
void parse(const uint8_t *buf);
};
#endif /** _PMS5003_BASE_H_ */

View File

@@ -266,3 +266,44 @@ int PMS5003::getFailCount(void) { return pms.getFailCount(); }
* @return int
*/
int PMS5003::getFailCountMax(void) { return pms.getFailCountMax(); }
// Standby mode. For low power consumption and prolong the life of the sensor.
void PMS5003::sleep() {
uint8_t command[] = {0x42, 0x4D, 0xE4, 0x00, 0x00, 0x01, 0x73};
size_t bytesWritten = this->_serial->write(command, sizeof(command));
Serial.printf("%d byte(s) written\n", bytesWritten);
}
// Operating mode. Stable data should be got at least 30 seconds after the sensor wakeup from the sleep mode because of the fan's performance.
void PMS5003::wakeUp() {
uint8_t command[] = {0x42, 0x4D, 0xE4, 0x00, 0x01, 0x01, 0x74};
size_t bytesWritten = this->_serial->write(command, sizeof(command));
Serial.printf("%d byte(s) written\n", bytesWritten);
}
// Active mode. Default mode after power up. In this mode sensor would send serial data to the host automatically.
void PMS5003::activeMode() {
uint8_t command[] = {0x42, 0x4D, 0xE1, 0x00, 0x01, 0x01, 0x71};
size_t bytesWritten = this->_serial->write(command, sizeof(command));
Serial.printf("%d byte(s) written\n", bytesWritten);
// _mode = MODE_ACTIVE;
}
// Passive mode. In this mode sensor would send serial data to the host only for request.
void PMS5003::passiveMode() {
uint8_t command[] = {0x42, 0x4D, 0xE1, 0x00, 0x00, 0x01, 0x70};
size_t bytesWritten = this->_serial->write(command, sizeof(command));
Serial.printf("%d byte(s) written\n", bytesWritten);
// _mode = MODE_PASSIVE;
}
// Request read in Passive Mode.
void PMS5003::requestRead() {
// if (_mode == MODE_PASSIVE)
// {
uint8_t command[] = {0x42, 0x4D, 0xE2, 0x00, 0x00, 0x01, 0x71};
size_t bytesWritten = this->_serial->write(command, sizeof(command));
Serial.printf("%d byte(s) written\n", bytesWritten);
// }
}

View File

@@ -19,6 +19,14 @@ public:
#else
bool begin(HardwareSerial &serial);
#endif
// Modes
void sleep();
void wakeUp();
void activeMode();
void passiveMode();
void requestRead();
void end(void);
void handle(void);
void updateFailCount(void);