Browse Source

Local broker

pull/461/head
Xose Pérez 6 years ago
parent
commit
90ce2a8e37
9 changed files with 94 additions and 2 deletions
  1. +32
    -0
      code/espurna/broker.ino
  2. +1
    -0
      code/espurna/config/arduino.h
  3. +8
    -0
      code/espurna/config/general.h
  4. +5
    -0
      code/espurna/config/prototypes.h
  5. +22
    -1
      code/espurna/light.ino
  6. +10
    -0
      code/espurna/ntp.ino
  7. +5
    -0
      code/espurna/relay.ino
  8. +4
    -0
      code/espurna/sensor.ino
  9. +7
    -1
      code/espurna/utils.ino

+ 32
- 0
code/espurna/broker.ino View File

@ -0,0 +1,32 @@
/*
BROKER MODULE
Copyright (C) 2017-2018 by Xose Pérez <xose dot perez at gmail dot com>
*/
#if BROKER_SUPPORT
#include <vector>
std::vector<void (*)(const char *, unsigned char, const char *)> _broker_callbacks;
// -----------------------------------------------------------------------------
void brokerRegister(void (*callback)(const char *, unsigned char, const char *)) {
_broker_callbacks.push_back(callback);
}
void brokerPublish(const char * topic, unsigned char id, const char * message) {
DEBUG_MSG_P(PSTR("[BROKER] Message %s[%u] => %s\n"), topic, id, message);
for (unsigned char i=0; i<_broker_callbacks.size(); i++) {
(_broker_callbacks[i])(topic, id, message);
}
}
void brokerPublish(const char * topic, const char * message) {
brokerPublish(topic, 0, message);
}
#endif // BROKER_SUPPORT

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

@ -76,6 +76,7 @@
//#define LLMNR_SUPPORT 1 // Only with Arduino Core 2.4.0
//#define MDNS_SERVER_SUPPORT 0
//#define MDNS_CLIENT_SUPPORT 1
//#define BROKER_SUPPORT 0
//#define MQTT_SUPPORT 0
//#define NETBIOS_SUPPORT 1 // Only with Arduino Core 2.4.0
//#define NOFUSS_SUPPORT 1


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

@ -570,6 +570,14 @@ PROGMEM const char* const custom_reset_string[] = {
#define MQTT_SETTER "/set"
#endif
// -----------------------------------------------------------------------------
// BROKER
// -----------------------------------------------------------------------------
#ifndef BROKER_SUPPORT
#define BROKER_SUPPORT 1 // The broker is a poor-man's pubsub manager
#endif
// -----------------------------------------------------------------------------
// SETTINGS
// -----------------------------------------------------------------------------


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

@ -47,6 +47,11 @@ typedef std::function<void(unsigned int, const char *, const char *)> mqtt_callb
void mqttRegister(mqtt_callback_f callback);
String mqttTopicKey(char * topic);
// -----------------------------------------------------------------------------
// Broker
// -----------------------------------------------------------------------------
void brokerRegister(void (*)(const char *, unsigned char, const char *));
// -----------------------------------------------------------------------------
// Settings
// -----------------------------------------------------------------------------


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

@ -571,7 +571,7 @@ void lightMQTT() {
// Channels
for (unsigned int i=0; i < _light_channel.size(); i++) {
snprintf_P(buffer, sizeof(buffer), PSTR("%d"), _light_channel[i].value);
itoa(_light_channel[i].value, buffer, 10);
mqttSend(MQTT_TOPIC_CHANNEL, i, buffer);
}
@ -588,6 +588,22 @@ void lightMQTTGroup() {
#endif
// -----------------------------------------------------------------------------
// Broker
// -----------------------------------------------------------------------------
#if BROKER_SUPPORT
void lightBroker() {
char buffer[10];
for (unsigned int i=0; i < _light_channel.size(); i++) {
itoa(_light_channel[i].value, buffer, 10);
brokerPublish(MQTT_TOPIC_CHANNEL, i, buffer);
}
}
#endif
// -----------------------------------------------------------------------------
// API
// -----------------------------------------------------------------------------
@ -610,6 +626,11 @@ void lightUpdate(bool save, bool forward, bool group_forward) {
_light_steps_left = _light_use_transitions ? LIGHT_TRANSITION_STEPS : 1;
_light_transition_ticker.attach_ms(LIGHT_TRANSITION_STEP, _lightProviderUpdate);
// Report channels to local broker
#if BROKER_SUPPORT
lightBroker();
#endif
// Report color & brightness to MQTT broker
#if MQTT_SUPPORT
if (forward) lightMQTT();


+ 10
- 0
code/espurna/ntp.ino View File

@ -95,7 +95,17 @@ void ntpSetup() {
}
void ntpLoop() {
now();
#if BROKER_SUPPORT
static unsigned char last_minute = 60;
if (ntpSynced() && (minute() != last_minute)) {
last_minute = minute();
brokerPublish(MQTT_TOPIC_DATETIME, ntpDateTime().c_str());
}
#endif
}
#endif // NTP_SUPPORT

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

@ -762,6 +762,11 @@ void relayLoop(void) {
// Call the provider to perform the action
_relayProviderStatus(id, status);
// Send to Broker
#if BROKER_SUPPORT
brokerPublish(MQTT_TOPIC_RELAY, id, status ? "1" : "0");
#endif
// Send MQTT
#if MQTT_SUPPORT
relayMQTT(id);


+ 4
- 0
code/espurna/sensor.ino View File

@ -692,6 +692,10 @@ void sensorLoop() {
_magnitudes[i].reported = filtered;
dtostrf(filtered, 1-sizeof(buffer), decimals, buffer);
#if BROKER_SUPPORT
brokerPublish(_magnitudeTopic(magnitude.type).c_str(), magnitude.local, buffer);
#endif
#if MQTT_SUPPORT
if (SENSOR_USE_INDEX || (_counts[magnitude.type] > 1)) {


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

@ -271,6 +271,9 @@ void info() {
#if ALEXA_SUPPORT
DEBUG_MSG_P(PSTR(" ALEXA"));
#endif
#if BROKER_SUPPORT
DEBUG_MSG_P(PSTR(" BROKER"));
#endif
#if DEBUG_SERIAL_SUPPORT
DEBUG_MSG_P(PSTR(" DEBUG_SERIAL"));
#endif
@ -296,7 +299,10 @@ void info() {
DEBUG_MSG_P(PSTR(" LLMNR"));
#endif
#if MDNS_SERVER_SUPPORT
DEBUG_MSG_P(PSTR(" MDNS"));
DEBUG_MSG_P(PSTR(" MDNS_SERVER"));
#endif
#if MDNS_CLIENT_SUPPORT
DEBUG_MSG_P(PSTR(" MDNS_CLIENT"));
#endif
#if NETBIOS_SUPPORT
DEBUG_MSG_P(PSTR(" NETBIOS"));


Loading…
Cancel
Save