@@ -1237,44 +1237,65 @@ float getVoltage(){
12371237SensirionI2CScd4x scd4x;
12381238// CO2 0x62
12391239void init_scd4x (){
1240- bool ret = false ;
1241- Wire.begin ();
1240+ uint16_t error;
1241+ char errorMessage[256 ];
1242+ Wire.begin ();
12421243 scd4x.begin (Wire);
12431244
12441245// stop potentially previously started measurement
1245- ret != scd4x.stopPeriodicMeasurement ();
1246- // if (ret){
1247- // Serial.print("Error trying to execute stopPeriodicMeasurement(): ");
1248- // errorToString(error, errorMessage, 256);
1249- // Serial.println(errorMessage);
1250- // } else ret = true;
1251-
1252- ret != scd4x.startPeriodicMeasurement ();
1253-
1254- if (!ret){
1246+ error = scd4x.stopPeriodicMeasurement ();
1247+ if (error){
1248+ Serial.print (" Error trying to execute stopPeriodicMeasurement(): " );
1249+ errorToString (error, errorMessage, 256 );
1250+ Serial.println (errorMessage);
1251+ } else error = true ;
1252+
1253+ error = scd4x.startPeriodicMeasurement ();
1254+
1255+ if (error){
1256+ Serial.print (" Error trying to execute startPeriodicMeasurement(): " );
1257+ errorToString (error, errorMessage, 256 );
1258+ Serial.println (errorMessage);
12551259 Logger.println (" [ERROR] SCD4X init FAILED" );
12561260 }
12571261else Logger.println (" [ENV] SCD4X is ACTIVE" );
12581262// return ret;
12591263}
12601264
1261- void print_scd4x (){
1265+ float get_scd4x (uint8_t channel = 0 ,bool update=true ){
1266+ static uint16_t scd4x_co2;
1267+ static float scd4x_temperature;
1268+ static float scd4x_humidity;
1269+ static uint16_t scd4x_error = 0 ;
1270+ if (update){
1271+ char errorMessage[256 ];
1272+ scd4x_error = scd4x.readMeasurement (scd4x_co2, scd4x_temperature, scd4x_humidity);
1273+ if (scd4x_error){
1274+ Serial.print (" Error trying to execute readMeasurement(): " );
1275+ errorToString (scd4x_error, errorMessage, 256 );
1276+ Serial.println (errorMessage);
1277+ }
1278+ }
1279+ if (channel == 0 ) return scd4x_co2;
1280+ if (channel == 1 ) return scd4x_temperature;
1281+ if (channel == 2 ) return scd4x_humidity;
1282+ if (channel == 3 ) return scd4x_error;
1283+ return 0 ;
12621284}
12631285
1264- float get_scd4x (uint8_t channel = 0 ){
1265- uint16_t co2;
1266- float temperature;
1267- float humidity;
1268- uint16_t error;
1269-
1270- error = scd4x.readMeasurement (co2, temperature, humidity);
1286+ void print_scd4x (){
12711287
1272- // print_env();
1273- if (channel == 0 ) return co2;
1274- if (channel == 1 ) return temperature;
1275- if (channel == 2 ) return humidity;
1276- return 0 ;
1288+ Serial.print (__FUNCTION__);
1289+ Serial.print (" co2: " );
1290+ Serial.print (get_scd4x (0 ));
1291+ Serial.print (" temp: " );
1292+ Serial.print (get_scd4x (1 ,false ));
1293+ Serial.print (" humidity: " );
1294+ Serial.print (get_scd4x (2 ,false ));
1295+ Serial.print (" error: " );
1296+ Serial.println ();
12771297}
1298+
12781299#endif
12791300
12801301
0 commit comments