Ultrasonic sensor + wemos D1 + blynk

hello Every one :slight_smile:
need help with my iot project counting people coming into room using ultrasonic sensor and using wemos D1
,using blynk having problem with the output at blynk

i have write code


#include <Wire.h>  // Comes with Arduino IDE
#define echoPin D6 // Echo Pin (wemos D1)
#define trigPin D7 // Trigger Pin(wemos D1)

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


char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

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


int counter = 0;

void setup() {
  // put your setup code here, to run once:
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

  Blynk.begin(auth, ssid, pass );
}

BLYNK_wRITE(V5){
if(param.asInt())
{
counter++;
}

if(counter == 50)
{
 counter = 0;
}

}

void loop() {
  // put your main code here, to run repeatedly:
long duration, distance;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH );
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  Blynk.virtualWrite(V5, counter);
  delay(200);
  Blynk();
}

sorry for bad English

you need to clean the loop and use timer to call ultrasonic function

void loop() {
   Blynk.run();
}
1 Like

thank you for the tip
here the new code that me wrote
dont sure the code right or not

#include <Wire.h> // Comes with Arduino IDE
#define echoPin D6 // Echo Pin
#define trigPin D7 // Trigger Pin
#include <Blynk.h>
#define BLYNK_PRINT Serial

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = “b6da92205d3c44dfa91f079ae363cf16”;

// Your WiFi credentials.
// Set password to “” for open networks.
char ssid[] = “okazaki”;
char pass[] = “zakizaini96”;

long duration, distance; // Duration used to calculate distance
int sensorCounter = 0; // counter for the number of button presses
int lastsensorDistance = 0;
int incomingByte;

BlynkTimer timer;

void setup() {
Serial.begin (9600);

pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
timer.setInterval(1000L, sensorDataSend);

Blynk.begin(auth, ssid, pass);

}

void sensorDataSend()
{

if (Serial.available() > 0) { // see if there’s incoming serial data:
incomingByte = Serial.read(); // read the oldest byte in the serial buffer:

}

/* The following trigPin/echoPin cycle is used to determine the
distance of the nearest object by bouncing soundwaves off of it. */
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

digitalWrite(trigPin, HIGH);
delayMicroseconds(10);

digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);

//Calculate the distance (in cm) based on the speed of sound.
distance = duration/58.2;

if (distance <= 20 && lastsensorDistance >= 40){
sensorCounter++;
Blynk.virtualWrite(V1,sensorCounter);
Serial.println(sensorCounter);
}

else {

//Serial.println(“off”); not needed.

}

lastsensorDistance = distance;
delay(500);
}

void loop()
{

Blynk.run();
timer.run();
}

please format your code with back tips and delete ‘delay(500);’