security — Esp32 Devkit C V4 simulation

An interactive Esp32 Devkit C V4 circuit simulation you can run free in your browser on Velxio, by danielolasunkami9.

Components

Sketch code

#include <WiFi.h>
#include <HTTPClient.h>

// =========================
// PINS
// =========================
#define PIR_PIN     21
#define LED_PIN     15
#define BUZZER_PIN  17

// =========================
// WIFI
// =========================
const char* ssid = "YOUR_WIFI_NAME";
const char* password = "YOUR_WIFI_PASSWORD";

// =========================
// CLOUDflare TUNNEL
// =========================
// Use the HTTP version deliberately.
// Replace the hostname with the current
// trycloudflare.com hostname shown by cloudflared.
const char* serverURL =
  "http://trim-series-wider-determine.trycloudflare.com/alert";

// =========================
// SETTINGS
// =========================
const unsigned long ALERT_COOLDOWN = 5000; // 15 seconds

unsigned long lastAlertTime = 0;
bool previousMotion = false;


// =========================
// SEND ALERT
// =========================
void sendAlert() {

  if (WiFi.status() != WL_CONNECTED) {
    Serial.println("WiFi not connected.");
    return;
  }

  HTTPClient http;

  String url = String(serverURL) +
               "?device=DXSecurity&motion=1";

  Serial.println();
  Serial.println("Sending security alert...");
  Serial.println(url);

  http.begin(url);

  int httpCode = http.GET();

  Serial.print("HTTP response code: ");
  Serial.println(httpCode);

  if (httpCode > 0) {
    String response = http.getString();

    Serial.println("Bridge response:");
    Serial.println(response);
  } 
  else {
    Serial.print("HTTP request failed: ");
    Serial.println(http.errorToString(httpCode));
  }

  http.end();
}


// =========================
// SETUP
// =========================
void setup() {

  Serial.begin(115200);

  pinMode(PIR_PIN, INPUT);
  pinMode(LED_PIN, OUTPUT);
  pinMode(BUZZER_PIN, OUTPUT);

  digitalWrite(LED_PIN, LOW);
  digitalWrite(BUZZER_PIN, LOW);

  Serial.println();
  Serial.println("==============================");
  Serial.println("DX SECURITY SYSTEM");
  Serial.println("==============================");

  Serial.println("Connecting to WiFi...");

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println();
  Serial.println("WiFi connected!");

  Serial.print("ESP32 IP: ");
  Serial.println(WiFi.localIP());

  Serial.println("Security system ARMED.");
}


// =========================
// LOOP
// =========================
void loop() {

  bool motion = digitalRead(PIR_PIN);

  // =========================
  // MOTION DETECTED
  // =========================
  if (motion) {

    digitalWrite(LED_PIN, HIGH);
    digitalWrite(BUZZER_PIN, HIGH);

    Serial.println("🚨 MOTION DETECTED!");

    // Only trigger on a new motion event
    if (!previousMotion) {

      unsigned long currentTime = millis();

      // Cooldown check
      if (currentTime - lastAlertTime >= ALERT_COOLDOWN ||
          lastAlertTime == 0) {

        sendAlert();

        lastAlertTime = currentTime;
      }
      else {
        Serial.

More projects by danielolasunkami9