Browse Source

Refactor MQTT topics and reports setting names

fastled
Xose Pérez 7 years ago
parent
commit
6b1803d389
7 changed files with 120 additions and 84 deletions
  1. +3
    -3
      code/espurna/button.ino
  2. +32
    -21
      code/espurna/config/general.h
  3. +58
    -35
      code/espurna/espurna.ino
  4. +4
    -4
      code/espurna/led.ino
  5. +3
    -3
      code/espurna/light.ino
  6. +10
    -10
      code/espurna/mqtt.ino
  7. +10
    -8
      code/espurna/relay.ino

+ 3
- 3
code/espurna/button.ino View File

@ -21,12 +21,12 @@ typedef struct {
std::vector<button_t> _buttons; std::vector<button_t> _buttons;
#ifdef MQTT_BUTTON_TOPIC
#ifdef MQTT_TOPIC_BUTTON
void buttonMQTT(unsigned char id, uint8_t event) { void buttonMQTT(unsigned char id, uint8_t event) {
if (id >= _buttons.size()) return; if (id >= _buttons.size()) return;
char payload[2]; char payload[2];
sprintf(payload, "%d", event); sprintf(payload, "%d", event);
mqttSend(MQTT_BUTTON_TOPIC, id, payload);
mqttSend(MQTT_TOPIC_BUTTON, id, payload);
} }
#endif #endif
@ -69,7 +69,7 @@ void buttonEvent(unsigned int id, unsigned char event) {
DEBUG_MSG("[BUTTON] Pressed #%d, event: %d\n", id, event); DEBUG_MSG("[BUTTON] Pressed #%d, event: %d\n", id, event);
if (event == 0) return; if (event == 0) return;
#ifdef MQTT_BUTTON_TOPIC
#ifdef MQTT_TOPIC_BUTTON
buttonMQTT(id, event); buttonMQTT(id, event);
#endif #endif


+ 32
- 21
code/espurna/config/general.h View File

@ -145,17 +145,38 @@
#define MQTT_MAX_TRIES 12 #define MQTT_MAX_TRIES 12
#define MQTT_SKIP_RETAINED 1 #define MQTT_SKIP_RETAINED 1
#define MQTT_SKIP_TIME 1000 #define MQTT_SKIP_TIME 1000
#define MQTT_ACTION_TOPIC "/action"
#define MQTT_RELAY_TOPIC "/relay"
#define MQTT_LED_TOPIC "/led"
#define MQTT_COLOR_TOPIC "/color"
#define MQTT_BUTTON_TOPIC "/button"
#define MQTT_IP_TOPIC "/ip"
#define MQTT_VERSION_TOPIC "/version"
#define MQTT_UPTIME_TOPIC "/uptime"
#define MQTT_FREEHEAP_TOPIC "/freeheap"
#define MQTT_VCC_TOPIC "/vcc"
#define MQTT_STATUS_TOPIC "/status"
#define MQTT_TOPIC_ACTION "/action"
#define MQTT_TOPIC_RELAY "/relay"
#define MQTT_TOPIC_LED "/led"
#define MQTT_TOPIC_COLOR "/color"
#define MQTT_TOPIC_BUTTON "/button"
#define MQTT_TOPIC_IP "/ip"
#define MQTT_TOPIC_VERSION "/version"
#define MQTT_TOPIC_UPTIME "/uptime"
#define MQTT_TOPIC_FREEHEAP "/freeheap"
#define MQTT_TOPIC_VCC "/vcc"
#define MQTT_TOPIC_STATUS "/status"
#define MQTT_TOPIC_MAC "/mac"
#define MQTT_TOPIC_APP "/app"
#define MQTT_TOPIC_INTERVAL "/interval"
#define MQTT_TOPIC_HOSTNAME "/hostname"
// Periodic reports
#define MQTT_REPORT_STATUS 1
#define MQTT_REPORT_IP 1
#define MQTT_REPORT_MAC 1
#define MQTT_REPORT_UPTIME 1
#define MQTT_REPORT_FREEHEAP 1
#define MQTT_REPORT_VCC 1
#define MQTT_REPORT_RELAY 1
#define MQTT_REPORT_HOSTNAME 1
#define MQTT_REPORT_APP 1
#define MQTT_REPORT_VERSION 1
#define MQTT_REPORT_INTERVAL 0
#define MQTT_STATUS_ONLINE "1"
#define MQTT_STATUS_OFFLINE "0"
#define MQTT_ACTION_RESET "reset" #define MQTT_ACTION_RESET "reset"
@ -168,16 +189,6 @@
#define MQTT_USE_GETTER "" #define MQTT_USE_GETTER ""
#define MQTT_USE_SETTER "" #define MQTT_USE_SETTER ""
// Periodic reports
#define MQTT_STATUS_REPORT 1
#define MQTT_IP_REPORT 2
#define MQTT_UPTIME_REPORT 4
#define MQTT_FREEHEAP_REPORT 8
#define MQTT_VCC_REPORT 16
#define MQTT_RELAY_REPORT 32
#define MQTT_REPORTS (MQTT_STATUS_REPORT | MQTT_UPTIME_REPORT | MQTT_FREEHEAP_REPORT | MQTT_RELAY_REPORT)
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// I2C // I2C
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------


+ 58
- 35
code/espurna/espurna.ino View File

@ -32,6 +32,62 @@ String getIdentifier() {
return String(identifier); return String(identifier);
} }
void heartbeat() {
static unsigned long last_uptime = 0;
static unsigned char uptime_overflows = 0;
if (millis() < last_uptime) ++uptime_overflows;
last_uptime = millis();
unsigned long uptime_seconds = uptime_overflows * (UPTIME_OVERFLOW / 1000) + (last_uptime / 1000);
DEBUG_MSG("[MAIN] Time: %s\n", (char *) NTP.getTimeDateString().c_str());
if (!mqttConnected()) {
DEBUG_MSG("[MAIN] Uptime: %ld seconds\n", uptime_seconds);
DEBUG_MSG("[MAIN] Free heap: %d bytes\n", ESP.getFreeHeap());
#if ENABLE_ADC_VCC
DEBUG_MSG("[MAIN] Power: %d mV\n", ESP.getVcc());
#endif
}
#if (MQTT_REPORT_INTERVAL)
mqttSend(MQTT_TOPIC_INTERVAL, HEARTBEAT_INTERVAL / 1000);
#endif
#if (MQTT_REPORT_APP)
mqttSend(MQTT_TOPIC_APP, APP_NAME);
#endif
#if (MQTT_REPORT_VERSION)
mqttSend(MQTT_TOPIC_VERSION, APP_VERSION);
#endif
#if (MQTT_REPORT_HOSTNAME)
mqttSend(MQTT_TOPIC_HOSTNAME, getSetting("hostname").c_str());
#endif
#if (MQTT_REPORT_IP)
mqttSend(MQTT_TOPIC_IP, getIP().c_str());
#endif
#if (MQTT_REPORT_MAC)
mqttSend(MQTT_TOPIC_MAC, WiFi.macAddress().c_str());
#endif
#if (MQTT_REPORT_UPTIME)
mqttSend(MQTT_TOPIC_UPTIME, String(uptime_seconds).c_str());
#endif
#if (MQTT_REPORT_FREEHEAP)
mqttSend(MQTT_TOPIC_FREEHEAP, String(ESP.getFreeHeap()).c_str());
#endif
#if (MQTT_REPORT_RELAY)
relayMQTT();
#endif
#if (MQTT_REPORT_VCC)
#if ENABLE_ADC_VCC
mqttSend(MQTT_TOPIC_VCC, String(ESP.getVcc()).c_str());
#endif
#endif
#if (MQTT_REPORT_STATUS)
mqttSend(MQTT_TOPIC_STATUS, MQTT_STATUS_ONLINE);
#endif
}
void hardwareSetup() { void hardwareSetup() {
EEPROM.begin(4096); EEPROM.begin(4096);
Serial.begin(SERIAL_BAUDRATE); Serial.begin(SERIAL_BAUDRATE);
@ -42,44 +98,11 @@ void hardwareSetup() {
void hardwareLoop() { void hardwareLoop() {
static unsigned long last_uptime = 0;
static unsigned char uptime_overflows = 0;
// Heartbeat // Heartbeat
static unsigned long last_uptime = 0;
if ((millis() - last_uptime > HEARTBEAT_INTERVAL) || (last_uptime == 0)) { if ((millis() - last_uptime > HEARTBEAT_INTERVAL) || (last_uptime == 0)) {
if (millis() < last_uptime) ++uptime_overflows;
last_uptime = millis(); last_uptime = millis();
unsigned long uptime_seconds = uptime_overflows * (UPTIME_OVERFLOW / 1000) + (last_uptime / 1000);
DEBUG_MSG("[MAIN] Time: %s\n", (char *) NTP.getTimeDateString().c_str());
DEBUG_MSG("[MAIN] Uptime: %ld seconds\n", uptime_seconds);
DEBUG_MSG("[MAIN] Free heap: %d bytes\n", ESP.getFreeHeap());
#if ENABLE_ADC_VCC
DEBUG_MSG("[MAIN] Power: %d mV\n", ESP.getVcc());
#endif
#if (MQTT_REPORTS & MQTT_STATUS_REPORT)
mqttSend(MQTT_STATUS_TOPIC, "1");
#endif
#if (MQTT_REPORTS & MQTT_IP_REPORT)
mqttSend(MQTT_IP_TOPIC, getIP().c_str());
#endif
#if (MQTT_REPORTS & MQTT_UPTIME_REPORT)
mqttSend(MQTT_UPTIME_TOPIC, String(uptime_seconds).c_str());
#endif
#if (MQTT_REPORTS & MQTT_FREEHEAP_REPORT)
mqttSend(MQTT_FREEHEAP_TOPIC, String(ESP.getFreeHeap()).c_str());
#endif
#if (MQTT_REPORTS & MQTT_RELAY_REPORT)
relayMQTT();
#endif
#if (MQTT_REPORTS & MQTT_VCC_REPORT)
#if ENABLE_ADC_VCC
mqttSend(MQTT_VCC_TOPIC, String(ESP.getVcc()).c_str());
#endif
#endif
heartbeat();
} }
} }


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

@ -63,8 +63,8 @@ void ledMQTTCallback(unsigned int type, const char * topic, const char * payload
static bool isFirstMessage = true; static bool isFirstMessage = true;
if (type == MQTT_CONNECT_EVENT) { if (type == MQTT_CONNECT_EVENT) {
char buffer[strlen(MQTT_LED_TOPIC) + 3];
sprintf(buffer, "%s/+", MQTT_LED_TOPIC);
char buffer[strlen(MQTT_TOPIC_LED) + 3];
sprintf(buffer, "%s/+", MQTT_TOPIC_LED);
mqttSubscribe(buffer); mqttSubscribe(buffer);
} }
@ -72,10 +72,10 @@ void ledMQTTCallback(unsigned int type, const char * topic, const char * payload
// Match topic // Match topic
String t = mqttSubtopic((char *) topic); String t = mqttSubtopic((char *) topic);
if (!t.startsWith(MQTT_LED_TOPIC)) return;
if (!t.startsWith(MQTT_TOPIC_LED)) return;
// Get led ID // Get led ID
unsigned int ledID = t.substring(strlen(MQTT_LED_TOPIC)+1).toInt();
unsigned int ledID = t.substring(strlen(MQTT_TOPIC_LED)+1).toInt();
if (ledID >= ledCount()) { if (ledID >= ledCount()) {
DEBUG_MSG("[LED] Wrong ledID (%d)\n", ledID); DEBUG_MSG("[LED] Wrong ledID (%d)\n", ledID);
return; return;


+ 3
- 3
code/espurna/light.ino View File

@ -94,7 +94,7 @@ void lightColor(const char * rgb, bool save, bool forward) {
if (save) colorTicker.once(LIGHT_SAVE_DELAY, lightColorSave); if (save) colorTicker.once(LIGHT_SAVE_DELAY, lightColorSave);
// Report color to MQTT broker // Report color to MQTT broker
if (forward) mqttSend(MQTT_COLOR_TOPIC, rgb);
if (forward) mqttSend(MQTT_TOPIC_COLOR, rgb);
// Report color to WS clients // Report color to WS clients
char message[20]; char message[20];
@ -143,14 +143,14 @@ void lightMQTTCallback(unsigned int type, const char * topic, const char * paylo
if (type == MQTT_CONNECT_EVENT) { if (type == MQTT_CONNECT_EVENT) {
mqttSubscribe(MQTT_COLOR_TOPIC);
mqttSubscribe(MQTT_TOPIC_COLOR);
} }
if (type == MQTT_MESSAGE_EVENT) { if (type == MQTT_MESSAGE_EVENT) {
// Match topic // Match topic
String t = mqttSubtopic((char *) topic); String t = mqttSubtopic((char *) topic);
if (!t.equals(MQTT_COLOR_TOPIC)) return;
if (!t.equals(MQTT_TOPIC_COLOR)) return;
lightColor(payload, true, mqttForward()); lightColor(payload, true, mqttForward());


+ 10
- 10
code/espurna/mqtt.ino View File

@ -21,6 +21,8 @@ bool _mqttConnected = false;
String mqttTopic; String mqttTopic;
bool _mqttForward; bool _mqttForward;
char * _mqtt_pass = 0;
char * _mqtt_user = 0;
std::vector<void (*)(unsigned int, const char *, const char *)> _mqtt_callbacks; std::vector<void (*)(unsigned int, const char *, const char *)> _mqtt_callbacks;
#if MQTT_SKIP_RETAINED #if MQTT_SKIP_RETAINED
unsigned long mqttConnectedAt = 0; unsigned long mqttConnectedAt = 0;
@ -118,13 +120,11 @@ void _mqttOnConnect() {
// Build MQTT topics // Build MQTT topics
buildTopics(); buildTopics();
// Say hello and report our IP and VERSION
mqttSend(MQTT_IP_TOPIC, getIP().c_str());
mqttSend(MQTT_VERSION_TOPIC, APP_VERSION);
mqttSend(MQTT_STATUS_TOPIC, "1");
// Send first Heartbeat
heartbeat();
// Subscribe to system topics // Subscribe to system topics
mqttSubscribe(MQTT_ACTION_TOPIC);
mqttSubscribe(MQTT_TOPIC_ACTION);
// Send connect event to subscribers // Send connect event to subscribers
for (unsigned char i = 0; i < _mqtt_callbacks.size(); i++) { for (unsigned char i = 0; i < _mqtt_callbacks.size(); i++) {
@ -160,7 +160,7 @@ void _mqttOnMessage(char* topic, char* payload, unsigned int len) {
// Check system topics // Check system topics
String t = mqttSubtopic((char *) topic); String t = mqttSubtopic((char *) topic);
if (t.equals(MQTT_ACTION_TOPIC)) {
if (t.equals(MQTT_TOPIC_ACTION)) {
if (strcmp(message, MQTT_ACTION_RESET) == 0) { if (strcmp(message, MQTT_ACTION_RESET) == 0) {
ESP.restart(); ESP.restart();
} }
@ -203,13 +203,13 @@ void mqttConnect() {
char * user = strdup(getSetting("mqttUser").c_str()); char * user = strdup(getSetting("mqttUser").c_str());
char * pass = strdup(getSetting("mqttPassword").c_str()); char * pass = strdup(getSetting("mqttPassword").c_str());
DEBUG_MSG("[MQTT] Connecting to broker at %s", host);
DEBUG_MSG("[MQTT] Connecting to broker at %s:%d", host, port);
mqtt.setServer(host, port); mqtt.setServer(host, port);
#if MQTT_USE_ASYNC #if MQTT_USE_ASYNC
mqtt.setKeepAlive(MQTT_KEEPALIVE).setCleanSession(false); mqtt.setKeepAlive(MQTT_KEEPALIVE).setCleanSession(false);
mqtt.setWill((mqttTopic + MQTT_STATUS_TOPIC).c_str(), MQTT_QOS, MQTT_RETAIN, "0");
mqtt.setWill((mqttTopic + MQTT_TOPIC_STATUS).c_str(), MQTT_QOS, MQTT_RETAIN, "0");
if ((strlen(user) > 0) && (strlen(pass) > 0)) { if ((strlen(user) > 0) && (strlen(pass) > 0)) {
DEBUG_MSG(" as user '%s'.", user); DEBUG_MSG(" as user '%s'.", user);
mqtt.setCredentials(user, pass); mqtt.setCredentials(user, pass);
@ -223,10 +223,10 @@ void mqttConnect() {
if ((strlen(user) > 0) && (strlen(pass) > 0)) { if ((strlen(user) > 0) && (strlen(pass) > 0)) {
DEBUG_MSG(" as user '%s'\n", user); DEBUG_MSG(" as user '%s'\n", user);
response = mqtt.connect(getIdentifier().c_str(), user, pass, (mqttTopic + MQTT_STATUS_TOPIC).c_str(), MQTT_QOS, MQTT_RETAIN, "0");
response = mqtt.connect(getIdentifier().c_str(), user, pass, (mqttTopic + MQTT_TOPIC_STATUS).c_str(), MQTT_QOS, MQTT_RETAIN, "0");
} else { } else {
DEBUG_MSG("\n"); DEBUG_MSG("\n");
response = mqtt.connect(getIdentifier().c_str(), (mqttTopic + MQTT_STATUS_TOPIC).c_str(), MQTT_QOS, MQTT_RETAIN, "0");
response = mqtt.connect(getIdentifier().c_str(), (mqttTopic + MQTT_TOPIC_STATUS).c_str(), MQTT_QOS, MQTT_RETAIN, "0");
} }
if (response) { if (response) {


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

@ -121,8 +121,8 @@ void relayPulseMode(unsigned int value, bool report) {
/* /*
if (report) { if (report) {
char topic[strlen(MQTT_RELAY_TOPIC) + 10];
sprintf(topic, "%s/pulse", MQTT_RELAY_TOPIC);
char topic[strlen(MQTT_TOPIC_RELAY) + 10];
sprintf(topic, "%s/pulse", MQTT_TOPIC_RELAY);
char value[2]; char value[2];
sprintf(value, "%d", value); sprintf(value, "%d", value);
mqttSend(topic, value); mqttSend(topic, value);
@ -373,7 +373,7 @@ void relayDomoticzSetup() {
void relayMQTT(unsigned char id) { void relayMQTT(unsigned char id) {
if (id >= _relays.size()) return; if (id >= _relays.size()) return;
mqttSend(MQTT_RELAY_TOPIC, id, relayStatus(id) ? "1" : "0");
mqttSend(MQTT_TOPIC_RELAY, id, relayStatus(id) ? "1" : "0");
} }
void relayMQTT() { void relayMQTT() {
@ -386,10 +386,12 @@ void relayMQTTCallback(unsigned int type, const char * topic, const char * paylo
if (type == MQTT_CONNECT_EVENT) { if (type == MQTT_CONNECT_EVENT) {
relayMQTT();
#if not MQTT_REPORT_RELAY
relayMQTT();
#endif
char buffer[strlen(MQTT_RELAY_TOPIC) + 3];
sprintf(buffer, "%s/+", MQTT_RELAY_TOPIC);
char buffer[strlen(MQTT_TOPIC_RELAY) + 3];
sprintf(buffer, "%s/+", MQTT_TOPIC_RELAY);
mqttSubscribe(buffer); mqttSubscribe(buffer);
} }
@ -398,7 +400,7 @@ void relayMQTTCallback(unsigned int type, const char * topic, const char * paylo
// Match topic // Match topic
String t = mqttSubtopic((char *) topic); String t = mqttSubtopic((char *) topic);
if (!t.startsWith(MQTT_RELAY_TOPIC)) return;
if (!t.startsWith(MQTT_TOPIC_RELAY)) return;
// Get value // Get value
unsigned int value = (char)payload[0] - '0'; unsigned int value = (char)payload[0] - '0';
@ -410,7 +412,7 @@ void relayMQTTCallback(unsigned int type, const char * topic, const char * paylo
} }
// Get relay ID // Get relay ID
unsigned int relayID = t.substring(strlen(MQTT_RELAY_TOPIC)+1).toInt();
unsigned int relayID = t.substring(strlen(MQTT_TOPIC_RELAY)+1).toInt();
if (relayID >= relayCount()) { if (relayID >= relayCount()) {
DEBUG_MSG("[RELAY] Wrong relayID (%d)\n", relayID); DEBUG_MSG("[RELAY] Wrong relayID (%d)\n", relayID);
return; return;


Loading…
Cancel
Save