#include <Adafruit_NeoPixel.h>
#include <PubSubClient.h>
#include <ESP8266WiFi.h>
int col[12];
char str[12] = "0,0,0";
char on = '0';
char brit[3];
unsigned int mil = 0;
const char* mqtt_server = "*";
const int mqtt_port = *;
const char* mqtt_user = "*";
const char* mqtt_password = "*";
const String color = "svt/lenta/col";
const String britness = "svt/lenta/brit";
const String ont = "svt/lenta/on";
WiFiClient espClient;
PubSubClient client(espClient);
Adafruit_NeoPixel pixels(151, 0, NEO_GRB + NEO_KHZ800);
void setup() {
wifi();
client.setServer(mqtt_server, mqtt_port);
client.setCallback(callback);
pixels.begin();
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
if (on == '1') {
pixels.setBrightness(atoi(brit));
for (int i = 0; i < 151; i++) {
pixels.setPixelColor(i, pixels.Color(col[0],col[1],col[2]));
}
} else {
pixels.clear();
}
pixels.show();
}
void pars() {
int count = 0;
char* offset = str;
while (true) {
col[count++] = atoi(offset);
offset = strchr(offset, ',');
if (offset) offset++;
else break;
}
}
void wifi() {
delay(10);
int cout = 0;
WiFi.mode(WIFI_STA);
WiFi.begin("*", "*");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
cout++;
if (cout == 30) {
while (true) {
}
}
}
randomSeed(micros());
}
void callback(char* topic, byte* payload, unsigned int length) {
if (String(topic) == color) {
for (int x = 0; x < sizeof(str) / sizeof(str[0]); x++) {
str[x] = char(0);
}
for (int i = 0; i < length; i++) {
str[i] = char(payload[i]);
}
pars();
} else if (String(topic) == britness) {
for (int o = 0; o < sizeof(brit) / sizeof(brit[0]); o++) {
brit[o] = char(0);
}
for (int y = 0; y < length; y++) {
brit[y] = char(payload[y]);
}
} else if (String(topic) == ont) {
on = char(payload[0]);
}
}
void reconnect() {
while (!client.connected()) {
String clientId = "ESP8266-" + WiFi.macAddress();
if (client.connect(clientId.c_str(), mqtt_user, mqtt_password)) {
client.subscribe((color + "/#").c_str());
client.subscribe((britness + "/#").c_str());
client.subscribe((ont + "/#").c_str());
} else {
delay(5000);
}
}
}