Device offline in blynk console and blynk app

my project is to connect the esp32 with ultrasonic sensor using blynk .I created template for device in both blynk app and blynk console but there is issue in connecting the device to blynk app .it shows my device is offline .kindly please help me for connecting the device to blynk.

#define BLYNK_TEMPLATE_NAME "water level monitoring system"
#define BLYNK_AUTH_TOKEN "Me4IN1fCtr4ZfncWyg6TnJvW7iNX81rc"
// Your WiFi credentials.
// Set password to "" for open networks.

char ssid[] = "AAAAAFW7MdAADwEhRedmi4";
char pass[] = "9944063800";

//Set Water Level Distance in CM
int emptyTankDistance = 70 ;  //Distance when tank is empty
int fullTankDistance =  30 ;  //Distance when tank is full

//Set trigger value in percentage
int triggerPer =   10 ;  //alarm will start when water level drop below triggerPer

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

// Define connections to sensor
#define TRIGPIN    27  //D27
#define ECHOPIN    26  //D26

//Change the virtual pins according the rooms
#define VPIN_BUTTON_1    V1 
#define VPIN_BUTTON_2    V2

float duration;
float distance;
int   waterLevelPer;
bool  toggleBuzzer = HIGH; //Define to remember the toggle state

char auth[] = BLYNK_AUTH_TOKEN;

BlynkTimer timer;

void checkBlynkStatus() { // called every 3 seconds by SimpleTimer

  bool isconnected = Blynk.connected();
  if (isconnected == false) {
    Serial.println("Blynk Not Connected");
    
  }
  if (isconnected == true) {
    
    Serial.println("Blynk Connected");
  }
}

BLYNK_CONNECTED() {
  Blynk.syncVirtual(VPIN_BUTTON_1);
  Blynk.syncVirtual(VPIN_BUTTON_2);
}


void measureDistance(){
  // Set the trigger pin LOW for 2uS
  digitalWrite(TRIGPIN, LOW);
  delayMicroseconds(2);
 
  // Set the trigger pin HIGH for 20us to send pulse
  digitalWrite(TRIGPIN, HIGH);
  delayMicroseconds(20);
 
  // Return the trigger pin to LOW
  digitalWrite(TRIGPIN, LOW);
 
  // Measure the width of the incoming pulse
  duration = pulseIn(ECHOPIN, HIGH);
 
  // Determine distance from duration
  // Use 343 metres per second as speed of sound
  // Divide by 1000 as we want millimeters
 
  distance = ((duration / 2) * 0.343)/10;

  if (distance > (fullTankDistance - 10)  && distance < emptyTankDistance ){
    waterLevelPer = map((int)distance ,emptyTankDistance, fullTankDistance, 0, 100);
    Blynk.virtualWrite(VPIN_BUTTON_1, waterLevelPer);
    Blynk.virtualWrite(VPIN_BUTTON_2, (String(distance) + " cm"));

    // Print result to serial monitor
    Serial.print("Distance: ");
    Serial.print(distance);
    Serial.println(" cm");

   
  }
  
  // Delay before repeating measurement
  delay(100);
}

 
void setup() {
  // Set up serial monitor
  Serial.begin(115200);
 
  // Set pinmodes for sensor connections
  pinMode(ECHOPIN, INPUT);
  pinMode(TRIGPIN, OUTPUT);
  delay(1000);  
  

  WiFi.begin(ssid, pass);
  timer.setInterval(2000L, checkBlynkStatus); // check if Blynk server is connected every 2 seconds
  Blynk.config(auth);
  delay(1000);
 
}
 void loop() {

  measureDistance();

  Blynk.run();
  timer.run(); // Initiates SimpleTimer

   
}

the serial monitor output for this code is

This makes no sense.
The template and corresponding device needs to be created in EITHER the app OR the web console. You can’t do it in both, as it will create two different templates and two different devices.
Once the template and device has been created then you need to create dashboards in both the web console and the app, if you want to see the results in both places.

You should start by adding #define BLYNK_PRINT Serial near the top of your sketch, it will give you much more information about Blynk connection attempts and the status of the connection.

Please DO NOT POST SCREENSHOTS OF YOUR SERIAL OUTPUT. Copy/paste the test from the serial monitor into your post and use triple backticks at the beginning and end the same as you do when posting code.

You’ve chosen to manually manage your WiFi connection and use Blynk.config() rather than using Blynk.begin. Why?

Unfortunately, your WiFi connection code is inadequate , and won’t give reliable connections, and you don’t have any feedback to tell you if the WiFi connection has been successful. In addition, you have no WiFi regular WiFi connected check or re-connection code.

You aren’t using Blynk.connect() after Blynk.config(), and instead relying on the first iteration of Blynk.run() to perform the connection, which isn’t a good idea.

If you want your sketch to work offline with no Blynk connection then you need to do a Blynk.connected() logical test before executing Blynk.run().

The default timeout for Blynk.connected() to return false once the connection is dropped is around 18 seconds, so having a timed function which checks this evert two seconds (or three seconds as it says in your comments) isn’t going to work well. Also, your connection testing routine does nothing other than inform the user whether Blynk is connected or not. This should be a re-connection routine, which also checks and re-connects to WiFi if necessary.

The biggest problem with your sketch, from a Blynk point of view, is that you have measureDistance() being called in your void loop rather than being called by a BlynkTimer. Once you do manage to successfully connect to WiFi and Blynk this will flood the Blynk server with virtualWrite commands.

What type of Blynk subscription do you have? (Free, Maker, Pro) ?
If it’s a free subscription, what does your Billing screen show regarding the maximum number of allowable Templates?

Pete.

free subscription and it shows 3 of 10 templates

i made changes in my code but device is not connected to blynk

#define BLYNK_TEMPLATE_NAME "water level monitoring system"
#define BLYNK_AUTH_TOKEN "Me4IN1fCtr4ZfncWyg6TnJvW7iNX81rc"
// Your WiFi credentials
char ssid[] = "Redmi4";
char pass[] = "9944063800";

// Set Water Level Distance in CM
int emptyTankDistance = 70; // Distance when tank is empty
int fullTankDistance = 30; // Distance when tank is full

// Set trigger value in percentage
int triggerPer = 10; // alarm will start when water level drops below triggerPer

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

// Define connections to sensor
#define TRIGPIN 27 // D27
#define ECHOPIN 26 // D26

// Change the virtual pins according to the rooms
#define VPIN_BUTTON_1 V1
#define VPIN_BUTTON_2 V2

float duration;
float distance;
int waterLevelPer;
bool toggleBuzzer = HIGH; // Define to remember the toggle state

char auth[] = BLYNK_AUTH_TOKEN;

BlynkTimer timer;

// This function will check WiFi and Blynk connection and reconnect if necessary
void checkWiFiAndBlynkConnection() {
if (WiFi.status() != WL_CONNECTED) {
Serial.println("WiFi not connected, attempting to reconnect...");
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected.");
}

if (!Blynk.connected()) {
Serial.println("Blynk not connected, attempting to reconnect...");
Blynk.connect();
while (!Blynk.connected()) {
delay(500);
Serial.print(".");
}
Serial.println("Blynk connected.");
}
}

BLYNK_CONNECTED() {
Blynk.syncVirtual(VPIN_BUTTON_1);
Blynk.syncVirtual(VPIN_BUTTON_2);
Serial.println("Blynk Connected!");
}

void measureDistance() {
// Set the trigger pin LOW for 2uS
digitalWrite(TRIGPIN, LOW);
delayMicroseconds(2);

// Set the trigger pin HIGH for 20us to send pulse
digitalWrite(TRIGPIN, HIGH);
delayMicroseconds(20);

// Return the trigger pin to LOW
digitalWrite(TRIGPIN, LOW);

// Measure the width of the incoming pulse
duration = pulseIn(ECHOPIN, HIGH);

// Determine distance from duration
// Use 343 metres per second as speed of sound
// Divide by 1000 as we want millimeters
distance = ((duration / 2) * 0.343) / 10;

if (distance > (fullTankDistance - 10) && distance < emptyTankDistance) {
waterLevelPer = map((int)distance, emptyTankDistance, fullTankDistance, 0, 100);
Blynk.virtualWrite(VPIN_BUTTON_1, waterLevelPer);
Blynk.virtualWrite(VPIN_BUTTON_2, String(distance) + " cm");

// Print result to serial monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.print(" cm, Water Level: ");
Serial.print(waterLevelPer);
Serial.println(" %");
}
}

void setup() {
// Set up serial monitor
Serial.begin(115200);

// Set pin modes for sensor connections
pinMode(ECHOPIN, INPUT);
pinMode(TRIGPIN, OUTPUT);
delay(1000);

// Start WiFi and Blynk connection
WiFi.begin(ssid, pass);
Serial.println("Connecting to WiFi...");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected.");

// Initialize Blynk connection
Blynk.begin(auth, ssid, pass);
Serial.println("Connecting to Blynk...");
while (!Blynk.connected()) {
delay(500);
Serial.print(".");
}
Serial.println("Blynk connected.");

// Initialize BlynkTimer to measure water level every 2 seconds
timer.setInterval(2000L, measureDistance); // Measure every 2 seconds

// Check WiFi and Blynk connection every 5 seconds
timer.setInterval(5000L, checkWiFiAndBlynkConnection);
}
void loop() {
// Regular Blynk functions for connectivity management
Blynk.run();
timer.run(); // Initiates SimpleTimer (handles measureDistance every 2 seconds)
}```

You’re attempting to mix two different Blynk connection methods.

You either need to remove all of the WiFi and Blynk connection code, and just use Blynk.begin() or remove Blynk.begin() and manage the WiFi connection yourself and use Blynk.config() and Blynk.connect.

When you’re using Blynk.begin you can’t do this…

In that case, you cave a subscription that is limited to a total of 30,000 messages per month for all of your devices combined.

This device sends a message to Blynk once every 2 seconds. That means that after 60,000 seconds (1,000 minutes or around 16 hours) you will have used-up your monthly message allowance.

If you have other devices (which I assume you do as you’ve created multiple templates) then they will also contribute to the message count when they are online, so that 16 hour timescale could be significantly shorter.

Pete.

i am planning to get maker subscription in future after my project work without any issues

i made changes in the code but the result is same

#define BLYNK_TEMPLATE_NAME "water level monitoring system"
#define BLYNK_AUTH_TOKEN "Me4IN1fCtr4ZfncWyg6TnJvW7iNX81rc"
// Your WiFi credentials
char ssid[] = "Redmi4";
char pass[] = "";

// Set Water Level Distance in CM
int emptyTankDistance = 70; // Distance when tank is empty
int fullTankDistance = 30; // Distance when tank is full

// Set trigger value in percentage
int triggerPer = 10; // alarm will start when water level drops below triggerPer

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

// Define connections to sensor
#define TRIGPIN 27 // D27
#define ECHOPIN 26 // D26

// Change the virtual pins according to the rooms
#define VPIN_BUTTON_1 V1
#define VPIN_BUTTON_2 V2

float duration;
float distance;
int waterLevelPer;
bool toggleBuzzer = HIGH; // Define to remember the toggle state

char auth[] = BLYNK_AUTH_TOKEN;

BlynkTimer timer;

BLYNK_CONNECTED() {
Blynk.syncVirtual(VPIN_BUTTON_1);
Blynk.syncVirtual(VPIN_BUTTON_2);
Serial.println("Blynk Connected!");
}

void measureDistance() {
// Set the trigger pin LOW for 2uS
digitalWrite(TRIGPIN, LOW);
delayMicroseconds(2);

// Set the trigger pin HIGH for 20us to send pulse
digitalWrite(TRIGPIN, HIGH);
delayMicroseconds(20);

// Return the trigger pin to LOW
digitalWrite(TRIGPIN, LOW);

// Measure the width of the incoming pulse
duration = pulseIn(ECHOPIN, HIGH);

// Determine distance from duration
// Use 343 metres per second as speed of sound
// Divide by 1000 as we want millimeters
distance = ((duration / 2) * 0.343) / 10;

if (distance > (fullTankDistance - 10) && distance < emptyTankDistance) {
waterLevelPer = map((int)distance, emptyTankDistance, fullTankDistance, 0, 100);
Blynk.virtualWrite(VPIN_BUTTON_1, waterLevelPer);
Blynk.virtualWrite(VPIN_BUTTON_2, String(distance) + " cm");

// Print result to serial monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.print(" cm, Water Level: ");
Serial.print(waterLevelPer);
Serial.println(" %");
}
}

void setup() {
// Set up serial monitor
Serial.begin(115200);

// Set pin modes for sensor connections
pinMode(ECHOPIN, INPUT);
pinMode(TRIGPIN, OUTPUT);
delay(1000);


// Start WiFi and Blynk connection
WiFi.begin(ssid, pass);
Serial.println("Connecting to WiFi...");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected.");

// Initialize Blynk connection
Blynk.begin(auth, ssid, pass);
Serial.println("Connecting to Blynk...");
while (!Blynk.connected()) {
delay(500);
Serial.print(".");
}
Serial.println("Blynk connected.");

// Initialize BlynkTimer to measure water level every 2 seconds
 timer.setInterval(2000L, measureDistance);// Measure every 2 seconds
}
void loop() {
// Regular Blynk functions for connectivity management
Blynk.run();
timer.run(); // Initiates SimpleTimer (handles measureDistance every 2 seconds)
}```
serial monitor output:
```.....................................```

The Maker subscription is no longer available. The cheapest paid option is the Pro subscription which starts at £99 USD

That’s because you’re still mixing connection methods.

Change your void setup to this:

void setup() {
// Set up serial monitor
Serial.begin(115200);

// Set pin modes for sensor connections
pinMode(ECHOPIN, INPUT);
pinMode(TRIGPIN, OUTPUT);
delay(1000);

// Initialize Blynk connection
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);

// Initialize BlynkTimer to measure water level every 2 seconds
 timer.setInterval(2000L, measureDistance);// Measure every 2 seconds
}

You also need to add this line near the top of your sketch:
#define BLYNK_PRINT Serial

and you can delete this line:

Also, when you post code or serial monitor output to the forum, the triple backticks need to be on a line of their own for them to work properly.

Pete.

what other changes i have to make in the code?

#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID "TMPL3bnqGLSpz"
#define BLYNK_TEMPLATE_NAME "water level monitoring system"
#define BLYNK_AUTH_TOKEN "Me4IN1fCtr4ZfncWyg6TnJvW7iNX81rc"
// Your WiFi credentials
char ssid[] = "Redmi4";
char pass[] = "9944063800";

// Set Water Level Distance in CM
int emptyTankDistance = 70; // Distance when tank is empty
int fullTankDistance = 30; // Distance when tank is full

// Set trigger value in percentage
int triggerPer = 10; // alarm will start when water level drops below triggerPer

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

// Define connections to sensor
#define TRIGPIN 27 // D27
#define ECHOPIN 26 // D26

// Change the virtual pins according to the rooms
#define VPIN_BUTTON_1 V1
#define VPIN_BUTTON_2 V2

float duration;
float distance;
int waterLevelPer;
bool toggleBuzzer = HIGH; // Define to remember the toggle state


BlynkTimer timer;

BLYNK_CONNECTED() {
Blynk.syncVirtual(VPIN_BUTTON_1);
Blynk.syncVirtual(VPIN_BUTTON_2);
Serial.println("Blynk Connected!");
}

void measureDistance() {
// Set the trigger pin LOW for 2uS
digitalWrite(TRIGPIN, LOW);
delayMicroseconds(2);

// Set the trigger pin HIGH for 20us to send pulse
digitalWrite(TRIGPIN, HIGH);
delayMicroseconds(20);

// Return the trigger pin to LOW
digitalWrite(TRIGPIN, LOW);

// Measure the width of the incoming pulse
duration = pulseIn(ECHOPIN, HIGH);

// Determine distance from duration
// Use 343 metres per second as speed of sound
// Divide by 1000 as we want millimeters
distance = ((duration / 2) * 0.343) / 10;

if (distance > (fullTankDistance - 10) && distance < emptyTankDistance) {
waterLevelPer = map((int)distance, emptyTankDistance, fullTankDistance, 0, 100);
Blynk.virtualWrite(VPIN_BUTTON_1, waterLevelPer);
Blynk.virtualWrite(VPIN_BUTTON_2, String(distance) + " cm");

// Print result to serial monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.print(" cm, Water Level: ");
Serial.print(waterLevelPer);
Serial.println(" %");
}
}

void setup() {
// Set up serial monitor
Serial.begin(115200);

// Set pin modes for sensor connections
pinMode(ECHOPIN, INPUT);
pinMode(TRIGPIN, OUTPUT);
delay(1000);

// Initialize Blynk connection
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);

// Initialize BlynkTimer to measure water level every 2 seconds
 timer.setInterval(2000L, measureDistance);// Measure every 2 seconds
}
void loop() {
// Regular Blynk functions for connectivity management
Blynk.run();
timer.run(); // Initiates SimpleTimer (handles measureDistance every 2 seconds)
}

i am using arduino 1.8.19 ide and blynk 1.3.2 library .
Is it suitable for making a connection between the device and Blynk?

What does your serial monitor show with this code?

Pete.

it shows empty blank screen




What baud rate is your serial monitor set to?

Pete.

baud rate is115200

In that case, you’re either doing something wrong, such as selecting the wrong UART port or using a non data cable, or you have a hardware problem.
Until you can view the serial data you won’t be able to debug your problem.

Pete.