From 6664b5ebd37a46643d842bf0b65fada2f9d1947e Mon Sep 17 00:00:00 2001
From: user2684 <user2684@users.noreply.github.com>
Date: Sat, 15 Jul 2017 17:37:36 +0200
Subject: [PATCH] Added support for BMP280 sensor (#88)

---
 NodeManager.cpp | 70 ++++++++++++++++++++++++++++++++++++++++++++++++-
 NodeManager.h   | 32 +++++++++++++++++++---
 NodeManager.ino |  4 ---
 README.md       | 10 ++++++-
 config.h        |  3 +++
 5 files changed, 110 insertions(+), 9 deletions(-)

diff --git a/NodeManager.cpp b/NodeManager.cpp
index 5a02a55..b6fc63d 100755
--- a/NodeManager.cpp
+++ b/NodeManager.cpp
@@ -1710,7 +1710,7 @@ void SensorMLX90614::onInterrupt() {
 /*
    SensorBosch
 */
-#if MODULE_BME280 == 1 || MODULE_BMP085 == 1
+#if MODULE_BME280 == 1 || MODULE_BMP085 == 1 || MODULE_BMP280 == 1
 // contructor
 SensorBosch::SensorBosch(NodeManager* node_manager, int child_id, int sensor_type): Sensor(node_manager, child_id,A4) {
   _sensor_type = sensor_type;
@@ -1998,6 +1998,54 @@ void SensorBMP085::onLoop() {
 }
 #endif
 
+/*
+ * SensorBMP280
+ */
+#if MODULE_BMP280 == 1
+SensorBMP280::SensorBMP280(NodeManager* node_manager, int child_id, Adafruit_BMP280* bmp, int sensor_type): SensorBosch(node_manager, child_id,sensor_type) {
+  _bmp = bmp;
+}
+
+void SensorBMP280::onLoop() {
+  // temperature sensor
+  if (_sensor_type == SensorBMP280::TEMPERATURE) {
+    // read the temperature
+    float temperature = _bmp->readTemperature();
+    // convert it
+    temperature = _node_manager->celsiusToFahrenheit(temperature);
+    #if DEBUG == 1
+      Serial.print(F("BMP I="));
+      Serial.print(_child_id);
+      Serial.print(F(" T="));
+      Serial.println(temperature);
+    #endif
+    if (isnan(temperature)) return;
+    // store the value
+    _value_float = temperature;
+  }
+  // Pressure Sensor
+  else if (_sensor_type == SensorBMP280::PRESSURE) {
+    // read pressure
+    float pressure = _bmp->readPressure() / 100.0F;
+    if (isnan(pressure)) return;
+    #if DEBUG == 1
+      Serial.print(F("BMP I="));
+      Serial.print(_child_id);
+      Serial.print(F(" P="));
+      Serial.println(pressure);
+    #endif
+    if (isnan(pressure)) return;
+    // store the value
+    _value_float = pressure;
+  }
+  // Forecast Sensor
+  else if (_sensor_type == SensorBMP280::FORECAST) {
+    float pressure = _bmp->readPressure() / 100.0F;
+    _forecast(pressure);
+  }
+}
+#endif
+
 /*
    SensorHCSR04
 */
@@ -3025,6 +3073,26 @@ int NodeManager::registerSensor(int sensor_type, int pin, int child_id) {
       return registerSensor(new SensorBME280(this,child_id,bme,SensorBME280::FORECAST));
     }
   #endif
+  #if MODULE_BMP280 == 1
+    else if (sensor_type == SENSOR_BMP280) {
+      Adafruit_BMP280* bmp = new Adafruit_BMP280();
+      if (! bmp->begin(SensorBosch::GetI2CAddress(0x58))) {
+        #if DEBUG == 1
+          Serial.println(F("NO BMP"));
+        #endif
+        return -1;
+      }
+      // register temperature sensor
+      registerSensor(new SensorBMP280(this,child_id,bmp,SensorBMP280::TEMPERATURE));
+      child_id = _getAvailableChildId();
+      // register pressure sensor
+      child_id = _getAvailableChildId();
+      registerSensor(new SensorBMP280(this,child_id,bmp,SensorBMP280::PRESSURE));
+      // register forecast sensor
+      child_id = _getAvailableChildId();
+      return registerSensor(new SensorBMP280(this,child_id,bmp,SensorBMP280::FORECAST));
+    }
+  #endif
   #if MODULE_SONOFF == 1
     else if (sensor_type == SENSOR_SONOFF) {
       return registerSensor(new SensorSonoff(this,child_id));
diff --git a/NodeManager.h b/NodeManager.h
index 2f7b9db..77dac5e 100755
--- a/NodeManager.h
+++ b/NodeManager.h
@@ -187,6 +187,10 @@
 #ifndef MODULE_PT100
   #define SENSOR_PT100 0
 #endif
+// Enable this module to use one of the following sensors: SENSOR_BMP280
+#ifndef MODULE_BMP280
+  #define MODULE_BMP280 0
+#endif
 
 /***********************************
    Supported Sensors
@@ -254,7 +258,7 @@ enum supported_sensors {
     SENSOR_MLX90614,
   #endif
   #if MODULE_BME280 == 1
-    // MLX90614 sensor, contactless temperature sensor
+    // BME280 sensor, return temperature, humidity and pressure
     SENSOR_BME280,
   #endif
   #if MODULE_SONOFF == 1
@@ -293,6 +297,10 @@ enum supported_sensors {
     // High temperature sensor associated with DFRobot Driver, return the temperature in C° from the attached PT100 sensor
     SENSOR_PT100,
   #endif
+  #if MODULE_BMP280 == 1
+    // BMP280 sensor, return temperature and pressure
+    SENSOR_BMP280,
+  #endif
 };
  
 /***********************************
@@ -363,7 +371,12 @@ enum supported_sensors {
   #include <Wire.h>
 #endif
 #if MODULE_PT100 == 1
-  #include<DFRobotHighTemperatureSensor.h>
+  #include <DFRobotHighTemperatureSensor.h>
+#endif
+#if MODULE_BMP280 == 1
+  #include <Wire.h>
+  #include <Adafruit_Sensor.h>
+  #include <Adafruit_BMP280.h>
 #endif
 
 /*******************************************************************
@@ -984,7 +997,7 @@ class SensorMLX90614: public Sensor {
  * SensorBosch
 */
 
-#if MODULE_BME280 == 1 || MODULE_BMP085 == 1
+#if MODULE_BME280 == 1 || MODULE_BMP085 == 1 || MODULE_BMP280 == 1
 class SensorBosch: public Sensor {
   public:
     SensorBosch(NodeManager* node_manager, int child_id, int sensor_type);
@@ -1044,6 +1057,19 @@ class SensorBMP085: public SensorBosch {
 };
 #endif
 
+/*
+   SensorBMP280
+*/
+#if MODULE_BMP280 == 1
+class SensorBMP280: public SensorBosch {
+  public:
+    SensorBMP280(NodeManager* node_manager, int child_id, Adafruit_BMP280* bmp, int sensor_type);
+    void onLoop();
+  protected:
+    Adafruit_BMP280* _bmp;
+};
+#endif
+
 /*
    SensorHCSR04
 */
diff --git a/NodeManager.ino b/NodeManager.ino
index 657bfaf..ad7a457 100755
--- a/NodeManager.ino
+++ b/NodeManager.ino
@@ -33,10 +33,6 @@ void before() {
    * Register below your sensors
   */
 
-   
-  /*
-   * Register below your sensors
-  */
 
 
   /*
diff --git a/README.md b/README.md
index 3c85131..24e820d 100755
--- a/README.md
+++ b/README.md
@@ -179,6 +179,11 @@ Those NodeManager's directives in the `config.h` file control which module/libra
 #define MODULE_AM2320 0
 // Enable this module to use one of the following sensors: SENSOR_TSL2561
 #define MODULE_TSL2561 0
+// Enable this module to use one of the following sensors: SENSOR_PT100
+#define MODULE_PT100 0
+// Enable this module to use one of the following sensors: SENSOR_BMP280
+#define MODULE_BMP280 0
+
 ~~~
 
 ### Installing the dependencies
@@ -199,6 +204,7 @@ MODULE_HCSR04 | https://github.com/mysensors/MySensorsArduinoExamples/tree/maste
 MODULE_MCP9808 | https://github.com/adafruit/Adafruit_MCP9808_Library
 MODULE_AM2320 | https://github.com/thakshak/AM2320
 MODULE_TSL2561 | https://github.com/adafruit/TSL2561-Arduino-Library
+MODULE_BMP280 | https://github.com/adafruit/Adafruit_BMP280_Library
 
 ### Configure NodeManager
 
@@ -348,6 +354,8 @@ SENSOR_SOIL_MOISTURE | Soil moisture sensor, return the percentage of moisture f
 SENSOR_MHZ19 | MH-Z19 CO2 sensor via UART (SoftwareSerial, default on pins 6(Rx) and 7(Tx)
 SENSOR_TSL2561 | TSL2561 sensor, return light in lux
 SENSOR_AM2320 | AM2320 sensors, return temperature/humidity based on the attached AM2320 sensor
+SENSOR_PT100 | High temperature sensor associated with DFRobot Driver, return the temperature in C° from the attached PT100 sensor
+SENSOR_BMP280 | BMP280 sensor, return temperature/pressure based on the attached BMP280 sensor
 
 To register a sensor simply call the NodeManager instance with the sensory type and the pin the sensor is conncted to. For example:
 ~~~c
@@ -578,7 +586,7 @@ Each sensor class can expose additional methods.
     DeviceAddress* getDeviceAddress();
 ~~~
 
-*  SensorBME280 / SensorBMP085
+*  SensorBME280 / SensorBMP085 / SensorBMP280
 ~~~c
     // [101] define how many pressure samples to keep track of for calculating the forecast (default: 5)
     void setForecastSamplesCount(int value);
diff --git a/config.h b/config.h
index 42d0447..5d3945b 100755
--- a/config.h
+++ b/config.h
@@ -148,5 +148,8 @@
 #define MODULE_TSL2561 0
 // Enable this module to use one of the following sensors: SENSOR_PT100
 #define MODULE_PT100 0
+// Enable this module to use one of the following sensors: SENSOR_BMP280
+#define MODULE_BMP280 0
+
 #endif
 
-- 
GitLab