Browse Source

Define MQTT mode with a soft setting

fastled
Xose Pérez 7 years ago
parent
commit
629309bde2
3 changed files with 50 additions and 66 deletions
  1. +12
    -14
      code/espurna/espurna.ino
  2. +37
    -49
      code/espurna/mqtt.ino
  3. +1
    -3
      code/espurna/relay.ino

+ 12
- 14
code/espurna/espurna.ino View File

@ -53,34 +53,34 @@ void heartbeat() {
#if (MQTT_REPORT_INTERVAL) #if (MQTT_REPORT_INTERVAL)
mqttAppend(MQTT_TOPIC_INTERVAL, HEARTBEAT_INTERVAL / 1000);
mqttSend(MQTT_TOPIC_INTERVAL, HEARTBEAT_INTERVAL / 1000);
#endif #endif
#if (MQTT_REPORT_APP) #if (MQTT_REPORT_APP)
mqttAppend(MQTT_TOPIC_APP, APP_NAME);
mqttSend(MQTT_TOPIC_APP, APP_NAME);
#endif #endif
#if (MQTT_REPORT_VERSION) #if (MQTT_REPORT_VERSION)
mqttAppend(MQTT_TOPIC_VERSION, APP_VERSION);
mqttSend(MQTT_TOPIC_VERSION, APP_VERSION);
#endif #endif
#if (MQTT_REPORT_HOSTNAME) #if (MQTT_REPORT_HOSTNAME)
//mqttAppend(MQTT_TOPIC_HOSTNAME, getSetting("hostname").c_str());
//mqttSend(MQTT_TOPIC_HOSTNAME, getSetting("hostname").c_str());
#endif #endif
#if (MQTT_REPORT_IP) #if (MQTT_REPORT_IP)
mqttAppend(MQTT_TOPIC_IP, getIP().c_str());
mqttSend(MQTT_TOPIC_IP, getIP().c_str());
#endif #endif
#if (MQTT_REPORT_MAC) #if (MQTT_REPORT_MAC)
mqttAppend(MQTT_TOPIC_MAC, WiFi.macAddress().c_str());
mqttSend(MQTT_TOPIC_MAC, WiFi.macAddress().c_str());
#endif #endif
#if (MQTT_REPORT_RSSI) #if (MQTT_REPORT_RSSI)
mqttAppend(MQTT_TOPIC_RSSI, String(WiFi.RSSI()).c_str());
mqttSend(MQTT_TOPIC_RSSI, String(WiFi.RSSI()).c_str());
#endif #endif
#if (MQTT_REPORT_UPTIME) #if (MQTT_REPORT_UPTIME)
mqttAppend(MQTT_TOPIC_UPTIME, String(uptime_seconds).c_str());
mqttSend(MQTT_TOPIC_UPTIME, String(uptime_seconds).c_str());
#if ENABLE_INFLUXDB #if ENABLE_INFLUXDB
influxDBSend(MQTT_TOPIC_UPTIME, String(uptime_seconds).c_str()); influxDBSend(MQTT_TOPIC_UPTIME, String(uptime_seconds).c_str());
#endif #endif
#endif #endif
#if (MQTT_REPORT_FREEHEAP) #if (MQTT_REPORT_FREEHEAP)
mqttAppend(MQTT_TOPIC_FREEHEAP, String(free_heap).c_str());
mqttSend(MQTT_TOPIC_FREEHEAP, String(free_heap).c_str());
#if ENABLE_INFLUXDB #if ENABLE_INFLUXDB
influxDBSend(MQTT_TOPIC_FREEHEAP, String(free_heap).c_str()); influxDBSend(MQTT_TOPIC_FREEHEAP, String(free_heap).c_str());
#endif #endif
@ -90,20 +90,18 @@ void heartbeat() {
#endif #endif
#if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
#if (MQTT_REPORT_COLOR) #if (MQTT_REPORT_COLOR)
mqttAppend(MQTT_TOPIC_COLOR, lightColor().c_str());
mqttSend(MQTT_TOPIC_COLOR, lightColor().c_str());
#endif #endif
#endif #endif
#if (MQTT_REPORT_VCC) #if (MQTT_REPORT_VCC)
#if ENABLE_ADC_VCC #if ENABLE_ADC_VCC
mqttAppend(MQTT_TOPIC_VCC, String(ESP.getVcc()).c_str());
mqttSend(MQTT_TOPIC_VCC, String(ESP.getVcc()).c_str());
#endif #endif
#endif #endif
#if (MQTT_REPORT_STATUS) #if (MQTT_REPORT_STATUS)
mqttAppend(MQTT_TOPIC_STATUS, MQTT_STATUS_ONLINE);
mqttSend(MQTT_TOPIC_STATUS, MQTT_STATUS_ONLINE, true);
#endif #endif
mqttSend();
} }
void customReset(unsigned char status) { void customReset(unsigned char status) {


+ 37
- 49
code/espurna/mqtt.ino View File

@ -9,6 +9,7 @@ Copyright (C) 2016-2017 by Xose Pérez <xose dot perez at gmail dot com>
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <vector> #include <vector>
#include <Ticker.h>
const char *mqtt_user = 0; const char *mqtt_user = 0;
const char *mqtt_pass = 0; const char *mqtt_pass = 0;
@ -34,10 +35,10 @@ std::vector<void (*)(unsigned int, const char *, const char *)> _mqtt_callbacks;
typedef struct { typedef struct {
char * topic; char * topic;
unsigned char index;
char * message; char * message;
} mqtt_message_t; } mqtt_message_t;
std::vector<mqtt_message_t> _mqtt_queue; std::vector<mqtt_message_t> _mqtt_queue;
Ticker mqttFlushTicker;
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// Public API // Public API
@ -88,40 +89,23 @@ void mqttSendRaw(const char * topic, const char * message) {
} }
} }
void mqttSend() {
void _mqttFlush() {
#if MQTT_USE_JSON
if (_mqtt_queue.size() == 0) return;
DynamicJsonBuffer jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
for (unsigned char i=0; i<_mqtt_queue.size(); i++) {
mqtt_message_t element = _mqtt_queue[i];
if (element.index < 255) {
String topic = String(element.topic) + String("/") + String(element.index);
root[topic] = element.message;
} else {
root[element.topic] = element.message;
}
}
root["time"] = NTP.getTimeDateString();
root["hostname"] = getSetting("hostname", HOSTNAME);
DynamicJsonBuffer jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
for (unsigned char i=0; i<_mqtt_queue.size(); i++) {
mqtt_message_t element = _mqtt_queue[i];
root[element.topic] = element.message;
}
root["time"] = NTP.getTimeDateString();
root["hostname"] = getSetting("hostname", HOSTNAME);
String output;
root.printTo(output);
String path = mqttTopic + String(MQTT_TOPIC_JSON);
mqttSendRaw(path.c_str(), output.c_str());
#else
String mqttGetter = getSetting("mqttGetter", MQTT_USE_GETTER);
for (unsigned char i=0; i<_mqtt_queue.size(); i++) {
mqtt_message_t element = _mqtt_queue[i];
String path = mqttTopic + String(element.topic);
if (element.index < 255) path += String ("/") + String(element.index);
path += mqttGetter;
mqttSendRaw(path.c_str(), element.message);
}
#endif
String output;
root.printTo(output);
String path = mqttTopic + String(MQTT_TOPIC_JSON);
mqttSendRaw(path.c_str(), output.c_str());
for (unsigned char i = 0; i < _mqtt_queue.size(); i++) { for (unsigned char i = 0; i < _mqtt_queue.size(); i++) {
mqtt_message_t element = _mqtt_queue[i]; mqtt_message_t element = _mqtt_queue[i];
@ -132,29 +116,33 @@ void mqttSend() {
} }
void mqttSend(const char * topic, const char * message) {
String mqttGetter = getSetting("mqttGetter", MQTT_USE_GETTER);
String path = mqttTopic + String(topic) + mqttGetter;
mqttSendRaw(path.c_str(), message);
void mqttSend(const char * topic, const char * message, bool force) {
bool useJson = force ? false : getSetting("mqttUseJson", MQTT_USE_JSON).toInt() == 1;
if (useJson) {
mqtt_message_t element;
element.topic = strdup(topic);
element.message = strdup(message);
_mqtt_queue.push_back(element);
mqttFlushTicker.once_ms(100, _mqttFlush);
} else {
String mqttGetter = getSetting("mqttGetter", MQTT_USE_GETTER);
String path = mqttTopic + String(topic) + mqttGetter;
mqttSendRaw(path.c_str(), message);
}
} }
void mqttSend(const char * topic, unsigned int index, const char * message) {
String mqttGetter = getSetting("mqttGetter", MQTT_USE_GETTER);
String path = mqttTopic + String(topic) + String ("/") + String(index) + mqttGetter;;
mqttSendRaw(path.c_str(), message);
void mqttSend(const char * topic, const char * message) {
mqttSend(topic, message, false);
} }
unsigned int mqttAppend(const char * topic, unsigned int index, const char * message) {
mqtt_message_t element;
element.topic = strdup(topic);
element.index = index;
element.message = strdup(message);
_mqtt_queue.push_back(element);
return _mqtt_queue.size();
void mqttSend(const char * topic, unsigned int index, const char * message, bool force) {
char buffer[strlen(topic)+5];
sprintf(buffer, "%s/%d", topic, index);
mqttSend(buffer, message, force);
} }
unsigned int mqttAppend(const char * topic, const char * message) {
return mqttAppend(topic, 255, message);
void mqttSend(const char * topic, unsigned int index, const char * message) {
mqttSend(topic, index, message, false);
} }
void mqttSubscribeRaw(const char * topic) { void mqttSubscribeRaw(const char * topic) {


+ 1
- 3
code/espurna/relay.ino View File

@ -381,7 +381,7 @@ void relayDomoticzSetup() {
void relayMQTT(unsigned char id) { void relayMQTT(unsigned char id) {
if (id >= _relays.size()) return; if (id >= _relays.size()) return;
mqttAppend(MQTT_TOPIC_RELAY, id, relayStatus(id) ? "1" : "0");
mqttSend(MQTT_TOPIC_RELAY, id, relayStatus(id) ? "1" : "0");
} }
#if ENABLE_INFLUXDB #if ENABLE_INFLUXDB
@ -405,7 +405,6 @@ void relayMQTTCallback(unsigned int type, const char * topic, const char * paylo
#if not MQTT_REPORT_RELAY #if not MQTT_REPORT_RELAY
relayMQTT(); relayMQTT();
mqttSend();
#endif #endif
char buffer[strlen(MQTT_TOPIC_RELAY) + 3]; char buffer[strlen(MQTT_TOPIC_RELAY) + 3];
@ -526,7 +525,6 @@ void relayLoop(void) {
if (_relays[id].scheduledReport) { if (_relays[id].scheduledReport) {
relayMQTT(id); relayMQTT(id);
mqttSend();
} }
if (!recursive) { if (!recursive) {
relayPulse(id); relayPulse(id);


Loading…
Cancel
Save