if interested, I can give an example
UPD:
fast, dirty code, I think it will start;)
turn on, turn off, change the mode, ota (fill in your access point and mosquito credentials and the number of LEDs in the tape and the pin number to which the LED ribbon is connected):
#include <WS2812FX.h>
#include <ArduinoOTA.h>
#include <PubSubClient.h>
WiFiClient eClient;
#define mqtt_server "192.168.1.14"
#define mqtt_port 1983
#define mqtt_client "ws2812"
#define mqtt_user "MQTT-login"
#define mqtt_password "MQTT-password"
#define will_topic "state/ws2812/LWT"
#define will_message "Offline"
#define lwt_msg "Online"
#define subscribe_topic "cmd/ws2812/#"
#define will_QoS 0
#define will_retain true
#define LED_PIN 12
#define LED_COUNT 140
#define WIFI_SSID "myAP"
#define WIFI_PASSWORD "pin-ap"
#define host_name "led-fx"
int fx_mode = 7;
unsigned long lastTime = millis();
WS2812FX ws2812fx = WS2812FX(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
void callback(char*topic, byte* payload, unsigned int length);
PubSubClient client(mqtt_server, mqtt_port, callback, eClient);
void reconnect() {
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
if (client.connect(mqtt_client, mqtt_user, mqtt_password, will_topic, will_QoS, will_retain, will_message)) {
Serial.println("connected");
client.publish(will_topic, lwt_msg, will_retain);
client.subscribe(subscribe_topic);
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
String mqtt_data = "";
for (int i = 0; i < length; i++) {
mqtt_data += (char)payload[i];
}
Serial.println(mqtt_data);
if (mqtt_data == "ON") {
ws2812fx.start();
} else if (mqtt_data == "OFF") {
ws2812fx.stop();
} else {
fx_mode = mqtt_data.toInt();
ws2812fx.setSegment(0, 0, LED_COUNT - 1, fx_mode, (const uint32_t[]) {0xff0000, 0x000000, 0x000000}, 240, false);
}
}
void setup() {
Serial.begin(115200);
Serial.println("\r\n");
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
WiFi.hostname(host_name);
WiFi.mode(WIFI_STA);
Serial.print("Connecting to " WIFI_SSID);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.print("\nServer IP is ");
Serial.println(WiFi.localIP());
ArduinoOTA.setHostname(host_name);
ArduinoOTA.onStart([]() {
Serial.println("OTA start");
});
ArduinoOTA.onEnd([]() {
Serial.println("\nOTA end");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
});
ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
else if (error == OTA_END_ERROR) Serial.println("End Failed");
});
ArduinoOTA.begin();
ws2812fx.init();
ws2812fx.setBrightness(127);
ws2812fx.start();
ws2812fx.setSegment(0, 0, LED_COUNT - 1, fx_mode, (const uint32_t[]) {0xff0000, 0x000000, 0x000000}, 240, false);
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
ArduinoOTA.handle();
ws2812fx.service();
}
Switch ws2812_power "power [%s]" { mqtt=">[mqtt-loc:cmd/ws2812/power:command:*:default]" }
Number ws2812_mode "mode [%d]" { mqtt=">[mqtt-loc:cmd/ws2812/mode:command:*:default]" }
sitemap test label="ws2812"
{
Frame {
Switch item=ws2812_power
Setpoint item=ws2812_mode step=1 minValue=0 maxValue=55
}
}