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