NextPCB
Published © CC BY-NC-SA

Motion Detector With Blynk Notifications

How to make Motion Detector With Blynk Notifications (WeMos D1 Mini + HC-SR04)

BeginnerFull instructions provided1 hour612
Motion Detector With Blynk Notifications

Things used in this project

Hardware components

Wemos D1 Mini
Espressif Wemos D1 Mini
×1
Ultrasonic Sensor - HC-SR04
SparkFun Ultrasonic Sensor - HC-SR04
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Schematics

Code

Code

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

char auth[] = "Your Blynk token"; //Blynk token
char ssid[] = "SSID"; //Your WiFI SSID
char pass[] = "WiFi password"; //Your Wifi password

// Define pin numbers
const int echoPin = D6;
const int trigPin = D7;

// Define variables
long duration;
int distance;
int saveDis = 0;

SimpleTimer timer;

void sensorRead() {
  // 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);

  duration = pulseIn(echoPin, HIGH);  // Reads the echoPin, returns the sound wave travel time in microseconds
  saveDis = distance;                 // Save the distance to compare with the next value (for Blynk notification)
  distance = duration * 0.034 / 2;    // Calculating the distance

  Serial.println("Distance: " + String(distance) + "cm");  // Prints the distance on the Serial Monitor
  // Set Blynk notification for given range (in this case between 1-49cm)
  if (0 < distance && distance < 50) {
    Blynk.notify("Garage: Distance changed from " + String(saveDis) + "cm to " + String(distance) + "cm!");
  }
}
  void setup() {
    Blynk.begin(auth, ssid, pass);
    Blynk.notify("Garage: Connected to: " + String(ssid));
    pinMode(trigPin, OUTPUT);
    pinMode(echoPin, INPUT);
    Serial.begin(115200);
    Serial.println("Motion Detector initializing...");
    timer.setInterval(1000L, sensorRead);
  }

  void loop() {
    Blynk.run();
    timer.run();
  }

Credits

NextPCB
20 projects • 50 followers
We share Electrical, Electronics, Power, Robotics, Software, Communication, IOT “Internet Of Things”, based projects

Comments