Browse Source

Changes in MQTT calls. Using default client_id from AsyncMqttClient

fastled
Xose Pérez 8 years ago
parent
commit
6d6878d59f
8 changed files with 38 additions and 29 deletions
  1. +2
    -2
      code/espurna/dht.ino
  2. +1
    -1
      code/espurna/ds18b20.ino
  3. +1
    -1
      code/espurna/emon.ino
  4. +1
    -1
      code/espurna/espurna.ino
  5. +2
    -2
      code/espurna/led.ino
  6. +28
    -10
      code/espurna/mqtt.ino
  7. +1
    -1
      code/espurna/pow.ino
  8. +2
    -11
      code/espurna/relay.ino

+ 2
- 2
code/espurna/dht.ino View File

@ -60,8 +60,8 @@ void dhtLoop() {
DEBUG_MSG("[DHT] Humidity: %s\n", dhtHumidity); DEBUG_MSG("[DHT] Humidity: %s\n", dhtHumidity);
// Send MQTT messages // Send MQTT messages
mqttSend((char *) getSetting("dhtTmpTopic", DHT_TEMPERATURE_TOPIC).c_str(), dhtTemperature);
mqttSend((char *) getSetting("dhtHumTopic", DHT_HUMIDITY_TOPIC).c_str(), dhtHumidity);
mqttSend(getSetting("dhtTmpTopic", DHT_TEMPERATURE_TOPIC).c_str(), dhtTemperature);
mqttSend(getSetting("dhtHumTopic", DHT_HUMIDITY_TOPIC).c_str(), dhtHumidity);
// Update websocket clients // Update websocket clients
char buffer[100]; char buffer[100];


+ 1
- 1
code/espurna/ds18b20.ino View File

@ -54,7 +54,7 @@ void dsLoop() {
DEBUG_MSG("[DS18B20] Temperature: %s\n", dsTemperature); DEBUG_MSG("[DS18B20] Temperature: %s\n", dsTemperature);
// Send MQTT messages // Send MQTT messages
mqttSend((char *) getSetting("dsTmpTopic", DS_TEMPERATURE_TOPIC).c_str(), dsTemperature);
mqttSend(getSetting("dsTmpTopic", DS_TEMPERATURE_TOPIC).c_str(), dsTemperature);
// Update websocket clients // Update websocket clients
char buffer[100]; char buffer[100];


+ 1
- 1
code/espurna/emon.ino View File

@ -104,7 +104,7 @@ void powerMonitorLoop() {
if (measurements == EMON_MEASUREMENTS) { if (measurements == EMON_MEASUREMENTS) {
double p = (sum - max - min) * mainsVoltage / (measurements - 2); double p = (sum - max - min) * mainsVoltage / (measurements - 2);
sprintf(power, "%d", int(p)); sprintf(power, "%d", int(p));
mqttSend((char *) getSetting("emonPowerTopic", EMON_POWER_TOPIC).c_str(), power);
mqttSend(getSetting("emonPowerTopic", EMON_POWER_TOPIC).c_str(), power);
sum = 0; sum = 0;
measurements = 0; measurements = 0;
} }


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

@ -67,7 +67,7 @@ void hardwareLoop() {
if (mqttConnected()) { if (mqttConnected()) {
if ((millis() - last_heartbeat > HEARTBEAT_INTERVAL) || (last_heartbeat == 0)) { if ((millis() - last_heartbeat > HEARTBEAT_INTERVAL) || (last_heartbeat == 0)) {
last_heartbeat = millis(); last_heartbeat = millis();
mqttSend((char *) MQTT_HEARTBEAT_TOPIC, (char *) "1");
mqttSend(MQTT_HEARTBEAT_TOPIC, "1");
DEBUG_MSG("[BEAT] Free heap: %d\n", ESP.getFreeHeap()); DEBUG_MSG("[BEAT] Free heap: %d\n", ESP.getFreeHeap());
DEBUG_MSG("[NTP] Time: %s\n", (char *) NTP.getTimeDateString().c_str()); DEBUG_MSG("[NTP] Time: %s\n", (char *) NTP.getTimeDateString().c_str());
} }


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

@ -74,7 +74,7 @@ void ledMQTTCallback(unsigned int type, const char * topic, const char * payload
if (type == MQTT_MESSAGE_EVENT) { if (type == MQTT_MESSAGE_EVENT) {
// Match topic // Match topic
String t = String(topic);
String t = String(topic + mqttTopicRootLength());
if (!t.startsWith(MQTT_LED_TOPIC)) return; if (!t.startsWith(MQTT_LED_TOPIC)) return;
if (!t.endsWith(mqttSetter)) return; if (!t.endsWith(mqttSetter)) return;
@ -134,7 +134,7 @@ void ledSetup() {
mqttRegister(ledMQTTCallback); mqttRegister(ledMQTTCallback);
DEBUG_MSG("[LED] Number of leds: %d\n", _leds.size()); DEBUG_MSG("[LED] Number of leds: %d\n", _leds.size());
DEBUG_MSG("[LED] Led auto indicator is %s.\n", ledAuto ? "ON" : "OFF" );
DEBUG_MSG("[LED] Led auto indicator is %s\n", ledAuto ? "ON" : "OFF" );
} }


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

@ -37,17 +37,32 @@ void buildTopics() {
mqttTopic.replace("{identifier}", getSetting("hostname")); mqttTopic.replace("{identifier}", getSetting("hostname"));
} }
unsigned int mqttTopicRootLength() {
return mqttTopic.length();
}
void mqttSendRaw(const char * topic, const char * message) {
if (mqtt.connected()) {
DEBUG_MSG("[MQTT] Sending %s %s\n", topic, message);
mqtt.publish(topic, MQTT_QOS, MQTT_RETAIN, message);
}
}
void mqttSend(const char * topic, const char * message) { void mqttSend(const char * topic, const char * message) {
if (!mqtt.connected()) return;
String path = mqttTopic + String(topic); String path = mqttTopic + String(topic);
DEBUG_MSG("[MQTT] Sending %s %s\n", (char *) path.c_str(), message);
mqtt.publish(path.c_str(), MQTT_QOS, MQTT_RETAIN, message);
mqttSendRaw(path.c_str(), message);
}
void mqttSubscribeRaw(const char * topic) {
if (mqtt.connected()) {
DEBUG_MSG("[MQTT] Subscribing to %s\n", topic);
mqtt.subscribe(topic, MQTT_QOS);
}
} }
void mqttSubscribe(const char * topic) { void mqttSubscribe(const char * topic) {
String path = mqttTopic + String(topic); String path = mqttTopic + String(topic);
DEBUG_MSG("[MQTT] Subscribing to %s\n", (char *) path.c_str());
mqtt.subscribe(path.c_str(), MQTT_QOS);
mqttSubscribeRaw(path.c_str());
} }
void mqttRegister(void (*callback)(unsigned int, const char *, const char *)) { void mqttRegister(void (*callback)(unsigned int, const char *, const char *)) {
@ -67,11 +82,11 @@ void _mqttOnConnect(bool sessionPresent) {
mqtt.setWill((mqttTopic + MQTT_HEARTBEAT_TOPIC).c_str(), MQTT_QOS, MQTT_RETAIN, (char *) "0"); mqtt.setWill((mqttTopic + MQTT_HEARTBEAT_TOPIC).c_str(), MQTT_QOS, MQTT_RETAIN, (char *) "0");
// Say hello and report our IP and VERSION // Say hello and report our IP and VERSION
mqttSend((char *) MQTT_IP_TOPIC, (char *) getIP().c_str());
mqttSend((char *) MQTT_VERSION_TOPIC, (char *) APP_VERSION);
mqttSend(MQTT_IP_TOPIC, getIP().c_str());
mqttSend(MQTT_VERSION_TOPIC, APP_VERSION);
char buffer[50]; char buffer[50];
getFSVersion(buffer); getFSVersion(buffer);
mqttSend((char *) MQTT_FSVERSION_TOPIC, buffer);
mqttSend(MQTT_FSVERSION_TOPIC, buffer);
// 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++) {
@ -93,7 +108,10 @@ void _mqttOnDisconnect(AsyncMqttClientDisconnectReason reason) {
void _mqttOnMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) { void _mqttOnMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {
DEBUG_MSG("[MQTT] Received %s %c", topic, payload[0]);
char message[len+1];
strlcpy(message, payload, len+1);
DEBUG_MSG("[MQTT] Received %s => %s", topic, message);
#if MQTT_SKIP_RETAINED #if MQTT_SKIP_RETAINED
if (millis() - mqttConnectedAt < MQTT_SKIP_TIME) { if (millis() - mqttConnectedAt < MQTT_SKIP_TIME) {
@ -107,7 +125,7 @@ void _mqttOnMessage(char* topic, char* payload, AsyncMqttClientMessageProperties
// Send message event to subscribers // Send message event to subscribers
// Topic is set to the specific part each one might be checking // Topic is set to the specific part each one might be checking
for (unsigned char i = 0; i < _mqtt_callbacks.size(); i++) { for (unsigned char i = 0; i < _mqtt_callbacks.size(); i++) {
(*_mqtt_callbacks[i])(MQTT_MESSAGE_EVENT, topic + mqttTopic.length(), payload);
(*_mqtt_callbacks[i])(MQTT_MESSAGE_EVENT, topic, message);
} }
} }


+ 1
- 1
code/espurna/pow.ino View File

@ -151,7 +151,7 @@ void powLoop() {
wsSend(buffer); wsSend(buffer);
if (--report_count == 0) { if (--report_count == 0) {
mqttSend((char *) getSetting("powPowerTopic", POW_POWER_TOPIC).c_str(), (char *) String(power).c_str());
mqttSend(getSetting("powPowerTopic", POW_POWER_TOPIC).c_str(), String(power).c_str());
report_count = POW_REPORT_EVERY; report_count = POW_REPORT_EVERY;
} }


+ 2
- 11
code/espurna/relay.ino View File

@ -26,7 +26,7 @@ void relayMQTT(unsigned char id) {
String mqttGetter = getSetting("mqttGetter", MQTT_USE_GETTER); String mqttGetter = getSetting("mqttGetter", MQTT_USE_GETTER);
char buffer[strlen(MQTT_RELAY_TOPIC) + mqttGetter.length() + 3]; char buffer[strlen(MQTT_RELAY_TOPIC) + mqttGetter.length() + 3];
sprintf(buffer, "%s/%d%s", MQTT_RELAY_TOPIC, id, mqttGetter.c_str()); sprintf(buffer, "%s/%d%s", MQTT_RELAY_TOPIC, id, mqttGetter.c_str());
mqttSend(buffer, (char *) (relayStatus(id) ? "1" : "0"));
mqttSend(buffer, relayStatus(id) ? "1" : "0");
} }
void relayMQTT() { void relayMQTT() {
@ -175,8 +175,6 @@ unsigned char relayCount() {
void relayMQTTCallback(unsigned int type, const char * topic, const char * payload) { void relayMQTTCallback(unsigned int type, const char * topic, const char * payload) {
static bool isFirstMessage = true;
String mqttSetter = getSetting("mqttSetter", MQTT_USE_SETTER); String mqttSetter = getSetting("mqttSetter", MQTT_USE_SETTER);
String mqttGetter = getSetting("mqttGetter", MQTT_USE_GETTER); String mqttGetter = getSetting("mqttGetter", MQTT_USE_GETTER);
bool sameSetGet = mqttGetter.compareTo(mqttSetter) == 0; bool sameSetGet = mqttGetter.compareTo(mqttSetter) == 0;
@ -191,17 +189,10 @@ void relayMQTTCallback(unsigned int type, const char * topic, const char * paylo
if (type == MQTT_MESSAGE_EVENT) { if (type == MQTT_MESSAGE_EVENT) {
// Match topic // Match topic
String t = String(topic);
String t = String(topic + mqttTopicRootLength());
if (!t.startsWith(MQTT_RELAY_TOPIC)) return; if (!t.startsWith(MQTT_RELAY_TOPIC)) return;
if (!t.endsWith(mqttSetter)) return; if (!t.endsWith(mqttSetter)) return;
// If relayMode is not SAME avoid responding to a retained message
if (sameSetGet && isFirstMessage) {
isFirstMessage = false;
byte relayMode = getSetting("relayMode", RELAY_MODE).toInt();
if (relayMode != RELAY_MODE_SAME) return;
}
// Get relay ID // Get relay ID
unsigned int relayID = topic[strlen(MQTT_RELAY_TOPIC)+1] - '0'; unsigned int relayID = topic[strlen(MQTT_RELAY_TOPIC)+1] - '0';
if (relayID >= relayCount()) relayID = 0; if (relayID >= relayCount()) relayID = 0;


Loading…
Cancel
Save