split C code into header- and implementation-files
This commit is contained in:
parent
2970dad868
commit
50c38f4060
66
things/DeviceDht.cpp
Normal file
66
things/DeviceDht.cpp
Normal file
@ -0,0 +1,66 @@
|
||||
#include "DeviceDht.h"
|
||||
|
||||
void DeviceDht::deviceSetup() {
|
||||
pinMode(pin, INPUT);
|
||||
dht.begin();
|
||||
}
|
||||
|
||||
void DeviceDht::deviceRegister() {
|
||||
Homie.registerNode(humidityNode);
|
||||
Homie.registerNode(temperatureNode);
|
||||
Homie.registerNode(heatindexNode);
|
||||
}
|
||||
|
||||
// compare float values
|
||||
bool isEqual(float a, float b, float epsilon=0.001) {
|
||||
return fabs(a - b) <= epsilon * fabs(a);
|
||||
}
|
||||
|
||||
void DeviceDht::deviceLoop() {
|
||||
if (millis() - lastSentDHT >= INTERVAL_DHT * 1000UL || lastSentDHT == 0) {
|
||||
float previousHumidity = humidity;
|
||||
float previousTemperature = temperature;
|
||||
float previousHeatindex = heatindex;
|
||||
humidity = dht.readHumidity(); // read humidity as a percent
|
||||
temperature = dht.readTemperature(); // read temperature as Celsius
|
||||
heatindex = dht.computeHeatIndex(temperature, humidity, false);
|
||||
|
||||
// check if any reads failed and exit early
|
||||
if (isnan(humidity) || isnan(temperature)) {
|
||||
Serial.println("Failed to read from DHT sensor!");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!isEqual(humidity, previousHumidity)) {
|
||||
Serial.print("humidity: ");
|
||||
Serial.println(humidity);
|
||||
if (!Homie.setNodeProperty(humidityNode, "value", String(humidity), true)) {
|
||||
Serial.println("Sending failed");
|
||||
}
|
||||
} else {
|
||||
Serial.println("humidity unchanged");
|
||||
}
|
||||
|
||||
if (!isEqual(temperature, previousTemperature)) {
|
||||
Serial.print("temperature: ");
|
||||
Serial.println(temperature);
|
||||
if (!Homie.setNodeProperty(temperatureNode, "value", String(temperature), true)) {
|
||||
Serial.println("Sending failed");
|
||||
}
|
||||
} else {
|
||||
Serial.println("temperature unchanged");
|
||||
}
|
||||
|
||||
if (!isEqual(heatindex, previousHeatindex)) {
|
||||
Serial.print("heatindex: ");
|
||||
Serial.println(heatindex);
|
||||
if (!Homie.setNodeProperty(heatindexNode, "value", String(heatindex), true)) {
|
||||
Serial.println("Sending failed");
|
||||
}
|
||||
} else {
|
||||
Serial.println("heatindex unchanged");
|
||||
}
|
||||
|
||||
lastSentDHT = millis();
|
||||
}
|
||||
}
|
@ -27,69 +27,4 @@ class DeviceDht : public Device {
|
||||
HomieNode heatindexNode{"heatindex", "heatindex"};
|
||||
};
|
||||
|
||||
void DeviceDht::deviceSetup() {
|
||||
pinMode(pin, INPUT);
|
||||
dht.begin();
|
||||
}
|
||||
|
||||
void DeviceDht::deviceRegister() {
|
||||
Homie.registerNode(humidityNode);
|
||||
Homie.registerNode(temperatureNode);
|
||||
Homie.registerNode(heatindexNode);
|
||||
}
|
||||
|
||||
// compare float values
|
||||
bool isEqual(float a, float b, float epsilon=0.001) {
|
||||
return fabs(a - b) <= epsilon * fabs(a);
|
||||
}
|
||||
|
||||
void DeviceDht::deviceLoop() {
|
||||
if (millis() - lastSentDHT >= INTERVAL_DHT * 1000UL || lastSentDHT == 0) {
|
||||
float previousHumidity = humidity;
|
||||
float previousTemperature = temperature;
|
||||
float previousHeatindex = heatindex;
|
||||
humidity = dht.readHumidity(); // read humidity as a percent
|
||||
temperature = dht.readTemperature(); // read temperature as Celsius
|
||||
heatindex = dht.computeHeatIndex(temperature, humidity, false);
|
||||
|
||||
// check if any reads failed and exit early
|
||||
if (isnan(humidity) || isnan(temperature)) {
|
||||
Serial.println("Failed to read from DHT sensor!");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!isEqual(humidity, previousHumidity)) {
|
||||
Serial.print("humidity: ");
|
||||
Serial.println(humidity);
|
||||
if (!Homie.setNodeProperty(humidityNode, "value", String(humidity), true)) {
|
||||
Serial.println("Sending failed");
|
||||
}
|
||||
} else {
|
||||
Serial.println("humidity unchanged");
|
||||
}
|
||||
|
||||
if (!isEqual(temperature, previousTemperature)) {
|
||||
Serial.print("temperature: ");
|
||||
Serial.println(temperature);
|
||||
if (!Homie.setNodeProperty(temperatureNode, "value", String(temperature), true)) {
|
||||
Serial.println("Sending failed");
|
||||
}
|
||||
} else {
|
||||
Serial.println("temperature unchanged");
|
||||
}
|
||||
|
||||
if (!isEqual(heatindex, previousHeatindex)) {
|
||||
Serial.print("heatindex: ");
|
||||
Serial.println(heatindex);
|
||||
if (!Homie.setNodeProperty(heatindexNode, "value", String(heatindex), true)) {
|
||||
Serial.println("Sending failed");
|
||||
}
|
||||
} else {
|
||||
Serial.println("heatindex unchanged");
|
||||
}
|
||||
|
||||
lastSentDHT = millis();
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
23
things/DeviceIrRx.cpp
Normal file
23
things/DeviceIrRx.cpp
Normal file
@ -0,0 +1,23 @@
|
||||
#include "DeviceIrRx.h"
|
||||
|
||||
void DeviceIrRx::deviceSetup() {
|
||||
// pinMode(pin_irrx, INPUT); // this is done by enableIRIn()
|
||||
irrecv.enableIRIn();
|
||||
pinMode(pin_power, OUTPUT);
|
||||
digitalWrite(pin_power, HIGH);
|
||||
}
|
||||
|
||||
void DeviceIrRx::deviceRegister() {
|
||||
Homie.registerNode(irRxNode);
|
||||
}
|
||||
|
||||
void DeviceIrRx::deviceLoop() {
|
||||
if (irrecv.decode(&results)) {
|
||||
Serial.print("IR-Rx: ");
|
||||
Serial.println(results.value, HEX);
|
||||
if (!Homie.setNodeProperty(irRxNode, "value", String(results.value), false)) {
|
||||
Serial.println("Sending failed");
|
||||
}
|
||||
irrecv.resume(); // Receive the next value
|
||||
}
|
||||
}
|
@ -22,26 +22,4 @@ class DeviceIrRx : public Device {
|
||||
HomieNode irRxNode = HomieNode("irrx", "irrx");
|
||||
};
|
||||
|
||||
void DeviceIrRx::deviceSetup() {
|
||||
// pinMode(pin_irrx, INPUT); // this is done by enableIRIn()
|
||||
irrecv.enableIRIn();
|
||||
pinMode(pin_power, OUTPUT);
|
||||
digitalWrite(pin_power, HIGH);
|
||||
}
|
||||
|
||||
void DeviceIrRx::deviceRegister() {
|
||||
Homie.registerNode(irRxNode);
|
||||
}
|
||||
|
||||
void DeviceIrRx::deviceLoop() {
|
||||
if (irrecv.decode(&results)) {
|
||||
Serial.print("IR-Rx: ");
|
||||
Serial.println(results.value, HEX);
|
||||
if (!Homie.setNodeProperty(irRxNode, "value", String(results.value), false)) {
|
||||
Serial.println("Sending failed");
|
||||
}
|
||||
irrecv.resume(); // Receive the next value
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
81
things/DeviceIrTx.cpp
Normal file
81
things/DeviceIrTx.cpp
Normal file
@ -0,0 +1,81 @@
|
||||
#include "DeviceIrTx.h"
|
||||
|
||||
void DeviceIrTx::deviceSetup() {
|
||||
// pinMode(pin_irtx, OUTPUT); // this is done by begin()
|
||||
irsend.begin();
|
||||
}
|
||||
|
||||
bool DeviceIrTx::irTxHandler(String message) {
|
||||
DynamicJsonBuffer json_inBuffer;
|
||||
JsonObject& json_in = json_inBuffer.parseObject(message);
|
||||
if (json_in.success()) {
|
||||
unsigned long data = 0;
|
||||
int nbits = 0;
|
||||
if (json_in.containsKey("data")) {
|
||||
data = json_in["data"];
|
||||
}
|
||||
if (json_in.containsKey("nbits")) {
|
||||
nbits = json_in["nbits"];
|
||||
}
|
||||
if (json_in.containsKey("protocol")) {
|
||||
String protocol = json_in["protocol"];
|
||||
protocol.toUpperCase();
|
||||
Serial.print("IR-Tx: sending ");
|
||||
Serial.print(data);
|
||||
Serial.print(" (");
|
||||
Serial.print(nbits);
|
||||
Serial.print(") via ");
|
||||
Serial.print(protocol);
|
||||
Serial.println(" Protocol");
|
||||
for (int i = 0; i < 5; i++) {
|
||||
if (protocol == "NEC") {
|
||||
irsend.sendNEC(data, nbits);
|
||||
} else if (protocol == "SONY") {
|
||||
irsend.sendSony(data, nbits);
|
||||
} else if (protocol == "RC5") {
|
||||
irsend.sendRC5(data, nbits);
|
||||
} else if (protocol == "RC6") {
|
||||
irsend.sendRC6(data, nbits);
|
||||
} else if (protocol == "DISH") {
|
||||
irsend.sendDISH(data, nbits);
|
||||
} else if (protocol == "SHARP") {
|
||||
irsend.sendSharp(data, nbits);
|
||||
} else if (protocol == "PANASONIC") {
|
||||
irsend.sendPanasonic(data, nbits);
|
||||
/* JVC needs a repeat parameter, will be implemented if necessary
|
||||
} else if (protocol == "JVC") {
|
||||
int repeat = 0;
|
||||
irsend.sendJVC(data, nbits, repeat);
|
||||
*/
|
||||
/* Sanyo and Mitsubishi aren't implemented in the library
|
||||
} else if (protocol == "SANYO") {
|
||||
irsend.sendSanyo(data, nbits);
|
||||
} else if (protocol == "MITSUBISHI") {
|
||||
irsend.sendMitsubishi(data, nbits);
|
||||
*/
|
||||
} else if (protocol == "SAMSUNG") {
|
||||
irsend.sendSAMSUNG(data, nbits);
|
||||
} else if (protocol == "LG") {
|
||||
irsend.sendLG(data, nbits);
|
||||
} else if (protocol == "WHYNTER") {
|
||||
irsend.sendLG(data, nbits);
|
||||
} else {
|
||||
Serial.println("IR protocol not implemented");
|
||||
}
|
||||
delay(40);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
Serial.println("parsing of JSON failed");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void DeviceIrTx::deviceRegister() {
|
||||
irTxNode.subscribe("irtx", [this](String value) { return irTxHandler(value); });
|
||||
Homie.registerNode(irTxNode);
|
||||
}
|
||||
|
||||
void DeviceIrTx::deviceLoop() {
|
||||
return;
|
||||
}
|
@ -20,84 +20,4 @@ class DeviceIrTx : public Device {
|
||||
HomieNode irTxNode = HomieNode("irtx", "irtx");
|
||||
};
|
||||
|
||||
void DeviceIrTx::deviceSetup() {
|
||||
// pinMode(pin_irtx, OUTPUT); // this is done by begin()
|
||||
irsend.begin();
|
||||
}
|
||||
|
||||
bool DeviceIrTx::irTxHandler(String message) {
|
||||
DynamicJsonBuffer json_inBuffer;
|
||||
JsonObject& json_in = json_inBuffer.parseObject(message);
|
||||
if (json_in.success()) {
|
||||
unsigned long data = 0;
|
||||
int nbits = 0;
|
||||
if (json_in.containsKey("data")) {
|
||||
data = json_in["data"];
|
||||
}
|
||||
if (json_in.containsKey("nbits")) {
|
||||
nbits = json_in["nbits"];
|
||||
}
|
||||
if (json_in.containsKey("protocol")) {
|
||||
String protocol = json_in["protocol"];
|
||||
protocol.toUpperCase();
|
||||
Serial.print("IR-Tx: sending ");
|
||||
Serial.print(data);
|
||||
Serial.print(" (");
|
||||
Serial.print(nbits);
|
||||
Serial.print(") via ");
|
||||
Serial.print(protocol);
|
||||
Serial.println(" Protocol");
|
||||
for (int i = 0; i < 5; i++) {
|
||||
if (protocol == "NEC") {
|
||||
irsend.sendNEC(data, nbits);
|
||||
} else if (protocol == "SONY") {
|
||||
irsend.sendSony(data, nbits);
|
||||
} else if (protocol == "RC5") {
|
||||
irsend.sendRC5(data, nbits);
|
||||
} else if (protocol == "RC6") {
|
||||
irsend.sendRC6(data, nbits);
|
||||
} else if (protocol == "DISH") {
|
||||
irsend.sendDISH(data, nbits);
|
||||
} else if (protocol == "SHARP") {
|
||||
irsend.sendSharp(data, nbits);
|
||||
} else if (protocol == "PANASONIC") {
|
||||
irsend.sendPanasonic(data, nbits);
|
||||
/* JVC needs a repeat parameter, will be implemented if necessary
|
||||
} else if (protocol == "JVC") {
|
||||
int repeat = 0;
|
||||
irsend.sendJVC(data, nbits, repeat);
|
||||
*/
|
||||
/* Sanyo and Mitsubishi aren't implemented in the library
|
||||
} else if (protocol == "SANYO") {
|
||||
irsend.sendSanyo(data, nbits);
|
||||
} else if (protocol == "MITSUBISHI") {
|
||||
irsend.sendMitsubishi(data, nbits);
|
||||
*/
|
||||
} else if (protocol == "SAMSUNG") {
|
||||
irsend.sendSAMSUNG(data, nbits);
|
||||
} else if (protocol == "LG") {
|
||||
irsend.sendLG(data, nbits);
|
||||
} else if (protocol == "WHYNTER") {
|
||||
irsend.sendLG(data, nbits);
|
||||
} else {
|
||||
Serial.println("IR protocol not implemented");
|
||||
}
|
||||
delay(40);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
Serial.println("parsing of JSON failed");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void DeviceIrTx::deviceRegister() {
|
||||
irTxNode.subscribe("irtx", [this](String value) { return irTxHandler(value); });
|
||||
Homie.registerNode(irTxNode);
|
||||
}
|
||||
|
||||
void DeviceIrTx::deviceLoop() {
|
||||
return;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
27
things/DeviceLdr.cpp
Normal file
27
things/DeviceLdr.cpp
Normal file
@ -0,0 +1,27 @@
|
||||
#include "DeviceLdr.h"
|
||||
|
||||
void DeviceLdr::deviceSetup() {
|
||||
pinMode(pin, INPUT);
|
||||
}
|
||||
|
||||
void DeviceLdr::deviceRegister() {
|
||||
Homie.registerNode(ldrNode);
|
||||
}
|
||||
|
||||
void DeviceLdr::deviceLoop() {
|
||||
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();
|
||||
}
|
||||
}
|
@ -20,30 +20,4 @@ class DeviceLdr : public Device {
|
||||
HomieNode ldrNode = HomieNode("ldr", "ldr");
|
||||
};
|
||||
|
||||
void DeviceLdr::deviceSetup() {
|
||||
pinMode(pin, INPUT);
|
||||
}
|
||||
|
||||
void DeviceLdr::deviceRegister() {
|
||||
Homie.registerNode(ldrNode);
|
||||
}
|
||||
|
||||
void DeviceLdr::deviceLoop() {
|
||||
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
|
||||
|
72
things/DeviceLed.cpp
Normal file
72
things/DeviceLed.cpp
Normal file
@ -0,0 +1,72 @@
|
||||
#include "DeviceLed.h"
|
||||
|
||||
void DeviceLed::deviceSetup() {
|
||||
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::deviceRegister() {
|
||||
ledNode.subscribe("on", [this](String value) { return ledOnHandler(value); });
|
||||
ledNode.subscribe("color", [this](String value) { return ledColorHandler(value); });
|
||||
Homie.registerNode(ledNode);
|
||||
}
|
||||
|
||||
void DeviceLed::deviceLoop() {
|
||||
return;
|
||||
}
|
@ -26,75 +26,4 @@ class DeviceLed : public Device {
|
||||
HomieNode ledNode{"led", "rgb"};
|
||||
};
|
||||
|
||||
void DeviceLed::deviceSetup() {
|
||||
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::deviceRegister() {
|
||||
ledNode.subscribe("on", [this](String value) { return ledOnHandler(value); });
|
||||
ledNode.subscribe("color", [this](String value) { return ledColorHandler(value); });
|
||||
Homie.registerNode(ledNode);
|
||||
}
|
||||
|
||||
void DeviceLed::deviceLoop() {
|
||||
return;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user