From 1b4965cd63f0bfd5cba1b183d2145d9269ed01bb Mon Sep 17 00:00:00 2001 From: Hendrik Langer Date: Wed, 7 Mar 2018 00:15:58 +0100 Subject: [PATCH] add SDS011 and geiger counter --- README.md | 11 ++-- platformio.ini | 3 ++ src/main.cpp | 141 ++++++++++++++++++++++++++++++++++++++++++------- 3 files changed, 134 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 4d2ee51..3b18cdb 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ NodeMCU v0.9 RSV 3V3 - BMP_VCC RSV GND - BMP_GND GND D5 - 3V3 D6 + 3V3 D6 - GEIGER_VIN GND D7 - DHT_DATA 3V3 D8 EN RX @@ -27,7 +27,12 @@ SDS011 - DHT22 - VDD -> 3V3 + VDD -> 3V3 DATA -> D7 -> - GND -> GND + GND -> GND + +RadiationD + GND -> GND + 5V -> 5V + VIN -> D6 diff --git a/platformio.ini b/platformio.ini index 5e845e5..f1963e0 100644 --- a/platformio.ini +++ b/platformio.ini @@ -14,5 +14,8 @@ board = nodemcuv2 framework = arduino lib_deps = + ArduinoJson Adafruit BMP085 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 e8654c2..cda9bf9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,12 +7,17 @@ #include #include +#include +#include + #include #include #include #include #include +#include + const char* server = "ingress.opensensemap.org"; const char* ssid = "Freifunk"; const char* password = ""; @@ -37,6 +42,8 @@ constexpr unsigned int postingInterval = 60000; //Uploadintervall in Millisekund #define SENSOR6_ID "5a9e9e38f55bff001a494879" // Voltage #define SENSOR7_ID "5a9e9e38f55bff001a494878" +// RSSI +#define SENSOR8_ID "5a9eddb1f55bff001a51de52" static constexpr uint8_t BMP_SCL = D4; @@ -45,10 +52,22 @@ static constexpr uint8_t BMP_SDA = D3; static constexpr uint8_t DHT22_PIN = D7; static constexpr uint8_t DHTTYPE = DHT22; // DHT 22 (AM2302) +static constexpr uint8_t SDS_TX = D1; +static constexpr uint8_t SDS_RX = D2; + +static constexpr uint8_t GEIGER_PIN = D6; +static constexpr float CONV_FACTOR = 0.008120; +static constexpr unsigned long logging_period_ms = 60000; + +ADC_MODE(ADC_VCC); + Adafruit_BMP085_Unified bmp = Adafruit_BMP085_Unified(10085); DHT dht(DHT22_PIN, DHTTYPE); +SDS011 sds; +volatile unsigned long geiger_counts = 0; +unsigned long geiger_previousMillis; -void sendValues() { +void ICACHE_FLASH_ATTR sendValues() { WiFi.forceSleepWake(); WiFi.begin(ssid, password); Serial.print("Connecting to wifi"); @@ -60,39 +79,125 @@ void sendValues() { Serial.print("IP address: "); Serial.println(WiFi.localIP()); - Wire.begin(BMP_SDA, BMP_SCL); - if (!bmp.begin()) { - Serial.println("No valid BMP085 sensor!"); - } - - dht.begin(); - - float temp = dht.readTemperature(); -// float pressure = bme.readPressure() / 100.0F; + float temperature = dht.readTemperature(); float humidity = dht.readHumidity(); - Serial.println(temp); - Serial.println(humidity); - float pressure, temp2; bmp.getPressure(&pressure); pressure /= 100; bmp.getTemperature(&temp2); - Serial.println(temp2); - Serial.println(pressure); -// float temperature = sensor.readTemperature(); -// postFloatValue(temperature, 1, temperatureSensorID); + float p10, p2_5; + int sds_error = sds.read(&p2_5, &p10); + if(!sds_error) { + Serial.println("SDS011 updated."); + } else { + Serial.println("SDS011 no new values."); + } + +// sds.sleep(); + + unsigned long cpm = 0; + if (millis() - geiger_previousMillis > logging_period_ms) { + cpm = geiger_counts * 60000 / (millis() - geiger_previousMillis); + geiger_previousMillis = millis(); + geiger_counts = 0; + Serial.println(cpm); + } else { + Serial.println("Geiger Counter: no new value"); + } + + DynamicJsonBuffer jsonBuffer; + JsonArray& array = jsonBuffer.createArray(); + + JsonObject& temperatureObject = array.createNestedObject(); + temperatureObject["sensor"] = SENSOR1_ID; + temperatureObject["value"] = temperature; + + JsonObject& humidityObject = array.createNestedObject(); + humidityObject["sensor"] = SENSOR2_ID; + humidityObject["value"] = humidity; + + JsonObject& pressureObject = array.createNestedObject(); + pressureObject["sensor"] = SENSOR3_ID; + pressureObject["value"] = pressure; + + if (!sds_error) { + JsonObject& pm10Object = array.createNestedObject(); + pm10Object["sensor"] = SENSOR4_ID; + pm10Object["value"] = p10; + + JsonObject& pm25Object = array.createNestedObject(); + pm25Object["sensor"] = SENSOR5_ID; + pm25Object["value"] = p2_5; + + } + + if (cpm > 0) { + float radiationValue = cpm * CONV_FACTOR; + JsonObject& cpmObject = array.createNestedObject(); + cpmObject["sensor"] = SENSOR6_ID; + cpmObject["value"] = radiationValue; + } + + JsonObject& voltageObject = array.createNestedObject(); + voltageObject["sensor"] = SENSOR7_ID; + voltageObject["value"] = ESP.getVcc()/1024.0; + + JsonObject& rssiObject = array.createNestedObject(); + rssiObject["sensor"] = SENSOR8_ID; + rssiObject["value"] = WiFi.RSSI(); + + + char buffer[500]; + array.printTo(buffer, sizeof(buffer)); + + + HTTPClient httpclient; + char url[100]; + sprintf(url, "http://%s/boxes/%s/data", server, SENSEBOX_ID); + + httpclient.begin(url); + httpclient.addHeader("Content-Type", "application/json"); + + int httpCode = httpclient.POST(buffer); + + if (httpCode > 0) { + if (httpCode == HTTP_CODE_CREATED) { + httpclient.writeToStream(&Serial); + } else { + Serial.printf("[HTTP] POST... failed, error: %s\n", httpclient.errorToString(httpCode).c_str()); + } + } + + httpclient.end(); + WiFi.disconnect(); WiFi.mode(WIFI_OFF); WiFi.forceSleepBegin(); } +void ICACHE_RAM_ATTR ISR_geiger_impulse() { + geiger_counts++; +} + void setup() { Serial.begin(115200); - pinMode(LED_BUILTIN, OUTPUT); + + sds.begin(SDS_TX, SDS_RX); + sds.mode_mon_300(); + + Wire.begin(BMP_SDA, BMP_SCL); + if (!bmp.begin()) { + Serial.println("No valid BMP085 sensor!"); + } + + dht.begin(); + + pinMode(GEIGER_PIN, INPUT); + attachInterrupt(digitalPinToInterrupt(GEIGER_PIN), ISR_geiger_impulse, FALLING); } void loop() {