Adding support for Home Assistant

Don't know if this the most memory efficient way, but my first attempt at integrating into HA. A better implementation would be using Arduino JSON.
This commit is contained in:
Deb 2018-02-12 15:09:13 -05:00 committed by GitHub
parent 51ea2b9b36
commit 6ab2e6ee38
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,90 +1,101 @@
// Neopixel // Neopixel
#define PIN 5 // PIN (5 / D1) where neopixel / WS2811 strip is attached #define PIN 5 // PIN (5 / D1) where neopixel / WS2811 strip is attached
#define NUMLEDS 24 // Number of leds in the strip #define NUMLEDS 24 // Number of leds in the strip
//#define BUILTIN_LED 2 // ESP-12F has the built in LED on GPIO2, see https://github.com/esp8266/Arduino/issues/2192 //#define BUILTIN_LED 2 // ESP-12F has the built in LED on GPIO2, see https://github.com/esp8266/Arduino/issues/2192
#define BUTTON 4 // Input pin (4 / D2) for switching the LED strip on / off, connect this PIN to ground to trigger button. #define BUTTON 4 // Input pin (4 / D2) for switching the LED strip on / off, connect this PIN to ground to trigger button.
const char HOSTNAME[] = "ESP8266_01"; // Friedly hostname const char HOSTNAME[] = "ESP8266_01"; // Friedly hostname
#define ENABLE_OTA // If defined, enable Arduino OTA code. #define ENABLE_OTA // If defined, enable Arduino OTA code.
#define ENABLE_MQTT // If defined, enable MQTT client code, see: https://github.com/toblum/McLighting/wiki/MQTT-API #define ENABLE_MQTT // If defined, enable MQTT client code, see: https://github.com/toblum/McLighting/wiki/MQTT-API
// #define ENABLE_BUTTON // If defined, enable button handling code, see: https://github.com/toblum/McLighting/wiki/Button-control // #define ENABLE_BUTTON // If defined, enable button handling code, see: https://github.com/toblum/McLighting/wiki/Button-control
// parameters for automatically cycling favorite patterns // parameters for automatically cycling favorite patterns
uint32_t autoParams[][4] = { // color, speed, mode, duration (seconds) uint32_t autoParams[][4] = { // color, speed, mode, duration (seconds)
{0xff0000, 200, 1, 5.0}, // blink red for 5 seconds {0xff0000, 200, 1, 5.0}, // blink red for 5 seconds
{0x00ff00, 200, 3, 10.0}, // wipe green for 10 seconds {0x00ff00, 200, 3, 10.0}, // wipe green for 10 seconds
{0x0000ff, 200, 11, 5.0}, // dual scan blue for 5 seconds {0x0000ff, 200, 11, 5.0}, // dual scan blue for 5 seconds
{0x0000ff, 200, 42, 15.0} // fireworks for 15 seconds {0x0000ff, 200, 42, 15.0} // fireworks for 15 seconds
}; };
#ifdef ENABLE_MQTT #ifdef ENABLE_MQTT
#define MQTT_MAX_PACKET_SIZE 256 #define MQTT_MAX_PACKET_SIZE 256
#define MQTT_MAX_RECONNECT_TRIES 4 #define MQTT_MAX_RECONNECT_TRIES 4
int mqtt_reconnect_retries = 0; int mqtt_reconnect_retries = 0;
char mqtt_intopic[strlen(HOSTNAME) + 4]; // Topic in will be: <HOSTNAME>/in char mqtt_intopic[strlen(HOSTNAME) + 4]; // Topic in will be: <HOSTNAME>/in
char mqtt_outtopic[strlen(HOSTNAME) + 5]; // Topic out will be: <HOSTNAME>/out char mqtt_outtopic[strlen(HOSTNAME) + 5]; // Topic out will be: <HOSTNAME>/out
const char mqtt_clientid[] = "ESP8266Client"; // MQTT ClientID String mqtt_ha = "home/" + String(HOSTNAME) + "_ha/";
String mqtt_ha_state_in = mqtt_ha + "state/in";
char mqtt_host[64] = ""; String mqtt_ha_state_out = mqtt_ha + "state/out";
char mqtt_port[6] = ""; String mqtt_ha_effect_in = mqtt_ha + "effect/in";
char mqtt_user[32] = ""; String mqtt_ha_effect_out = mqtt_ha + "effect/out";
char mqtt_pass[32] = ""; String mqtt_ha_brightness_in = mqtt_ha + "brightness/in";
#endif String mqtt_ha_brightness_out = mqtt_ha + "brightness/out";
String mqtt_ha_rgb_in = mqtt_ha + "rgb/in";
String mqtt_ha_rgb_out = mqtt_ha + "rgb/out";
// *************************************************************************** String mqtt_ha_speed = mqtt_ha + "speed";
// Global variables / definitions
// *************************************************************************** const char mqtt_clientid[] = "ESP8266Client"; // MQTT ClientID
#define DBG_OUTPUT_PORT Serial // Set debug output port
char mqtt_host[64] = "";
// List of all color modes char mqtt_port[6] = "";
enum MODE { SET_MODE, HOLD, OFF, ALL, WIPE, RAINBOW, RAINBOWCYCLE, THEATERCHASE, TWINKLERANDOM, THEATERCHASERAINBOW, TV, CUSTOM }; char mqtt_user[32] = "";
char mqtt_pass[32] = "";
MODE mode = RAINBOW; // Standard mode that is active when software starts #endif
int ws2812fx_speed = 196; // Global variable for storing the delay between color changes --> smaller == faster
int brightness = 196; // Global variable for storing the brightness (255 == 100%) // ***************************************************************************
// Global variables / definitions
int ws2812fx_mode = 0; // Helper variable to set WS2812FX modes // ***************************************************************************
#define DBG_OUTPUT_PORT Serial // Set debug output port
bool exit_func = false; // Global helper variable to get out of the color modes when mode changes
// List of all color modes
bool shouldSaveConfig = false; // For WiFiManger custom config enum MODE { SET_MODE, HOLD, OFF, ALL, WIPE, RAINBOW, RAINBOWCYCLE, THEATERCHASE, TWINKLERANDOM, THEATERCHASERAINBOW, TV, CUSTOM };
struct ledstate // Data structure to store a state of a single led MODE mode = RAINBOW; // Standard mode that is active when software starts
{
uint8_t red; int ws2812fx_speed = 196; // Global variable for storing the delay between color changes --> smaller == faster
uint8_t green; int brightness = 196; // Global variable for storing the brightness (255 == 100%)
uint8_t blue;
}; int ws2812fx_mode = 0; // Helper variable to set WS2812FX modes
typedef struct ledstate LEDState; // Define the datatype LEDState bool exit_func = false; // Global helper variable to get out of the color modes when mode changes
LEDState ledstates[NUMLEDS]; // Get an array of led states to store the state of the whole strip
LEDState main_color = { 255, 0, 0 }; // Store the "main color" of the strip used in single color modes bool shouldSaveConfig = false; // For WiFiManger custom config
#define ENABLE_STATE_SAVE // If defined, save state on reboot struct ledstate // Data structure to store a state of a single led
#ifdef ENABLE_STATE_SAVE {
char current_state[32]; // Keeps the current state representation uint8_t red;
char last_state[32]; // Save the last state as string representation uint8_t green;
unsigned long time_statechange = 0; // Time when the state last changed uint8_t blue;
int timeout_statechange_save = 5000; // Timeout in ms to wait before state is saved };
bool state_save_requested = false; // State has to be saved after timeout
#endif typedef struct ledstate LEDState; // Define the datatype LEDState
LEDState ledstates[NUMLEDS]; // Get an array of led states to store the state of the whole strip
// Button handling LEDState main_color = { 255, 0, 0 }; // Store the "main color" of the strip used in single color modes
#ifdef ENABLE_BUTTON
#define BTN_MODE_SHORT "STA| 1| 0|245|196|255|255|255" // Static white #define ENABLE_STATE_SAVE // If defined, save state on reboot
#define BTN_MODE_MEDIUM "STA| 1| 48|245|196|255|102| 0" // Fire flicker #ifdef ENABLE_STATE_SAVE
#define BTN_MODE_LONG "STA| 1| 46|253|196|255|102| 0" // Fireworks random char current_state[32]; // Keeps the current state representation
char last_state[32]; // Save the last state as string representation
unsigned long keyPrevMillis = 0; unsigned long time_statechange = 0; // Time when the state last changed
const unsigned long keySampleIntervalMs = 25; int timeout_statechange_save = 5000; // Timeout in ms to wait before state is saved
byte longKeyPressCountMax = 80; // 80 * 25 = 2000 ms bool state_save_requested = false; // State has to be saved after timeout
byte mediumKeyPressCountMin = 20; // 20 * 25 = 500 ms #endif
byte KeyPressCount = 0;
byte prevKeyState = HIGH; // button is active low // Button handling
boolean buttonState = false; #ifdef ENABLE_BUTTON
#define BTN_MODE_SHORT "STA| 1| 0|245|196|255|255|255" // Static white
#define BTN_MODE_MEDIUM "STA| 1| 48|245|196|255|102| 0" // Fire flicker
#define BTN_MODE_LONG "STA| 1| 46|253|196|255|102| 0" // Fireworks random
unsigned long keyPrevMillis = 0;
const unsigned long keySampleIntervalMs = 25;
byte longKeyPressCountMax = 80; // 80 * 25 = 2000 ms
byte mediumKeyPressCountMin = 20; // 20 * 25 = 500 ms
byte KeyPressCount = 0;
byte prevKeyState = HIGH; // button is active low
boolean buttonState = false;
#endif #endif