Browse Source

Added MQTT message to reset the board

fastled
Xose Pérez 7 years ago
parent
commit
a1a774198c
5 changed files with 27 additions and 15 deletions
  1. +3
    -0
      code/espurna/config/general.h
  2. +1
    -0
      code/espurna/config/prototypes.h
  3. +4
    -3
      code/espurna/led.ino
  4. +14
    -4
      code/espurna/mqtt.ino
  5. +5
    -8
      code/espurna/relay.ino

+ 3
- 0
code/espurna/config/general.h View File

@ -86,6 +86,7 @@
#define MQTT_RECONNECT_DELAY 10000
#define MQTT_SKIP_RETAINED 1
#define MQTT_SKIP_TIME 1000
#define MQTT_ACTION_TOPIC "/action"
#define MQTT_RELAY_TOPIC "/relay"
#define MQTT_LED_TOPIC "/led"
#define MQTT_BUTTON_TOPIC "/button"
@ -93,6 +94,8 @@
#define MQTT_VERSION_TOPIC "/version"
#define MQTT_HEARTBEAT_TOPIC "/status"
#define MQTT_ACTION_RESET "reset"
#define MQTT_CONNECT_EVENT 0
#define MQTT_DISCONNECT_EVENT 1
#define MQTT_MESSAGE_EVENT 2


+ 1
- 0
code/espurna/config/prototypes.h View File

@ -9,6 +9,7 @@ typedef std::function<void(char *, size_t)> apiGetCallbackFunction;
typedef std::function<void(const char *)> apiPutCallbackFunction;
void apiRegister(const char * url, const char * key, apiGetCallbackFunction getFn, apiPutCallbackFunction putFn = NULL);
void mqttRegister(void (*callback)(unsigned int, const char *, const char *));
char * mqttSubtopic(char * topic);
template<typename T> bool setSetting(const String& key, T value);
template<typename T> String getSetting(const String& key, T defaultValue);
template<typename T> void domoticzSend(const char * key, T value);


+ 4
- 3
code/espurna/led.ino View File

@ -73,9 +73,10 @@ void ledMQTTCallback(unsigned int type, const char * topic, const char * payload
if (type == MQTT_MESSAGE_EVENT) {
// Match topic
String t = String(topic + mqttTopicRootLength());
if (!t.startsWith(MQTT_LED_TOPIC)) return;
if (!t.endsWith(mqttSetter)) return;
char * t = mqttSubtopic((char *) topic);
if (strncmp(t, MQTT_LED_TOPIC, strlen(MQTT_LED_TOPIC)) != 0) return;
int len = mqttSetter.length();
if (strncmp(t + strlen(t) - len, mqttSetter.c_str(), len) != 0) return;
// Get led ID
unsigned int ledID = topic[strlen(topic) - mqttSetter.length() - 1] - '0';


+ 14
- 4
code/espurna/mqtt.ino View File

@ -36,8 +36,9 @@ void buildTopics() {
mqttTopic.replace("{identifier}", getSetting("hostname"));
}
unsigned int mqttTopicRootLength() {
return mqttTopic.length();
char * mqttSubtopic(char * topic) {
int pos = min(mqttTopic.length(), strlen(topic));
return topic + pos;
}
void mqttSendRaw(const char * topic, const char * message) {
@ -83,6 +84,9 @@ void _mqttOnConnect(bool sessionPresent) {
mqttSend(MQTT_IP_TOPIC, getIP().c_str());
mqttSend(MQTT_VERSION_TOPIC, APP_VERSION);
// Subscribe to system topics
mqttSubscribe(MQTT_ACTION_TOPIC);
// Send connect event to subscribers
for (unsigned char i = 0; i < _mqtt_callbacks.size(); i++) {
(*_mqtt_callbacks[i])(MQTT_CONNECT_EVENT, NULL, NULL);
@ -107,16 +111,22 @@ void _mqttOnMessage(char* topic, char* payload, AsyncMqttClientMessageProperties
strlcpy(message, payload, len+1);
DEBUG_MSG("[MQTT] Received %s => %s", topic, message);
#if MQTT_SKIP_RETAINED
if (millis() - mqttConnectedAt < MQTT_SKIP_TIME) {
DEBUG_MSG(" - SKIPPED\n");
return;
}
#endif
DEBUG_MSG("\n");
// Check system topics
char * p = mqttSubtopic(topic);
if (strcmp(p, MQTT_ACTION_TOPIC) == 0) {
if (strcmp(payload, MQTT_ACTION_RESET) == 0) {
ESP.reset();
}
}
// Send message event to subscribers
// Topic is set to the specific part each one might be checking
for (unsigned char i = 0; i < _mqtt_callbacks.size(); i++) {


+ 5
- 8
code/espurna/relay.ino View File

@ -378,24 +378,21 @@ void relayMQTTCallback(unsigned int type, const char * topic, const char * paylo
sprintf(buffer, "%s/+%s", MQTT_RELAY_TOPIC, mqttSetter.c_str());
mqttSubscribe(buffer);
sprintf(buffer, "%s/pulse%s", MQTT_RELAY_TOPIC, mqttSetter.c_str());
mqttSubscribe(buffer);
}
if (type == MQTT_MESSAGE_EVENT) {
// Match topic
if (strncmp(topic, "domoticz", 8) == 0) return;
String t = String(topic + mqttTopicRootLength());
if (!t.startsWith(MQTT_RELAY_TOPIC)) return;
if (!t.endsWith(mqttSetter)) return;
char * t = mqttSubtopic((char *) topic);
if (strncmp(t, MQTT_RELAY_TOPIC, strlen(MQTT_RELAY_TOPIC)) != 0) return;
int len = mqttSetter.length();
if (strncmp(t + strlen(t) - len, mqttSetter.c_str(), len) != 0) return;
// Get value
unsigned int value = (char)payload[0] - '0';
// Pulse topic
if (t.indexOf("pulse") > 0) {
if (strncmp(t + strlen(MQTT_RELAY_TOPIC) + 1, "pulse", 5) == 0) {
relayPulseMode(value, !sameSetGet);
return;
}


Loading…
Cancel
Save