From 275a645b15ccfa72703729f81abd14a9344284b1 Mon Sep 17 00:00:00 2001
From: user2684 <you@example.com>
Date: Fri, 31 Mar 2017 15:41:13 +0200
Subject: [PATCH] Add support for BME280 temperature/humudity/pressure sensor
 #21

---
 NodeManagerTemplate/NodeManager.cpp         | 106 +++++++++++++++++++-
 NodeManagerTemplate/NodeManager.h           |  35 ++++++-
 NodeManagerTemplate/NodeManagerTemplate.ino |   2 +-
 NodeManagerTemplate/config.h                |   2 +
 4 files changed, 138 insertions(+), 7 deletions(-)

diff --git a/NodeManagerTemplate/NodeManager.cpp b/NodeManagerTemplate/NodeManager.cpp
index 2c8e2f0..d2ff380 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 b3dcb60..ff89495 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 37b2d81..e95de27 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 f68717d..facbfd1 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
-- 
GitLab