diff --git a/README.md b/README.md index 8749eeb..21ee3ba 100644 --- a/README.md +++ b/README.md @@ -41,10 +41,10 @@ CAD files (OpenSCAD) and source code included. A0 D0 RSV (5)D1 ----------------------- TXD RSV (4)D2 ----------------------- RXD - RSV (0)D3 -- BMP_SDA .---- GND - RSV (2)D4 -- BMP_SCL | ... - RSV 3V3 -- BMP_VCC | .--- 5V - RSV .-GND -- BMP_GND | | ... + RSV (0)D3 -- BME_SDA .---- GND + RSV (2)D4 -- BME_SCL | ... + RSV 3V3 -- BME_VCC | .--- 5V + RSV .-GND -- BME_GND | | ... GND =C= D5 | | ... 3V3 '- D6 ------------. | | GND D7 -- DHT_DATA \ +-| --------- GND @@ -65,7 +65,7 @@ SDS011: - - -DHT22: +DHT22: (WARNING: This sensor has proven to be unreliable outdoors!) - VDD -> 3V3 - DATA -> D7 - -> diff --git a/platformio.ini b/platformio.ini index aec83b6..39ac7e2 100644 --- a/platformio.ini +++ b/platformio.ini @@ -43,7 +43,9 @@ build_flags = lib_deps = ArduinoJson Adafruit Unified Sensor - Adafruit BMP085 Unified +; Adafruit BMP085 Unified + adafruit/Adafruit BusIO + adafruit/Adafruit BME280 Library DHT sensor library ; SDS011 sensor Library ; https://github.com/Zanop/SDS011.git#more-sensor-modes diff --git a/src/main.cpp b/src/main.cpp index 84ac414..6300936 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -18,7 +18,7 @@ extern "C" { #include #include #include -#include +#include #include #include @@ -66,8 +66,8 @@ uint32_t loop_count = 0; #define SENSOR8_ID "5a9eddb1f55bff001a51de52" -static constexpr uint8_t BMP_SCL = D4; -static constexpr uint8_t BMP_SDA = D3; +static constexpr uint8_t BME_SCL = D4; +static constexpr uint8_t BME_SDA = D3; static constexpr uint8_t DHT22_PIN = D7; static constexpr uint8_t DHTTYPE = DHT22; // DHT 22 (AM2302) @@ -87,6 +87,7 @@ ADC_MODE(ADC_VCC); ESP8266WiFiMulti wifiMulti; +WiFiClient wificlient; HTTPClient httpclient; BearSSL::WiFiClientSecure net; MQTTClient mqttclient(MQTT_MAX_PACKET_SIZE); @@ -94,7 +95,10 @@ MQTTClient mqttclient(MQTT_MAX_PACKET_SIZE); os_timer_t Timer1; RunningAverage geigeraverage(10); -Adafruit_BMP085_Unified bmp = Adafruit_BMP085_Unified(10085); +Adafruit_BME280 bme; // use I2C interface +//Adafruit_Sensor *bme_temp = bme.getTemperatureSensor(); +//Adafruit_Sensor *bme_pressure = bme.getPressureSensor(); +//Adafruit_Sensor *bme_humidity = bme.getHumiditySensor(); DHT dht(DHT22_PIN, DHTTYPE); SdsDustSensor sds(SDS_TX, SDS_RX); volatile unsigned long geiger_counts = 0; @@ -112,6 +116,7 @@ struct __attribute__((packed)) SENSOR_DATA { float humidity; float pressure; float temp2; + float humidity2; float p10; float p25; float cpm; @@ -233,12 +238,18 @@ void ICACHE_FLASH_ATTR getValuesJSON(char* buffer, const size_t buf_len, int for void ICACHE_FLASH_ATTR sendValues() { unsigned long currentMillis = rtcMillis(); - sd.temperature = dht.readTemperature(); - sd.humidity = dht.readHumidity(); + bme.takeForcedMeasurement(); // has no effect in normal mode - bmp.getPressure(&(sd.pressure)); - sd.pressure /= 100; - bmp.getTemperature(&(sd.temp2)); + sd.temp2 = dht.readTemperature(); + sd.humidity2 = dht.readHumidity(); + + //bmp.getPressure(&(sd.pressure)); + //sd.pressure /= 100; + //bmp.getTemperature(&(sd.temp2)); + + sd.temperature = bme.readTemperature(); + sd.humidity = bme.readHumidity(); + sd.pressure = bme.readPressure() / 100.0F; #ifndef EXTERNAL_POWER sd.voltage = ESP.getVcc()/1024.0; @@ -260,7 +271,7 @@ void ICACHE_FLASH_ATTR sendValues() { if (rtcMillis() - geiger_previousMillis > 10000) { sd.cpm = geiger_counts * 60000 / (rtcMillis() - geiger_previousMillis); - DEBUG_MSG("[DEBUG] resetting geiger counts. was: %d\n", geiger_counts); + DEBUG_MSG("[DEBUG] resetting geiger counts. was: %ld\n", geiger_counts); geiger_previousMillis = rtcMillis(); geiger_counts = 0; geigeraverage.addValue(sd.cpm); @@ -269,10 +280,11 @@ void ICACHE_FLASH_ATTR sendValues() { float constexpr own_cpm = OWN_BACKGROUND_CPS * 60; sd.radioactivity = (sd.cpm - own_cpm) * CONV_FACTOR; - DEBUG_MSG("Temperature : %6.2f°C (DHT22)\n", sd.temperature); - DEBUG_MSG("Humidity : %6.2f%% (DHT22)\n", sd.humidity); - DEBUG_MSG("Temperature : %6.2f°C (BMP180)\n", sd.temp2); - DEBUG_MSG("Pressure : %6.2fhPa (BMP180)\n", sd.pressure); + DEBUG_MSG("Temperature : %6.2f°C (BME280)\n", sd.temperature); + DEBUG_MSG("Humidity : %6.2f%% (BME280)\n", sd.humidity); + DEBUG_MSG("Pressure : %6.2fhPa (BME280)\n", sd.pressure); + DEBUG_MSG("Temperature : %6.2f°C (DHT22)\n", sd.temp2); + DEBUG_MSG("Humidity : %6.2f%% (DHT22)\n", sd.humidity2); if (sd.sds_updated) DEBUG_MSG("Particles 10 : %6.2fµg/m³ (SDS011)\n", sd.p10); if (sd.sds_updated) DEBUG_MSG("Particles 2.5: %6.2fµg/m³ (SDS011)\n", sd.p25); if (sd.cpm > 0) DEBUG_MSG("Radiation : %6.2fµSv/h (J305)\n", sd.radioactivity); @@ -359,7 +371,7 @@ void ICACHE_FLASH_ATTR sendValues() { char url[100]; sprintf(url, "http://%s/boxes/%s/data", server, SENSEBOX_ID); - httpclient.begin(url); + httpclient.begin(wificlient, url); httpclient.addHeader("Content-Type", "application/json"); httpCode = httpclient.POST(buffer); @@ -431,20 +443,28 @@ void setup() { Serial.begin(115200); #endif - pinMode(LED_BUILTIN, OUTPUT); - digitalWrite(LED_BUILTIN, HIGH); // turn OFF board led + //pinMode(LED_BUILTIN, OUTPUT); + //digitalWrite(LED_BUILTIN, HIGH); // turn OFF board led //wifi_status_led_uninstall(); + Wire.begin(BME_SDA, BME_SCL); + for (int tries=0; tries<5 && !bme.begin(0x76, &Wire); tries++) { + DEBUG_MSG("[ERROR] No valid BME280 sensor!\n"); + delay(20); + } + + // weather monitoring + bme.setSampling(Adafruit_BME280::MODE_FORCED, + Adafruit_BME280::SAMPLING_X1, // temperature + Adafruit_BME280::SAMPLING_X1, // pressure + Adafruit_BME280::SAMPLING_X1, // humidity + Adafruit_BME280::FILTER_OFF ); + sds.begin(); sds.setCustomWorkingPeriod(5); // sensor sends data every 5 minutes sds.setQueryReportingMode(); // ensures sensor is in 'query' reporting mode - Wire.begin(BMP_SDA, BMP_SCL); - if (!bmp.begin(BMP085_MODE_STANDARD)) { - DEBUG_MSG("[ERROR] No valid BMP085 sensor!\n"); - } - dht.begin(); pinMode(GEIGER_PIN, INPUT); @@ -466,11 +486,11 @@ void loop() { unsigned long currentMillis = rtcMillis(); if ((currentMillis - previousMillis) >= postingInterval) { previousMillis = currentMillis; - DEBUG_MSG("[DEBUG] sending values... previousMillis:%u, millis():%u, difference:%d\n", previousMillis, currentMillis, currentMillis-previousMillis); + DEBUG_MSG("[DEBUG] sending values... previousMillis:%lu, millis():%lu, difference:%ld\n", previousMillis, currentMillis, currentMillis-previousMillis); sendValues(); } DEBUG_MSG("[DEBUG] heap: %d\n", ESP.getFreeHeap()); - DEBUG_MSG("[DEBUG] sleeping now... (%u)\n", currentMillis); + DEBUG_MSG("[DEBUG] sleeping now... (%lu)\n", currentMillis); delay(10); detachInterrupt(digitalPinToInterrupt(GEIGER_PIN));