Help with my project pleas

i am not able to write value on blynk widget
i can see distance on serial monitor
using esp8266
helpp

// defines pins numbers
const int trigPin = 12;
const int echoPin = 15;
// defines variables
long duration;
int distance;
float h;

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char auth[] = โ€œxxxโ€;

void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
Blynk.begin(auth, โ€œxxโ€, โ€œxxxโ€);

}

int data()
{
// 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;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
delay(800);
return(distance);

Blynk.virtualWrite(1, distance);

}

void loop()
{
Blynk.run();
data();

}

@scientist1995

A. Learn how to post your code to the Blynk Community, ask if you canโ€™t work out how to do it.

B. Blynk sketches are quite different to regular Arduino sketches as they involve a connection to a server (cloud / local). This connection must be maintained so delay() is a no, no and that is why SimpleTimer is provided when you install the full Blynk library package.

C. You have a nice short loop which is how it needs to be but you need to remove data() and call data() with SimpleTimer.

D. Remove your token, SSID and pwd by editing your post and change to XXXXX or we will all hack your device.

1 Like