From 12bd11bbe12346bd24dab300e1680b34af5efcfe Mon Sep 17 00:00:00 2001 From: Florian <1technophile@users.noreply.github.com> Date: Mon, 4 Nov 2019 20:51:29 +0100 Subject: [PATCH] make ON OFF actuator more flexible (#481) * make ON OFF actuator more flexible enable to choose the pin when sending the MQTT command * add on off environment example --- docs/use/actuators.md | 14 ++++++-- main/ZactuatorONOFF.ino | 79 ++++++++++++++++++++++------------------- main/config_ONOFF.h | 4 +++ main/main.ino | 3 -- prod_env.ini.example | 16 +++++++++ 5 files changed, 73 insertions(+), 43 deletions(-) diff --git a/docs/use/actuators.md b/docs/use/actuators.md index 76b12fb..b7b908b 100644 --- a/docs/use/actuators.md +++ b/docs/use/actuators.md @@ -3,13 +3,21 @@ This module enables to actuate things by giving to a PIN a HIGH or LOW value corresponding to an MQTT topic. Example usage: Connect a transistor to power a relay, connect a led... -So as to pilot the pin use the following commands: +So as to pilot the pin use the following commands with [simple receiving](../upload/pio.md#api): OFF command: -`mosquitto_pub -t home/OpenMQTTGateway_MEGA/commands/MQTTtoONOFF -m OFF` +`mosquitto_pub -t home/OpenMQTTGateway_MEGA/commands/MQTTtoONOFF/setOFF -m 15` ON command -`mosquitto_pub -t home/OpenMQTTGateway_MEGA/commands/MQTTtoONOFF -m ON` +`mosquitto_pub -t home/OpenMQTTGateway_MEGA/commands/MQTTtoONOFF/setON -m 15` + +or with [json receiving](../upload/pio.md#api) + +OFF command: +`mosquitto_pub -t home/OpenMQTTGateway_MEGA/commands/MQTTtoONOFF -m '{"pin":15,"state":0}'` + +ON command +`mosquitto_pub -t home/OpenMQTTGateway_MEGA/commands/MQTTtoONOFF -m '{"pin":15,"state":1}'` ## FASTLED diff --git a/main/ZactuatorONOFF.ino b/main/ZactuatorONOFF.ino index 7b32d26..90d50b0 100644 --- a/main/ZactuatorONOFF.ino +++ b/main/ZactuatorONOFF.ino @@ -1,10 +1,10 @@ /* OpenMQTTGateway Addon - ESP8266 or Arduino program for home automation - Act as a wifi or ethernet gateway between your 433mhz/infrared IR signal and a MQTT broker + Act as a wifi or ethernet gateway between your 433mhz/infrared IR signal and a MQTT broker Send and receiving command by MQTT - Output pin High or Low + Output pin defined to High or Low Copyright: (c)Florian ROBERT @@ -30,44 +30,49 @@ #ifdef ZactuatorONOFF -void setupONOFF(){ - trc(F("ACTUATOR_ONOFF_PIN")); - trc(ACTUATOR_ONOFF_PIN); - //init - pinMode(ACTUATOR_ONOFF_PIN, OUTPUT); - trc(F("Set to OFF")); - digitalWrite(ACTUATOR_ONOFF_PIN, LOW); - trc(F("ZactuatorONOFF setup done ")); -} - - void MQTTtoONOFF(char * topicOri, char * datacallback){ +#ifdef jsonReceiving +void MQTTtoONOFF(char * topicOri, JsonObject& ONOFFdata){ - int boolSWITCHTYPE; - if (strcmp(datacallback, "ON") == 0) boolSWITCHTYPE = 1; - if (strcmp(datacallback, "OFF") == 0) boolSWITCHTYPE = 0; - - if (strcmp(topicOri,subjectMQTTtoONOFF) == 0){ - trc(F("MQTTtoONOFF data analysis")); + if (strcmp(topicOri,subjectMQTTtoONOFF) == 0){ + trc(F("MQTTtoONOFF json data analysis")); + int boolSWITCHTYPE = ONOFFdata["state"] | 99; + int pin = ONOFFdata["pin"] | ACTUATOR_ONOFF_PIN; + if (boolSWITCHTYPE != 99) { + trc(F("MQTTtoONOFF boolSWITCHTYPE ok")); trc(boolSWITCHTYPE); - digitalWrite(ACTUATOR_ONOFF_PIN, boolSWITCHTYPE); + trc(F("pin number")); + trc(pin); + pinMode(pin, OUTPUT); + digitalWrite(pin, boolSWITCHTYPE); // we acknowledge the sending by publishing the value to an acknowledgement topic - pub(subjectGTWONOFFtoMQTT, datacallback); - } - } - void MQTTtoONOFF(char * topicOri, JsonObject& ONOFFdata){ - - if (strcmp(topicOri,subjectMQTTtoONOFF) == 0){ - trc(F("MQTTtoONOFF json data analysis")); - int boolSWITCHTYPE = ONOFFdata["switchType"] | 99; - if (boolSWITCHTYPE != 99) { - trc(F("MQTTtoONOFF boolSWITCHTYPE ok")); - trc(boolSWITCHTYPE); - digitalWrite(ACTUATOR_ONOFF_PIN, boolSWITCHTYPE); - // we acknowledge the sending by publishing the value to an acknowledgement topic - pub(subjectGTWONOFFtoMQTT, ONOFFdata); - }else{ - trc(F("MQTTtoONOFF failed json read")); - } + pub(subjectGTWONOFFtoMQTT, ONOFFdata); + }else{ + trc(F("MQTTtoONOFF failed json read")); } } +} +#endif + +#ifdef simpleReceiving +void MQTTtoONOFF(char * topicOri, char * datacallback) { + if ((strstr(topicOri,subjectMQTTtoONOFF) != NULL) ){ + + trc(F("MQTTtoONOFF")); + int pin = strtol(datacallback, NULL, 10); // we will not be able to pass values > 4294967295 + trc(F("pin number")); + trc(pin); + pinMode(pin, OUTPUT); + + bool ON = false; + if (strstr(topicOri,ONKey) != NULL) ON = true; + if (strstr(topicOri,OFFKey) != NULL) ON = false; + + digitalWrite(pin, ON); + // we acknowledge the sending by publishing the value to an acknowledgement topic + pub(subjectGTWONOFFtoMQTT, ON); + } +} +#endif + + #endif diff --git a/main/config_ONOFF.h b/main/config_ONOFF.h index 4545882..9ad05a2 100644 --- a/main/config_ONOFF.h +++ b/main/config_ONOFF.h @@ -34,7 +34,11 @@ extern void MQTTtoONOFF(char * topicOri, JsonObject& RFdata); #define subjectMQTTtoONOFF Base_Topic Gateway_Name "/commands/MQTTtoONOFF" #define subjectGTWONOFFtoMQTT Base_Topic Gateway_Name "/ONOFFtoMQTT" +#define ONKey "setON" +#define OFFKey "setOFF" + /*-------------------PIN DEFINITIONS----------------------*/ +// default pin, if not set into the MQTT json #ifndef ACTUATOR_ONOFF_PIN #ifdef ESP8266 #define ACTUATOR_ONOFF_PIN 15 //12 for sonoff basic relay diff --git a/main/main.ino b/main/main.ino index 786a5a5..409607d 100644 --- a/main/main.ino +++ b/main/main.ino @@ -579,9 +579,6 @@ void setup() #ifdef ZsensorTSL2561 setupZsensorTSL2561(); #endif - #ifdef ZactuatorONOFF - setupONOFF(); - #endif #ifdef Zgateway2G setup2G(); #endif diff --git a/prod_env.ini.example b/prod_env.ini.example index e11ef58..5e34f97 100644 --- a/prod_env.ini.example +++ b/prod_env.ini.example @@ -2,6 +2,7 @@ default_envs = esp32dev-ble-1 esp32dev-ble-2 + env:nodemcuv2-ONOFFPILIGHT ;esp32 1 [env:esp32dev-ble-1] @@ -41,3 +42,18 @@ upload_flags = --auth=OTAPASSWORD upload_speed = 512000 monitor_speed = 115200 + +[env:nodemcuv2-ONOFFPILIGHT] +platform = ${com.esp8266_platform} +board = nodemcuv2 +lib_deps = + ${com-esp.lib_deps} + ${libraries.esppilight} + ${libraries.esp8266_mdns} + ${libraries.wire} +build_flags = + ${com-esp.build_flags} + '-DZgatewayPilight="Pilight"' + '-DZactuatorONOFF="ONOFF"' + '-DGateway_Name="OpenMQTTGateway_ESP8266_ONOFFRF"' +board_build.flash_mode = dout