Esp8266-12e nodemcu disconect issue

Hello all, I’ve been making a code for my new BB8 robot (wish me luck), based around the esp8266 nodemcu dev kit ver.2. The code uses standard blynk functions, but disconnects and lags whenever it outputs to the screen, and has other performance issues. The code runs most of the time, but occasionally (every minute or so) the phone disconnects and looses connection. Any help is appreciated, thanks. @vshymanskyy

Here is the full code… ignore the commented out pieces.

/*
 *  This sketch demonstrates how to scan WiFi networks. 
 *  The API is almost the same as with the WiFi Shield library, 
 *  the most obvious difference being the different file you need to include:
 */
#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
char auth[] = "067578673a874414b78bef06883d98c2";

//char auth[] = "aa171bd062714f3c9473b186fb0c9816";
void setup()
{
static const uint8_t D0   = 16;
static const uint8_t D1   = 5;
static const uint8_t D2   = 4;
static const uint8_t D3   = 0;
static const uint8_t D4   = 2;
static const uint8_t D5   = 14;
static const uint8_t D6   = 12;
static const uint8_t D7   = 13;
static const uint8_t D8   = 15;
static const uint8_t D9   = 3;
static const uint8_t D10  = 1;
  Serial.begin(9600);
  Blynk.begin(auth, "FPR", "cutthroat");
  
  }
  int XAxis;
  int YAxis;
  int Gain;
  int MenuStatus; 
  int BootUp = 1;
  int Enable = 1;
  int EnableStatus;
  int TotalThrottle;
  long WidgetTimer1;
  int LCDEnable;
BLYNK_WRITE(V3) {
  LCDEnable = param.asInt();
}
BLYNK_WRITE(V1) {
  XAxis = param.asInt();
  
}
BLYNK_WRITE(V2) {
  YAxis = param.asInt();
  
}
//BLYNK_WRITE(V3) {
//  Gain = YAxis = param.asInt()/100;
//}
BLYNK_WRITE(V10) {
MenuStatus = param.asInt();
}

WidgetLCD lcd(V4);

long WidgetTimer = 0;
void loop()
{
  Blynk.run();

Serial.println(EnableStatus);

  
  WidgetTimer++;
  digitalWrite(D3, LOW);
  digitalWrite(D4, LOW);

  String LeftMotorDir;
  float LeftMotorPower;
  String RightMotorDir;
  float RightMotorPower;
  int LeftMotorDirOutput = D3;
  int LeftMotorPowerOutput = D1;
  int RightMotorDirOutput = D4;
  int RightMotorPowerOutput = D2;
  int LeftMotorPowerFinalOutput;
  int RightMotorPowerFinalOutput;
  int BatteryLevel;
  int BatteryVoltage;
  int BatteryVoltageInput = 700;
  String quad;
  int VDir = 1;
  int HDir = 1;
  String Shifter;

  


//Begin calculations


//Find Up and Down, Left and Right

if((YAxis >= 1) && (YAxis <= 220)){
  VDir = 1; 
}
if((YAxis <= -1) && (YAxis >= -220)){
  VDir = 0; 
}
if((XAxis >= 1) && (XAxis <= 220)){
  HDir = 1; 
}
if((XAxis <= -1) && (XAxis >= -220)){
  HDir = 0; 
}
if((XAxis == 0) && (YAxis == 0)){
  VDir = 3;
  HDir = 3;
}


//Determine Quadrent, and perform calculations


if((VDir == 1) && (HDir == 1)){
  Serial.println("Top Right");
  quad = "Top Right";
  RightMotorPower = YAxis - XAxis;
  LeftMotorPower = XAxis + YAxis;
  RightMotorDir = "Forward";
  LeftMotorDir = "Forward";
  analogWrite(D3, 255);
  analogWrite(D4, 255);
  Shifter = "Forward";
  //Top Right
}
else if((VDir == 1) && (HDir == 0)){
  Serial.println("Top Left"); 
  quad = "TopLeft";
  RightMotorPower = YAxis - XAxis;
  LeftMotorPower = YAxis + XAxis;
  RightMotorDir = "Forward";
  LeftMotorDir = "Forward";
  analogWrite(D3, 255);
  analogWrite(D4, 255);
  Shifter = "Forward";
  //Top Left
}
else if((VDir == 0) && (HDir == 1)){
  Serial.println("Bottom Right");
  quad = "Bottom Right";
  RightMotorPower = abs(YAxis) - XAxis;
  LeftMotorPower = abs(YAxis) + XAxis;
  RightMotorDir = "Backward";
  LeftMotorDir = "Backward";
  Shifter = "Reverse";
  analogWrite(D3, 0);
  analogWrite(D4, 0);
  
   //Bottom Right
}
else if((VDir == 0) && (HDir == 0)){
  Serial.println("Bottom Left");
  quad = "BottomLeft";
  RightMotorPower = abs(YAxis) - XAxis;
  LeftMotorPower = abs(YAxis) + XAxis;
  RightMotorDir = "Backward";
  LeftMotorDir = "Backward";
  analogWrite(D3, 0);
  analogWrite(D4, 0); 
  Shifter = "Reverse"; 
  //Bottom Left
}
else if((VDir == 3) && (HDir == 3)){
  Serial.println("Neutral");
  RightMotorPower = 0;
  LeftMotorPower = 0;
  Shifter = "Neutral";
}


//If Motor Throttle Exceeds Max, Set To Max

if(RightMotorPower > 244) {
  RightMotorPower = 244;
}
if(LeftMotorPower > 244){
  LeftMotorPower = 244;
}

//If Motor Throttle Is Negative, Ignore It

if(RightMotorPower < 0){
  RightMotorPower = 0;
}
if(LeftMotorPower < 0){
  LeftMotorPower = 0;
}

//Write Direction Pins

/*if(LeftMotorDir == "Backward"){
 digitalWrite(LeftMotorDirOutput, LOW);
}
if(LeftMotorDir == "Forward"){
  digitalWrite(LeftMotorDirOutput, HIGH);
}
if(RightMotorDir == "Backward"){
 digitalWrite(RightMotorDirOutput, LOW);
}
if(RightMotorDir == "Forward"){
  digitalWrite(RightMotorDirOutput, HIGH);
}
*/
//Write Throttle Values

//LeftMotorPower = LeftMotorPower * Gain;
//RightMotorPower = RightMotorPower * Gain;
LeftMotorPowerFinalOutput = (int) LeftMotorPower;
RightMotorPowerFinalOutput = (int) RightMotorPower;
  analogWrite(LeftMotorPowerOutput, (LeftMotorPowerFinalOutput));
  analogWrite(RightMotorPowerOutput, (RightMotorPowerFinalOutput));
  BatteryVoltage = BatteryVoltageInput * 0.00488758553;
  BatteryLevel = (BatteryVoltage / 3.7) * 100; 
//Serial.println("Loop End");
//Serial.println(LeftMotorPowerOutput);

Serial.println(MenuStatus);
TotalThrottle = LeftMotorPower + RightMotorPower;

if(LCDEnable == 1){
if(MenuStatus == 1){
if(WidgetTimer >= 500){
lcd.clear(); //Use it to clear the LCD Widget
  lcd.print(0, 0, "ThrottleR:"); // use: (position X: 0-15, position Y: 0-1, "Message you want to print")
   Blynk.run();
  lcd.print(0, 1, "ThrottleL:");
   Blynk.run();
  lcd.print(10, 1, (int)LeftMotorPower);
   Blynk.run();
  lcd.print(10, 0, (int)RightMotorPower);
   Blynk.run();
  WidgetTimer = 0;
  Serial.println("Writing to lcd");
  Blynk.virtualWrite(V0, BatteryLevel);
   Blynk.run();
      if(BatteryLevel <= 80){
      lcd.print(13,1, "LB");
       Blynk.run();
    }
}  
}
if(MenuStatus <= 0){
if(WidgetTimer >= 500){
    lcd.clear();
     Blynk.run();
    lcd.print(7,0, "BB8");
     Blynk.run();
    lcd.print(1,1, Shifter);
     Blynk.run();  
    WidgetTimer = 0;
    if(BatteryLevel <= 80){
      lcd.print(13,1, "LB");
       Blynk.run();
    }
    }
  }
}

  if(WidgetTimer1 >= 100){
    WidgetTimer1 = 0;
    Blynk.virtualWrite(V0, BatteryLevel);
     Blynk.run();
    Blynk.virtualWrite(V5, TotalThrottle);
     Blynk.run();
    
  }

if(BootUp == 1){
  lcd.print(0,0, "Initializing");
   Blynk.run();
  BootUp = 0;
}

Serial.println(WidgetTimer);
WidgetTimer ++;
WidgetTimer1 ++;
delay(50);
Serial.println(TotalThrottle);
}

  1. Your code is terribly optimised. Mainly pointless use of ints where you could easily go with booleans.
  2. ESP stack has no time to execute its tasks your code just kills it. Read documentation for ESP on Arduino. It need time to execute network related stuff it’s explicitly said there to use either many delay functions or yeld() which is essentially 0 delay.
  3. Lag will always be there if you communicate with Blynk cloud even if you use local server. If you want super robust and responsive communication write your own UDP based protocol with as few components as you need. However in my opinion if you know how to use Blynk with ESP in right way it’s more than sufficient.
  4. When I look at your code I find even more problems with it like calculating battery voltage when you multiply int by float or later divide int by float. If you manage to fix all the problems with your code, check if you are not flooding Blynk cloud with to many requests.

Why are you calling Blynk.run all the times?

I saw a post on this forum of where someone was having similar issues, and one post was to put blank.run after each blynk command. It was just a test, since this post I have removed it. Thanks for the quick replies.

I’m aware of the code optimization, this was a first draft, and ill try to fix the calculation issues. How much time would you recommend to give the esp after network related commands, the esp never crashes since the serial monitor goes uninterrupted, the problem is just the esp disconnecting. Regardless, ill try to fix the issues you mentioned and get back here.

Thanks for the help.