How to use my ESP32 as a flight controller

Good day everyone, please i need help with my project. I’m trying to use my ESP32 and the blynk app as a transmitter and a receiver in my drone project and i don’t know what I’m doing wrong cause it’s not working but i only get an output reading in the serial monitor. Here’s the code i used


#define BLYNK_PRINT Serial

#define BLYNK_TEMPLATE_ID "****"
#define BLYNK_TEMPLATE_NAME "****"
#define BLYNK_AUTH_TOKEN "***"

#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

#include <ESP32Servo.h>


// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "*****";
char pass[] = "****";

//Change the pin numbers as per your connections and board used
#define channel_pin_1 16  //Throttle 
#define channel_pin_2 17  //Roll
#define channel_pin_3 18  //Pitch
#define channel_pin_4 19  //Yaw
#define channel_pin_5 21  //Flight Mode
#define status_led 13    //LED for WiFi status indication

int input_YAW, input_PITCH, input_ROLL, throttle, flight_mode, dummy;

Servo channel_1, channel_2, channel_3, channel_4, channel_5;

BLYNK_WRITE(0)
{   
  throttle = param.asInt(); 
}  

BLYNK_WRITE(1)
{   
  input_YAW = param.asInt();  
} 

BLYNK_WRITE(2){   
  input_PITCH = param.asInt();  
} 

BLYNK_WRITE(3){   
  input_ROLL = param.asInt();  
}

BLYNK_WRITE(4){   
  flight_mode = param.asInt();  
} 

BLYNK_WRITE(5){                         
  dummy = param.asInt();  
}

void setup()
{
  // Debug console
  Serial.begin(9600);

  pinMode(status_led,OUTPUT);              
  digitalWrite(status_led,HIGH);                          // LED on when not connected to cloud

  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);

  if(Blynk.connected()){
    digitalWrite(status_led, LOW);                         // LED off when connected to cloud
  }

  channel_1.attach(channel_pin_1);                         // Throttle output to Flight Controller
  channel_2.attach(channel_pin_2);                         // Roll output to Flight Controller
  channel_3.attach(channel_pin_3);                         // Pitch output to Flight Controller
  channel_4.attach(channel_pin_4);                         // Yaw output to Flight Controller
  channel_5.attach(channel_pin_5);                         // Flight Mode output to Flight Controller

}

void loop()
{
  if(!Blynk.connected()){                          //On disconnection with the cloud                                 
    digitalWrite(status_led, HIGH);  

  }

  channel_1.writeMicroseconds(throttle);           // Sending throttle data to flight controller
  channel_2.writeMicroseconds(input_ROLL);         // Sending roll data to flight controller
  channel_3.writeMicroseconds(input_PITCH);        // Sending pitch data to flight controller
  channel_4.writeMicroseconds(input_YAW);          // Sending yaw data to flight controller
  channel_5.writeMicroseconds(flight_mode);        // Sending flight mode data to flight controller
  
  //Printing data in serial monitor
  Serial.print(throttle);
  Serial.print('\t');
  Serial.print(input_ROLL);
  Serial.print('\t');
  Serial.print(input_PITCH);
  Serial.print('\t');
  Serial.print(input_YAW);
  Serial.print('\t');
  Serial.println(flight_mode);                     

  Blynk.run();
}



First of all I think using Blynk as a drone controller is a bad idea.

  1. you would need to send 100’s or 1000’s of commands to the drone to control it. This means you would need to send the blynk sever the same commands to relay it to your ESP32 and there is a limit of 10 commands per second limitation. If you send more commands you will be flooding the server and eventually your device will be blocked until you restart the device.

  2. Wifi has limited range of few meters. As soon as you go past that, you will loose control over the drone and crash it.

If at all you want to continue, read this :point_down:t2:

Https://docs.blynk.io/en/legacy-platform/legacy-articles/keep-your-void-loop-clean

It depends which ESP32Servo.h library you’re using, and what type of servos you’re using, but my guess is that your servo initialisation and control code is wrong.

Have you tried a non-blynk sketch that polls physical input pins and controls the servo depending on whether or not that pin is HIGH or LOW?

Pete.