Browse Source

Merge branch 'dev' into refactor-terminal

refactor-terminal
Xose Pérez 5 years ago
parent
commit
8ba0cdb231
14 changed files with 83 additions and 42 deletions
  1. +4
    -1
      code/espurna/alexa.ino
  2. +6
    -6
      code/espurna/broker.ino
  3. +4
    -0
      code/espurna/config/general.h
  4. +1
    -1
      code/espurna/config/hardware.h
  5. +1
    -1
      code/espurna/config/prototypes.h
  6. +9
    -0
      code/espurna/config/types.h
  7. +6
    -1
      code/espurna/domoticz.ino
  8. +5
    -2
      code/espurna/influxdb.ino
  9. +6
    -1
      code/espurna/led.ino
  10. +27
    -16
      code/espurna/light.ino
  11. +1
    -1
      code/espurna/ntp.ino
  12. +1
    -1
      code/espurna/relay.ino
  13. +1
    -9
      code/espurna/sensor.ino
  14. +11
    -2
      code/espurna/thinkspeak.ino

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

@ -46,8 +46,11 @@ bool _alexaRequestCallback(AsyncWebServerRequest *request) {
}
#if BROKER_SUPPORT
void _alexaBrokerCallback(const char * topic, unsigned char id, const char * payload) {
void _alexaBrokerCallback(const unsigned char type, const char * topic, unsigned char id, const char * payload) {
// Only process status messages
if (BROKER_MSG_TYPE_STATUS != type) return;
unsigned char value = atoi(payload);
if (strcmp(MQTT_TOPIC_CHANNEL, topic) == 0) {


+ 6
- 6
code/espurna/broker.ino View File

@ -10,23 +10,23 @@ Copyright (C) 2017-2018 by Xose Pérez <xose dot perez at gmail dot com>
#include <vector>
std::vector<void (*)(const char *, unsigned char, const char *)> _broker_callbacks;
std::vector<void (*)(const unsigned char, const char *, unsigned char, const char *)> _broker_callbacks;
// -----------------------------------------------------------------------------
void brokerRegister(void (*callback)(const char *, unsigned char, const char *)) {
void brokerRegister(void (*callback)(const unsigned char, const char *, unsigned char, const char *)) {
_broker_callbacks.push_back(callback);
}
void brokerPublish(const char * topic, unsigned char id, const char * message) {
void brokerPublish(const unsigned char type, 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);
(_broker_callbacks[i])(type, topic, id, message);
}
}
void brokerPublish(const char * topic, const char * message) {
brokerPublish(topic, 0, message);
void brokerPublish(const unsigned char type, const char * topic, const char * message) {
brokerPublish(type, topic, 0, message);
}
#endif // BROKER_SUPPORT

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

@ -881,6 +881,10 @@
#define LIGHT_SAVE_ENABLED 1 // Light channel values saved by default after each change
#endif
#ifndef LIGHT_COMMS_DELAY
#define LIGHT_COMMS_DELAY 100 // Delay communication after light update (in ms)
#endif
#ifndef LIGHT_SAVE_DELAY
#define LIGHT_SAVE_DELAY 5 // Persist color after 5 seconds to avoid wearing out
#endif


+ 1
- 1
code/espurna/config/hardware.h View File

@ -112,7 +112,7 @@
#define MANUFACTURER "WEMOS"
#define DEVICE "D1_MINI_RELAYSHIELD"
// Buttons
// Buttons
// No buttons on the D1 MINI alone, but defining it without adding a button doen't create problems
#define BUTTON1_PIN 0 // Connect a pushbutton between D3 and GND,
// it's the same as using a Wemos one button shield


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

@ -24,7 +24,7 @@ extern "C" {
// Broker
// -----------------------------------------------------------------------------
#if BROKER_SUPPORT
void brokerRegister(void (*)(const char *, unsigned char, const char *));
void brokerRegister(void (*)(const unsigned char, const char *, unsigned char, const char *));
#endif
// -----------------------------------------------------------------------------


+ 9
- 0
code/espurna/config/types.h View File

@ -3,6 +3,15 @@
// Do not touch this definitions
//------------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// BROKER
// -----------------------------------------------------------------------------
#define BROKER_MSG_TYPE_SYSTEM 0
#define BROKER_MSG_TYPE_DATETIME 1
#define BROKER_MSG_TYPE_STATUS 2
#define BROKER_MSG_TYPE_SENSOR 3
// -----------------------------------------------------------------------------
// WIFI
// -----------------------------------------------------------------------------


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

@ -149,11 +149,16 @@ void _domoticzMqtt(unsigned int type, const char * topic, const char * payload)
};
#if BROKER_SUPPORT
void _domoticzBrokerCallback(const char * topic, unsigned char id, const char * payload) {
void _domoticzBrokerCallback(const unsigned char type, const char * topic, unsigned char id, const char * payload) {
// Only process status messages
if (BROKER_MSG_TYPE_STATUS != type) return;
if (strcmp(MQTT_TOPIC_RELAY, topic) == 0) {
unsigned char value = atoi(payload);
domoticzSendRelay(id, value == 1);
}
}
#endif // BROKER_SUPPORT


+ 5
- 2
code/espurna/influxdb.ino View File

@ -39,10 +39,13 @@ void _idbConfigure() {
}
#if BROKER_SUPPORT
void _idbBrokerCallback(const char * topic, unsigned char id, const char * payload) {
if (strcmp(MQTT_TOPIC_RELAY, topic) == 0) {
void _idbBrokerCallback(const unsigned char type, const char * topic, unsigned char id, const char * payload) {
// Only process status & senssor messages
if ((BROKER_MSG_TYPE_STATUS == type) || (BROKER_MSG_TYPE_SENSOR == type)) {
idbSend(topic, id, (char *) payload);
}
}
#endif // BROKER_SUPPORT


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

@ -74,10 +74,15 @@ void _ledWebSocketOnSend(JsonObject& root) {
#endif
#if BROKER_SUPPORT
void _ledBrokerCallback(const char * topic, unsigned char id, const char * payload) {
void _ledBrokerCallback(const unsigned char type, const char * topic, unsigned char id, const char * payload) {
// Only process status messages
if (BROKER_MSG_TYPE_STATUS != type) return;
if (strcmp(MQTT_TOPIC_RELAY, topic) == 0) {
ledUpdate(true);
}
}
#endif // BROKER_SUPPORT


+ 27
- 16
code/espurna/light.ino View File

@ -25,6 +25,7 @@ extern "C" {
// -----------------------------------------------------------------------------
Ticker _light_comms_ticker;
Ticker _light_save_ticker;
Ticker _light_transition_ticker;
@ -640,7 +641,7 @@ void lightBroker() {
char buffer[10];
for (unsigned int i=0; i < _light_channel.size(); i++) {
itoa(_light_channel[i].inputValue, buffer, 10);
brokerPublish(MQTT_TOPIC_CHANNEL, i, buffer);
brokerPublish(BROKER_MSG_TYPE_STATUS, MQTT_TOPIC_CHANNEL, i, buffer);
}
}
@ -658,6 +659,26 @@ bool lightHasColor() {
return _light_has_color;
}
void _lightComms(unsigned char mask) {
// Report color & brightness to MQTT broker
#if MQTT_SUPPORT
if (mask & 0x01) lightMQTT();
if (mask & 0x02) lightMQTTGroup();
#endif
// Report color to WS clients (using current brightness setting)
#if WEB_SUPPORT
wsSend(_lightWebSocketOnSend);
#endif
// Report channels to local broker
#if BROKER_SUPPORT
lightBroker();
#endif
}
void lightUpdate(bool save, bool forward, bool group_forward) {
_generateBrightness();
@ -672,21 +693,11 @@ void lightUpdate(bool save, bool forward, bool group_forward) {
_light_steps_left = _light_use_transitions ? _light_transition_time / LIGHT_TRANSITION_STEP : 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();
if (group_forward) lightMQTTGroup();
#endif
// Report color to WS clients (using current brightness setting)
#if WEB_SUPPORT
wsSend(_lightWebSocketOnSend);
#endif
// Delay every communication 100ms to avoid jamming
unsigned char mask = 0;
if (forward) mask += 1;
if (group_forward) mask += 2;
_light_comms_ticker.once_ms(LIGHT_COMMS_DELAY, _lightComms, mask);
#if LIGHT_SAVE_ENABLED
// Delay saving to EEPROM 5 seconds to avoid wearing it out unnecessarily


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

@ -108,7 +108,7 @@ void _ntpLoop() {
static unsigned char last_minute = 60;
if (ntpSynced() && (minute() != last_minute)) {
last_minute = minute();
brokerPublish(MQTT_TOPIC_DATETIME, ntpDateTime().c_str());
brokerPublish(BROKER_MSG_TYPE_DATETIME, MQTT_TOPIC_DATETIME, ntpDateTime().c_str());
}
#endif


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

@ -177,7 +177,7 @@ void _relayProcess(bool mode) {
// Send to Broker
#if BROKER_SUPPORT
brokerPublish(MQTT_TOPIC_RELAY, id, target ? "1" : "0");
brokerPublish(BROKER_MSG_TYPE_STATUS, MQTT_TOPIC_RELAY, id, target ? "1" : "0");
#endif
// Send MQTT


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

@ -1186,7 +1186,7 @@ void _sensorReport(unsigned char index, double value) {
dtostrf(value, 1-sizeof(buffer), decimals, buffer);
#if BROKER_SUPPORT
brokerPublish(magnitudeTopic(magnitude.type).c_str(), magnitude.local, buffer);
brokerPublish(BROKER_MSG_TYPE_SENSOR ,magnitudeTopic(magnitude.type).c_str(), magnitude.local, buffer);
#endif
#if MQTT_SUPPORT
@ -1205,14 +1205,6 @@ void _sensorReport(unsigned char index, double value) {
#endif // MQTT_SUPPORT
#if INFLUXDB_SUPPORT
if (SENSOR_USE_INDEX || (_counts[magnitude.type] > 1)) {
idbSend(magnitudeTopic(magnitude.type).c_str(), magnitude.global, buffer);
} else {
idbSend(magnitudeTopic(magnitude.type).c_str(), buffer);
}
#endif // INFLUXDB_SUPPORT
#if THINGSPEAK_SUPPORT
tspkEnqueueMeasurement(index, buffer);
#endif


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

@ -36,11 +36,20 @@ unsigned char _tspk_tries = 0;
// -----------------------------------------------------------------------------
#if BROKER_SUPPORT
void _tspkBrokerCallback(const char * topic, unsigned char id, const char * payload) {
if (strcmp(MQTT_TOPIC_RELAY, topic) == 0) {
void _tspkBrokerCallback(const unsigned char type, const char * topic, unsigned char id, const char * payload) {
// Process status messages
if (BROKER_MSG_TYPE_STATUS == type) {
tspkEnqueueRelay(id, (char *) payload);
tspkFlush();
}
// Porcess sensor messages
if (BROKER_MSG_TYPE_SENSOR == type) {
//tspkEnqueueMeasurement(id, (char *) payload);
//tspkFlush();
}
}
#endif // BROKER_SUPPORT


Loading…
Cancel
Save