Skip to content

Commit

Permalink
Support for toggling relay via websockets
Browse files Browse the repository at this point in the history
  • Loading branch information
xoseperez committed Sep 4, 2017
1 parent f7617ac commit e6c625a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
8 changes: 4 additions & 4 deletions code/espurna/relay.ino
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ unsigned char relayCount() {
return _relays.size();
}

unsigned char _relayValueFromPayload(const char * payload) {
unsigned char relayParsePayload(const char * payload) {

// Payload could be "OFF", "ON", "TOGGLE"
// or its number equivalents: 0, 1 or 2
Expand Down Expand Up @@ -365,7 +365,7 @@ void relaySetupAPI() {
snprintf_P(buffer, len, PSTR("%d"), relayStatus(relayID) ? 1 : 0);
},
[relayID](const char * payload) {
unsigned char value = _relayValueFromPayload(payload);
unsigned char value = relayParsePayload(payload);
if (value == 0xFF) {
DEBUG_MSG_P(PSTR("[RELAY] Wrong payload (%s)\n"), payload);
return;
Expand Down Expand Up @@ -440,7 +440,7 @@ void relayMQTTCallback(unsigned int type, const char * topic, const char * paylo
if (!t.startsWith(MQTT_TOPIC_RELAY)) return;

// Get value
unsigned char value = _relayValueFromPayload(payload);
unsigned char value = relayParsePayload(payload);
if (value == 0xFF) {
DEBUG_MSG_P(PSTR("[RELAY] Wrong payload (%s)\n"), payload);
return;
Expand All @@ -463,7 +463,7 @@ void relayMQTTCallback(unsigned int type, const char * topic, const char * paylo
if (value == 2) {
relayToggle(relayID);
} else {
relayStatus(relayID, value > 0, mqttForward());
relayStatus(relayID, value == 1, mqttForward());
}

}
Expand Down
25 changes: 18 additions & 7 deletions code/espurna/web.ino
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,26 @@ void _wsParse(uint32_t client_id, uint8_t * payload, size_t length) {

if (data.containsKey("status")) {

bool status = (strcmp(data["status"], "1") == 0);
unsigned char value = relayParsePayload(data["status"]);
if (value == 0xFF) {

unsigned int relayID = 0;
if (data.containsKey("id")) {
String value = data["id"];
relayID = value.toInt();
}
relayWS();

} else {

unsigned int relayID = 0;
if (data.containsKey("id")) {
String value = data["id"];
relayID = value.toInt();
}

relayStatus(relayID, status);
if (value == 2) {
relayToggle(relayID);
} else {
relayStatus(relayID, value == 1);
}

}

}

Expand Down

0 comments on commit e6c625a

Please sign in to comment.