diff --git a/NodeManagerTemplate/NodeManager.cpp b/NodeManagerTemplate/NodeManager.cpp index 2c8e2f092b545546efe28f2405a13ea24e2b48d4..d2ff38068a5b3c1bf725cf6de88f57fb1ce813c5 100644 --- a/NodeManagerTemplate/NodeManager.cpp +++ b/NodeManagerTemplate/NodeManager.cpp @@ -888,6 +888,93 @@ void SensorMLX90614::onReceive(const MyMessage & message) { } #endif + +/* + SensorBME280 +*/ +#if MODULE_BME280 == 1 +// contructor +SensorBME280::SensorBME280(int child_id, Adafruit_BME280* bme, int sensor_type): Sensor(child_id,A4) { + // store the sensor type (0: temperature, 1: humidity, 2: pressure) + _sensor_type = sensor_type; + if (_sensor_type == 0) { + // temperature sensor + setPresentation(S_TEMP); + setType(V_TEMP); + setValueType(TYPE_FLOAT); + } + else if (_sensor_type == 1) { + // humidity sensor + setPresentation(S_HUM); + setType(V_HUM); + setValueType(TYPE_FLOAT); + } + else if (_sensor_type == 2) { + // pressure sensor + setPresentation(S_BARO); + setType(V_PRESSURE); + setValueType(TYPE_FLOAT); + } +} + +// what do to during setup +void SensorBME280::onBefore() { + // initialize the library +} + +// what do to during loop +void SensorBME280::onLoop() { + // temperature sensor + if (_sensor_type == 0) { + // read the temperature + float temperature = _bme->readTemperature(); + // convert it + if (! getControllerConfig().isMetric) temperature = temperature * 1.8 + 32; + #if DEBUG == 1 + Serial.print("BME I="); + Serial.print(_child_id); + Serial.print(" T="); + Serial.println(temperature); + #endif + // store the value + if (! isnan(temperature)) _value_float = temperature; + } + // Humidity Sensor + else if (_sensor_type == 1) { + // read humidity + float humidity = _bme->readHumidity(); + if (isnan(humidity)) return; + #if DEBUG == 1 + Serial.print("BME I="); + Serial.print(_child_id); + Serial.print(" H="); + Serial.println(humidity); + #endif + // store the value + if (! isnan(humidity)) _value_float = humidity; + } + // Pressure Sensor + else if (_sensor_type == 2) { + // read humidity + float pressure = _bme->readPressure() / 100.0F; + if (isnan(pressure)) return; + #if DEBUG == 1 + Serial.print("BME I="); + Serial.print(_child_id); + Serial.print(" P="); + Serial.println(pressure); + #endif + // store the value + if (! isnan(pressure)) _value_float = pressure; + } +} + +// what do to as the main task when receiving a message +void SensorBME280::onReceive(const MyMessage & message) { + onLoop(); +} +#endif + /******************************************* NodeManager */ @@ -1050,15 +1137,28 @@ int NodeManager::registerSensor(int sensor_type, int pin, int child_id) { #endif #if MODULE_MLX90614 == 1 else if (sensor_type == SENSOR_MLX90614) { - Serial.println("1"); Adafruit_MLX90614* mlx = new Adafruit_MLX90614(); - Serial.println("2"); registerSensor(new SensorMLX90614(child_id,mlx,0)); - Serial.println("3"); child_id = _getAvailableChildId(); return registerSensor(new SensorMLX90614(child_id,mlx,1)); } #endif + #if MODULE_BME280 == 1 + else if (sensor_type == SENSOR_BME280) { + Adafruit_BME280* bme = new Adafruit_BME280(); + if (! bme->begin()) { + #if DEBUG == 1 + Serial.println("NO BME"); + #endif + return -1; + } + registerSensor(new SensorBME280(child_id,bme,0)); + child_id = _getAvailableChildId(); + registerSensor(new SensorBME280(child_id,bme,1)); + child_id = _getAvailableChildId(); + return registerSensor(new SensorBME280(child_id,bme,2)); + } + #endif else { #if DEBUG == 1 Serial.print("INVALID "); diff --git a/NodeManagerTemplate/NodeManager.h b/NodeManagerTemplate/NodeManager.h index b3dcb603cdcc4c23f50694c1ccec375cc3b46e75..ff894953f0267beee8dd0e3d1bc6ea1145b2668a 100644 --- a/NodeManagerTemplate/NodeManager.h +++ b/NodeManagerTemplate/NodeManager.h @@ -133,7 +133,10 @@ #ifndef MODULE_MLX90614 #define MODULE_MLX90614 0 #endif - +// Enable this module to use one of the following sensors: SENSOR_BME280 +#ifndef MODULE_BME280 + #define MODULE_BME280 0 +#endif /*********************************** Sensors types */ @@ -187,8 +190,11 @@ // MLX90614 sensor, contactless temperature sensor #define SENSOR_MLX90614 17 #endif - -// last Id: 17 +#if MODULE_BME280 == 1 + // MLX90614 sensor, contactless temperature sensor + #define SENSOR_BME280 18 +#endif +// last Id: 18 /*********************************** Libraries */ @@ -218,6 +224,12 @@ #include <Wire.h> #include <Adafruit_MLX90614.h> #endif +#if MODULE_BME280 == 1 + #include <Wire.h> + #include <SPI.h> + #include <Adafruit_Sensor.h> + #include <Adafruit_BME280.h> +#endif /************************************** Classes @@ -578,6 +590,23 @@ class SensorMLX90614: public Sensor { }; #endif +/* + SensorBME280 +*/ +#if MODULE_BME280 == 1 +class SensorBME280: public Sensor { + public: + SensorBME280(int child_id, Adafruit_BME280* bme, int sensor_type); + // define what to do at each stage of the sketch + void onBefore(); + void onLoop(); + void onReceive(const MyMessage & message); + protected: + Adafruit_BME280* _bme; + int _sensor_type; +}; +#endif + /*************************************** NodeManager: manages all the aspects of the node */ diff --git a/NodeManagerTemplate/NodeManagerTemplate.ino b/NodeManagerTemplate/NodeManagerTemplate.ino index 37b2d8196dc68732e9cdb59dc3b425b089bee3fd..e95de27429633b464f006092bd5fda6ba2f0e63f 100644 --- a/NodeManagerTemplate/NodeManagerTemplate.ino +++ b/NodeManagerTemplate/NodeManagerTemplate.ino @@ -29,7 +29,7 @@ void before() { /* * Register below your sensors */ - + /* * Register above your sensors diff --git a/NodeManagerTemplate/config.h b/NodeManagerTemplate/config.h index f68717d2472ae5b517f4e786c2971af314104759..facbfd1acdb1fb3f8cbe9bf3769c5c53f5305421 100644 --- a/NodeManagerTemplate/config.h +++ b/NodeManagerTemplate/config.h @@ -66,5 +66,7 @@ #define MODULE_BH1750 0 // Enable this module to use one of the following sensors: SENSOR_MLX90614 #define MODULE_MLX90614 0 +// Enable this module to use one of the following sensors: SENSOR_BME280 +#define MODULE_BME280 0 #endif