modularized led, doesn't work yet, though
This commit is contained in:
parent
2dd13a685e
commit
8523833139
101
things/DeviceLed.h
Normal file
101
things/DeviceLed.h
Normal file
@ -0,0 +1,101 @@
|
|||||||
|
#ifndef DEVICELED_H
|
||||||
|
#define DEVICELED_H
|
||||||
|
|
||||||
|
#include "Device.h"
|
||||||
|
#include <Homie.h>
|
||||||
|
|
||||||
|
class DeviceLed : public Device {
|
||||||
|
public:
|
||||||
|
inline DeviceLed(byte ledRed, byte ledGreen, byte ledBlue) {
|
||||||
|
pinRed = ledRed;
|
||||||
|
pinGreen = ledGreen;
|
||||||
|
pinBlue = ledBlue;
|
||||||
|
}
|
||||||
|
virtual void setup();
|
||||||
|
virtual void homieRegister();
|
||||||
|
virtual void loop();
|
||||||
|
bool ledOnHandler(String value);
|
||||||
|
bool ledColorHandler(String message);
|
||||||
|
private:
|
||||||
|
byte pinRed;
|
||||||
|
byte pinGreen;
|
||||||
|
byte pinBlue;
|
||||||
|
int led_red = 0;
|
||||||
|
int led_green = 0;
|
||||||
|
int led_blue = 0;
|
||||||
|
HomieNode ledNode{"led", "rgb"};
|
||||||
|
};
|
||||||
|
|
||||||
|
void DeviceLed::setup() {
|
||||||
|
pinMode(pinRed, OUTPUT);
|
||||||
|
pinMode(pinGreen, OUTPUT);
|
||||||
|
pinMode(pinBlue, OUTPUT);
|
||||||
|
analogWrite(pinRed, led_red);
|
||||||
|
analogWrite(pinGreen, led_green);
|
||||||
|
analogWrite(pinBlue, led_blue);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool DeviceLed::ledOnHandler(String value) {
|
||||||
|
if (value == "true") {
|
||||||
|
analogWrite(pinRed, led_red);
|
||||||
|
analogWrite(pinGreen, led_green);
|
||||||
|
analogWrite(pinBlue, led_blue);
|
||||||
|
Homie.setNodeProperty(ledNode, "on", "true");
|
||||||
|
Serial.println("led is on");
|
||||||
|
} else if (value == "false") {
|
||||||
|
analogWrite(pinRed, 0);
|
||||||
|
analogWrite(pinGreen, 0);
|
||||||
|
analogWrite(pinBlue, 0);
|
||||||
|
Homie.setNodeProperty(ledNode, "on", "false");
|
||||||
|
Serial.println("led is off");
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool DeviceLed::ledColorHandler(String message) {
|
||||||
|
DynamicJsonBuffer json_inBuffer;
|
||||||
|
JsonObject& json_in = json_inBuffer.parseObject(message);
|
||||||
|
if (json_in.success()) {
|
||||||
|
if (json_in.containsKey("red")) {
|
||||||
|
led_red = json_in["red"];
|
||||||
|
analogWrite(pinRed, led_red);
|
||||||
|
}
|
||||||
|
if (json_in.containsKey("green")) {
|
||||||
|
led_green = json_in["green"];
|
||||||
|
analogWrite(pinGreen, led_green);
|
||||||
|
}
|
||||||
|
if (json_in.containsKey("blue")) {
|
||||||
|
led_blue = json_in["blue"];
|
||||||
|
analogWrite(pinBlue, led_blue);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Serial.println("parsing of JSON failed");
|
||||||
|
}
|
||||||
|
|
||||||
|
DynamicJsonBuffer json_outBuffer;
|
||||||
|
JsonObject& json_out = json_outBuffer.createObject();
|
||||||
|
json_out["red"] = led_red;
|
||||||
|
json_out["green"] = led_green;
|
||||||
|
json_out["blue"] = led_blue;
|
||||||
|
String response;
|
||||||
|
json_out.printTo(response);
|
||||||
|
Serial.print("led state: ");
|
||||||
|
Serial.println(response);
|
||||||
|
Homie.setNodeProperty(ledNode, "color", response);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void DeviceLed::homieRegister() {
|
||||||
|
// TODO this doesn't work :-(
|
||||||
|
//ledNode.subscribe("on", &DeviceLed::ledOnHandler());
|
||||||
|
//ledNode.subscribe("color", &DeviceLed::ledColorHandler());
|
||||||
|
Homie.registerNode(ledNode);
|
||||||
|
}
|
||||||
|
|
||||||
|
void DeviceLed::loop() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
@ -1,6 +1,7 @@
|
|||||||
#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 "DeviceLed.h"
|
||||||
#include "DeviceLdr.h"
|
#include "DeviceLdr.h"
|
||||||
|
|
||||||
#define HAS_LDR
|
#define HAS_LDR
|
||||||
@ -11,10 +12,7 @@
|
|||||||
#define PIN_LED_RED D8
|
#define PIN_LED_RED D8
|
||||||
#define PIN_LED_GREEN D6
|
#define PIN_LED_GREEN D6
|
||||||
#define PIN_LED_BLUE D7
|
#define PIN_LED_BLUE D7
|
||||||
HomieNode ledNode("led", "rgb");
|
DeviceLed deviceLed(PIN_LED_RED, PIN_LED_GREEN, PIN_LED_BLUE);
|
||||||
int led_red = 0;
|
|
||||||
int led_green = 0;
|
|
||||||
int led_blue = 0;
|
|
||||||
|
|
||||||
// HAS_LDR
|
// HAS_LDR
|
||||||
#define PIN_LDR A0
|
#define PIN_LDR A0
|
||||||
@ -37,70 +35,13 @@ void setupHandler() {
|
|||||||
deviceLdr.setup();
|
deviceLdr.setup();
|
||||||
#endif
|
#endif
|
||||||
#ifdef HAS_LED
|
#ifdef HAS_LED
|
||||||
pinMode(PIN_LED_RED, OUTPUT);
|
deviceLed.setup();
|
||||||
pinMode(PIN_LED_GREEN, OUTPUT);
|
|
||||||
pinMode(PIN_LED_BLUE, OUTPUT);
|
|
||||||
analogWrite(PIN_LED_RED, led_red);
|
|
||||||
analogWrite(PIN_LED_GREEN, led_green);
|
|
||||||
analogWrite(PIN_LED_BLUE, led_blue);
|
|
||||||
#endif
|
#endif
|
||||||
#ifdef HAS_DHT
|
#ifdef HAS_DHT
|
||||||
dht.begin();
|
dht.begin();
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ledOnHandler(String value) {
|
|
||||||
if (value == "true") {
|
|
||||||
analogWrite(PIN_LED_RED, led_red);
|
|
||||||
analogWrite(PIN_LED_GREEN, led_green);
|
|
||||||
analogWrite(PIN_LED_BLUE, led_blue);
|
|
||||||
Homie.setNodeProperty(ledNode, "on", "true");
|
|
||||||
Serial.println("led is on");
|
|
||||||
} else if (value == "false") {
|
|
||||||
analogWrite(PIN_LED_RED, 0);
|
|
||||||
analogWrite(PIN_LED_GREEN, 0);
|
|
||||||
analogWrite(PIN_LED_BLUE, 0);
|
|
||||||
Homie.setNodeProperty(ledNode, "on", "false");
|
|
||||||
Serial.println("led is off");
|
|
||||||
} else {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool ledColorHandler(String message) {
|
|
||||||
DynamicJsonBuffer json_inBuffer;
|
|
||||||
JsonObject& json_in = json_inBuffer.parseObject(message);
|
|
||||||
if (json_in.success()) {
|
|
||||||
if (json_in.containsKey("red")) {
|
|
||||||
led_red = json_in["red"];
|
|
||||||
analogWrite(PIN_LED_RED, led_red);
|
|
||||||
}
|
|
||||||
if (json_in.containsKey("green")) {
|
|
||||||
led_green = json_in["green"];
|
|
||||||
analogWrite(PIN_LED_GREEN, led_green);
|
|
||||||
}
|
|
||||||
if (json_in.containsKey("blue")) {
|
|
||||||
led_blue = json_in["blue"];
|
|
||||||
analogWrite(PIN_LED_BLUE, led_blue);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Serial.println("parsing of JSON failed");
|
|
||||||
}
|
|
||||||
|
|
||||||
DynamicJsonBuffer json_outBuffer;
|
|
||||||
JsonObject& json_out = json_outBuffer.createObject();
|
|
||||||
json_out["red"] = led_red;
|
|
||||||
json_out["green"] = led_green;
|
|
||||||
json_out["blue"] = led_blue;
|
|
||||||
String response;
|
|
||||||
json_out.printTo(response);
|
|
||||||
Serial.print("led state: ");
|
|
||||||
Serial.println(response);
|
|
||||||
Homie.setNodeProperty(ledNode, "color", response);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// compare float values
|
// compare float values
|
||||||
bool isEqual(float a, float b, float epsilon=0.001) {
|
bool isEqual(float a, float b, float epsilon=0.001) {
|
||||||
return fabs(a - b) <= epsilon * fabs(a);
|
return fabs(a - b) <= epsilon * fabs(a);
|
||||||
@ -159,6 +100,9 @@ void loopHandler() {
|
|||||||
#ifdef HAS_LDR
|
#ifdef HAS_LDR
|
||||||
deviceLdr.loop();
|
deviceLdr.loop();
|
||||||
#endif
|
#endif
|
||||||
|
#ifdef HAS_LED
|
||||||
|
deviceLed.loop();
|
||||||
|
#endif
|
||||||
#ifdef HAS_DHT
|
#ifdef HAS_DHT
|
||||||
loopHandlerDHT();
|
loopHandlerDHT();
|
||||||
#endif
|
#endif
|
||||||
@ -170,9 +114,7 @@ void setup() {
|
|||||||
deviceLdr.homieRegister();
|
deviceLdr.homieRegister();
|
||||||
#endif
|
#endif
|
||||||
#ifdef HAS_LED
|
#ifdef HAS_LED
|
||||||
ledNode.subscribe("on", ledOnHandler);
|
deviceLed.homieRegister();
|
||||||
ledNode.subscribe("color", ledColorHandler);
|
|
||||||
Homie.registerNode(ledNode);
|
|
||||||
#endif
|
#endif
|
||||||
#ifdef HAS_DHT
|
#ifdef HAS_DHT
|
||||||
Homie.registerNode(humidityNode);
|
Homie.registerNode(humidityNode);
|
||||||
|
Loading…
Reference in New Issue
Block a user