extract Dht in separate file and disable it

This commit is contained in:
Nicu Hodos 2022-10-08 16:05:33 +02:00
parent be7755c381
commit 5957c39045
3 changed files with 41 additions and 21 deletions

35
gateway/include/Dht.h Normal file
View File

@ -0,0 +1,35 @@
#if DHT_SENSOR
#include <DHT.h>
#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

View File

@ -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

View File

@ -1,25 +1,20 @@
#include <Arduino.h>
#include <RCSwitch.h>
#include <ArduinoJson.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <ArduinoJson.h>
#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();
}
}