diff --git a/code/espurna/config/general.h b/code/espurna/config/general.h index 42671b26..3260496f 100644 --- a/code/espurna/config/general.h +++ b/code/espurna/config/general.h @@ -86,6 +86,14 @@ #define MQTT_USE_SETTER "" +// ----------------------------------------------------------------------------- +// DOMOTICZ +// ----------------------------------------------------------------------------- + +#define ENABLE_DOMOTICZ 1 +#define DOMOTICZ_IN_TOPIC "domoticz/in" +#define DOMOTICZ_OUT_TOPIC "domoticz/out" + // ----------------------------------------------------------------------------- // NTP // ----------------------------------------------------------------------------- diff --git a/code/espurna/domoticz.ino b/code/espurna/domoticz.ino new file mode 100644 index 00000000..be9b4e59 --- /dev/null +++ b/code/espurna/domoticz.ino @@ -0,0 +1,78 @@ +/* + +ESPurna +DOMOTICZ MODULE + +Copyright (C) 2016 by Xose PĂ©rez + +*/ + +#if ENABLE_DOMOTICZ + +#include +#include + +void domoticzMQTTCallback(unsigned int type, const char * topic, const char * payload) { + + String dczTopicOut = getSetting("dczTopicOut", DOMOTICZ_OUT_TOPIC); + + if (type == MQTT_CONNECT_EVENT) { + mqttSubscribeRaw(dczTopicOut.c_str()); + } + + if (type == MQTT_MESSAGE_EVENT) { + + // Check topic + if (dczTopicOut.equals(topic)) { + + // Parse response + DynamicJsonBuffer jsonBuffer; + JsonObject& root = jsonBuffer.parseObject((char *) payload); + if (!root.success()) { + DEBUG_MSG("[DOMOTICZ] Error parsing data\n"); + return; + } + + // IDX + unsigned long idx = root["idx"]; + int relayID = domoticzRelay(idx); + if (relayID >= 0) { + unsigned long value = root["nvalue"]; + DEBUG_MSG("[DOMOTICZ] Received value %d for IDX %d\n", value, idx); + relayStatus(relayID, value == 1); + } + + } + + } + +} + +int domoticzIdx(unsigned int relayID) { + return getSetting("dczIdx" + String(relayID)).toInt(); +} + +int domoticzRelay(unsigned int idx) { + for (int relayID=0; relayID 0) { + unsigned int value = relayStatus(relayID) ? 1 : 0; + char payload[45]; + sprintf(payload, "{\"idx\": %d, \"nvalue\": %d, \"svalue\": \"\"}", idx, value); + mqttSendRaw(getSetting("dczTopicIn", DOMOTICZ_IN_TOPIC).c_str(), payload); + } +} + +void domoticzSetup() { + mqttRegister(domoticzMQTTCallback); +} + +#endif diff --git a/code/espurna/espurna.ino b/code/espurna/espurna.ino index 52e02a50..24b6ae57 100644 --- a/code/espurna/espurna.ino +++ b/code/espurna/espurna.ino @@ -125,6 +125,9 @@ void setup() { webSetup(); ntpSetup(); + #if ENABLE_DOMOTICZ + domoticzSetup(); + #endif #if ENABLE_FAUXMO fauxmoSetup(); #endif diff --git a/code/espurna/relay.ino b/code/espurna/relay.ino index 64ed9e36..a0d11cb8 100644 --- a/code/espurna/relay.ino +++ b/code/espurna/relay.ino @@ -92,6 +92,10 @@ bool relayStatus(unsigned char id, bool status, bool report) { relaySave(); } + #ifdef ENABLE_DOMOTICZ + domoticzSend(id); + #endif + } if (report) relayMQTT(id); diff --git a/code/espurna/web.ino b/code/espurna/web.ino index 6419bf2a..f7f85836 100644 --- a/code/espurna/web.ino +++ b/code/espurna/web.ino @@ -94,6 +94,7 @@ void _wsParse(uint32_t client_id, uint8_t * payload, size_t length) { bool fauxmoEnabled = false; #endif unsigned int network = 0; + unsigned int dczIdx = 0; String adminPass; for (unsigned int i=0; i= relayCount()) continue; + key = key + String(dczIdx); + ++dczIdx; + } + + #else + + if (key.startsWith("dcz")) continue; + #endif // Check password @@ -242,6 +261,19 @@ void _wsStart(uint32_t client_id) { root["apiEnabled"] = getSetting("apiEnabled").toInt() == 1; root["apiKey"] = getSetting("apiKey"); + #if ENABLE_DOMOTICZ + + root["dczVisible"] = 1; + root["dczTopicIn"] = getSetting("dczTopicIn", DOMOTICZ_IN_TOPIC); + root["dczTopicOut"] = getSetting("dczTopicOut", DOMOTICZ_OUT_TOPIC); + + JsonArray& dczIdx = root.createNestedArray("dczIdx"); + for (byte i=0; i div").length; + if (current > 0) return; + + var template = $("#idxTemplate .pure-g")[0]; + for (var id=0; id 1) $(".id", line).html(" " + id); + line.appendTo("#idxs"); + } + +} + function processData(data) { // title @@ -162,6 +179,19 @@ function processData(data) { } + // Domoticz + if (key == "dczIdx") { + var idxs = data.dczIdx; + createIdxs(idxs.length); + + for (var i in idxs) { + var element = $(".dczIdx[data=" + i + "]"); + if (element.length > 0) element.val(idxs[i]); + } + + return; + } + // Messages if (key == "message") { diff --git a/code/html/index.html b/code/html/index.html index c4606df0..3497b5b6 100644 --- a/code/html/index.html +++ b/code/html/index.html @@ -52,6 +52,10 @@ MQTT +
  • + DOMOTICZ +
  • +
  • POWER
  • @@ -139,14 +143,13 @@
    -
    +
    - @@ -335,10 +338,53 @@
    - +
     
    -
    Send a 0 or a 1 as a payload to the provided topic below to switch it on or off. You can also send a 2 to toggle its current state. The switch will also report its current open/close status to the same topic and its IP address, hertbeat, firmware version and file system version to the topic you define plus "/ip", "/heartbeat", "/version" and "/fsversion" respectively.
    +
    This is the root topic for this device. The {identifier} placeholder will be replaces by the device hostname.
    + - <root>/relay/# Send a 0 or a 1 as a payload to this topic to switch it on or off. You can also send a 2 to toggle its current state. Replace # with the relay ID (starting from 0). If the board has only one relay it will be 0.
    + - <root>/led/# Send a 0 or a 1 as a payload to this topic to set the onboard LED to the given state, send a 3 to turn it back to WIFI indicator. Replace # with the LED ID (starting from 0). If the board has only one LED it will be 0.
    + - <root>/ip The device will report to this topic its IP.
    + - <root>/heartbeat The device will report to this topic every few minutes.
    + - <root>/version The device will report to this topic its firmware version on boot.
    +
    + + +
    + + + +
    + +
    +

    DOMOTICZ

    +

    + Configure the connection to your Domoticz server. +

    +
    + +
    + +
    + +
    + + +
    + +
    + + +
    + +
    +
    + +
    +
    + +
    +
    @@ -361,7 +407,7 @@
    - +
     
    If you are using a pure resistive load like a bulb this will be writen on it, otherwise use a socket multimeter to get this value.