2016-04-08 10:18:59 +02:00
|
|
|
#ifndef DEVICELDR_H
|
|
|
|
#define DEVICELDR_H
|
|
|
|
|
|
|
|
#include "Device.h"
|
|
|
|
#include <Homie.h>
|
|
|
|
|
|
|
|
class DeviceLdr : public Device {
|
|
|
|
public:
|
|
|
|
inline DeviceLdr(byte ldrPin) {
|
|
|
|
pin = ldrPin;
|
|
|
|
}
|
2016-04-11 00:29:05 +02:00
|
|
|
virtual void deviceSetup();
|
|
|
|
virtual void deviceRegister();
|
|
|
|
virtual void deviceLoop();
|
2016-04-08 10:18:59 +02:00
|
|
|
private:
|
|
|
|
byte pin;
|
|
|
|
const int INTERVAL_LDR = 60;
|
|
|
|
unsigned long lastSentLDR = 0;
|
|
|
|
int ldr = 0;
|
2016-04-10 00:20:50 +02:00
|
|
|
HomieNode ldrNode = HomieNode("ldr", "ldr");
|
2016-04-08 10:18:59 +02:00
|
|
|
};
|
|
|
|
|
2016-04-11 00:29:05 +02:00
|
|
|
void DeviceLdr::deviceSetup() {
|
2016-04-08 10:18:59 +02:00
|
|
|
pinMode(pin, INPUT);
|
|
|
|
}
|
|
|
|
|
2016-04-11 00:29:05 +02:00
|
|
|
void DeviceLdr::deviceRegister() {
|
2016-04-08 10:18:59 +02:00
|
|
|
Homie.registerNode(ldrNode);
|
|
|
|
}
|
|
|
|
|
2016-04-11 00:29:05 +02:00
|
|
|
void DeviceLdr::deviceLoop() {
|
2016-04-08 10:18:59 +02:00
|
|
|
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
|