Skip to content
This repository has been archived by the owner on Apr 11, 2018. It is now read-only.

Commit

Permalink
Add support for local relay toggling
Browse files Browse the repository at this point in the history
Add two new options for button handling. The first toggles the relay state
when a button is pressed, the second controls whether or not MQTT messages
are sent in response to a button press.
  • Loading branch information
mjg59 committed Dec 19, 2017
1 parent 89d754b commit af9246b
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 28 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ password=password
clientid=Wink_Relay1
topic_prefix=Relay1
screen_timeout=20
switch_toggle=false
send_switch=true
```
and put that in /sdcard/mqtt.ini on the Wink Relay.

Expand All @@ -63,6 +65,8 @@ password: Password used to authenticate to the MQTT broker (optional)
clientid: Client ID passed to the broker (optional - Wink_Relay if not provided)
topic_prefix: Prefix to the topics presented by the device (optional - Relay if not provided)
screen_timeout: Time in seconds until the screen turns off after a touch or proximity detection (optional - 10s if not provided)
switch_toggle: Whether pressing the switch should toggle the relay directly (optional - false if not provided)
send_switch: Whether pressing the switch should generate an MQTT message (optional - true if not provided)

Finally, reset your Relay.

Expand Down
97 changes: 69 additions & 28 deletions wink-handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
#include <stdbool.h>
#include <linux/input.h>
#include <sys/time.h>
#include <sys/resource.h>
Expand All @@ -20,44 +21,62 @@ struct configuration {
char *topic_prefix;
int port;
int screen_timeout;
bool switch_toggle;
bool send_switch;
};

static struct configuration config;

void handle_relay1(MessageData *md)
#define UPPER_RELAY "/sys/class/gpio/gpio203/value"
#define LOWER_RELAY "/sys/class/gpio/gpio204/value"

void toggle_relay(char *path)
{
int fd;
char state;

fd = open(path, O_RDWR);
read(fd, &state, sizeof(state));
lseek(fd, 0, SEEK_SET);
if (state == '0') {
state = '1';
} else {
state = '0';
}
write(fd, &state, 1);
close(fd);
}

void handle_relay(char *path, char *payload, int len)
{
int fd;
MQTTMessage *message = md->message;
char power;

signal(SIGPIPE, SIG_IGN);

fd = open("/sys/class/gpio/gpio203/value", O_RDWR);
if (strncmp(message->payload, "ON", message->payloadlen) == 0) {
fd = open(path, O_RDWR);
if (strncmp(payload, "ON", len) == 0) {
power = '1';
write(fd, &power, 1);
} else if (strncmp(message->payload, "OFF", message->payloadlen) == 0) {
} else if (strncmp(payload, "OFF", len) == 0) {
power = '0';
write(fd, &power, 1);
}
close(fd);
}

void handle_relay1(MessageData *md)
{
MQTTMessage *message = md->message;

handle_relay(UPPER_RELAY, message->payload, message->payloadlen);
}

void handle_relay2(MessageData *md)
{
int fd;
MQTTMessage *message = md->message;
char power;

fd = open("/sys/class/gpio/gpio204/value", O_RDWR);
if (strncmp(message->payload, "ON", message->payloadlen) == 0) {
power = '1';
write(fd, &power, 1);
} else if (strncmp(message->payload, "OFF", message->payloadlen) == 0) {
power = '0';
write(fd, &power, 1);
}
close(fd);
handle_relay(LOWER_RELAY, message->payload, message->payloadlen);
}

int mqtt_connect(Network *n, MQTTClient *c, char *buf, char *readbuf) {
Expand Down Expand Up @@ -116,6 +135,18 @@ static int config_handler(void* data, const char* section, const char* name,
config.port = atoi(value);
} else if (strcmp(name, "screen_timeout") == 0) {
config.screen_timeout = atoi(value);
} else if (strcmp(name, "switch_toggle") == 0) {
if (strcmp(value, "true") == 0) {
config.switch_toggle = true;
} else if (strcmp(value, "false") == 0) {
config.switch_toggle = false;
}
} else if (strcmp(name, "send_switch") == 0) {
if (strcmp(value, "true") == 0) {
config.send_switch = true;
} else if (strcmp(value, "false") == 0) {
config.send_switch = false;
}
}
return 1;
}
Expand Down Expand Up @@ -143,6 +174,8 @@ int main() {
char *prefix, topic[1024];
int timeout;

config.send_switch = true;

if (ini_parse("/sdcard/mqtt.ini", config_handler, NULL) < 0) {
printf("Can't load /sdcard/mqtt.ini\n");
return 1;
Expand Down Expand Up @@ -224,25 +257,33 @@ int main() {
read(uswitch, buffer, sizeof(buffer));
if (buffer[0] == '0' && uswitchstate == 1) {
uswitchstate = 0;
message.qos = 1;
message.payload = payload;
sprintf(payload, "on");
message.payloadlen = strlen(payload);
sprintf(topic, "%s/switches/upper", prefix);
MQTTPublish(&c, topic, &message);
if (config.send_switch) {
message.qos = 1;
message.payload = payload;
sprintf(payload, "on");
message.payloadlen = strlen(payload);
sprintf(topic, "%s/switches/upper", prefix);
MQTTPublish(&c, topic, &message);
}
if (config.switch_toggle)
toggle_relay(UPPER_RELAY);
} else if (buffer[0] == '1' && uswitchstate == 0) {
uswitchstate = 1;
}
lseek(lswitch, 0, SEEK_SET);
read(lswitch, buffer, sizeof(buffer));
if (buffer[0] == '0' && lswitchstate == 1) {
lswitchstate = 0;
message.qos = 1;
message.payload = payload;
sprintf(payload, "on");
message.payloadlen = strlen(payload);
sprintf(topic, "%s/switches/lower", prefix);
MQTTPublish(&c, topic, &message);
if (config.send_switch) {
message.qos = 1;
message.payload = payload;
sprintf(payload, "on");
message.payloadlen = strlen(payload);
sprintf(topic, "%s/switches/lower", prefix);
MQTTPublish(&c, topic, &message);
}
if (config.switch_toggle)
toggle_relay(LOWER_RELAY);
} else if (buffer[0] == '1' && lswitchstate == 0) {
lswitchstate = 1;
}
Expand Down

0 comments on commit af9246b

Please sign in to comment.