Skip to content

Commit 275a645

Browse files
author
user2684
committed
Add support for BME280 temperature/humudity/pressure sensor #21
1 parent d2a785b commit 275a645

File tree

4 files changed

+138
-7
lines changed

4 files changed

+138
-7
lines changed

NodeManagerTemplate/NodeManager.cpp

Lines changed: 103 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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 ");

NodeManagerTemplate/NodeManager.h

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,10 @@
133133
#ifndef MODULE_MLX90614
134134
#define MODULE_MLX90614 0
135135
#endif
136-
136+
// Enable this module to use one of the following sensors: SENSOR_BME280
137+
#ifndef MODULE_BME280
138+
#define MODULE_BME280 0
139+
#endif
137140
/***********************************
138141
Sensors types
139142
*/
@@ -187,8 +190,11 @@
187190
// MLX90614 sensor, contactless temperature sensor
188191
#define SENSOR_MLX90614 17
189192
#endif
190-
191-
// last Id: 17
193+
#if MODULE_BME280 == 1
194+
// MLX90614 sensor, contactless temperature sensor
195+
#define SENSOR_BME280 18
196+
#endif
197+
// last Id: 18
192198
/***********************************
193199
Libraries
194200
*/
@@ -218,6 +224,12 @@
218224
#include <Wire.h>
219225
#include <Adafruit_MLX90614.h>
220226
#endif
227+
#if MODULE_BME280 == 1
228+
#include <Wire.h>
229+
#include <SPI.h>
230+
#include <Adafruit_Sensor.h>
231+
#include <Adafruit_BME280.h>
232+
#endif
221233

222234
/**************************************
223235
Classes
@@ -578,6 +590,23 @@ class SensorMLX90614: public Sensor {
578590
};
579591
#endif
580592

593+
/*
594+
SensorBME280
595+
*/
596+
#if MODULE_BME280 == 1
597+
class SensorBME280: public Sensor {
598+
public:
599+
SensorBME280(int child_id, Adafruit_BME280* bme, int sensor_type);
600+
// define what to do at each stage of the sketch
601+
void onBefore();
602+
void onLoop();
603+
void onReceive(const MyMessage & message);
604+
protected:
605+
Adafruit_BME280* _bme;
606+
int _sensor_type;
607+
};
608+
#endif
609+
581610
/***************************************
582611
NodeManager: manages all the aspects of the node
583612
*/

NodeManagerTemplate/NodeManagerTemplate.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ void before() {
2929
/*
3030
* Register below your sensors
3131
*/
32-
32+
3333

3434
/*
3535
* Register above your sensors

NodeManagerTemplate/config.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,7 @@
6666
#define MODULE_BH1750 0
6767
// Enable this module to use one of the following sensors: SENSOR_MLX90614
6868
#define MODULE_MLX90614 0
69+
// Enable this module to use one of the following sensors: SENSOR_BME280
70+
#define MODULE_BME280 0
6971

7072
#endif

0 commit comments

Comments
 (0)