Hey guys.
After setting up my led strip with a simple sketch, everything is fine, but as soon as I upload a more complex sketch with networking and MQTT, this weird blinking is starting to show. (Please see video at: https://www.reddit.com/r/arduino/comments/cgj8uo/led_strip_blinking/ )
I have a WS2811 strip connected to a NodeMCU(Amica).
I am using a notebook charger as my power supply for the strip and the nodemcu. Everything is taken from BruhAutomation's guide (GitHub - bruhautomation/ESP-MQTT-JSON-Digital-LEDs: (OBSOLETE) ESP8266 MQTT JSON Digital LEDs for Home Assistant), same wiring and all.
Mosquitto server running on Raspberry pi 3.
First simple sketch, with this one everything works as expected.
#include <FastLED.h>
#define LED_PIN 5
#define NUM_LEDS 100
#define LED_TYPE WS2811
#define COLOR_ORDER BRG
CRGB leds[NUM_LEDS];
bool stateOn = false;
void setup() {
FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);
}
void loop() {
if(stateOn)
{
leds[0] = CRGB::Red;
FastLED.show();
delay(10000);
stateOn = false;
}
else
{
leds[0] = CRGB::Black;
FastLED.show();
delay(1000);
stateOn=true;
}
}
Second one is a more complex one and the blinking start showing.
#include <FastLED.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
/*** FastLED ***/
#define LED_PIN 5
#define NUM_LEDS 100
#define BRIGHTNESS 64
#define LED_TYPE WS2811
#define COLOR_ORDER BRG
CRGB leds[NUM_LEDS];
byte red = 255;
byte green = 255;
byte blue = 255;
byte brightness = 255;
/*** WIFI ***/
const char* ssid = "";
const char* password = "";
/*** MQTT ***/
const char* mqtt_server = "";
const char* mqtt_username = "";
const char* mqtt_password = "";
const int mqtt_port = 1883;
const char* light_state_topic = "esp/state";
const char* light_set_topic = "esp/set";
const char* on_cmd = "ON";
const char* off_cmd = "OFF";
const char* effect = "solid";
/*** JSON ***/
const int BUFFER_SIZE = JSON_OBJECT_SIZE(10);
#define MQTT_MAX_PACKET_SIZE 512
/*** Globals ***/
bool stateOn = false;
String effectString = "solid";
WiFiClient espClient;
PubSubClient client(espClient);
void setup_wifi()
{
WiFi.begin(ssid, password);
while(WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("Wifi connected.");
Serial.println();
}
void reconnect()
{
Serial.println("Connecting to MQTT..");
while(!client.connected())
{
Serial.print("Attempting MQTT connection..");
if(client.connect("ESP", mqtt_username, mqtt_password))
{
Serial.println("Connected.");
client.subscribe("esp/set");
}
else
{
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}
void setup()
{
Serial.begin(9600);
FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);
setup_wifi();
client.setServer(mqtt_server, mqtt_port);
client.setCallback(callback);
Serial.println("Ready.");
Serial.println("IP Address: ");
Serial.print(WiFi.localIP());
}
void callback(char* topic, byte* payload, unsigned int length)
{
Serial.println("Message arrived [");
Serial.print(topic);
Serial.print("]");
char message[length + 1];
for (int i = 0; i<length; i++)
{
message[i] = (char)payload[i];
}
message[length] = '\0';
Serial.println(message);
if(!processJson(message))
{
return;
}
if(stateOn)
{
Serial.println("STATE IS ON");
}
else
{
Serial.println("STATE IS OFF");
}
sendState();
}
bool processJson(char* message)
{
StaticJsonBuffer<BUFFER_SIZE> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(message);
if(!root.success())
{
Serial.println("parseObject() failed");
return false;
}
else
{
Serial.println("Successfully parsed");
}
if(root.containsKey("state"))
{
if(strcmp(root["state"], on_cmd) == 0)
{
stateOn = true;
}
else if(strcmp(root["state"], off_cmd) == 0)
{
stateOn = false;
}
}
if(root.containsKey("effect"))
{
effect = root["effect"];
effectString = effect;
}
if(root.containsKey("color"))
{
red = root["color"]["r"];
green = root["color"]["g"];
blue = root["color"]["b"];
}
if(root.containsKey("brightness"))
{
brightness = root["brightness"];
}
return true;
}
void sendState()
{
StaticJsonBuffer<BUFFER_SIZE> jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
root["state"] = (stateOn) ? on_cmd : off_cmd;
JsonObject& color = root.createNestedObject("color");
color["r"] = red;
color["g"] = green;
color["b"] = blue;
root["brightness"] = brightness;
root["effect"] = effectString.c_str();
char buffer[root.measureLength() + 1];
root.printTo(buffer, sizeof(buffer));
//client.publish(light_set_topic, buffer, true);
}
void loop()
{/*
for(int dot = 0; dot < NUM_LEDS; dot++){
leds[dot] = CRGB::Blue;
FastLED.show();
leds[dot] = CRGB::Black;
delay(30);
}*/
if(!client.connected())
{
reconnect();
}
if(WiFi.status() != WL_CONNECTED)
{
delay(1);
Serial.print("WIFI disconnected. Attempting reconnection.");
setup_wifi();
return;
}
client.loop();
if(!stateOn)
{
leds[0] = CRGB::Black;
FastLED.show();
}
else
{
leds[0] = CRGB::Red;
FastLED.show();
}
}
After setting the state to on, the led lights up as expected, but it also starts to blink.
I also tried a third sketch, from from BruhAutomation's git (Linked above), thought I was doing something wrong, so I took his code, but the blinking still remains.
I tried the same things with 2 different setups, different nodeMCUs, different led strips, so I don't think it is hardware related. Could you guys please point out what's wrong? I'm at a loss.
