first version with working relay function
This commit is contained in:
parent
7fd1d56eae
commit
e59d3d0f73
34
things/DeviceRelay.cpp
Normal file
34
things/DeviceRelay.cpp
Normal file
@ -0,0 +1,34 @@
|
||||
#include "DeviceRelay.h"
|
||||
|
||||
void DeviceRelay::setRelay(bool value) {
|
||||
digitalWrite(pinRelay, value);
|
||||
}
|
||||
|
||||
void DeviceRelay::deviceSetup() {
|
||||
pinMode(pinRelay, OUTPUT);
|
||||
setRelay(HIGH);
|
||||
}
|
||||
|
||||
bool DeviceRelay::relayOnHandler(String value) {
|
||||
if (value == "true") {
|
||||
setRelay(HIGH);
|
||||
Homie.setNodeProperty(relayNode, "on", "true");
|
||||
Serial.println("relay is on");
|
||||
} else if (value == "false") {
|
||||
setRelay(LOW);
|
||||
Homie.setNodeProperty(relayNode, "on", "false");
|
||||
Serial.println("relay is off");
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void DeviceRelay::deviceRegister() {
|
||||
relayNode.subscribe("on", [this](String value) { return relayOnHandler(value); });
|
||||
Homie.registerNode(relayNode);
|
||||
}
|
||||
|
||||
void DeviceRelay::deviceLoop() {
|
||||
return;
|
||||
}
|
21
things/DeviceRelay.h
Normal file
21
things/DeviceRelay.h
Normal file
@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
#include "Device.h"
|
||||
#include <Homie.h>
|
||||
|
||||
class DeviceRelay : public Device {
|
||||
public:
|
||||
inline DeviceRelay(byte relayPin, char* relayName):relayNode(relayName, "relay") {
|
||||
pinRelay = relayPin;
|
||||
nameRelay = relayName;
|
||||
}
|
||||
virtual void deviceSetup();
|
||||
virtual void deviceRegister();
|
||||
virtual void deviceLoop();
|
||||
bool relayOnHandler(String value);
|
||||
private:
|
||||
byte pinRelay;
|
||||
char* nameRelay;
|
||||
void setRelay(bool value);
|
||||
HomieNode relayNode;
|
||||
};
|
@ -9,14 +9,22 @@
|
||||
*/
|
||||
|
||||
#include "DeviceButton.h"
|
||||
#include "DeviceRelay.h"
|
||||
|
||||
const char* FWNAME = "things@Sonoff";
|
||||
|
||||
const byte PIN_BUTTON = 0;
|
||||
DeviceButton deviceButton(PIN_BUTTON);
|
||||
|
||||
const byte PIN_RELAY = 12;
|
||||
DeviceRelay deviceRelay(PIN_RELAY, "relay");
|
||||
|
||||
Device* devices[] = {
|
||||
&deviceButton,
|
||||
&deviceRelay,
|
||||
};
|
||||
|
||||
void hardwareSetup() {};
|
||||
void hardwareSetup() {
|
||||
pinMode(PIN_RELAY, OUTPUT);
|
||||
digitalWrite(PIN_RELAY, HIGH);
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user