Skip to content

Commit

Permalink
added flag for force only i2c detection (improve speed)
Browse files Browse the repository at this point in the history
  • Loading branch information
hpsaturn committed Jun 1, 2021
1 parent d74bb61 commit 4a8890b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 17 deletions.
9 changes: 5 additions & 4 deletions examples/sps30i2c/sps30i2c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,18 @@ void onSensorDataError(const char * msg){

void setup() {
Serial.begin(115200);
delay(200);
Serial.println("\n== Sensor test setup ==\n");
delay(1000);

Serial.println("\n== Sensor test setup ==\n");
Serial.println("-->[SETUP] Detecting sensors..");

sensors.setSampleTime(5); // config sensors sample time interval
sensors.setOnDataCallBack(&onSensorDataOk); // all data read callback
sensors.setOnErrorCallBack(&onSensorDataError); // [optional] error callback
sensors.setDebugMode(true); // [optional] debug mode
sensors.setDebugMode(false); // [optional] debug mode
sensors.detectI2COnly(true); // skip UART detection

sensors.init(sensors.Auto);
sensors.init();

if(sensors.isPmSensorConfigured())
Serial.println("-->[SETUP] Sensor configured: " + sensors.getPmDeviceSelected());
Expand Down
28 changes: 15 additions & 13 deletions src/Sensors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,16 @@ void Sensors::init(int pms_type, int pms_rx, int pms_tx) {

DEBUG("-->[SENSORS] sample time set to: ", String(sample_time).c_str());

if (!sensorSerialInit(pms_type, pms_rx, pms_tx)) {
DEBUG("-->[E][PMSENSOR] init failed!");
if (!_only_i2c_sensors && !sensorSerialInit(pms_type, pms_rx, pms_tx)) {
DEBUG("-->[PMSENSOR] not found any PM sensor via UART");
}

#ifdef M5COREINK
Wire.begin(25,26); // M5CoreInk hat pines (header on top)
#endif
Wire.begin();

DEBUG("-->[SENSORS] try to load I2C sensor..");
DEBUG("-->[SENSORS] trying to load I2C sensors..");
sps30I2CInit();
am2320Init();
sht31Init();
Expand Down Expand Up @@ -205,6 +205,10 @@ int Sensors::getPmDeviceTypeSelected() {
return device_type;
}

void Sensors::detectI2COnly(bool enable) {
_only_i2c_sensors = enable;
}

/******************************************************************************
* U A R T S E N S O R P R I V A T E M E T H O D S
******************************************************************************/
Expand Down Expand Up @@ -527,24 +531,24 @@ void Sensors::sps30Errorloop(char *mess, uint8_t r) {
bool Sensors::sensorSerialInit(int pms_type, int pms_rx, int pms_tx) {
// set UART for autodetection sensors (Honeywell, Plantower)
if (pms_type == Auto) {
DEBUG("-->[PMSENSOR] detecting Generic PM sensor..");
DEBUG("-->[PMSENSOR][UART] detecting Generic PM sensor..");
if (!serialInit(pms_type, 9600, pms_rx, pms_tx)) return false;
}
// set UART for custom sensors
else if (pms_type == Panasonic) {
DEBUG("-->[PMSENSOR] detecting Panasonic PM sensor..");
DEBUG("-->[PMSENSOR][UART] detecting Panasonic PM sensor..");
if (!serialInit(pms_type, 9600, pms_rx, pms_tx)) return false;
} else if (pms_type == Sensirion) {
DEBUG("-->[PMSENSOR] detecting Sensirion PM sensor..");
DEBUG("-->[PMSENSOR][UART] detecting SPS30 PM sensor..");
if (!serialInit(pms_type, 115200, pms_rx, pms_tx)) return false;
} else if (pms_type == SDS011) {
DEBUG("-->[PMSENSOR] detecting SDS011 PM sensor..");
DEBUG("-->[PMSENSOR][UART] detecting SDS011 PM sensor..");
if (!serialInit(pms_type, 9600, pms_rx, pms_tx)) return false;
} else if (pms_type == Mhz19) {
DEBUG("-->[CO2SENSOR] detecting Mhz19 sensor..");
DEBUG("-->[CO2SENSOR][UART] detecting MHZ19 sensor..");
if (!serialInit(pms_type, 9600, pms_rx, pms_tx)) return false;
} else if (pms_type == CM1106) {
DEBUG("-->[CO2SENSOR] detecting CM1106 sensor..");
DEBUG("-->[CO2SENSOR][UART] detecting CM1106 sensor..");
if (!serialInit(pms_type, 9600, pms_rx, pms_tx)) return false;
}

Expand All @@ -556,11 +560,9 @@ bool Sensors::sensorSerialInit(int pms_type, int pms_rx, int pms_tx) {
if (device_type >= 0) {
DEBUG("-->[PMSENSOR] detected: ", device_selected.c_str());
return true;
} else {
DEBUG("-->[E][PMSENSOR] detection failed!");
if (_onErrorCb) _onErrorCb("-->[E][PMSENSOR] detection failed!");
return false;
}

return false;
}
/**
* @brief Generic PM sensor auto detection.
Expand Down
4 changes: 4 additions & 0 deletions src/Sensors.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ class Sensors {

void setCO2RecalibrationFactor(int ppmValue);

void detectI2COnly(bool enable);

private:
/// DHT library
uint32_t delayMS;
Expand Down Expand Up @@ -178,6 +180,8 @@ class Sensors {
float CO2humi = 0.0; // temperature of the CO2 sensor
float CO2temp = 0.0; // temperature of the CO2 sensor

bool _only_i2c_sensors;

void am2320Init();
void am2320Read();

Expand Down

0 comments on commit 4a8890b

Please sign in to comment.