@@ -888,6 +888,93 @@ void SensorMLX90614::onReceive(const MyMessage & message) {
888888}
889889#endif
890890
891+
892+ /*
893+ SensorBME280
894+ */
895+ #if MODULE_BME280 == 1
896+ // contructor
897+ SensorBME280::SensorBME280 (int child_id, Adafruit_BME280* bme, int sensor_type): Sensor(child_id,A4) {
898+ // store the sensor type (0: temperature, 1: humidity, 2: pressure)
899+ _sensor_type = sensor_type;
900+ if (_sensor_type == 0 ) {
901+ // temperature sensor
902+ setPresentation (S_TEMP);
903+ setType (V_TEMP);
904+ setValueType (TYPE_FLOAT);
905+ }
906+ else if (_sensor_type == 1 ) {
907+ // humidity sensor
908+ setPresentation (S_HUM);
909+ setType (V_HUM);
910+ setValueType (TYPE_FLOAT);
911+ }
912+ else if (_sensor_type == 2 ) {
913+ // pressure sensor
914+ setPresentation (S_BARO);
915+ setType (V_PRESSURE);
916+ setValueType (TYPE_FLOAT);
917+ }
918+ }
919+
920+ // what do to during setup
921+ void SensorBME280::onBefore () {
922+ // initialize the library
923+ }
924+
925+ // what do to during loop
926+ void SensorBME280::onLoop () {
927+ // temperature sensor
928+ if (_sensor_type == 0 ) {
929+ // read the temperature
930+ float temperature = _bme->readTemperature ();
931+ // convert it
932+ if (! getControllerConfig ().isMetric ) temperature = temperature * 1.8 + 32 ;
933+ #if DEBUG == 1
934+ Serial.print (" BME I=" );
935+ Serial.print (_child_id);
936+ Serial.print (" T=" );
937+ Serial.println (temperature);
938+ #endif
939+ // store the value
940+ if (! isnan (temperature)) _value_float = temperature;
941+ }
942+ // Humidity Sensor
943+ else if (_sensor_type == 1 ) {
944+ // read humidity
945+ float humidity = _bme->readHumidity ();
946+ if (isnan (humidity)) return ;
947+ #if DEBUG == 1
948+ Serial.print (" BME I=" );
949+ Serial.print (_child_id);
950+ Serial.print (" H=" );
951+ Serial.println (humidity);
952+ #endif
953+ // store the value
954+ if (! isnan (humidity)) _value_float = humidity;
955+ }
956+ // Pressure Sensor
957+ else if (_sensor_type == 2 ) {
958+ // read humidity
959+ float pressure = _bme->readPressure () / 100 .0F ;
960+ if (isnan (pressure)) return ;
961+ #if DEBUG == 1
962+ Serial.print (" BME I=" );
963+ Serial.print (_child_id);
964+ Serial.print (" P=" );
965+ Serial.println (pressure);
966+ #endif
967+ // store the value
968+ if (! isnan (pressure)) _value_float = pressure;
969+ }
970+ }
971+
972+ // what do to as the main task when receiving a message
973+ void SensorBME280::onReceive (const MyMessage & message) {
974+ onLoop ();
975+ }
976+ #endif
977+
891978/* ******************************************
892979 NodeManager
893980*/
@@ -1050,15 +1137,28 @@ int NodeManager::registerSensor(int sensor_type, int pin, int child_id) {
10501137 #endif
10511138 #if MODULE_MLX90614 == 1
10521139 else if (sensor_type == SENSOR_MLX90614) {
1053- Serial.println (" 1" );
10541140 Adafruit_MLX90614* mlx = new Adafruit_MLX90614 ();
1055- Serial.println (" 2" );
10561141 registerSensor (new SensorMLX90614 (child_id,mlx,0 ));
1057- Serial.println (" 3" );
10581142 child_id = _getAvailableChildId ();
10591143 return registerSensor (new SensorMLX90614 (child_id,mlx,1 ));
10601144 }
10611145 #endif
1146+ #if MODULE_BME280 == 1
1147+ else if (sensor_type == SENSOR_BME280) {
1148+ Adafruit_BME280* bme = new Adafruit_BME280 ();
1149+ if (! bme->begin ()) {
1150+ #if DEBUG == 1
1151+ Serial.println (" NO BME" );
1152+ #endif
1153+ return -1 ;
1154+ }
1155+ registerSensor (new SensorBME280 (child_id,bme,0 ));
1156+ child_id = _getAvailableChildId ();
1157+ registerSensor (new SensorBME280 (child_id,bme,1 ));
1158+ child_id = _getAvailableChildId ();
1159+ return registerSensor (new SensorBME280 (child_id,bme,2 ));
1160+ }
1161+ #endif
10621162 else {
10631163 #if DEBUG == 1
10641164 Serial.print (" INVALID " );
0 commit comments