FastLED crashes Blynk when using Arduino and Esp8266 Wifi Module

Hello Blynk Community. I am trying to use an Arduino Uno and Esp8266 wifi module to control a WS2812 LED strip using Blynk via WiFi. I am using the software serial port to enable WiFi communication between my project and Blynk. Heres a sample of the code I am working with:

// Hardware Serial on Mega, Leonardo, Micro...
//#define EspSerial Serial1

// or Software Serial on Uno, Nano...
#include <SoftwareSerial.h>
SoftwareSerial EspSerial(12, 13); // RX, TX

// Your ESP8266 baud rate:
#define ESP8266_BAUD 9600

ESP8266 wifi(&EspSerial);



void setup() {
  
  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  delay(10);
  Serial.begin(9600);
  Serial.println(("Initialising FastLED"));
  FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);              //***SEEMS TO BE A PROBLEM HERE. WITHOUT THIS LINE OF CODE, THE CONNECTION IS ESTABLISHED***
  Serial.println(("Establishing connections..."));
  Blynk.begin(auth, wifi, ssid1, pass1);
  pinMode(10,OUTPUT);
  Serial.println(("WS2812 RGB LED strip test: Functioning of all preset light settings with\nbrightness and colour selection capabilities"));
  timer1.setInterval(300, MainFunction);
  
}

The program freezes during the execution of void setup, specially as soon as the FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS); is reached.
I have tested this code segment with this line commented out, then the program does not freeze. I have also tested this program by commenting out the Blynk.begin() (whilst keeping the fastLED setup code) setup code and the program also does not freeze. I have also run this program on an arduino mega, without removing either of the above mentioned 2 lines, using its hardware serial port and found the program runs perfectly without freezing. I am not sure how but somewhere along the lines the FastLED setup code and Blynk begin code is interfering with each other when a WiFI connection is initiated using the Uno’s software serial. If someone has experienced an issue like this or has any idea how i can tackle this issue, please share your advice. Help would be much appreciated

The full code might be more useful.

Pete.

Hello @PeteKnight
I have reduced my code to the bare-bones in order to test Blynk connection to my hardware. I did not think the rest of the code would be of consequence and hence omitted it. Here is the bare-bones program that I am testing:

#include <Blynk.h>
//Suggested to resolve issue by adding these 2 lines from Blynk community: https://community.blynk.cc/t/using-esp32-with-blynk-and-fastled-disabled-interrupts/18654
//#define FASTLED_ALLOW_INTERRUPTS 0
//#define FASTLED_INTERRUPT_RETRY_COUNT 1
#include <FastLED.h>
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#define LED_PIN     10
#define NUM_LEDS    30

CRGB leds[NUM_LEDS];

BlynkTimer timer1;

byte SwitchLED=0;
void MainFunction();

char auth[]="XXXXXXXXXXXXXXXXX"

//Network credentials
char ssid1[] = "XXXXXXXXXXX";
char pass1[] = "XXXXXXXXXX";

// Hardware Serial on Mega, Leonardo, Micro...
//#define EspSerial Serial1

// or Software Serial on Uno, Nano...
#include <SoftwareSerial.h>
SoftwareSerial EspSerial(12, 13); // RX, TX

//ESP8266 baud rate:
#define ESP8266_BAUD 9600

ESP8266 wifi(&EspSerial);


void setup() {
  
  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  delay(10);
  Serial.begin(9600);
  Serial.println(("Initialising FastLED"));
  FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);              
  Serial.println(("Establishing connections..."));
  Blynk.begin(auth, wifi, ssid1, pass1);
  pinMode(11,OUTPUT);                          //Connected to a hardware LED 
  Serial.println(("WS2812 RGB LED strip test: Functioning of all preset light settings 
  with\nbrightness and colour selection capabilities"));
  timer1.setInterval(300L, MainFunction);
  
}

void loop()
{
  Blynk.run();
  timer1.run();

}

void MainFunction(){

  Serial.println("Main function executed");
  
}

BLYNK_WRITE(V9){                       

    SwitchLED=param.asInt();
    
    if(SwitchLED==1){
      
      Serial.println("Push Button switched On");
      digitalWrite(11, HIGH);
      
    }
    else if(SwitchLED==0){

      Serial.println("Push Button switched Off");
      digitalWrite(11, LOW);
      
    }
    
} 

I have also edited my initial statements, by adding very little to the explanations i had written before, with the intention that might be understood clearer.

Update

I have tested the program above with an Arduino Mega and using the same software serial port and everything works perfectly. Also something strange is happening, the program above is running the Arduino Uno when both the timer1.setInterval(300L, MainFunction); and FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS); are removed. Cant see why this would cause the program to freeze, especially if it works perfectly on the Arduino Mega. :confused:

When you compile it for the Uno, how much memory is remaining?

Pete.

This is all the memory that i am using according to the IDE compiler:

Sketch uses 17922 bytes (55%) of program storage space. Maximum is 32256 bytes.
Global variables use 1376 bytes (67%) of dynamic memory, leaving 672 bytes for local variables. Maximum is 2048 bytes.```

And what is the result (in terms of stability) if you use the F macro in your serial print statements…

  Serial.println(F("Initialising FastLED"));

Pete

I do not have many serial prints in this bare bones test program, but i have used this macro on my actual program over a bluetooth communication medium and found the results were quite stable. Well as stable as a bluetooth connection can be. And since that program uses more dynamic and program storage space I figured the amount of dynamic and program storage used by this test program would be of no consequence for stable operation of the arduino.

I concede Pete. The program storage space increased but the dynamic memory was reduced by 4%, which it seems was the amount of space that the arduino uno needed in order to run the program without freezing. Thank you Pete. Problem appears to be fixed.:+1::slightly_smiling_face:

Well @Pete i may have spoken too soon, my orignal program seems to require more memory then the Uno is comfortable with. The software serial actually uses up quite a chunk of memory as well:

Sketch uses 27142 bytes (84%) of program storage space. Maximum is 32256 bytes.
Global variables use 1547 bytes (75%) of dynamic memory, leaving 501 bytes for local variables. Maximum is 2048 bytes.

However this is an issue for another forum. I came across some techniques to minimise dynamic memory. Will investigate that and check the arduino forums. Thanks for your assistance Pete

1 Like

Have you considered dumping the big, under-powered and under-resourced Arduino boards and going for something smaller, cheaper, faster and with built-in Wi-Fi?

Pete.

1 Like