From 4a580376388d4feffda93a262ecec07ef4959c73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Tue, 27 Dec 2016 04:53:26 +0100 Subject: [PATCH] Set inching mode through MQTT --- code/src/button.ino | 5 +-- code/src/config/hardware.h | 8 ++--- code/src/relay.ino | 69 ++++++++++++++++++++++++++++++++------ 3 files changed, 63 insertions(+), 19 deletions(-) diff --git a/code/src/button.ino b/code/src/button.ino index 7cc0f5bf..529dfcfe 100644 --- a/code/src/button.ino +++ b/code/src/button.ino @@ -99,10 +99,7 @@ void buttonLoop() { } #ifdef ITEAD_1CH_INCHING if (i == 1) { - byte relayInch = getSetting("relayInch", String(RELAY_INCHING)).toInt(); - relayInch = (relayInch == RELAY_INCHING_NONE) ? RELAY_INCHING_OFF : RELAY_INCHING_NONE; - setSetting("relayInch", String() + relayInch); - digitalWrite(LED_INCHING, relayInch != RELAY_INCHING_NONE); + relayInchingToggle(); continue; } #endif diff --git a/code/src/config/hardware.h b/code/src/config/hardware.h index ace41432..a67a7261 100644 --- a/code/src/config/hardware.h +++ b/code/src/config/hardware.h @@ -136,11 +136,11 @@ #define MANUFACTURER "ITEAD" #define DEVICE "1CH_INCHING" #define BUTTON1_PIN 0 - #define BUTTON2_PIN 9 + #define BUTTON2_PIN 15 #define RELAY1_PIN 12 - #define LED_PIN 13 - #define LED_PIN_INVERSE 0 - #define LED_INCHING 5 + #define LED1_PIN 13 + #define LED1_PIN_INVERSE 0 + #define LED_INCHING 14 // ----------------------------------------------------------------------------- // Electrodragon boards diff --git a/code/src/relay.ino b/code/src/relay.ino index 5042eab7..1396f172 100644 --- a/code/src/relay.ino +++ b/code/src/relay.ino @@ -69,9 +69,9 @@ void relayInchingBack(unsigned char id) { inching.detach(); } -void relayInching(unsigned char id) { +void relayInchingStart(unsigned char id) { - byte relayInch = getSetting("relayInch", String(RELAY_INCHING)).toInt(); + byte relayInch = getSetting("relayInch", RELAY_INCHING).toInt(); if (relayInch == RELAY_INCHING_NONE) return; bool status = relayStatus(id); @@ -82,13 +82,53 @@ void relayInching(unsigned char id) { } inching.attach( - getSetting("relayInchTime", String(RELAY_INCHING_TIME)).toInt(), + getSetting("relayInchTime", RELAY_INCHING_TIME).toInt(), relayInchingBack, id ); } +unsigned int relayInching() { + unsigned int relayInch = getSetting("relayInch", RELAY_INCHING).toInt(); + return relayInch; +} + +void relayInching(unsigned int relayInch, bool report) { + + setSetting("relayInch", relayInch); + + /* + if (report) { + String mqttGetter = getSetting("mqttGetter", MQTT_USE_GETTER); + char topic[strlen(MQTT_RELAY_TOPIC) + mqttGetter.length() + 10]; + sprintf(topic, "%s/inching%s", MQTT_RELAY_TOPIC, mqttGetter.c_str()); + char value[2]; + sprintf(value, "%d", relayInch); + mqttSend(topic, value); + } + */ + + char message[20]; + sprintf(message, "{\"relayInch\": %d}", relayInch); + wsSend(message); + + #ifdef LED_INCHING + digitalWrite(LED_INCHING, relayInch != RELAY_INCHING_NONE); + #endif + +} + +void relayInching(unsigned int relayInch) { + relayInching(relayInch, true); +} + +void relayInchingToggle() { + unsigned int relayInch = relayInching(); + relayInch = (relayInch == RELAY_INCHING_NONE) ? RELAY_INCHING_OFF : RELAY_INCHING_NONE; + relayInching(relayInch); +} + bool relayStatus(unsigned char id, bool status, bool report) { if (id >= _relays.size()) return false; @@ -115,7 +155,7 @@ bool relayStatus(unsigned char id, bool status, bool report) { #endif if (!recursive) { - relayInching(id); + relayInchingStart(id); relaySync(id); relaySave(); } @@ -209,10 +249,16 @@ void relayMQTTCallback(unsigned int type, const char * topic, const char * paylo bool sameSetGet = mqttGetter.compareTo(mqttSetter) == 0; if (type == MQTT_CONNECT_EVENT) { + relayMQTT(); - char buffer[strlen(MQTT_RELAY_TOPIC) + mqttSetter.length() + 3]; + char buffer[strlen(MQTT_RELAY_TOPIC) + mqttSetter.length() + 10]; + sprintf(buffer, "%s/+%s", MQTT_RELAY_TOPIC, mqttSetter.c_str()); mqttSubscribe(buffer); + + sprintf(buffer, "%s/inching%s", MQTT_RELAY_TOPIC, mqttSetter.c_str()); + mqttSubscribe(buffer); + } if (type == MQTT_MESSAGE_EVENT) { @@ -222,11 +268,13 @@ void relayMQTTCallback(unsigned int type, const char * topic, const char * paylo if (!t.startsWith(MQTT_RELAY_TOPIC)) return; if (!t.endsWith(mqttSetter)) return; - // If relayMode is not SAME avoid responding to a retained message - if (sameSetGet && isFirstMessage) { - isFirstMessage = false; - byte relayMode = getSetting("relayMode", RELAY_MODE).toInt(); - if (relayMode != RELAY_MODE_SAME) return; + // Get value + unsigned int value = (char)payload[0] - '0'; + + // Inching topic + if (t.indexOf("inching") > 0) { + relayInching(value, !sameSetGet); + return; } // Get relay ID @@ -234,7 +282,6 @@ void relayMQTTCallback(unsigned int type, const char * topic, const char * paylo if (relayID >= relayCount()) relayID = 0; // Action to perform - unsigned int value = (char)payload[0] - '0'; if (value == 2) { relayToggle(relayID); } else {