Browse Source

Support for toggling relay via websockets

fastled
Xose Pérez 7 years ago
parent
commit
e6c625ad93
2 changed files with 22 additions and 11 deletions
  1. +4
    -4
      code/espurna/relay.ino
  2. +18
    -7
      code/espurna/web.ino

+ 4
- 4
code/espurna/relay.ino View File

@ -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
@ -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;
@ -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;
@ -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());
}
}


+ 18
- 7
code/espurna/web.ino View File

@ -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);
}
}
}


Loading…
Cancel
Save