Browse Source

Manage LED status from MQTT

fastled
Xose Pérez 7 years ago
parent
commit
bda0c4d8f4
4 changed files with 135 additions and 37 deletions
  1. +9
    -0
      code/src/config/general.h
  2. +26
    -26
      code/src/config/hardware.h
  3. +99
    -11
      code/src/led.ino
  4. +1
    -0
      code/src/main.ino

+ 9
- 0
code/src/config/general.h View File

@ -27,6 +27,15 @@
// 0 means ANY, 1 zero or one and 2 one and only one
#define RELAY_SYNC RELAY_SYNC_ANY
//--------------------------------------------------------------------------------
// LED
//--------------------------------------------------------------------------------
// All defined LEDs in the board can be managed through MQTT
// except the first one when LED_AUTO is set to 1.
// If LED_AUTO is set to 1 the board will use first defined LED to show wifi status.
#define LED_AUTO 1
// -----------------------------------------------------------------------------
// WIFI & WEB
// -----------------------------------------------------------------------------


+ 26
- 26
code/src/config/hardware.h View File

@ -26,8 +26,8 @@
#define DEVICE "LOLIN"
#define BUTTON1_PIN 0
#define RELAY1_PIN 12
#define LED_PIN 2
#define LED_PIN_INVERSE 0
#define LED1_PIN 2
#define LED1_PIN_INVERSE 0
// -----------------------------------------------------------------------------
// Itead Studio boards
@ -39,8 +39,8 @@
#define DEVICE "SONOFF"
#define BUTTON1_PIN 0
#define RELAY1_PIN 12
#define LED_PIN 13
#define LED_PIN_INVERSE 0
#define LED1_PIN 13
#define LED1_PIN_INVERSE 0
#elif defined(SONOFF_TH)
@ -48,8 +48,8 @@
#define DEVICE "SONOFF_TH"
#define BUTTON1_PIN 0
#define RELAY1_PIN 12
#define LED_PIN 13
#define LED_PIN_INVERSE 0
#define LED1_PIN 13
#define LED1_PIN_INVERSE 0
#elif defined(SONOFF_TOUCH)
@ -57,8 +57,8 @@
#define DEVICE "SONOFF_TOUCH"
#define BUTTON1_PIN 0
#define RELAY1_PIN 12
#define LED_PIN 13
#define LED_PIN_INVERSE 1
#define LED1_PIN 13
#define LED1_PIN_INVERSE 1
#elif defined(SONOFF_POW)
@ -66,8 +66,8 @@
#define DEVICE "SONOFF_POW"
#define BUTTON1_PIN 0
#define RELAY1_PIN 12
#define LED_PIN 15
#define LED_PIN_INVERSE 1
#define LED1_PIN 15
#define LED1_PIN_INVERSE 1
#define ENABLE_POW 1
#elif defined(SONOFF_DUAL)
@ -75,8 +75,8 @@
#define MANUFACTURER "ITEAD"
#define DEVICE "SONOFF_DUAL"
#define BUTTON1_PIN 0
#define LED_PIN 13
#define LED_PIN_INVERSE 0
#define LED1_PIN 13
#define LED1_PIN_INVERSE 0
#undef SERIAL_BAUDRATE
#define SERIAL_BAUDRATE 19230
@ -92,8 +92,8 @@
#define RELAY2_PIN 5
#define RELAY3_PIN 4
#define RELAY4_PIN 15
#define LED_PIN 13
#define LED_PIN_INVERSE 1
#define LED1_PIN 13
#define LED1_PIN_INVERSE 1
#elif defined(SONOFF_SV)
@ -101,8 +101,8 @@
#define DEVICE "SONOFF_SV"
#define BUTTON1_PIN 0
#define RELAY1_PIN 12
#define LED_PIN 13
#define LED_PIN_INVERSE 1
#define LED1_PIN 13
#define LED1_PIN_INVERSE 1
#elif defined(SLAMPHER)
@ -110,8 +110,8 @@
#define DEVICE "SLAMPHER"
#define BUTTON1_PIN 0
#define RELAY1_PIN 12
#define LED_PIN 13
#define LED_PIN_INVERSE 0
#define LED1_PIN 13
#define LED1_PIN_INVERSE 0
#elif defined(S20)
@ -119,8 +119,8 @@
#define DEVICE "S20"
#define BUTTON1_PIN 0
#define RELAY1_PIN 12
#define LED_PIN 13
#define LED_PIN_INVERSE 0
#define LED1_PIN 13
#define LED1_PIN_INVERSE 0
// -----------------------------------------------------------------------------
// Electrodragon boards
@ -134,8 +134,8 @@
#define BUTTON2_PIN 2
#define RELAY1_PIN 12
#define RELAY2_PIN 13
#define LED_PIN 16
#define LED_PIN_INVERSE 1
#define LED1_PIN 16
#define LED1_PIN_INVERSE 1
// -----------------------------------------------------------------------------
// WorkChoice ecoPlug
@ -147,8 +147,8 @@
#define DEVICE "ECOPLUG"
#define BUTTON1_PIN 13
#define RELAY_PIN 15
#define LED_PIN 2
#define LED_PIN_INVERSE 1
#define LED1_PIN 2
#define LED1_PIN_INVERSE 1
// -----------------------------------------------------------------------------
// ESPurna board (still beta)
@ -160,8 +160,8 @@
#define DEVICE "ESPURNA"
#define BUTTON1_PIN 0
#define RELAY1_PIN 12
#define LED_PIN 13
#define LED_PIN_INVERSE 0
#define LED1_PIN 13
#define LED1_PIN_INVERSE 0
// -----------------------------------------------------------------------------
// Unknown hardware


+ 99
- 11
code/src/led.ino View File

@ -11,36 +11,124 @@ Copyright (C) 2016 by Xose Pérez <xose dot perez at gmail dot com>
// LED
// -----------------------------------------------------------------------------
#ifdef LED_PIN
#ifdef LED1_PIN
void blink(unsigned long delayOff, unsigned long delayOn) {
typedef struct {
unsigned char pin;
bool reverse;
} led_t;
std::vector<led_t> _leds;
bool ledAuto;
bool ledStatus(unsigned char id) {
bool status = digitalRead(_leds[id].pin);
return _leds[id].reverse ? !status : status;
}
bool ledStatus(unsigned char id, bool status) {
bool s = _leds[id].reverse ? !status : status;
digitalWrite(_leds[id].pin, _leds[id].reverse ? !status : status);
return status;
}
bool ledToggle(unsigned char id) {
return ledStatus(id, !ledStatus(id));
}
void ledBlink(unsigned char id, unsigned long delayOff, unsigned long delayOn) {
static unsigned long next = millis();
static bool status = HIGH;
if (next < millis()) {
status = !status;
digitalWrite(LED_PIN, LED_PIN_INVERSE ? !status : status);
next += ((status) ? delayOff : delayOn);
next += (ledToggle(id) ? delayOn : delayOff);
}
}
void showStatus() {
if (wifiConnected()) {
if (WiFi.getMode() == WIFI_AP) {
blink(2000, 2000);
ledBlink(0, 2000, 2000);
} else {
blink(5000, 500);
ledBlink(0, 5000, 500);
}
} else {
blink(500, 500);
ledBlink(0, 500, 500);
}
}
void ledMQTTCallback(unsigned int type, const char * topic, const char * payload) {
static bool isFirstMessage = true;
if (type == MQTT_CONNECT_EVENT) {
mqttSubscribe("/led/#");
}
if (type == MQTT_MESSAGE_EVENT) {
// Match topic
if (memcmp("/led/", topic, 5) != 0) return;
// Get led ID
unsigned int ledID = topic[strlen(topic)-1] - '0';
if (ledID >= relayCount()) ledID = 0;
// get value
unsigned int value = (char)payload[0] - '0';
bool bitAuto = (value & 0x02) > 0;
bool bitState = (value & 0x01) > 0;
// Check ledAuto
if (ledID == 0) {
if (bitAuto) {
ledAuto = bitState;
setSetting("ledAuto", String() + (ledAuto ? "1" : "0"));
return;
} else if (ledAuto) {
return;
}
}
// Action to perform
ledStatus(ledID, bitState);
}
}
void ledConfigure() {
ledAuto = getSetting("ledAuto", String() + LED_AUTO).toInt() == 1;
}
void ledSetup() {
pinMode(LED_PIN, OUTPUT);
#ifdef LED1_PIN
_leds.push_back((led_t) { LED1_PIN, LED1_PIN_INVERSE });
#endif
#ifdef LED2_PIN
_leds.push_back((led_t) { LED2_PIN, LED2_PIN_INVERSE });
#endif
#ifdef LED3_PIN
_leds.push_back((led_t) { LED3_PIN, LED3_PIN_INVERSE });
#endif
#ifdef LED4_PIN
_leds.push_back((led_t) { LED4_PIN, LED4_PIN_INVERSE });
#endif
for (unsigned int i=0; i < _leds.size(); i++) {
pinMode(_leds[i].pin, OUTPUT);
ledStatus(i, false);
}
ledConfigure();
mqttRegister(ledMQTTCallback);
DEBUG_MSG("[LED] Number of leds: %d\n", _leds.size());
}
void ledLoop() {
showStatus();
if (ledAuto) showStatus();
}
#else


+ 1
- 0
code/src/main.ino View File

@ -31,6 +31,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "FS.h"
String getSetting(const String& key, String defaultValue = "");
bool relayStatus(unsigned char id, bool status, bool report = true);
void mqttRegister(void (*callback)(unsigned int, const char *, const char *));
// -----------------------------------------------------------------------------
// METHODS


Loading…
Cancel
Save