diff --git a/things/Device.h b/things/Device.h new file mode 100644 index 0000000..decfd91 --- /dev/null +++ b/things/Device.h @@ -0,0 +1,12 @@ +#ifndef DEVICE_H +#define DEVICE_H + +class Device { + public: + // purely virtual functions, need to be implemented + virtual void setup() = 0; + virtual void homieRegister() = 0; + virtual void loop() = 0; +}; + +#endif diff --git a/things/DeviceLdr.h b/things/DeviceLdr.h new file mode 100644 index 0000000..3b08adb --- /dev/null +++ b/things/DeviceLdr.h @@ -0,0 +1,51 @@ +#ifndef DEVICELDR_H +#define DEVICELDR_H + +#include "Device.h" +#include + +class DeviceLdr : public Device { + public: + inline DeviceLdr(byte ldrPin) { + pin = ldrPin; + } + virtual void setup(); + virtual void homieRegister(); + virtual void loop(); + private: + byte pin; + const int INTERVAL_LDR = 60; + unsigned long lastSentLDR = 0; + int ldr = 0; + HomieNode ldrNode{"ldr", "ldr"}; +}; + +void DeviceLdr::setup() { + pinMode(pin, INPUT); +} + +void DeviceLdr::homieRegister() { + Homie.registerNode(ldrNode); +} + +void DeviceLdr::loop() { + if (millis() - lastSentLDR >= INTERVAL_LDR * 1000UL || lastSentLDR == 0) { + int ldr_new = analogRead(pin); + if (ldr_new != ldr) { + ldr = ldr_new; + float ldr_float = map(ldr, 0, 1023, 0, 10000) / 100.0; + Serial.print("LDR: "); + Serial.println(ldr_float); + if (!Homie.setNodeProperty(ldrNode, "value", String(ldr_float), true)) { + Serial.println("Sending failed"); + } + } else { + Serial.println("LDR value unchanged"); + } + lastSentLDR = millis(); + } +} + +#endif + + diff --git a/things/things.ino b/things/things.ino index c53e485..f0a0b85 100644 --- a/things/things.ino +++ b/things/things.ino @@ -1,6 +1,8 @@ #include // https://github.com/marvinroger/homie-esp8266 #include // https://github.com/adafruit/DHT-sensor-library +#include "DeviceLdr.h" + #define HAS_LDR #define HAS_LED #undef HAS_DHT @@ -16,10 +18,7 @@ int led_blue = 0; // HAS_LDR #define PIN_LDR A0 -HomieNode ldrNode("ldr", "ldr"); -const int INTERVAL_LDR = 60; -unsigned long lastSentLDR = 0; -int ldr = 0; +DeviceLdr deviceLdr(PIN_LDR); // HAS_DHT #define PIN_DHT D4 @@ -35,7 +34,7 @@ float heatindex; // computed value from the sensor void setupHandler() { #ifdef HAS_LDR - pinMode(PIN_LDR, INPUT); + deviceLdr.setup(); #endif #ifdef HAS_LED pinMode(PIN_LED_RED, OUTPUT); @@ -107,24 +106,6 @@ bool isEqual(float a, float b, float epsilon=0.001) { return fabs(a - b) <= epsilon * fabs(a); } -void loopHandlerLDR() { - if (millis() - lastSentLDR >= INTERVAL_LDR * 1000UL || lastSentLDR == 0) { - int ldr_new = analogRead(PIN_LDR); - if (ldr_new != ldr) { - ldr = ldr_new; - float ldr_float = map(ldr, 0, 1023, 0, 10000) / 100.0; - Serial.print("LDR: "); - Serial.println(ldr_float); - if (!Homie.setNodeProperty(ldrNode, "value", String(ldr_float), true)) { - Serial.println("Sending failed"); - } - } else { - Serial.println("LDR value unchanged"); - } - lastSentLDR = millis(); - } -} - void loopHandlerDHT() { if (millis() - lastSentDHT >= INTERVAL_DHT * 1000UL || lastSentDHT == 0) { float previousHumidity = humidity; @@ -176,7 +157,7 @@ void loopHandlerDHT() { void loopHandler() { #ifdef HAS_LDR - loopHandlerLDR(); + deviceLdr.loop(); #endif #ifdef HAS_DHT loopHandlerDHT(); @@ -186,7 +167,7 @@ void loopHandler() { void setup() { Homie.setFirmware("things", "1.0.0"); #ifdef HAS_LDR - Homie.registerNode(ldrNode); + deviceLdr.homieRegister(); #endif #ifdef HAS_LED ledNode.subscribe("on", ledOnHandler);