Need help on how to get data from ultrasonic sensor by using virtual pin

hello i need help on how to get data from ultrasonic sensor to blynk 2.0 app by using virtual pin. im using arduino uno and esp 8266. here is my code.;


#define BLYNK_TEMPLATE_ID "TMPLhuoSg04W"
#define BLYNK_DEVICE_NAME "ultrasonic sensor"
#define BLYNK_AUTH_TOKEN "z9o7uAXi7tWxExlQwGDZditz2GV2eI4q"

#define BLYNK_PRINT Serial


#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>

char auth[] = "z9o7uAXi7tWxExlQwGDZditz2GV2eI";
char ssid[] = "Bilik PAK 21_2.4G@unifi";
char pass[] = "smttjnke4144";


#include <SoftwareSerial.h>
SoftwareSerial EspSerial(2, 3); // RX, TX

#define ESP8266_BAUD 38400
ESP8266 wifi(&EspSerial);

// defines pins numbers
const int trigPin = 9;
const int echoPin = 10;
const int buzzer = 11; 
const int ledPin = 13;
                        
// defines variables
long duration;
int distance;
int safetyDistance;
 
            
void setup() {
Serial.begin(38400);

EspSerial.begin(ESP8266_BAUD);
delay(10);

Blynk.begin(auth, wifi, ssid, pass);
  
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(buzzer, OUTPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(38400); // Starts the serial communication
}
 
void loop() {
Blynk.run();
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
 
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

 
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
 
// Calculating the distance
distance= duration*0.034/2;
 
safetyDistance = distance;

  if (safetyDistance <= 5 && safetyDistance != 0) // You can change safe distance from here changing value Ex. 20 , 40 , 60 , 80 , 100, all in cm
  {
    digitalWrite(buzzer, HIGH);
    digitalWrite(ledPin, HIGH);
  }
  else{
    digitalWrite(buzzer, LOW);
    digitalWrite(ledPin, LOW);
  }
 
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
}

First of all, DO NOT RUN THIS CODE! it will flood the Blynk server with far too many Blynk.virtualWrites per second.

Start by reading this:

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

Pete.

Your Uno isn’t able to consistently read and write dat to a SoftwareSerial port at this speed, it just doesn’t have the processing power to emulate a serial port with this baud rate. You should read this…

Pete.

so um there’s no way for my ultrasonic sensor send its data to my arduino uno to blynk server?

Yes, it’s easy to do, and many people have posted code examples of how to do that here on the forum.
However, you have to configure your ESP-01 correctly - and use the correct corresponding baud rate in your sketch, and your sketch needs to use a timer rather than having the code in the void loop.

Pete.

i’ve followed almost every step on this video : youtube.com/watch?v=hZAX2d8qrbs&t=1186s
the only thing that i changed is i added an ultrasonic sensor and a buzzer. is it possible for the ultrasonic sensor send its data to my blynk?. thanks. sorry for my bad grammar btw

Yes, I’ve already confirmed that in an earlier reply in this topic, and explained that there are dozens and dozens of projects that do just this that are already on this forum, so you don’t need to re-invent the wheel.

The problem with that video is that the code comes from the Blynk example browser, and by default it trues to use 38400 baud, because by default it is set-up to use the Arduino Mega which has three hardware serial ports - each of which can easily support that baud rate.
The Youtuber manages to get his project working at that baud rate on the Uno for the duration of the video, but trust me when I tell you that if you want it to work consistently then you’ll need to use a lower baud rate.

Of course, a much simpler alternative would be to use a NodeMCU or ESP32 instead of the Uno/ESP-01 combination - which is that hardware that the majority of forum users end-up with.

Pete.

You should use this command

Blynk.virtualWrite(pin, value)

to send data from your hardware to the cloud. and this function

BLYNK_WRITE(vPIN)

to get data from the cloud.

You can check the documentation for more details
https://docs.blynk.io/en/blynk.edgent-firmware-api/virtual-pins#blynk.virtualwrite-vpin-value