Perfectly worked with BLYNK legacy, facing issues in BLYNK 2.0

Hey, I’ve been a blynk user for a long time now and have tried out multiple projects involving IoT on the platform. Recently I have been working on this project to control 2 servos using PWN with blynk UI and it was working perfectly fine with Blynk legacy app. After the recent porting to blynk 2.0, I’ve been facing this issue of heavy delay at times when writing to the microcontroller, and generally it struggles to write to the servo.

What’s interesting is that, I’m able to write to one Servo with no issue whereas the other one struggles to be written to*. I’m using the same business code (to control the servos) that I used for Blynk legacy and when i switched back to legacy to check if I faced the same issue (to pin-point if it was a problem with the micro-controller/hardware), I found no issues and the system worked perfectly fine.

*I switched the servos to check if it was a problem with the servo, but now the servo1 that was working stopped and the other one worked properly (so, not a servo problem). Suspected some sort of issue with the ESP32 pinOuts (servoPin1 - D12, servoPin - D13), I changed the GPIOs to a completely different set, but am facing the same issue.Also switched the pinOuts define in the code (servoPin1 - D13, servoPin2 - D12) and interestingly, servo2 starts working fine and servo1 doesnt (therefore, not an issue with the pinOut of ESP32).

I’ve narrowed down the issue to be something on the software side maybe my code, but again the same code works perfectly fine for Blynk legacy. Hence it leaves out the only factor that has changed in the whole process, Blynk 2.0. I need some help from the blynk staff and some sort of 3rd person perspective to this problem and hopefully someone can help me figure out the issue here :slight_smile:

Some snips of the code,

[Unformatted code removed by moderator]

@Kabilan please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```

Pete.

Have you tried to use your code with blynk iot without using blynk edgent ?

I just converted my code from Legacy to Blynk 2.0… I did not use all the “includes.h” available in the BlynkEdgent example. Basically I kept my code clean from BlynkEdgent.h and it’s sub-includes. Manual connecction to blynk.clould worked well for me (and by doing this way my code now is very similar to the previous one using Legacy UI).

Correct, you can use blynk legacy sketch with blynk iot just by adding #define BLYNK_TEMPLATE_ID “YourTemplateID” in the beginning of the sketch.

https://docs.blynk.io/en/blynk-1.0-and-2.0-comparison/migrate-from-1.0-to-2.0

1 Like

Ah Right, Okay. For some reason, I can’t edit the original post, I’ll just add some snips of the code here…

void setup() {
Serial.begin(115200);
BlynkEdgent.begin();
myServo1.attach(servo1);
myServo2.attach(servo2);
myServo1.write(x);
myServo2.write(y);
timer1.setInterval(30, Servowrite);
}

void Servowrite() {

if(left==1){

if (x>=angleMin_horPlane && x<=(angleMax_horPlane-stepAngle)){
  x+=stepAngle;
}
else{
  x=angleMax_horPlane;
}
myServo2.write(x);
}
if(right==1){

if (x>=(angleMin_horPlane+stepAngle)&&x<=angleMax_horPlane){
  x-=stepAngle;
}
else{
  x=angleMin_horPlane;
}
myServo2.write(x);
}
if(up==1){

if (y>=angleMin_camServo && y<=(angleMax_camServo-stepAngle)){
  y+=stepAngle;
}
else{
  y=angleMax_camServo;
}
myServo1.write(y);
}
if(down==1){

if (y>=(angleMin_camServo+stepAngle)&&y<=angleMax_camServo){
  y-=stepAngle;
}
else{
  y=angleMin_camServo;
}
myServo1.write(y);
}

Serial.print("X = “);
Serial.print(x);
Serial.print(”;Y = ");
Serial.println(y);
}

Nope, how do we do it though?

Ah Okay, here’s the thing though, I need to be able to connect to any wifi network on the go and not hard-code the credentials and auth token into the ESP32. If I remove the other libraries, would that not take away the option of connecting to any wifi by creating a temporary access point using the ESP : (

Snippets of code aren’t enough for us to help diagnose the issue that you’ve described.

Pete.

I need to be able to connect to any wifi network without needing to hard code the creds. So I need Blynk edgent to allow it right? : (

Alright, I’ll Combombulate and add it…

Here’s the whole business code,

#include <ESP32Servo.h>

#define servo1 13
#define servo2 14

Servo myServo1;
Servo myServo2;
BlynkTimer timer1;

int A_1= 0,A_2 = 0,B_1 = 0,B_2 = 0,C_1 = 0,C_2 = 0,D_1 = 0,D_2 = 0;
int up = 0,down = 0,left = 0,right = 0;
int x = 90;
int y = 90;

//Servo1
int angleMax_camServo = 225;  //225
int angleMin_camServo = 60;   //60

//Servo2
int angleMax_horPlane = 200;  //200
int angleMin_horPlane = 0;    //0

int stepAngle = 1;

int x_default = 90; 
int y_default = 90;

void setup() {
  Serial.begin(115200);                     
  BlynkEdgent.begin();            
  myServo1.attach(servo1);
  myServo2.attach(servo2);
  myServo1.write(x);
  myServo2.write(y);
  timer1.setInterval(30, Servowrite); 
}

void Servowrite() {
  
  if(left==1){
    
    if (x>=angleMin_horPlane && x<=(angleMax_horPlane-stepAngle)){
      x+=stepAngle;
    }
    else{
      x=angleMax_horPlane;
    }
    myServo2.write(x);
  }
  if(right==1){
    
    if (x>=(angleMin_horPlane+stepAngle)&&x<=angleMax_horPlane){
      x-=stepAngle;
    }
    else{
      x=angleMin_horPlane;
    }
    myServo2.write(x);
  }
  if(up==1){
    
    if (y>=angleMin_camServo && y<=(angleMax_camServo-stepAngle)){
      y+=stepAngle;
    }
    else{
      y=angleMax_camServo;
    }
    myServo1.write(y);
  }
  if(down==1){
    
    if (y>=(angleMin_camServo+stepAngle)&&y<=angleMax_camServo){
      y-=stepAngle;
    }
    else{
      y=angleMin_camServo;
    }
    myServo1.write(y);
  }
  if(reset==1){
   myServo2.write(x_default);
   myServo1.write(y_default);   
 }
  Serial.print("X = "); 
  Serial.print(x); 
  Serial.print(";Y = "); 
  Serial.println(y);   
}

//Blynk code for servo movement
BLYNK_WRITE(V0) { 
  up = param.asInt();
}

BLYNK_WRITE(V1) { 
  down = param.asInt(); 
}

BLYNK_WRITE(V2) { 
  left = param.asInt(); 
}

BLYNK_WRITE(V3) { 
  right = param.asInt();
}

//Blynk code to reset to original position
BLYNK_WRITE (V6){
  reset = param.asInt();
}

//Blynk code to control speed of servo
BLYNK_WRITE(V100){
  stepAngle = param.asInt();
}

void loop() {
  BlynkEdgent.run();
  timer1.run();
}

I want to see the entire .ino file, no editing, other than redacting the template ID and device name.
And, if you’ve made any changes to the Settings.h file then I want to see the section of that file that relates to whatever board type you’ve specified in the .ino file, or the default section if you haven’t specified a board type.

Pete.

1 Like

I haven’t added anything in the settings file, the following is the code unedited, it is a little big so I removed some comments is all. I have also added a header file where I have defined multiple variables.

#define BLYNK_FIRMWARE_VERSION        "0.1.0"

#define BLYNK_PRINT Serial
//#define BLYNK_DEBUG

#define APP_DEBUG

// Uncomment your board, or configure a custom board in Settings.h
//#define USE_WROVER_BOARD

#include "BlynkEdgent.h"
#include <ESP32Servo.h>
#include "Value_settings.h"
#define servo1 13
#define servo2 14

Servo myServo1;
Servo myServo2;

void setup() {
  Serial.begin(115200);                     
  BlynkEdgent.begin();            
  myServo1.attach(servo1);
  myServo2.attach(servo2);
  myServo1.write(x);
  myServo2.write(y);
  timer1.setInterval(30, Servowrite); 
}

void Servowrite() {
  
  if(left==1){
    
    if (x>=angleMin_horPlane && x<=(angleMax_horPlane-stepAngle)){
      x+=stepAngle;
    }
    else{
      x=angleMax_horPlane;
    }
    myServo2.write(x);
  }
  if(right==1){
    
    if (x>=(angleMin_horPlane+stepAngle)&&x<=angleMax_horPlane){
      x-=stepAngle;
    }
    else{
      x=angleMin_horPlane;
    }
    myServo2.write(x);
  }
  if(up==1){
    
    if (y>=angleMin_camServo && y<=(angleMax_camServo-stepAngle)){
      y+=stepAngle;
    }
    else{
      y=angleMax_camServo;
    }
    myServo1.write(y);
  }
  if(down==1){
    
    if (y>=(angleMin_camServo+stepAngle)&&y<=angleMax_camServo){
      y-=stepAngle;
    }
    else{
      y=angleMin_camServo;
    }
    myServo1.write(y);
  }
  if(reset==1){
    x = 60;
    myServo2.write(x);
    y = 90;
    myServo1.write(y);   
  }
  Serial.print("X = "); 
  Serial.print(x); 
  Serial.print(";Y = "); 
  Serial.println(y);   
}

//Storing Postions A,B,C,D
void storePos(flags flag){
  if (flag == f_1){
    A_1 = x;
    A_2 = y;    
  }

  if (flag == f_2){
    B_1 = x;
    B_2 = y;
  }

  if (flag == f_3){
    C_1 = x;
    C_2 = y;
  }

  if (flag == f_4){
    D_1 = x;
    D_2 = y;
  }
}

// Moving to Positions A,B,C,D

void setPosition(int pos1, int pos2){
  if (pos1 == A_1 && pos2 == A_2){
    for(int i = 0;i = (abs(A_1-x))/stepAngle;i++){
    if(x>A_1){
    x-=stepAngle;  
    }
    else if (x<A_1){
    x+=stepAngle;
    }
    else{
    x = A_1;
    }
  myServo2.write(x);
  }
  for(int i = 0;i = (abs(A_2-y))/stepAngle;i++){
  if(y>A_2){
    y-=stepAngle;  
    }
    else if (y<A_2){
    y+=stepAngle;
    }
    else{
    y = A_2;
    }
  myServo1.write(y);
  }
  }

  if (pos1 == B_1 && pos2 == B_2){
    for(int i = 0;i = (abs(B_1-x))/stepAngle;i++){
  if(x>B_1){
    x-=stepAngle;  
    }
    else if (x<B_1){
    x+=stepAngle;
    }
    else{
    x = B_1;
    }
  myServo2.write(x);
  }
  for(int i = 0;i = (abs(B_2-y))/stepAngle;i++){
  if(y>B_2){
    y-=stepAngle;  
    }
    else if (y<B_2){
    y+=stepAngle;
    }
    else{
    y = B_2;
    }
  myServo1.write(y);
  }
  }

  if (pos1 == C_1 && pos2 == C_2){
    for(int i = 0;i = (abs(C_1-x))/stepAngle;i++){
  if(x>C_1){
    x-=stepAngle;  
    }
    else if (x<C_1){
    x+=stepAngle;
    }
    else{
    x = C_1;
    }
  myServo2.write(x);
  }
  for(int i = 0;i = (abs(C_2-y))/stepAngle;i++){
  if(y>C_2){
    y-=stepAngle;  
    }
    else if (y<C_2){
    y+=stepAngle;
    }
    else{
    y = C_2;
    }
  myServo1.write(y);
  }
  }

  if (pos1 == D_1 && pos2 == D_2){
    for(int i = 0;i = (abs(D_1-x))/stepAngle;i++){
  if(x>D_1){
    x-=stepAngle;  
    }
    else if (x<D_1){
    x+=stepAngle;
    }
    else{
    x = D_1;
    }
  myServo2.write(x);
  }
  for(int i = 0;i = (abs(D_2-y))/stepAngle;i++){
  if(y>D_2){
    y-=stepAngle;  
    }
    else if (y<D_2){
    y+=stepAngle;
    }
    else{
    y = D_2;
    }
  myServo1.write(y);
  }
  }
}

//Blynk code for servo movement
BLYNK_WRITE(V0) { 
  up = param.asInt();
}

BLYNK_WRITE(V1) { 
  down = param.asInt(); 
}

BLYNK_WRITE(V2) { 
  left = param.asInt(); 
}

BLYNK_WRITE(V3) { 
  right = param.asInt();
}

//Blynk code for storing positions
BLYNK_WRITE(V20){
  int value = param.asInt();
  if (value && !lock){
    storePos(f_1);
  }
}
BLYNK_WRITE(V21){
  int value = param.asInt();
  if (value && !lock){
    storePos(f_2);
  }
}
BLYNK_WRITE(V22){
  int value = param.asInt();
  if (value && !lock){
    storePos(f_3);
  }
}
BLYNK_WRITE(V23){
  int value = param.asInt();
  if (value && !lock){
    storePos(f_4);
  }
}

BLYNK_WRITE(V10){
  int value = param.asInt();
  if (value){
    setPosition(A_1,A_2);
  }
}
BLYNK_WRITE(V11){
  int value = param.asInt();
  if (value){
    setPosition(B_1,B_2);
  }
}
BLYNK_WRITE(V12){
  int value = param.asInt();
  if (value){
    setPosition(C_1,C_2);
  }
}
BLYNK_WRITE(V13){
  int value = param.asInt();
  if (value){
    setPosition(D_1,D_2);
  }
}

BLYNK_WRITE(V100){
  stepAngle = param.asInt();
}

BLYNK_WRITE(V101){
  p_to_p_speed = param.asInt();
}

BLYNK_WRITE(V5){
  lock = param.asInt();
}

//Blynk code to reset to original position
BLYNK_WRITE (V6){
  reset = param.asInt();
}

void loop() {
  BlynkEdgent.run();
  timer1.run();
}

Additional header file:

int A_1= 0,A_2 = 0,B_1 = 0,B_2 = 0,C_1 = 0,C_2 = 0,D_1 = 0,D_2 = 0;
int up = 0,down = 0,left = 0,right = 0;
int lock = 0;
int reset = 0;

//flag variables
typedef enum flags{
  f_1,f_2,f_3,f_4
};

//Servo Settings
//Servo attached to the camera
int angleMax_camServo = 225;  //225
int angleMin_camServo = 60;   //60

//Servo responsible for movement on the horizontal plane
int angleMax_horPlane = 200;  //200
int angleMin_horPlane = 0;    //0

//Servo degrees per loop increment. 
int stepAngle = 1;      //Default set at 1.
int p_to_p_speed = 1;   //in case a separate slider is reqd. to move to set positions, default speed is set to 1.


//positions of servos
int x_default = 90; //horizontal plane. degrees. servo indexed from 0 to 180 degs. higher = counter-clockwise
int y_default = 90; //vertical plane. higher = counter-clockwise
int x = 90;
int y = 90;

BlynkTimer timer1;