start modularization

This commit is contained in:
Ronald Schaten 2016-04-08 10:18:59 +02:00
parent 563e22b81a
commit 2dd13a685e
3 changed files with 69 additions and 25 deletions

12
things/Device.h Normal file
View 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
View 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

View File

@ -1,6 +1,8 @@
#include <Homie.h> // https://github.com/marvinroger/homie-esp8266 #include <Homie.h> // https://github.com/marvinroger/homie-esp8266
#include <DHT.h> // https://github.com/adafruit/DHT-sensor-library #include <DHT.h> // https://github.com/adafruit/DHT-sensor-library
#include "DeviceLdr.h"
#define HAS_LDR #define HAS_LDR
#define HAS_LED #define HAS_LED
#undef HAS_DHT #undef HAS_DHT
@ -16,10 +18,7 @@ int led_blue = 0;
// HAS_LDR // HAS_LDR
#define PIN_LDR A0 #define PIN_LDR A0
HomieNode ldrNode("ldr", "ldr"); DeviceLdr deviceLdr(PIN_LDR);
const int INTERVAL_LDR = 60;
unsigned long lastSentLDR = 0;
int ldr = 0;
// HAS_DHT // HAS_DHT
#define PIN_DHT D4 #define PIN_DHT D4
@ -35,7 +34,7 @@ float heatindex; // computed value from the sensor
void setupHandler() { void setupHandler() {
#ifdef HAS_LDR #ifdef HAS_LDR
pinMode(PIN_LDR, INPUT); deviceLdr.setup();
#endif #endif
#ifdef HAS_LED #ifdef HAS_LED
pinMode(PIN_LED_RED, OUTPUT); 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); 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() { void loopHandlerDHT() {
if (millis() - lastSentDHT >= INTERVAL_DHT * 1000UL || lastSentDHT == 0) { if (millis() - lastSentDHT >= INTERVAL_DHT * 1000UL || lastSentDHT == 0) {
float previousHumidity = humidity; float previousHumidity = humidity;
@ -176,7 +157,7 @@ void loopHandlerDHT() {
void loopHandler() { void loopHandler() {
#ifdef HAS_LDR #ifdef HAS_LDR
loopHandlerLDR(); deviceLdr.loop();
#endif #endif
#ifdef HAS_DHT #ifdef HAS_DHT
loopHandlerDHT(); loopHandlerDHT();
@ -186,7 +167,7 @@ void loopHandler() {
void setup() { void setup() {
Homie.setFirmware("things", "1.0.0"); Homie.setFirmware("things", "1.0.0");
#ifdef HAS_LDR #ifdef HAS_LDR
Homie.registerNode(ldrNode); deviceLdr.homieRegister();
#endif #endif
#ifdef HAS_LED #ifdef HAS_LED
ledNode.subscribe("on", ledOnHandler); ledNode.subscribe("on", ledOnHandler);