Skip to content

Instantly share code, notes, and snippets.

@SBajonczak
Created Dec 18, 2019
Embed
What would you like to do?
ChristmasTown
#include <Homie.h>
#include <Ticker.h>
#define FW_NAME "bochumsba-iot-fw";#
##define FW_VERSION "2.0.0"
const int PIN_Church = 2;
const int PIN_TownHall = 0;
const int PIN_House1 = 4;
const int PIN_House2 = 16;
const int PIN_House3 = 5;
bool ChurchHandler(String property, String value);
bool TownHallHandler(String property, String value);
bool House1Handler(String property, String value);
bool House2Handler(String property, String value);
bool House3Handler(String property, String value);
bool PinOnHandler(String property, String value);
bool PinOffHandler(String property, String value);
HomieNode lightNodeChurch("Church", "switch");
HomieNode lightNodeTownHall("TownHall", "switch");
HomieNode lightNodeHouse1("House1", "switch");
HomieNode lightNodeHouse2("House2", "switch");
HomieNode lightNodeHouse3("House3", "switch");
HomieNode lightpinon("pinon", "switch");
HomieNode lightpinoff("pinoff", "switch");
HomieNode jsonNode("data", "__json__");
bool globalInputHandler(const HomieNode & node,
const String & property,
const HomieRange & range,
const String & value) {
Homie.getLogger() << "Calling event " << property << "" << node.getId() << endl;
StaticJsonBuffer < 200 > jsonBuffer;
JsonObject & root = jsonBuffer.createObject();
if (node.getId() == "pinon") {
Homie.getLogger() << "Running pinon" << endl;
digitalWrite(value.toInt(), HIGH);
lightpinon.setProperty("state").setRetained(true).send(value);
Homie.getLogger() << "ran pinon" << endl;
}
if (node.getId() == "pinoff") {
Homie.getLogger() << "Running pinoff" << endl;
digitalWrite(value.toInt(), LOW);
lightpinoff.setProperty("state").setRetained(true).send(value);
Homie.getLogger() << "ran pinoff" << endl;
}
root["Actor"] = node.getId();
root["LastState"] = (value == "true" ? "false" : "true");
String values;
root.printTo(values);
Homie.getLogger() << "Json data:" << values << endl;
jsonNode.setProperty("__json__").setRetained(false).send(values);
return false;
}
void setup() {
Serial.begin(115200);
Serial << endl << endl;
pinMode(2, OUTPUT);
pinMode(0, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(16, OUTPUT);
digitalWrite(0, HIGH);
digitalWrite(2, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
Serial << endl << endl;
Homie.__setBrand("Christmas-town");
Homie_setFirmware(FW_NAME, FW_VERSION);
Homie.disableResetTrigger();
Homie.disableLedFeedback(); // collides with Serial on ESP07
Homie.setGlobalInputHandler(globalInputHandler); // before Homie.setup()
lightNodeChurch.advertise("on").settable();
lightNodeTownHall.advertise("on").settable();
lightNodeHouse1.advertise("on").settable();
lightNodeHouse2.advertise("on").settable();
lightNodeHouse3.advertise("on").settable();
lightpinon.advertise("on").settable();
lightpinoff.advertise("on").settable();
lightpinon.advertise("state");
lightpinoff.advertise("state");
lightNodeChurch.advertise("state");
lightNodeTownHall.advertise("state");
lightNodeHouse1.advertise("state");
lightNodeHouse2.advertise("state");
lightNodeHouse3.advertise("state");
lightpinoff.setProperty("state").send("1");
lightpinon.setProperty("state").send("1");
lightNodeChurch.setProperty("state").send("false");
lightNodeTownHall.setProperty("state").send("false");
lightNodeHouse1.setProperty("state").send("false");
lightNodeHouse2.setProperty("state").send("false");
lightNodeHouse3.setProperty("state").send("false");
Homie.setup();
}
void loop() {
Homie.loop();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment