diff --git a/WifiControlSensor/BME680.h b/WifiControlSensor/BME680.h new file mode 100644 index 0000000..155c71f --- /dev/null +++ b/WifiControlSensor/BME680.h @@ -0,0 +1,28 @@ +#pragma once +#ifdef CONFIG_ENABLE_BME680 +#include +#include +#include +#include "Adafruit_BME680.h" +#include "debug_sketch.h" + +#define BME_SCK 13 +#define BME_MISO 12 +#define BME_MOSI 11 +#define BME_CS 10 + +//Use Default i2c pin GPIO4(D2): SDA, GPIO5(D1):SCL + +#define SEALEVELPRESSURE_HPA (1013.25) +#include +#include "debug_sketch.h" + +int BME680GetMeasure(float &t, float &p, float &h, float &g, float &a); +int BME680Setup(); +bool BME680IsConnected(); + +#else //CONFIG_ENABLE_BMP80 +int BME680GetMeasure(float &, float &, float&, float &, float &); +int BME680Setup(){SKETCH_DEBUG_PRINTLN("BME680 is disabled at build time"); return -1;}; +bool BME680IsConnected(){return false;}; +#endif diff --git a/WifiControlSensor/BME680.ino b/WifiControlSensor/BME680.ino new file mode 100644 index 0000000..06791dd --- /dev/null +++ b/WifiControlSensor/BME680.ino @@ -0,0 +1,40 @@ +#ifdef CONFIG_ENABLE_BME680 +#include "BME680.h" +Adafruit_BME680 bme; // I2C +//Adafruit_BME680 bme(BME_CS); // hardware SPI +//Adafruit_BME680 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK); + +int bme680Connected = 0; + +int BME680Setup() { + bme680Connected = bme.begin(); + if (!bme680Connected){ + SKETCH_DEBUG_PRINTLN("Cannot connect to BME680"); + return -1; + } + // Set up oversampling and filter initialization + bme.setTemperatureOversampling(BME680_OS_8X); + bme.setHumidityOversampling(BME680_OS_2X); + bme.setPressureOversampling(BME680_OS_4X); + bme.setIIRFilterSize(BME680_FILTER_SIZE_3); + bme.setGasHeater(320, 150); // 320*C for 150 ms + return 0; +} + +bool BME680IsConnected() { + return bme680Connected != 0; +} + +int BME680GetMeasure(float &t, float &p, float &h, float &g, float &a){ + if(!bme.performReading()){ + SKETCH_DEBUG_PRINTLN("Cannot read BME680 measure"); + return -1; + } + t = bme.temperature; + p = bme.pressure / 100.0; + h = bme.humidity; + g = bme.gas_resistance / 1000.0; + a = bme.readAltitude(SEALEVELPRESSURE_HPA); + return 0; +} +#endif diff --git a/WifiControlSensor/MQTT.h b/WifiControlSensor/MQTT.h index 97b1d56..9935637 100644 --- a/WifiControlSensor/MQTT.h +++ b/WifiControlSensor/MQTT.h @@ -11,6 +11,11 @@ #define PWM_FEED_FORMAT "/feeds/%s/%s/gpio/%d" #define PWM_SET_FEED_FORMAT "/feeds/%s/%s/gpio/%d/set" #define IP_FEED_FORMAT "/feeds/%s/%s/configuration/ip/addr" +#define BME680_TEMPERATURE_FEED_FORMAT "/feeds/%s/%s/bme680/temperature" +#define BME680_PRESSURE_FEED_FORMAT "/feeds/%s/%s/bme680/pressure" +#define BME680_HUMIDITY_FEED_FORMAT "/feeds/%s/%s/bme680/humidity" +#define BME680_GAZ_FEED_FORMAT "/feeds/%s/%s/bme680/gaz" +#define BME680_ALT_FEED_FORMAT "/feeds/%s/%s/bme680/alt" #ifndef CONFIG_DISABLE_MQTT #include "Adafruit_MQTT.h" diff --git a/WifiControlSensor/MQTT.ino b/WifiControlSensor/MQTT.ino index 4ba3f91..32774e4 100644 --- a/WifiControlSensor/MQTT.ino +++ b/WifiControlSensor/MQTT.ino @@ -87,7 +87,7 @@ int MqttBatchPublish(std::vector tab, ...) { va_start (args, tab); vsnprintf(buf, sizeof(buf), (const char *)info.topic, args); va_end(args); - SKETCH_DEBUG_PRINTF("publishing %f for %s\n", info.value, buf); + SKETCH_DEBUG_PRINTFLN("publishing %f for %s\n", info.value, buf); Adafruit_MQTT_Publish client(mqtt, buf, info.qos, info.retain); if (!client.publish(info.value)) SKETCH_DEBUG_PRINTLN("Fail :("); diff --git a/WifiControlSensor/WifiControlSensor.ino b/WifiControlSensor/WifiControlSensor.ino index 39c87ca..8e58a5b 100644 --- a/WifiControlSensor/WifiControlSensor.ino +++ b/WifiControlSensor/WifiControlSensor.ino @@ -61,6 +61,7 @@ extern "C" { double temp, pressure; float dhtTemp, dhtHumidity; +float bme680T, bme680P, bme680H, bme680G, bme680A; int dryness; uint8_t mode; int reconfig = 0; @@ -273,6 +274,9 @@ void setup() { if (!DrySetup(CONFIG_DRY_POWER_PIN)) { SKETCH_DEBUG_PRINTLN("DRY init success"); } + if(!BME680Setup()){ + SKETCH_DEBUG_PRINTLN("BME680 init success"); + } WebSetupServer(mode); } #ifdef CONFIG_ENABLE_POWER_SAVE @@ -309,6 +313,24 @@ void loop() { SKETCH_DEBUG_PRINTF("Current dryness %d %%\n", (dryness * 100) / 1024); batchInfo.push_back({dryness, DRY_FEED_FORMAT, 0, 0}); } + if (!BME680GetMeasure(bme680T, bme680P, bme680H, bme680G, bme680A)) { + SKETCH_DEBUG_PRINT("Current T°C: "); + SKETCH_DEBUG_PRINT(bme680T); + SKETCH_DEBUG_PRINT(" Pressure hPa: "); + SKETCH_DEBUG_PRINT(bme680P); + SKETCH_DEBUG_PRINT(" Humidity %: "); + SKETCH_DEBUG_PRINTLN(bme680H); + SKETCH_DEBUG_PRINT(" Gaz kOhms: "); + SKETCH_DEBUG_PRINTLN(bme680G); + SKETCH_DEBUG_PRINT(" Altitude M: "); + SKETCH_DEBUG_PRINTLN(bme680A); + batchInfo.push_back({bme680T, BME680_TEMPERATURE_FEED_FORMAT, 0, 0}); + batchInfo.push_back({bme680P, BME680_PRESSURE_FEED_FORMAT, 0, 0}); + batchInfo.push_back({bme680H, BME680_HUMIDITY_FEED_FORMAT, 0, 0}); + batchInfo.push_back({bme680G, BME680_GAZ_FEED_FORMAT, 0, 0}); + batchInfo.push_back({bme680A, BME680_ALT_FEED_FORMAT, 0, 0}); + } + if (mode == BOOTMODE_NORMAL) MqttBatchPublish(batchInfo, conf.mqttUser, conf.host); nbCycle = 0;