extract Dht in separate file and disable it
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone Build is passing

This commit is contained in:
Nicu Hodos 2022-10-08 16:05:33 +02:00
parent 0bfef8c916
commit 7bb1f231ca
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 bblanchon/ArduinoJson@6.16.1
adafruit/Adafruit Unified Sensor@^1.1.4 adafruit/Adafruit Unified Sensor@^1.1.4
adafruit/DHT sensor library@1.3.10 adafruit/DHT sensor library@1.3.10
build_flags = -D DHT_SENSOR=0
upload_port = /dev/ttyUSB0 upload_port = /dev/ttyUSB0

View File

@ -1,25 +1,20 @@
#include <Arduino.h> #include <Arduino.h>
#include <RCSwitch.h> #include <RCSwitch.h>
#include <ArduinoJson.h>
#include <Adafruit_Sensor.h> #include <Adafruit_Sensor.h>
#include <DHT.h> #include <ArduinoJson.h>
#include "Tiny.h" #include "Tiny.h"
#include "Dht.h"
#define RESET_PIN 10 #define RESET_PIN 10
#define SEND_PIN 11 #define SEND_PIN 11
#define RECEIVE_PIN 2 #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(); RCSwitch mySwitch = RCSwitch();
DHT dht = DHT(DHT11_PIN, DHT11);
void readRcSwitch(JsonDocument& jsonDoc); void readRcSwitch(JsonDocument& jsonDoc);
void readCommand(); void readCommand();
void readDht(JsonDocument& jsonDoc);
void setup() { void setup() {
digitalWrite(RESET_PIN, HIGH); digitalWrite(RESET_PIN, HIGH);
@ -30,7 +25,7 @@ void setup() {
mySwitch.enableTransmit(SEND_PIN); mySwitch.enableTransmit(SEND_PIN);
mySwitch.setRepeatTransmit(10); mySwitch.setRepeatTransmit(10);
dht.begin(); Dht::setup();
Serial.begin(9600); Serial.begin(9600);
@ -38,11 +33,10 @@ void setup() {
} }
void loop() { void loop() {
currentTime = millis();
StaticJsonDocument<200> jsonDoc; StaticJsonDocument<200> jsonDoc;
readCommand(); readCommand();
readRcSwitch(jsonDoc); readRcSwitch(jsonDoc);
readDht(jsonDoc); Dht::read(jsonDoc);
if (!jsonDoc.isNull()) { if (!jsonDoc.isNull()) {
serializeJson(jsonDoc, Serial); serializeJson(jsonDoc, Serial);
Serial.println(); Serial.println();
@ -154,13 +148,3 @@ void readCommand() {
runJsonCommand(cmd); 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();
}
}