Browse Source

use sensordata struct

lightsleep
Hendrik Langer 7 years ago
parent
commit
834710f856
  1. 57
      src/main.cpp

57
src/main.cpp

@ -82,22 +82,21 @@ struct __attribute__((packed)) SENSOR_DATA {
float temp2;
float p10;
float p25;
unsigned long cpm;
float radioactivity;
float voltage;
float rssi;
} sd;
void ICACHE_FLASH_ATTR sendValues() {
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
sd.temperature = dht.readTemperature();
sd.humidity = dht.readHumidity();
float pressure, temp2;
bmp.getPressure(&pressure);
pressure /= 100;
bmp.getTemperature(&temp2);
bmp.getPressure(&(sd.pressure));
sd.pressure /= 100;
bmp.getTemperature(&(sd.temp2));
float p10, p2_5;
int sds_error = sds.read(&p2_5, &p10);
int sds_error = sds.read(&(sd.p25), &(sd.p10));
if(!sds_error) {
// Serial.println("SDS011 updated.");
} else {
@ -106,57 +105,57 @@ void ICACHE_FLASH_ATTR sendValues() {
// sds.sleep();
unsigned long cpm = 0;
float radiationValue = -1;
sd.cpm = 0;
sd.radioactivity = -1;
if (millis() - geiger_previousMillis > logging_period_ms) {
cpm = geiger_counts * 60000 / (millis() - geiger_previousMillis);
sd.cpm = geiger_counts * 60000 / (millis() - geiger_previousMillis);
unsigned long constexpr own_cpm = OWN_BACKGROUND_CPS * 60;
geiger_previousMillis = millis();
geiger_counts = 0;
radiationValue = (cpm - own_cpm) * CONV_FACTOR;
sd.radioactivity = (sd.cpm - own_cpm) * CONV_FACTOR;
// Serial.println(cpm);
} else {
// Serial.println("Geiger Counter: no new value");
}
Serial.printf("Temperature : %6.2f°C (DHT22)\n", temperature);
Serial.printf("Humidity : %6.2f% (DHT22)\n", humidity);
Serial.printf("Temperature : %6.2f°C (BMP180)\n", temp2);
Serial.printf("Pressure : %6.2fhPa (BMP180)\n", pressure);
if (!sds_error) Serial.printf("Particles 10 : %6.2fµg/m³ (SDS011)\n", p10);
if (!sds_error) Serial.printf("Particles 2.5: %6.2fµg/m³ (SDS011)\n", p2_5);
if (cpm > 0) Serial.printf("Radiation : %6.2fµSv/h (J305)\n", radiationValue);
Serial.printf("Temperature : %6.2f°C (DHT22)\n", sd.temperature);
Serial.printf("Humidity : %6.2f% (DHT22)\n", sd.humidity);
Serial.printf("Temperature : %6.2f°C (BMP180)\n", sd.temp2);
Serial.printf("Pressure : %6.2fhPa (BMP180)\n", sd.pressure);
if (!sds_error) Serial.printf("Particles 10 : %6.2fµg/m³ (SDS011)\n", sd.p10);
if (!sds_error) Serial.printf("Particles 2.5: %6.2fµg/m³ (SDS011)\n", sd.p25);
if (sd.cpm > 0) Serial.printf("Radiation : %6.2fµSv/h (J305)\n", sd.radioactivity);
DynamicJsonBuffer jsonBuffer;
JsonArray& array = jsonBuffer.createArray();
JsonObject& temperatureObject = array.createNestedObject();
temperatureObject["sensor"] = SENSOR1_ID;
temperatureObject["value"] = temperature;
temperatureObject["value"] = sd.temperature;
JsonObject& humidityObject = array.createNestedObject();
humidityObject["sensor"] = SENSOR2_ID;
humidityObject["value"] = humidity;
humidityObject["value"] = sd.humidity;
JsonObject& pressureObject = array.createNestedObject();
pressureObject["sensor"] = SENSOR3_ID;
pressureObject["value"] = pressure;
pressureObject["value"] = sd.pressure;
if (!sds_error) {
JsonObject& pm10Object = array.createNestedObject();
pm10Object["sensor"] = SENSOR4_ID;
pm10Object["value"] = p10;
pm10Object["value"] = sd.p10;
JsonObject& pm25Object = array.createNestedObject();
pm25Object["sensor"] = SENSOR5_ID;
pm25Object["value"] = p2_5;
pm25Object["value"] = sd.p25;
}
if (cpm > 0) {
if (sd.cpm > 0) {
JsonObject& cpmObject = array.createNestedObject();
cpmObject["sensor"] = SENSOR6_ID;
cpmObject["value"] = radiationValue;
cpmObject["value"] = sd.radioactivity;
}
JsonObject& voltageObject = array.createNestedObject();
@ -184,8 +183,8 @@ void ICACHE_FLASH_ATTR sendValues() {
Serial.print(".");
if (tries > 20) {
Serial.println(" [ERROR]");
Serial.println("Retrying in 1 minute..");
delay(postingInterval);
Serial.println("Retrying in 10 seconds..");
delay(powerbankInterval);
ESP.restart();
}
delay(500);
@ -256,7 +255,7 @@ void sendESPNOW() {
WiFi.forceSleepBegin();
delay(1); // yield();
digitalWrite(LED_BUILTIN, HIGH);
Serial.printf("sendESPNOW() took %d ms\n", millis()-espnowmillis);
// Serial.printf("sendESPNOW() took %d ms\n", millis()-espnowmillis);
}
void ICACHE_RAM_ATTR ISR_geiger_impulse() {

Loading…
Cancel
Save