73 lines
2.0 KiB
C++
73 lines
2.0 KiB
C++
#include <Homie.h>
|
|
|
|
#include "RelayNode.hpp"
|
|
#include "DHT22Node.hpp"
|
|
#include "ButtonNode.hpp"
|
|
#include "ContactNode.hpp"
|
|
#include "Heartbeater.hpp"
|
|
// #include "PingNode.hpp"
|
|
//const int trigPin = D1;
|
|
//const int echoPin = D2;
|
|
const int relayPin = D1;
|
|
const int contactPin = D2;
|
|
const int buttonPin = D5;
|
|
const int dhtPin = D7; ;
|
|
const int ledPin = LED_BUILTIN;
|
|
const int heartbeatPin = LED_BUILTIN;
|
|
|
|
unsigned long TEMPERATURE_INTERVAL = 120;
|
|
unsigned long lastTemperatureUpdate = 0;
|
|
bool relayInverse = true;
|
|
|
|
//PingNode obstacleNode("obstacle",trigPin,echoPin);
|
|
DHT22Node airNode("air",dhtPin,TEMPERATURE_INTERVAL);
|
|
RelayNode relayNode("relay",relayPin,ledPin,relayInverse);
|
|
ButtonNode buttonNode("button",buttonPin);
|
|
ContactNode contactNode("contact",contactPin);
|
|
Heartbeater heartbeater(heartbeatPin, heartbeatPin == LED_BUILTIN ? LOW : HIGH);
|
|
|
|
void signal_led(bool on = true);
|
|
|
|
void changeHandler() {
|
|
signal_led();
|
|
}
|
|
|
|
void buttonChangeHandler(bool down) {
|
|
Homie.getLogger() << "Button changing relay to " << (down ? "on" : "off") << endl;
|
|
relayNode.setRelay(down);
|
|
}
|
|
|
|
void loopHandler() {
|
|
heartbeater.loop();
|
|
// if (millis() - lastTemperatureUpdate > TEMPERATURE_INTERVAL * 1000UL || lastTemperatureUpdate == 0) {
|
|
// obstacleNode.setTemperature(airNode.getTemperature());
|
|
// lastTemperatureUpdate = millis();
|
|
// }
|
|
}
|
|
|
|
void setup() {
|
|
Serial.begin(115200);
|
|
Serial << endl << endl;
|
|
randomSeed(analogRead(0));
|
|
pinMode(ledPin, OUTPUT);
|
|
|
|
Homie_setBrand("EtxeanIoT");
|
|
Homie_setFirmware("etxean-garagesensor", "1.2.3");
|
|
Homie.setLoopFunction(loopHandler);
|
|
// obstacleNode.setChangeHandler(changeHandler);
|
|
buttonNode.onChange(buttonChangeHandler);
|
|
Homie.setup();
|
|
}
|
|
|
|
void loop() {
|
|
Homie.loop();
|
|
}
|
|
|
|
const uint8_t TURN_ON = ledPin == LED_BUILTIN ? LOW : HIGH;
|
|
const uint8_t TURN_OFF = ledPin == LED_BUILTIN ? HIGH : LOW;
|
|
|
|
void signal_led(bool on)
|
|
{
|
|
digitalWrite(ledPin, on ? TURN_ON : TURN_OFF);
|
|
}
|