ESP8266-RGB5m/ESP8266-RGB5m.ino

266 lines
7.0 KiB
Arduino
Raw Normal View History

2018-04-08 11:48:58 +00:00
#include <Arduino.h>
#include <Homie.h>
#include <ArduinoOTA.h>
#include <Adafruit_NeoPixel.h>
#define NUMPIXELS 5 * 60 // 60 Pixel per meter, it is being used within NeoPatterns.h
#include "NeoPatterns.h"
#include <math.h>
#define PIN D4
#define PINLOW_RELAIS D2
NeoPatterns strip = NeoPatterns(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800, &StripComplete, &DebugOutput);
bool stopAfterCompletion;
HomieNode homieNode("strip", "strip");
void StripComplete() {
if (stopAfterCompletion)
{
strip.IconComplete();
}
return;
}
void DebugOutput(String value) {
homieNode.setProperty("DEBUG").send(value);
}
bool onSetColor(const HomieRange& range, const String& value) {
if (!range.isRange || range.index < 0 || range.index > 1) {
return false;
}
switch (range.index) {
case 0:
strip.SetColor1(value.toInt());
break;
case 1:
strip.SetColor2(value.toInt());
break;
}
homieNode.setProperty("color_" + String(range.index)).send(value);
}
bool onSetPixel(const HomieRange& range, const String& value) {
if (!range.isRange) {
strip.None();
strip.ColorSet(value.toInt());
homieNode.setProperty("pixel").send(value);
return true;
}
if (range.index < 0 || range.index > strip.numPixels() - 1) {
return false;
}
strip.None();
strip.setPixelColor(range.index, value.toInt());
strip.show();
homieNode.setProperty("pixel_" + String(range.index)).send(value);
}
bool onSetBrightness(const HomieRange& range, const String& value) {
long brightness = value.toInt();
if (brightness < 0 || brightness > 255) {
return false;
}
if (brightness > 160) {
brightness = 160; // Künstliche Begrenzung
}
strip.setBrightness(brightness);
strip.show();
homieNode.setProperty("brightness").send(value);
}
bool onSetEffect(const HomieRange& range, const String& value) {
digitalWrite(PINLOW_RELAIS, LOW); // This relais: LOW = ON
stopAfterCompletion = false;
String effect = value;
effect.toLowerCase();
if (effect == "scanner") {
strip.Scanner(strip.Color(255, 0, 0));
}
else if (effect == "randomscanner") {
strip.Scanner(strip.Color(255, 0, 0), 4, true);
}
else if (effect == "larsonspiral") {
strip.Scanner(strip.Color(255, 0, 0), 40, true, true);
}
else if (effect == "rainbowcycle") {
strip.RainbowCycle(50);
}
else if (effect == "theaterchase" || effect == "chase") {
strip.TheaterChase(strip.Color(255, 0, 0), strip.Color(0, 0, 255), 50);
}
else if (effect == "bvb") {
strip.BVBChase(strip.Color(255, 185, 0), strip.Color(0, 0, 0), 50);
}
else if (effect == "fade") {
strip.Fade(strip.Color(255, 0, 0), strip.Color(0, 0, 255), 200, 100);
}
else if (effect == "randomfade") {
strip.RandomFade();
}
else if (effect == "random") {
strip.Random();
}
else if (effect == "smooth") { //example: smooth|[wheelspeed]|[smoothing]|[strength] wheelspeed=1-255, smoothing=0-100, strength=1-255
strip.Smooth(16, 80, 50, 40);
}
else if (effect == "plasma") {
strip.Plasma();
}
else if (effect == "fire") {
strip.Fire();
}
else if (effect == "fireworks") {
strip.Fireworks();
}
else if (effect == "drop") {
strip.Drop();
}
else if (effect == "scannerrandom") {
strip.ScannerRandom(strip.Color(255, 0, 0), 4, true);
}
else if (effect == "ring") {
strip.Rings();
} else {
// Test whether command with parameters was sent
int sep = value.indexOf("|");
String command = value.substring(0, sep);
String parameters = value.substring(sep + 1);
if (command.equals("fill")) {
strip.ColorSetParameters(parameters);
}
else if (command.equals("randomfade")) {
int sepparam = parameters.indexOf("|");
int p1 = parameters.substring(0, sepparam).toInt();
if (p1 <= 0) {
p1 = 5;
}
strip.RandomFadeSingle(p1);
}
else if (command.equals("randomscanner")) {
int sepparam = parameters.indexOf("|");
int p1 = parameters.substring(0, sepparam).toInt();
if (p1 <= 0) {
p1 = 5;
}
homieNode.setProperty("effect").send(String(p1));
strip.Scanner(strip.Color(255, 0, 0), p1, true);
}
else {
strip.None();
digitalWrite(PINLOW_RELAIS, HIGH); // This relais: HIGH = OFF
digitalWrite(PIN, LOW); // D4 ist auch gleichzeitig der LED-Pin, daher abschalten... (TODO: TEST: FIXME)
}
}
homieNode.setProperty("effect").send(value);
}
bool onSetIcon(const HomieRange& range, const String& value) {
stopAfterCompletion = true;
String _iconname = value;
if (value[0] == '#') { //color given
strip.Icon(value.substring(7)[0], value.substring(0, 6));
}
else {
strip.Icon(value[0]);
}
homieNode.setProperty("icon").send(value);
}
bool onSetClear(const HomieRange& range, const String& value) {
strip.None();
strip.clear();
strip.show();
homieNode.setProperty("clear").send(value);
}
bool onSetLength(const HomieRange& range, const String& value) {
strip.None();
strip.clear();
strip.show();
int newLength = value.toInt();
if (newLength > 0) {
strip.updateLength(newLength);
}
homieNode.setProperty("length").send(value);
}
void loopHandler() {
strip.Update();
}
void setup() {
Serial.begin(115200);
pinMode(PINLOW_RELAIS, OUTPUT);
digitalWrite(PINLOW_RELAIS, HIGH); // This relais: HIGH = OFF
Homie_setFirmware("rgb5m", "1.0.4");
Homie.setLoopFunction(loopHandler);
homieNode.advertiseRange("pixel", 0, NUMPIXELS - 1).settable(onSetPixel);
homieNode.advertiseRange("color", 0, 1).settable(onSetColor);
homieNode.advertise("brightness").settable(onSetBrightness);
homieNode.advertise("effect").settable(onSetEffect);
homieNode.advertise("clear").settable(onSetClear);
homieNode.advertise("length").settable(onSetLength);
homieNode.advertise("icon").settable(onSetIcon);
// Homie.disableLedFeedback();
// Homie.setResetTrigger(D0, LOW, 2000);
Homie.setup();
strip.begin();
strip.clear();
strip.setBrightness(160); // Künstliche Begrenzung, da nur 6A Netzteil
strip.show();
stopAfterCompletion = false; // Default
ArduinoOTA.setHostname("rgb5m");
ArduinoOTA.onStart([]() {
strip.None();
strip.clear();
strip.setBrightness(25);
strip.show();
});
ArduinoOTA.onEnd([]() {
strip.clear();
strip.show();
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.print(progress);
Serial.print(" ");
Serial.print(total);
Serial.print(" ");
Serial.print(NUMPIXELS * (float)progress / (float)total);
// strip.setPixelColor(total, strip.Color(255,0,0));
// strip.setPixelColor(progress / (total / NUMPIXELS), strip.Color(255, 0, 0));
for (int i = 0; i < NUMPIXELS; i++)
{
if (i > (NUMPIXELS * (float)progress / (float)total))
{
// Higher: Default to off
strip.setPixelColor(i, strip.Color(0, 0, 0));
}
else
{
// Lower/Same: Enable
strip.setPixelColor(i, strip.Color(255, 0, 0));
}
}
strip.show();
});
ArduinoOTA.begin();
}
void loop() {
Homie.loop();
ArduinoOTA.handle();
}