[SOLVED]Esp8266 crashes

Hey Blynkers,
I need help with this project so the aim was the project was to

  1. Control RGB led strip with zeRGBa Widget and Alexa voice commands
  2. I want the colour change to be smooth and not sudden.
    so I did write a code for the same and I am not a good coder at all so I need some help.

THE PROBLEM
the problem is that whenever I change the colour, The ESP8266 just crashes and boots up with the new colour
PLEASE NOTE THAT I AM USING BELKIN WEMO EMULATER ,WIFI MANAGER AND ARDUINO OTA.

not quite sure as to what went wrong
i know that i have modified the code quite a bit but when i use the same code to controll relays, it works fine.

i know these external library might be a problem so instead of saying the “external modifications are a problem” please take some time and explain me how in detail

PLEASE HELP ME OUT GUYS

okay so i fixed the problem by removing the while loop but i still want to change the light colours smoothly. so now the question is what loop should i use in order to chang the colour smoothly and without crashing the ESP

THE CODE

 #include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <WiFiManager.h> 
#include <ESP8266WebServer.h>
#include <BlynkSimpleEsp8266.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <SimpleTimer.h>
#include "WemoSwitch.h"
#include "WemoManager.h"
#include "CallbackFunction.h"

#define VPIN V1  //Use a unique virtual pin for each device using the same token / dashboard

char auth[] = "AUTH"; //Get token from Blynk

//Colour callbacks

WemoManager wemoManager;
WemoSwitch *light = NULL;
WemoSwitch *light1 = NULL;
WemoSwitch *light2 = NULL;
WemoSwitch *light3 = NULL;
WemoSwitch *light4 = NULL;
WemoSwitch *light5 = NULL;
WemoSwitch *light6 = NULL;

int BlynkR,BlynkG,BlynkB;
int redPin = 12;
int greenPin = 14;
int bluePin = 13;
int r = 0;
int g = 0;
int b = 0;
SimpleTimer timer;

void setup()      
{
  Serial.begin(115200);

  WiFiManager wifi;   //WiFiManager intialization.
  wifi.autoConnect("RGB-LED STRIP"); //Create AP, if necessary

  wemoManager.begin();
  // Format: Alexa invocation name, local port no, on callback, off callback
  light = new WemoSwitch("Bed Lights",1000, LightOn, LightOff);
  light1 = new WemoSwitch("Blue Light",1001, BlueOn, BlueOff);
  light2 = new WemoSwitch("Green Light",1002, GreenOn, GreenOff);
  light3 = new WemoSwitch("Red Light",1003, RedOn, RedOff);
  light4 = new WemoSwitch("Pink Light",1004, PinkOn, PinkOff);
  light5 = new WemoSwitch("Purple Light",1005, PurpleOn, PurpleOff);
  light6 = new WemoSwitch("Teal Light",1006, TealOn, TealOff);
  wemoManager.addDevice(*light);
  wemoManager.addDevice(*light1);
  wemoManager.addDevice(*light2);
  wemoManager.addDevice(*light3);
  wemoManager.addDevice(*light4);
  wemoManager.addDevice(*light5);
  wemoManager.addDevice(*light6);
  Blynk.config(auth);
  ArduinoOTA.begin();
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);

}

void loop()
{
  wemoManager.serverLoop();
  Blynk.run();
  ArduinoOTA.handle();
  timer.run();
}
BLYNK_WRITE(VPIN) // zeRGBa assigned to V1 
{
    // get a RED channel value
     BlynkR = param[0].asInt();
    // get a GREEN channel value
     BlynkG = param[1].asInt();
    // get a BLUE channel value
     BlynkB = param[2].asInt();
     setColor(BlynkR, BlynkG, BlynkB);
}

void LightOn(){
  setColor(1023, 1023, 1023);
}

void LightOff(){
  setColor(0, 0, 0);
}

void BlueOn(){
  setColor(0, 0, 1023);
}

void BlueOff(){
  setColor(0, 0, 0);
}

void GreenOn(){
  setColor(0, 1023, 0);
}

void GreenOff(){
  setColor(0, 0, 0);
}

void RedOn(){
  setColor(1023, 0, 0);
}

void RedOff(){
  setColor(0, 0, 0);
}

void PinkOn(){
  setColor(1023, 0, 800);
}

void PinkOff(){
  setColor(0, 0, 0);
}

void PurpleOn(){
    setColor(465, 0, 750);
}

void PurpleOff(){
  setColor(0, 0, 0);  
}
void TealOn(){
    setColor(0, 718, 400);
}

void TealOff(){
  setColor(0, 0, 0);  
}

                              
void setColor(int red, int green, int blue) {
  while ( r != red || g != green || b != blue ) {
    if ( r < red ) r += 5;
    if ( r > red ) r -= 5;

    if ( g < green ) g += 5;
    if ( g > green ) g -= 5;

    if ( b < blue ) b += 5;
    if ( b > blue ) b -= 5;

    _setColor();

  }
}
void _setColor() {
  analogWrite(redPin, r);
  analogWrite(greenPin, g);
  analogWrite(bluePin, b);

you mention a while loop wich caused the crash. This leads me to believe that you either got a time out on the ESP watchdog HW or SW or the heartbeat of the Blynk software. All can be cause for a reboot (as they think the ESP is ‘stuck in a loop’).

what you can try is instead of a while loop use a simpleTimer timer which is called e.g. every 50 ms. that timer can call the _setColor() function which keeps updating until it reaches the correct value. then you can destruct/delete the timer. This should allow all the WTD timers to reset in between the function calls.

http://playground.arduino.cc/Code/SimpleTimer

Are you using MOSFETs on the output of your digital pins?

Pete.

Yes

yes

it would be really helpful if you could just write the a sample code.

I had a look at your code, but that’s quite a bit of work. (which im not gonna do).
I can give you the outline though of what you need to do.

  1. create 6 global variables:
currentR, targetR,
currentG, targetG,
currentB, targetB,
  1. rewrite these functions:
void TealOn(){
    setColor(0, 718, 400);
}

into

void TealOn(){
targetR = 0;
targetG = 718
targetB = 400
}
  1. this routine:
    void setColor(int red, int green, int blue) {
    becomes
    void setColor() {
    and does the same thing BUT with the new variables AND without the while.
  2. the tricky part is instantiating a timer and using it. I don’t know it by heart so here in speudo again

EDIT: replace timer with blynktimer

//constructor, somewhere in your main
BlynkTimer timer;
//initiating (which would happen AFTER you set the targetColors)
timerId = timer.setInterval(100, setColor); //=every 100ms or 0.1s, increase/decrease to your liking
//finishing (is called when all colors have reached the targetColor)
timer.deleteTimer(int timerId) //else the setColor keeps running pointlessly
//after everything is done set the 
currentColors = targetColors

Don’t bother loading in a timer library, Blynk now has that built in… just use BlynkTimer timer;

good to know. I presume it uses the same functions?

yes

A common cause of ESP8266 crashes is the use of the ZeRGBa widget in merge mode because now and then, wrong parameters are being received. In case e.g. only 2 parameters are received, this will lead to a crash reset of the ESP8266 in param[2].asInt() because of a NULL-pointer issue.

It is easy to fix, by doing a check if there are 3 parameters and all within the pwm-range .

BLYNK_WRITE(VPIN) // zeRGBa assigned to V1 
{
  BlynkParam::iterator param0 = param[0];
  BlynkParam::iterator param1 = param[1];
  BlynkParam::iterator param2 = param[2];

  if (!param0.isValid() || !param1.isValid() || !param2.isValid())
  {
    return;
  }

  int r = param0.asInt();
  int g = param1.asInt();
  int b = param2.asInt();

  if ((r < 0) || (r > PWMRANGE) || (g < 0) || (g > PWMRANGE) || (b < 0) || (b > PWMRANGE))
  {
    return;
  }
  else
  {
    BlynkR = r;
    BlynkG = g;
    BlynkB = b;
    setColor(BlynkR, BlynkG, BlynkB);
  }
}

But I doubt if this will solve your issue, because in your description it seems that it is always crashing. The fix I present is for the case of rare crashes e.g. if you use the ZeRGBa widget like a 5 year old kid trying to move it as fast as possible during 5 minutes :wink: It will also solve the issue that the ESP8266 crashes when you accidently forget to use the Merge-mode of ZeRGBa.

First time I have ever heard of this “common issue?” :thinking: and the zeRGBa was one of the first widgets I started messing around with . I still extensively use zeRGBa in merge mode without any issues (or fancy checks required), Arduino, ESP8266, ESP32… it never sends a NULL, all three values are sent even if one is always 0.

@Sai_Khurana Now with that wemoManager in the void Loop() trying to run thousands of times a second… that is not good.

Yes, use timer functions instead :stuck_out_tongue_winking_eye:

Timer Function Works Great And Wemo Manager is not a problem and even if i just spam the zeRGBa Widget. it’s pretty stable.

So this topic is solved then?

yes