Browse Source

#13 Custom setter and getter sufixes on MQTT topics

fastled
Xose Pérez 8 years ago
parent
commit
fee84cfaad
2 changed files with 26 additions and 14 deletions
  1. +7
    -1
      code/src/config/general.h
  2. +19
    -13
      code/src/relay.ino

+ 7
- 1
code/src/config/general.h View File

@ -58,7 +58,7 @@
#define MQTT_QOS 0 #define MQTT_QOS 0
#define MQTT_KEEPALIVE 30 #define MQTT_KEEPALIVE 30
#define MQTT_RECONNECT_DELAY 10000 #define MQTT_RECONNECT_DELAY 10000
#define MQTT_RELAY_TOPIC "/relay/%d"
#define MQTT_RELAY_TOPIC "/relay"
#define MQTT_IP_TOPIC "/ip" #define MQTT_IP_TOPIC "/ip"
#define MQTT_VERSION_TOPIC "/version" #define MQTT_VERSION_TOPIC "/version"
#define MQTT_FSVERSION_TOPIC "/fsversion" #define MQTT_FSVERSION_TOPIC "/fsversion"
@ -68,6 +68,12 @@
#define MQTT_DISCONNECT_EVENT 1 #define MQTT_DISCONNECT_EVENT 1
#define MQTT_MESSAGE_EVENT 2 #define MQTT_MESSAGE_EVENT 2
// Custom get and set postfixes
// Use something like "/status" or "/set", with trailing slash
#define MQTT_USE_GETTER ""
#define MQTT_USE_SETTER ""
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// NTP // NTP
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------


+ 19
- 13
code/src/relay.ino View File

@ -22,8 +22,9 @@ bool recursive = false;
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
void relayMQTT(unsigned char id) { void relayMQTT(unsigned char id) {
char buffer[10];
sprintf(buffer, MQTT_RELAY_TOPIC, id);
String mqttGetter = getSetting("mqttGetter", MQTT_USE_GETTER);
char buffer[strlen(MQTT_RELAY_TOPIC) + mqttGetter.length() + 3];
sprintf(buffer, "%s/%d%s", MQTT_RELAY_TOPIC, id, mqttGetter.c_str());
mqttSend(buffer, (char *) (relayStatus(id) ? "1" : "0")); mqttSend(buffer, (char *) (relayStatus(id) ? "1" : "0"));
} }
@ -169,36 +170,41 @@ void relayMQTTCallback(unsigned int type, const char * topic, const char * paylo
static bool isFirstMessage = true; static bool isFirstMessage = true;
String mqttSetter = getSetting("mqttSetter", MQTT_USE_SETTER);
String mqttGetter = getSetting("mqttGetter", MQTT_USE_GETTER);
bool sameSetGet = mqttGetter.compareTo(mqttSetter) == 0;
if (type == MQTT_CONNECT_EVENT) { if (type == MQTT_CONNECT_EVENT) {
relayMQTT(); relayMQTT();
mqttSubscribe("/relay/#");
char buffer[strlen(MQTT_RELAY_TOPIC) + mqttSetter.length() + 3];
sprintf(buffer, "%s/+%s", MQTT_RELAY_TOPIC, mqttSetter.c_str());
mqttSubscribe(buffer);
} }
if (type == MQTT_MESSAGE_EVENT) { if (type == MQTT_MESSAGE_EVENT) {
// Match topic // Match topic
if (memcmp("/relay/", topic, 7) != 0) return;
String t = String(topic);
if (!t.startsWith(MQTT_RELAY_TOPIC)) return;
if (!t.endsWith(mqttSetter)) return;
// If relayMode is not SAME avoid responding to a retained message // If relayMode is not SAME avoid responding to a retained message
if (isFirstMessage) {
if (sameSetGet && isFirstMessage) {
isFirstMessage = false; isFirstMessage = false;
byte relayMode = getSetting("relayMode", RELAY_MODE).toInt(); byte relayMode = getSetting("relayMode", RELAY_MODE).toInt();
if (relayMode != RELAY_MODE_SAME) return; if (relayMode != RELAY_MODE_SAME) return;
} }
// Get relay ID // Get relay ID
unsigned int relayID = topic[strlen(topic)-1] - '0';
unsigned int relayID = topic[strlen(MQTT_RELAY_TOPIC)+1] - '0';
if (relayID >= relayCount()) relayID = 0; if (relayID >= relayCount()) relayID = 0;
// Action to perform // Action to perform
if ((char)payload[0] == '0') {
relayStatus(relayID, false, false);
}
if ((char)payload[0] == '1') {
relayStatus(relayID, true, false);
}
if ((char)payload[0] == '2') {
unsigned int value = (char)payload[0] - '0';
if (value == 2) {
relayToggle(relayID); relayToggle(relayID);
} else {
relayStatus(relayID, value > 0, !sameSetGet);
} }
} }


Loading…
Cancel
Save