Skip to content

Commit

Permalink
Add support for BME280 temperature/humudity/pressure sensor #21
Browse files Browse the repository at this point in the history
  • Loading branch information
user2684 committed Mar 31, 2017
1 parent d2a785b commit 275a645
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 7 deletions.
106 changes: 103 additions & 3 deletions NodeManagerTemplate/NodeManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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 ");
Expand Down
35 changes: 32 additions & 3 deletions NodeManagerTemplate/NodeManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
*/
Expand Down
2 changes: 1 addition & 1 deletion NodeManagerTemplate/NodeManagerTemplate.ino
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ void before() {
/*
* Register below your sensors
*/


/*
* Register above your sensors
Expand Down
2 changes: 2 additions & 0 deletions NodeManagerTemplate/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 275a645

Please sign in to comment.