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

Commit

Permalink
Merge pull request #10 from marthoc/control-screen-with-mqtt
Browse files Browse the repository at this point in the history
Control screen with MQTT
  • Loading branch information
mjg59 authored Mar 13, 2018
2 parents 547642f + 33d82e6 commit bec6748
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 10 deletions.
22 changes: 15 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Installing
You'll need adb access to a rooted Wink Relay. Disable the existing Wink control software by running

```
pm disable http://com.quirky.android .wink.projectone
pm disable http://com.quirky.android.wink.projectone
```

as root. Remount /system read-write:
Expand All @@ -39,7 +39,7 @@ rm /system/bin/edisonwink
adb push wink-handler to /sdcard and then copy it over edisonwink and fix permissions:

```
cp /sdcard/wink0handler /system/bin/edisonwink
cp /sdcard/wink-handler /system/bin/edisonwink
chmod 755 /system/bin/edisonwink
```

Expand All @@ -66,9 +66,9 @@ 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)
motion_timeout: Time in seconds until the motion detection resets (i.e. time it takes for an "off" message to follow an "on" message)
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)
prox_delta: The percentage change in proximity reading required before triggering the screen to turn on and an mqtt motion detection message (optional - 0.75 if not provided).

Finally, reset your Relay.
Expand Down Expand Up @@ -154,10 +154,18 @@ Relay/relays/lower_state

state topics.

Screen control
--------------
Screen
------

The screen will turn on if the screen is touched or if the proximity sensor is triggered. It can also be controlled via the

```
Relay/screen
```

command topic: "ON" turns the screen on, "OFF" turns the screen off.

The screen will automatically turn on if the screen is touched and off 10 seconds later. It will also turn on and remain on if the proximity sensor is triggered, turning off 10 seconds after the last proximity detection.
The screen will turn off 10 seconds after the last touch, proximity, or mqtt "ON" command by default. This duration can be configured via the `screen_timeout` parameter in mqtt.ini.

Proximity Sensor Sensitivity
----------------------------
Expand Down
34 changes: 31 additions & 3 deletions wink-handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ struct configuration {

static struct configuration config;

int last_input;

#define UPPER_RELAY "/sys/class/gpio/gpio203/value"
#define LOWER_RELAY "/sys/class/gpio/gpio204/value"
#define SCREEN "/sys/class/gpio/gpio30/value"

void toggle_relay(char *path)
{
Expand Down Expand Up @@ -81,6 +84,27 @@ void handle_relay2(MessageData *md)
handle_relay(LOWER_RELAY, message->payload, message->payloadlen);
}

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

fd = open(SCREEN, O_RDWR);
lseek(fd, 0, SEEK_SET);
read(fd, &power, sizeof(power));

if (strncmp(message->payload, "ON", message->payloadlen) == 0) {
last_input = time(NULL);
power = '1';
write(fd, &power, 1);
} else if (strncmp(message->payload, "OFF", message->payloadlen) == 0) {
power = '0';
write(fd, &power, 1);
}
close(fd);
}

int mqtt_connect(Network *n, MQTTClient *c, char *buf, char *readbuf) {
int ret;
MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
Expand Down Expand Up @@ -185,6 +209,7 @@ int main() {
int last_proximity;
float delta;

last_input = time(NULL);
config.send_switch = true;

if (ini_parse("/sdcard/mqtt.ini", config_handler, NULL) < 0) {
Expand Down Expand Up @@ -229,9 +254,6 @@ int main() {
write(relay1, &power, 1);
write(relay2, &power, 1);

lseek(screen, 0, SEEK_SET);
read(screen, &power, sizeof(power));

limits.rlim_cur = RLIM_INFINITY;
limits.rlim_max = RLIM_INFINITY;
setrlimit(RLIMIT_CORE, &limits);
Expand All @@ -242,6 +264,9 @@ int main() {
MQTTSubscribe(&c, topic, 0, handle_relay1);
sprintf(topic, "%s/relays/lower", prefix);
MQTTSubscribe(&c, topic, 0, handle_relay2);
sprintf(topic, "%s/screen", prefix);
MQTTSubscribe(&c, topic, 0, handle_screen);


while (1) {
lseek(relay1, 0, SEEK_SET);
Expand Down Expand Up @@ -342,6 +367,9 @@ int main() {
lseek(prox, 0, SEEK_SET);
read(prox, proxdata, sizeof(proxdata));
proximity = strtol(proxdata, NULL, 10);
lseek(screen, 0, SEEK_SET);
read(screen, &power, sizeof(power));

if (proximity > (last_proximity * (1 + delta / 100))) {
last_input = time(NULL);
if (power != '1') {
Expand Down

0 comments on commit bec6748

Please sign in to comment.