Browse Source

add SDS011 and geiger counter

lightsleep
Hendrik Langer 7 years ago
parent
commit
1b4965cd63
  1. 7
      README.md
  2. 3
      platformio.ini
  3. 141
      src/main.cpp

7
README.md

@ -9,7 +9,7 @@ NodeMCU v0.9
RSV 3V3 - BMP_VCC RSV 3V3 - BMP_VCC
RSV GND - BMP_GND RSV GND - BMP_GND
GND D5 GND D5
3V3 D6 3V3 D6 - GEIGER_VIN
GND D7 - DHT_DATA GND D7 - DHT_DATA
3V3 D8 3V3 D8
EN RX EN RX
@ -31,3 +31,8 @@ DHT22
DATA -> D7 DATA -> D7
-> ->
GND -> GND GND -> GND
RadiationD
GND -> GND
5V -> 5V
VIN -> D6

3
platformio.ini

@ -14,5 +14,8 @@ board = nodemcuv2
framework = arduino framework = arduino
lib_deps = lib_deps =
ArduinoJson
Adafruit BMP085 Library Adafruit BMP085 Library
DHT sensor library DHT sensor library
; SDS011 sensor Library
https://github.com/Zanop/SDS011.git#more-sensor-modes

141
src/main.cpp

@ -7,12 +7,17 @@
#include <Arduino.h> #include <Arduino.h>
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>
#include <Wire.h> #include <Wire.h>
#include <SPI.h> #include <SPI.h>
#include <Adafruit_Sensor.h> #include <Adafruit_Sensor.h>
#include <Adafruit_BMP085_U.h> #include <Adafruit_BMP085_U.h>
#include <DHT.h> #include <DHT.h>
#include <SDS011.h>
const char* server = "ingress.opensensemap.org"; const char* server = "ingress.opensensemap.org";
const char* ssid = "Freifunk"; const char* ssid = "Freifunk";
const char* password = ""; const char* password = "";
@ -37,6 +42,8 @@ constexpr unsigned int postingInterval = 60000; //Uploadintervall in Millisekund
#define SENSOR6_ID "5a9e9e38f55bff001a494879" #define SENSOR6_ID "5a9e9e38f55bff001a494879"
// Voltage // Voltage
#define SENSOR7_ID "5a9e9e38f55bff001a494878" #define SENSOR7_ID "5a9e9e38f55bff001a494878"
// RSSI
#define SENSOR8_ID "5a9eddb1f55bff001a51de52"
static constexpr uint8_t BMP_SCL = D4; 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 DHT22_PIN = D7;
static constexpr uint8_t DHTTYPE = DHT22; // DHT 22 (AM2302) 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); Adafruit_BMP085_Unified bmp = Adafruit_BMP085_Unified(10085);
DHT dht(DHT22_PIN, DHTTYPE); 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.forceSleepWake();
WiFi.begin(ssid, password); WiFi.begin(ssid, password);
Serial.print("Connecting to wifi"); Serial.print("Connecting to wifi");
@ -60,39 +79,125 @@ void sendValues() {
Serial.print("IP address: "); Serial.print("IP address: ");
Serial.println(WiFi.localIP()); Serial.println(WiFi.localIP());
Wire.begin(BMP_SDA, BMP_SCL); float temperature = dht.readTemperature();
if (!bmp.begin()) {
Serial.println("No valid BMP085 sensor!");
}
dht.begin();
float temp = dht.readTemperature();
// float pressure = bme.readPressure() / 100.0F;
float humidity = dht.readHumidity(); float humidity = dht.readHumidity();
Serial.println(temp);
Serial.println(humidity);
float pressure, temp2; float pressure, temp2;
bmp.getPressure(&pressure); bmp.getPressure(&pressure);
pressure /= 100; pressure /= 100;
bmp.getTemperature(&temp2); bmp.getTemperature(&temp2);
Serial.println(temp2);
Serial.println(pressure);
// float temperature = sensor.readTemperature(); float p10, p2_5;
// postFloatValue(temperature, 1, temperatureSensorID); 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.mode(WIFI_OFF);
WiFi.forceSleepBegin(); WiFi.forceSleepBegin();
} }
void ICACHE_RAM_ATTR ISR_geiger_impulse() {
geiger_counts++;
}
void setup() { void setup() {
Serial.begin(115200); 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() { void loop() {

Loading…
Cancel
Save