diff --git a/gateway/include/Dht.h b/gateway/include/Dht.h new file mode 100644 index 0000000..0c0d62b --- /dev/null +++ b/gateway/include/Dht.h @@ -0,0 +1,35 @@ +#if DHT_SENSOR + +#include + +#define DHT11_PIN 12 +#define READ_INTERVAL(c) (c*60*1000UL) // read interval in minutes + +DHT dht = DHT(DHT11_PIN, DHT11); +unsigned long currentTime = 0; + +namespace Dht { + void setup() { + dht.begin(); + } + + void read(JsonDocument& jsonDoc) { + currentTime = millis(); + static unsigned long lastReadTime = 0; + if (currentTime > lastReadTime) { + lastReadTime = currentTime + READ_INTERVAL(5); + JsonObject dht11 = jsonDoc.createNestedObject("dht11"); + dht11["temperature"] = dht.readTemperature(); + dht11["humidity"] = dht.readHumidity(); + } + } +} +#else +namespace Dht { + void setup() { + } + + void read(JsonDocument& jsonDoc) { + } +} +#endif \ No newline at end of file diff --git a/gateway/platformio.ini b/gateway/platformio.ini index 7a54706..5d8e537 100644 --- a/gateway/platformio.ini +++ b/gateway/platformio.ini @@ -19,4 +19,5 @@ lib_deps = bblanchon/ArduinoJson@6.16.1 adafruit/Adafruit Unified Sensor@^1.1.4 adafruit/DHT sensor library@1.3.10 +build_flags = -D DHT_SENSOR=0 upload_port = /dev/ttyUSB0 diff --git a/gateway/src/gateway.cpp b/gateway/src/gateway.cpp index 843ac35..2870728 100644 --- a/gateway/src/gateway.cpp +++ b/gateway/src/gateway.cpp @@ -1,25 +1,20 @@ #include #include -#include #include -#include +#include #include "Tiny.h" +#include "Dht.h" + #define RESET_PIN 10 #define SEND_PIN 11 #define RECEIVE_PIN 2 -#define DHT11_PIN 12 -#define READ_INTERVAL(c) (c*60*1000UL) // read interval in minutes - -unsigned long currentTime = 0; RCSwitch mySwitch = RCSwitch(); -DHT dht = DHT(DHT11_PIN, DHT11); void readRcSwitch(JsonDocument& jsonDoc); void readCommand(); -void readDht(JsonDocument& jsonDoc); void setup() { digitalWrite(RESET_PIN, HIGH); @@ -30,7 +25,7 @@ void setup() { mySwitch.enableTransmit(SEND_PIN); mySwitch.setRepeatTransmit(10); - dht.begin(); + Dht::setup(); Serial.begin(9600); @@ -38,11 +33,10 @@ void setup() { } void loop() { - currentTime = millis(); StaticJsonDocument<200> jsonDoc; readCommand(); readRcSwitch(jsonDoc); - readDht(jsonDoc); + Dht::read(jsonDoc); if (!jsonDoc.isNull()) { serializeJson(jsonDoc, Serial); Serial.println(); @@ -154,13 +148,3 @@ void readCommand() { runJsonCommand(cmd); } } - -void readDht(JsonDocument& jsonDoc) { - static unsigned long lastReadTime = 0; - if (currentTime > lastReadTime) { - lastReadTime = currentTime + READ_INTERVAL(5); - JsonObject dht11 = jsonDoc.createNestedObject("dht11"); - dht11["temperature"] = dht.readTemperature(); - dht11["humidity"] = dht.readHumidity(); - } -}