first version with working relay function

This commit is contained in:
Ronald Schaten 2016-12-14 23:14:17 +01:00
parent 7fd1d56eae
commit e59d3d0f73
3 changed files with 64 additions and 1 deletions

34
things/DeviceRelay.cpp Normal file
View 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
View 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;
};

View File

@ -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);
};