Thank you for spending so much time trying to solve my annoying problem.
Here is a stripped version of my code. The 2 functions fadeTo and fadeTo2 are working well without WiFi.
When I WiFi.begin() is enabled the flickering begins.
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <FastLED.h>
IPAddress ip(192, 168, 0, 139);
/*
Gateway
Subnet
DNS
*/
bool updateFadeColors1 = false;
bool updateFadeColors2 = false;
#define NUM_LEDS 60
#define DATA_PIN 2 // 2 er D4 på NodeMCU
byte fadeRed = 255;
byte fadeGreen = 0;
byte fadeBlue = 0;
byte fadeRed2 = 0;
byte fadeGreen2 = 255;
byte fadeBlue2 = 0;
byte fadeAmount = 1;
byte fadeDelay = 100;
WiFiClient espClient;
PubSubClient client(espClient);
void setup() {
Serial.begin(115200);
FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip ); //Typical8mmPixel); // ( TypicalSMD5050 );
setup_wifi();
client.setServer(mqtt_server, mqtt_port);
client.setCallback(callback);
if (!client.connected()) {
reconnect();
}
setBright(brightness);
setAll(0,0,255);
delay(3000);
}
/******************************* START SETUP WIFI**************************************/
void setup_wifi() {
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.hostname(deviceName);
WiFi.setSleepMode(WIFI_LIGHT_SLEEP);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
WiFi.config(ip, gateway, subnet, dns);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {
if (!client.connected()) {
reconnect();
}
if (WiFi.status() != WL_CONNECTED) {
delay(1);
Serial.print("WIFI Disconnected. Attempting reconnection.");
setup_wifi();
return;
}
client.loop();
// Set true in callback split function
if (updateFadeColors1 == true) {
fadeTo(fadeRed, fadeGreen, fadeBlue, fadeDelay, fadeAmount);
} else if (updateFadeColors2 == true) {
fadeTo2(fadeRed, fadeGreen, fadeBlue, fadeRed2, fadeGreen2, fadeBlue2, fadeDelay, fadeAmount);
}
}
void reconnect() {
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
if (client.connect(deviceName, mqtt_username, mqtt_password)) {
Serial.println("connected");
di = "modelbane/neopixels/" + neoName;
di.toCharArray(diSubscribe, 60);
client.subscribe(diSubscribe);
delay(5000);
}
}
}
void callback(char* topic, byte* payload, unsigned int length) {
// Split received mqtt msg
}
// Helper function that blends one uint8_t toward another by a given amount
void nblendU8TowardU8( uint8_t& cur, const uint8_t target, uint8_t amount)
{
if( cur == target) return;
if( cur < target ) {
uint8_t delta = target - cur;
delta = scale8_video( delta, amount);
cur += delta;
} else {
uint8_t delta = cur - target;
delta = scale8_video( delta, amount);
cur -= delta;
}
}
CRGB fadeOneColor( CRGB& cur, const CRGB& target, uint8_t amount)
{
nblendU8TowardU8( cur.red, target.red, amount);
nblendU8TowardU8( cur.green, target.green, amount);
nblendU8TowardU8( cur.blue, target.blue, amount);
return cur;
}
void fadeTowardColor( CRGB* L, uint16_t N, const CRGB& bgColor, uint8_t fadeAmount)
{
fadeOneColor( L[0], bgColor, fadeAmount); // first LED - calc new color
if ((bgColor == L[0])) {
updateFadeColors1 = false;
}
for( uint16_t i = 0; i < N; i++) {
L[i] = L[0]; // copy color from first LED
}
}
void fadeTowardColor2( CRGB* L, uint16_t N, const CRGB& bgColor, const CRGB& bgColor2, uint8_t fadeAmount)
{
fadeOneColor( L[0], bgColor, fadeAmount); // Calc new color first LED
fadeOneColor( L[1], bgColor2, fadeAmount); // and second LED
if ((bgColor == L[0]) && (bgColor2 == L[1])) {
updateFadeColors2 = false;
}
for( uint16_t i = 0; i < N; i++) {
if ((i % 2) == 0) {
L[i] = L[0]; // copy 1st LED color
} else {
L[i] = L[1]; // copy seconde LED color
}
}
}
void fadeTo(byte toRed, byte toGreen, byte toBlue, int fadeDelay, byte fadeAmount) { //byte red, byte green, byte blue, byte toRed, byte toGreen, byte toBlue, int fadeDelay, byte fadeAmount) {
// Syntax: toRed, toGreen, toBlue, fade delay, how much to fade)
CRGB bgColor(toRed, toGreen , toBlue);
fadeTowardColor( leds, NUM_LEDS, bgColor, fadeAmount);
FastLED.show();
FastLED.delay(fadeDelay);
}
void fadeTo2(byte toRed, byte toGreen, byte toBlue, byte toRed2, byte toGreen2, byte toBlue2, int fadeDelay, byte fadeAmount) { //byte red, byte green, byte blue, byte toRed, byte toGreen, byte toBlue, int fadeDelay, byte fadeAmount) {
// Syntax: toRed, toGreen, toBlue, toRed2, toGreen2, toBlue2, fade delay, how much to fade)
CRGB bgColor(toRed, toGreen , toBlue);
CRGB bgColor2(toRed2, toGreen2, toBlue2);
fadeTowardColor2( leds, NUM_LEDS, bgColor, bgColor2, fadeAmount);
FastLED.show();
FastLED.delay(fadeDelay);
}