From 43ad7977f60d3fc0d954773393236e042330f3a9 Mon Sep 17 00:00:00 2001 From: Mathieu Maret Date: Tue, 14 May 2019 08:33:58 +0200 Subject: [PATCH] add bme support --- deepSleep.ino | 189 ++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 146 insertions(+), 43 deletions(-) diff --git a/deepSleep.ino b/deepSleep.ino index c786193..b7c8a3b 100644 --- a/deepSleep.ino +++ b/deepSleep.ino @@ -1,3 +1,4 @@ +#include #include extern "C" { #include "user_interface.h" // Required for wifi_station_connect() to work @@ -5,7 +6,28 @@ extern "C" { #include "Adafruit_MQTT.h" #include "Adafruit_MQTT_Client.h" -#include + +#define USE_BME680 + +#ifdef USE_BME680 +#include +#include "Adafruit_BME680.h" + +#define BME_SCK 13 +#define BME_MISO 12 +#define BME_MOSI 11 +#define BME_CS 10 + +#define SEALEVELPRESSURE_HPA (1013.25) + +Adafruit_BME680 bme; // I2C + +#define BME_TEMPERATURE_FEED_FORMAT "/feeds/mathieu/portable/bme/temperature" +#define BME_HUMIDITY_FEED_FORMAT "/feeds/mathieu/portable/bme/humidity" +#define BME_VOLTAGE_FEED_FORMAT "/feeds/mathieu/portable/bme/voltage" +#define BME_PRESSURE_FEED_FORMAT "/feeds/mathieu/portable/bme/pressure" +#define BME_GAZ_FEED_FORMAT "/feeds/mathieu/portable/bme/gaz" +#endif // if bigger than 4295, modem will wake up anyway? c.f. forceSleepBegin. // https://www.espressif.com/sites/default/files/documentation/2c-esp8266_non_os_sdk_api_reference_en.pdf @@ -13,11 +35,16 @@ extern "C" { // should stick on 64bits when mul by 1000000 -> shoudl be smaller than ~18000.10^9 ~580000year #define SLEEP_TIME_SECOND 10*60 - +#ifdef USE_DHT +#include #define DHTTYPE DHT11 #define DHTPIN D3 #define DHT_POWER_PIN 13 DHT dht(DHTPIN, DHTTYPE); +#define TEMPERATURE_FEED_FORMAT "/feeds/mathieu/portable/temperature" +#define HUMIDITY_FEED_FORMAT "/feeds/mathieu/portable/humidity" +#define VOLTAGE_FEED_FORMAT "/feeds/mathieu/portable/voltage" +#endif #define FPM_SLEEP_MAX_TIME 0xFFFFFFF // Could be replace by ESP.deepSleepMax() -> https://thingpulse.com/max-deep-sleep-for-esp8266/ @@ -25,9 +52,6 @@ DHT dht(DHTPIN, DHTTYPE); #define MQTT_USERNAME "XXXXXX" #define MQTT_PASSWD "XXXXX" #define MQTT_PORT 1883 -#define TEMPERATURE_FEED_FORMAT "/feeds/mathieu/portable/temperature" -#define HUMIDITY_FEED_FORMAT "/feeds/mathieu/portable/humidity" -#define VOLTAGE_FEED_FORMAT "/feeds/mathieu/portable/voltage" WiFiClient client; Adafruit_MQTT_Client *mqtt; @@ -35,21 +59,38 @@ Adafruit_MQTT_Publish *mqtt_temp; Adafruit_MQTT_Publish *mqtt_pressure; Adafruit_MQTT_Publish *mqtt_voltage; -const char *ssid = "XXXXX"; -const char *password = "XXXXX"; +//const char *ssid = "AndroidAP-Mathieu"; +//const char *password = "bonjourwifi"; +const char *ssid = "freebox_sarah"; +const char *password = "barca2grenada"; + uint8_t bssid[6] = {0x14, 0x0C, 0x76, 0xB1, 0x37, 0x32}; int32_t channel = 11; //0 for Auto ADC_MODE(ADC_VCC); void setup() { +#ifdef USE_DHT // Power on DHT11 pinMode(DHT_POWER_PIN, OUTPUT); - //digitalWrite(DHT_POWER_PIN, HIGH); - // put your setup code here, to run once: + digitalWrite(DHT_POWER_PIN, HIGH); +#endif + Serial.begin(115200); Serial.print("\nRunning\n"); +#ifdef USE_BME680 + if (!bme.begin()) { + Serial.println("Could not find a valid BME680 sensor, check wiring!"); + while (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 +#endif } // ESP.deepSleep( X, WAKE_RF_DISABLED ); disable the radio interface and it can't be @@ -78,9 +119,13 @@ int WiFiOff() // vs WiFi.forceSleepBegin(); return wifi_fpm_do_sleep(FPM_SLEEP_MAX_TIME); } -int mqttConnect(float h, float t) -{ - mqtt = new Adafruit_MQTT_Client(&client, MQTT_SERVER, MQTT_PORT, +struct mqttInfo { + float value; + char *topic; +}; + +int mqttConnect(std::vector tab) { + mqtt = new Adafruit_MQTT_Client(&client, MQTT_SERVER, MQTT_PORT, MQTT_USERNAME, MQTT_USERNAME, MQTT_PASSWD); uint8_t retries = 3; Serial.println("Connecting to mqtt"); @@ -94,20 +139,89 @@ int mqttConnect(float h, float t) } } Serial.println("Sending mqtt info"); - mqtt_temp = new Adafruit_MQTT_Publish(mqtt, TEMPERATURE_FEED_FORMAT, 0); - mqtt_pressure = new Adafruit_MQTT_Publish(mqtt, HUMIDITY_FEED_FORMAT, 0); - mqtt_voltage = new Adafruit_MQTT_Publish(mqtt, VOLTAGE_FEED_FORMAT, 0); - if (!mqtt_temp->publish(t)) { - Serial.println("failed to send temp"); + for (auto info : tab) { + Serial.print("publishing "); + Serial.print(info.value); + Serial.print(" for "); + Serial.println(info.topic); + Adafruit_MQTT_Publish client(mqtt, info.topic, 0); + if (!client.publish(info.value)) + Serial.println("Fail :("); } - mqtt_pressure->publish(h); - mqtt_voltage->publish(ESP.getVcc()); + //Do we need this ? Wifi will be cut anyway mqtt->disconnect(); return 0; } -int doMeasure() +int doBMEMeasure() { +#ifdef USE_BME680 + if (! bme.performReading()) { + Serial.println("Failed to perform reading :("); + return -1; + } + Serial.print("Temperature = "); + Serial.print(bme.temperature); + Serial.println(" *C"); + + Serial.print("Pressure = "); + Serial.print(bme.pressure / 100.0); + Serial.println(" hPa"); + + Serial.print("Humidity = "); + Serial.print(bme.humidity); + Serial.println(" %"); + + Serial.print("Gas = "); + Serial.print(bme.gas_resistance / 1000.0); + Serial.println(" KOhms"); + + std::vector bmeInfo; + bmeInfo.push_back({bme.humidity, BME_HUMIDITY_FEED_FORMAT}); + bmeInfo.push_back({bme.temperature, BME_TEMPERATURE_FEED_FORMAT}); + bmeInfo.push_back({bme.pressure / 100.0, BME_PRESSURE_FEED_FORMAT}); + bmeInfo.push_back({bme.gas_resistance / 1000.0, BME_GAZ_FEED_FORMAT}); + bmeInfo.push_back({(float)ESP.getVcc(), BME_VOLTAGE_FEED_FORMAT}); + + wifiConnect(); + mqttConnect(bmeInfo); + if (WiFiOff()) { + //if (WiFi.forceSleepBegin(0)) { + Serial.println("Fail to set sleep mode"); + } +#else + return 0; +#endif +} + +int wifiConnect() { + WiFiOn(); + //WiFi.forceSleepWake(); + IPAddress ip(192, 168, 0, 251); + IPAddress gateway(192, 168, 0, 254); + IPAddress subnet(255, 255, 255, 0); + WiFi.config(ip, gateway, subnet); + WiFi.begin(ssid, password, channel, bssid); + + int retry = 10; + while (WiFi.status() != WL_CONNECTED) { + delay(100); + Serial.print("."); + if (retry < 0) + continue; + retry --; + } + + + Serial.println(""); + Serial.println("WiFi connected"); + Serial.println("IP address: "); + Serial.println(WiFi.localIP()); + return 0; +} + +int doDHTMeasure() { +#ifdef USE_DHT float h = dht.readHumidity(); // Read temperature as Celsius (the default) float t = dht.readTemperature(); @@ -131,40 +245,29 @@ int doMeasure() Serial.print(hic); Serial.print(" *C "); - WiFiOn(); - //WiFi.forceSleepWake(); - IPAddress ip(192, 168, 0, 251); - IPAddress gateway(192, 168, 0, 254); - IPAddress subnet(255, 255, 255, 0); - WiFi.config(ip, gateway, subnet); - WiFi.begin(ssid, password, channel, bssid); + std::vector dhtInfo; + dhtInfo.push_back({h, HUMIDITY_FEED_FORMAT}); + dhtInfo.push_back({t, TEMPERATURE_FEED_FORMAT}); + dhtInfo.push_back({(float)ESP.getVcc(), VOLTAGE_FEED_FORMAT}); - int retry = 10; - while (WiFi.status() != WL_CONNECTED) { - delay(100); - Serial.print("."); - if (retry < 0) - continue; - retry --; - } - - - Serial.println(""); - Serial.println("WiFi connected"); - Serial.println("IP address: "); - Serial.println(WiFi.localIP()); - mqttConnect(h, t); + wifiConnect(); + mqttConnect(dhtInfo); if (WiFiOff()) { //if (WiFi.forceSleepBegin(0)) { Serial.println("Fail to set sleep mode"); } return 0; +#else + return 0; +#endif } + void loop() { // Wait for DHT to be ready delay(2000); - doMeasure(); + doDHTMeasure(); + doBMEMeasure(); ESP.deepSleep(SLEEP_TIME_SECOND * 1000000, WAKE_RF_DEFAULT); }