start modularization
This commit is contained in:
parent
563e22b81a
commit
2dd13a685e
12
things/Device.h
Normal file
12
things/Device.h
Normal file
@ -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
|
51
things/DeviceLdr.h
Normal file
51
things/DeviceLdr.h
Normal file
@ -0,0 +1,51 @@
|
||||
#ifndef DEVICELDR_H
|
||||
#define DEVICELDR_H
|
||||
|
||||
#include "Device.h"
|
||||
#include <Homie.h>
|
||||
|
||||
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
|
||||
|
||||
|
@ -1,6 +1,8 @@
|
||||
#include <Homie.h> // https://github.com/marvinroger/homie-esp8266
|
||||
#include <DHT.h> // 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);
|
||||
|
Loading…
Reference in New Issue
Block a user