Skip to content

Instantly share code, notes, and snippets.

@binarybucks
Created June 29, 2016 18:53
Show Gist options
  • Save binarybucks/66315c2eea1bdbaabccf1d8b7c60a245 to your computer and use it in GitHub Desktop.
Save binarybucks/66315c2eea1bdbaabccf1d8b7c60a245 to your computer and use it in GitHub Desktop.
#include <Homie.h>
#include "RCSwitch.h"
#define MAX_STRING_LEN 25
RCSwitch mySwitch = RCSwitch();
int transmitPort = D0;
String pad(String input, int len)
{
String output = input;
while (output.length() < len)
{
output = "0" + output;
}
return output;
}
void controlStatusSwitch(int houseCode, int socketCode, bool turnOn)
{
String houseCodeString = String(houseCode, BIN);
String socketCodeString = String(socketCode, BIN);
String message = statusPreamble + pad(houseCodeString, 4) + turnOn + pad(socketCodeString, 3);
char messageArray[24];
message.toCharArray(messageArray, 25);
mySwitch.send(messageArray);
}
char* pToS(String property) {
if(property == "1") {
return "10000";
} else if(property == "2") {
return "01000";
} else if(property == "3") {
return "00100";
} else if(property == "4") {
return "00010";
} else {
return "00001";
}
}
HomieNode rcSwitchNode("rcSwitch", "switch", rcSwitchOnHandler, true);
bool rcSwitchOnHandler(String property, String value) {
int socketCode = property.toInt();
if(socketCode > 5)
return false;
if (value == "true") {
mySwitch.switchOn("11011", pToS(property));
Homie.setNodeProperty(rcSwitchNode, property, "true"); // Update the state of the light
} else if (value == "false") {
mySwitch.switchOff("11011", pToS(property));
Homie.setNodeProperty(rcSwitchNode, property, "false");
} else {
return false;
}
return true;
}
void setup() {
// mySwitch.setProtocol(1);
// mySwitch.setRepeatTransmit(10);
// mySwitch.setPulseLength(307); // Bye Bye Standby
mySwitch.enableTransmit(transmitPort);
Homie.setFirmware("homie-switch", "1.0.0");
Homie.registerNode(rcSwitchNode);
Homie.setup();
}
void loop() {
Homie.loop();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment